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 $json = JSON->new->utf8;
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
23 my $track = $schema->resultset('Track')->first;
24 my %original_cols = $track->get_columns;
26 my $track_update_url = "$base/api/rpc/track/id/" . $track->id . "/update";
27 my $any_track_update_url =
28 "$base/api/rpc/any/track/id/" . $track->id . "/update";
29 my $tracks_update_url = "$base/api/rpc/track/update";
31 # test invalid track id caught
33 foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
34 my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
35 my $req = POST( $incorrect_url, { title => 'value' } );
37 $mech->request( $req, $content_type );
38 cmp_ok( $mech->status, '==', 400,
39 'Attempt with invalid track id caught' );
41 my $response = $json->decode( $mech->content );
43 $response->{messages}->[0],
44 qr/No object found for id/,
45 'correct message returned'
48 $track->discard_changes;
50 { $track->get_columns },
57 # validation when no params sent
59 my $req = POST( $track_update_url, { wrong_param => 'value' } );
60 $mech->request( $req, $content_type );
61 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
63 my $response = $json->decode( $mech->content );
64 is_deeply( $response->{messages}, ['No valid keys passed'],
65 'correct message returned' );
67 $track->discard_changes;
69 { $track->get_columns },
76 my $req = POST( $track_update_url, { wrong_param => 'value' } );
77 $mech->request( $req, $content_type );
78 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
80 my $response = $json->decode( $mech->content );
81 is_deeply( $response->{messages}, ['No valid keys passed'],
82 'correct message returned' );
84 $track->discard_changes;
86 { $track->get_columns },
93 my $req = POST( $track_update_url, { title => undef } );
94 $mech->request( $req, $content_type );
95 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
97 $track->discard_changes;
98 isnt( $track->title, $original_cols{title}, 'Title changed' );
99 is( $track->title, '', 'Title changed to undef' );
103 my $req = POST( $track_update_url, { title => 'monkey monkey' } );
104 $mech->request( $req, $content_type );
105 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
107 $track->discard_changes;
108 is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
114 { title => 'sheep sheep',
118 $mech->request( $req, $content_type );
119 cmp_ok( $mech->status, '==', 200,
120 'Update with key with value and related key okay' );
122 $track->discard_changes;
123 is( $track->title, 'sheep sheep', 'Title changed' );
124 is( $track->cd->year, '2009', 'Related field changed"' );
128 my $req = POST( $any_track_update_url, { title => 'baa' } );
129 $mech->request( $req, $content_type );
130 cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
132 $track->discard_changes;
133 is( $track->title, 'baa', 'Title changed' );
137 my $req = POST( $any_track_update_url, { position => '14' } );
138 $mech->request( $req, $content_type );
139 cmp_ok( $mech->status, '==', 200, 'Position update okay' );
141 $track->discard_changes;
142 is( $track->get_column('position'), '14', 'Position changed' );
145 # bulk_update existing objects
148 # order to get a stable order of rows
150 $schema->resultset('Track')
151 ->search( undef, { order_by => 'trackid', rows => 3 } );
152 my $test_data = $json->encode(
154 map +{ id => $_->id, title => 'Track ' . $_->id },
159 my $req = POST( $tracks_update_url, Content => $test_data );
160 $req->content_type('text/x-json');
161 $mech->request($req);
162 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
165 while ( my $track = $tracks_rs->next ) {
166 is( $track->title, 'Track ' . $track->id, 'Title changed' );
170 # bulk_update nonexisting objects
173 # order to get a stable order of rows
174 my $test_data = $json->encode(
176 map +{ id => $_, title => 'Track ' . $_ },
181 my $req = POST( $tracks_update_url, Content => $test_data );
182 $req->content_type('text/x-json');
183 $mech->request($req);
184 cmp_ok( $mech->status, '==', 400,
185 'Attempt to update three nonexisting tracks fails' );
186 my $response = $json->decode( $mech->content );
187 is( $response->{success}, 'false',
188 'success property returns quoted false' );
190 $response->{messages}->[0],
191 qr/No object found for id/,
192 'correct message returned'