6 my $base = 'http://localhost';
7 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
12 use Test::WWW::Mechanize::Catalyst 'RestTest';
13 use HTTP::Request::Common;
16 my $json = JSON::MaybeXS->new(utf8 => 1);
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
29 { wrong_param => 'value' },
30 'Accept' => 'text/json'
32 $mech->request( $req, $content_type );
33 cmp_ok( $mech->status, '==', 400,
34 'attempt without required params caught' );
35 my $response = $json->decode( $mech->content );
37 $response->{messages}->[0],
38 qr/No value supplied for name and no default/,
39 'correct message returned'
43 # test default value used if default value exists
50 'Accept' => 'text/json'
52 $mech->request( $req, $content_type );
53 cmp_ok( $mech->status, '==', 200,
54 'default value used when not supplied' );
55 ok( $schema->resultset('Producer')->find( { name => 'fred' } ),
56 'record created with default name' );
59 # test create works as expected when passing required value
63 { name => 'king luke' },
64 'Accept' => 'text/json'
66 $mech->request( $req, $content_type );
67 cmp_ok( $mech->status, '==', 200, 'param value used when supplied' );
70 $schema->resultset('Producer')->find( { name => 'king luke' } );
71 ok( $new_obj, 'record created with specified name' );
73 my $response = $json->decode( $mech->content );
76 { $new_obj->get_columns },
77 'json for new producer returned'
81 # test stash config handling
84 $any_artist_create_url,
85 { name => 'queen monkey' },
86 'Accept' => 'text/json'
88 $mech->request( $req, $content_type );
89 cmp_ok( $mech->status, '==', 200, 'stashed config okay' );
92 $schema->resultset('Artist')->find( { name => 'queen monkey' } );
93 ok( $new_obj, 'record created with specified name' );
95 my $response = $json->decode( $mech->content );
98 { success => 'true' },
99 'json for new artist returned'
103 # test create returns an error as expected when passing invalid value
110 my $req = POST($producer_create_url, $data, 'Accept' => 'text/json');
111 $mech->request( $req, $content_type );
112 cmp_ok( $mech->status, '==', 200, 'create with pk value ok' );
113 my $response = $json->decode( $mech->content );
116 { success => 'true', list => $data },
117 'json for producer with pk value ok'
119 # try to insert same data again, as this seems to be the only way to
120 # force an insert to fail for SQLite.
121 # It accepts too long columns as well as wrong datatypes without errors.
122 # The bind with datatype of newer DBIC versions numifies non-integer
123 # values passed as pk value too.
124 $mech->request( $req, $content_type );
125 cmp_ok( $mech->status, '==', 400, 'invalid param value produces error' );