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_url = "$base/api/rest/track/";
25 my $track_update_url = $track_url . $track->id;
26 my $tracks_update_url = $track_url;
28 # test invalid track id caught
30 foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
31 my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
32 my $test_data = $json->encode( { title => 'value' } );
33 my $req = POST( $incorrect_url, Content => $test_data );
34 $req->content_type('text/x-json');
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 $test_data = $json->encode( { wrong_param => 'value' } );
59 my $req = POST( $track_update_url, Content => $test_data );
60 $req->content_type('text/x-json');
63 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
65 my $response = $json->decode( $mech->content );
66 is_deeply( $response->{messages}, ['No valid keys passed'],
67 'correct message returned' );
69 $track->discard_changes;
71 { $track->get_columns },
78 my $test_data = $json->encode( { title => undef } );
79 my $req = POST( $track_update_url, Content => $test_data );
80 $req->content_type('text/x-json');
82 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
84 $track->discard_changes;
85 isnt( $track->title, $original_cols{title}, 'Title changed' );
86 is( $track->title, undef, 'Title changed to undef' );
90 my $test_data = $json->encode(
91 { title => 'monkey monkey', 'cd' => { year => 2009 } } );
92 my $req = POST( $track_update_url, Content => $test_data );
93 $req->content_type('text/x-json');
96 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
98 $track->discard_changes;
99 is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
100 is( $track->cd->year, 2009, 'related row updated' );
103 # bulk_update existing objects
106 # order to get a stable order of rows
108 $schema->resultset('Track')
109 ->search( undef, { order_by => 'trackid', rows => 3 } );
110 my $test_data = $json->encode(
112 map +{ id => $_->id, title => 'Track ' . $_->id },
117 my $req = PUT( $tracks_update_url, Content => $test_data );
118 $req->content_type('text/x-json');
119 $mech->request($req);
120 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
123 while ( my $track = $tracks_rs->next ) {
124 is( $track->title, 'Track ' . $track->id, 'Title changed' );
128 # bulk_update nonexisting objects
131 # order to get a stable order of rows
132 my $test_data = $json->encode(
134 map +{ id => $_, title => 'Track ' . $_ },
139 my $req = PUT( $tracks_update_url, Content => $test_data );
140 $req->content_type('text/x-json');
141 $mech->request($req);
142 cmp_ok( $mech->status, '==', 400,
143 'Attempt to update three nonexisting tracks fails' );
144 my $response = $json->decode( $mech->content );
145 is( $response->{success}, JSON::false,
146 'success property returns unquoted false' );
148 $response->{messages}->[0],
149 qr/No object found for id/,
150 'correct message returned'