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->new->utf8;
18 my $mech = Test::WWW::Mechanize::Catalyst->new;
19 ok( my $schema = DBICTest->init_schema(), 'got schema' );
21 my $track = $schema->resultset('Track')->first;
22 my %original_cols = $track->get_columns;
24 my $track_update_url = "$base/api/rpc/track/id/" . $track->id . "/update";
25 my $any_track_update_url =
26 "$base/api/rpc/any/track/id/" . $track->id . "/update";
27 my $tracks_update_url = "$base/api/rpc/track/update";
29 # test invalid track id caught
31 diag 'DBIx::Class warns about a non-numeric id which is ok because we test for that too';
32 foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
33 my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
34 my $req = POST( $incorrect_url, { title => 'value' } );
36 $mech->request( $req, $content_type );
37 cmp_ok( $mech->status, '==', 400,
38 'Attempt with invalid track id caught' );
40 my $response = $json->decode( $mech->content );
42 $response->{messages}->[0],
43 qr/No object found for id/,
44 'correct message returned'
47 $track->discard_changes;
49 { $track->get_columns },
56 # validation when no params sent
58 my $req = POST( $track_update_url, { wrong_param => 'value' } );
59 $mech->request( $req, $content_type );
60 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
62 my $response = $json->decode( $mech->content );
63 is_deeply( $response->{messages}, ['No valid keys passed'],
64 'correct message returned' );
66 $track->discard_changes;
68 { $track->get_columns },
75 my $req = POST( $track_update_url, { wrong_param => 'value' } );
76 $mech->request( $req, $content_type );
77 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
79 my $response = $json->decode( $mech->content );
80 is_deeply( $response->{messages}, ['No valid keys passed'],
81 'correct message returned' );
83 $track->discard_changes;
85 { $track->get_columns },
92 my $req = POST( $track_update_url, { title => undef } );
93 $mech->request( $req, $content_type );
94 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
96 $track->discard_changes;
97 isnt( $track->title, $original_cols{title}, 'Title changed' );
98 is( $track->title, '', 'Title changed to undef' );
102 my $req = POST( $track_update_url, { title => 'monkey monkey' } );
103 $mech->request( $req, $content_type );
104 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
106 $track->discard_changes;
107 is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
113 { title => 'sheep sheep',
117 $mech->request( $req, $content_type );
118 cmp_ok( $mech->status, '==', 200,
119 'Update with key with value and related key okay' );
121 $track->discard_changes;
122 is( $track->title, 'sheep sheep', 'Title changed' );
123 is( $track->cd->year, '2009', 'Related field changed"' );
127 my $req = POST( $any_track_update_url, { title => 'baa' } );
128 $mech->request( $req, $content_type );
129 cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
131 $track->discard_changes;
132 is( $track->title, 'baa', 'Title changed' );
136 my $req = POST( $any_track_update_url, { position => '14' } );
137 $mech->request( $req, $content_type );
138 cmp_ok( $mech->status, '==', 200, 'Position update okay' );
140 $track->discard_changes;
141 is( $track->get_column('position'), '14', 'Position changed' );
144 # bulk_update existing objects
147 # order to get a stable order of rows
149 $schema->resultset('Track')
150 ->search( undef, { order_by => 'trackid', rows => 3 } );
151 my $test_data = $json->encode(
153 map +{ id => $_->id, title => 'Track ' . $_->id },
158 my $req = POST( $tracks_update_url, Content => $test_data );
159 $req->content_type('text/x-json');
160 $mech->request($req);
161 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
164 while ( my $track = $tracks_rs->next ) {
165 is( $track->title, 'Track ' . $track->id, 'Title changed' );
169 # bulk_update nonexisting objects
172 # order to get a stable order of rows
173 my $test_data = $json->encode(
175 map +{ id => $_, title => 'Track ' . $_ },
180 my $req = POST( $tracks_update_url, Content => $test_data );
181 $req->content_type('text/x-json');
182 $mech->request($req);
183 cmp_ok( $mech->status, '==', 400,
184 'Attempt to update three nonexisting tracks fails' );
185 my $response = $json->decode( $mech->content );
186 is( $response->{success}, 'false',
187 'success property returns quoted false' );
189 $response->{messages}->[0],
190 qr/No object found for id/,
191 'correct message returned'