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 foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
32 my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
33 my $req = POST( $incorrect_url, { title => 'value' } );
35 $mech->request( $req, $content_type );
36 cmp_ok( $mech->status, '==', 400,
37 'Attempt with invalid track id caught' );
39 my $response = $json->decode( $mech->content );
41 $response->{messages}->[0],
42 qr/No object found for id/,
43 'correct message returned'
46 $track->discard_changes;
48 { $track->get_columns },
55 # validation when no params sent
57 my $req = POST( $track_update_url, { wrong_param => 'value' } );
58 $mech->request( $req, $content_type );
59 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
61 my $response = $json->decode( $mech->content );
62 is_deeply( $response->{messages}, ['No valid keys passed'],
63 'correct message returned' );
65 $track->discard_changes;
67 { $track->get_columns },
74 my $req = POST( $track_update_url, { wrong_param => 'value' } );
75 $mech->request( $req, $content_type );
76 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
78 my $response = $json->decode( $mech->content );
79 is_deeply( $response->{messages}, ['No valid keys passed'],
80 'correct message returned' );
82 $track->discard_changes;
84 { $track->get_columns },
91 my $req = POST( $track_update_url, { title => undef } );
92 $mech->request( $req, $content_type );
93 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
95 $track->discard_changes;
96 isnt( $track->title, $original_cols{title}, 'Title changed' );
97 is( $track->title, '', 'Title changed to undef' );
101 my $req = POST( $track_update_url, { title => 'monkey monkey' } );
102 $mech->request( $req, $content_type );
103 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
105 $track->discard_changes;
106 is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
112 { title => 'sheep sheep',
116 $mech->request( $req, $content_type );
117 cmp_ok( $mech->status, '==', 200,
118 'Update with key with value and related key okay' );
120 $track->discard_changes;
121 is( $track->title, 'sheep sheep', 'Title changed' );
122 is( $track->cd->year, '2009', 'Related field changed"' );
126 my $req = POST( $any_track_update_url, { title => 'baa' } );
127 $mech->request( $req, $content_type );
128 cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
130 $track->discard_changes;
131 is( $track->title, 'baa', 'Title changed' );
135 my $req = POST( $any_track_update_url, { position => '14' } );
136 $mech->request( $req, $content_type );
137 cmp_ok( $mech->status, '==', 200, 'Position update okay' );
139 $track->discard_changes;
140 is( $track->get_column('position'), '14', 'Position changed' );
143 # bulk_update existing objects
146 # order to get a stable order of rows
148 $schema->resultset('Track')
149 ->search( undef, { order_by => 'trackid', rows => 3 } );
150 my $test_data = $json->encode(
152 map +{ id => $_->id, title => 'Track ' . $_->id },
157 my $req = POST( $tracks_update_url, Content => $test_data );
158 $req->content_type('text/x-json');
159 $mech->request($req);
160 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
163 while ( my $track = $tracks_rs->next ) {
164 is( $track->title, 'Track ' . $track->id, 'Title changed' );
168 # bulk_update nonexisting objects
171 # order to get a stable order of rows
172 my $test_data = $json->encode(
174 map +{ id => $_, title => 'Track ' . $_ },
179 my $req = POST( $tracks_update_url, Content => $test_data );
180 $req->content_type('text/x-json');
181 $mech->request($req);
182 cmp_ok( $mech->status, '==', 400,
183 'Attempt to update three nonexisting tracks fails' );
184 my $response = $json->decode( $mech->content );
185 is( $response->{success}, 'false',
186 'success property returns quoted false' );
188 $response->{messages}->[0],
189 qr/No object found for id/,
190 'correct message returned'