8 my $base = 'http://localhost';
9 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
18 my $mech = Test::WWW::Mechanize::Catalyst->new;
19 ok(my $schema = DBICTest->init_schema(), 'got schema');
21 my $artist_create_url = "$base/api/rpc/artist/create";
22 my $any_artist_create_url = "$base/api/rpc/any/artist/create";
23 my $producer_create_url = "$base/api/rpc/producer/create";
25 # test validation when no params sent
27 my $req = POST( $artist_create_url, {
28 wrong_param => 'value'
29 }, 'Accept' => 'text/json' );
30 $mech->request($req, $content_type);
31 cmp_ok( $mech->status, '==', 400, 'attempt without required params caught' );
32 my $response = JSON::Any->Load( $mech->content);
33 like( $response->{messages}->[0], qr/No value supplied for name and no default/, 'correct message returned' );
36 # test default value used if default value exists
38 my $req = POST( $producer_create_url, {
40 }, 'Accept' => 'text/json' );
41 $mech->request($req, $content_type);
42 cmp_ok( $mech->status, '==', 200, 'default value used when not supplied' );
43 ok($schema->resultset('Producer')->find({ name => 'fred' }), 'record created with default name');
46 # test create works as expected when passing required value
48 my $req = POST( $producer_create_url, {
50 }, 'Accept' => 'text/json' );
51 $mech->request($req, $content_type);
52 cmp_ok( $mech->status, '==', 200, 'param value used when supplied' );
54 my $new_obj = $schema->resultset('Producer')->find({ name => 'king luke' });
55 ok($new_obj, 'record created with specified name');
57 my $response = JSON::Any->Load( $mech->content);
58 is_deeply( $response->{list}, { $new_obj->get_columns }, 'json for new producer returned' );
61 # test stash config handling
64 my $req = POST( $any_artist_create_url, {
65 name => 'queen monkey'
66 }, 'Accept' => 'text/json' );
67 $mech->request($req, $content_type);
68 cmp_ok( $mech->status, '==', 200, 'stashed config okay' );
70 my $new_obj = $schema->resultset('Artist')->find({ name => 'queen monkey' });
71 ok($new_obj, 'record created with specified name');
73 my $response = JSON::Any->Load( $mech->content);
74 is_deeply( $response, { success => 'true' }, 'json for new artist returned' );
77 # test create returns an error as expected when passing invalid value
79 my $long_string = '-' x 1024;
81 my $req = POST( $producer_create_url, {
82 producerid => $long_string,
84 }, 'Accept' => 'text/json' );
85 $mech->request($req, $content_type);
86 cmp_ok( $mech->status, '==', 400, 'invalid param value produces error' );