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_url = "$base/api/rest/track/";
27 my $track_update_url = $track_url . $track->id;
28 my $tracks_update_url = $track_url;
30 # test invalid track id caught
32 foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
33 my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
34 my $test_data = $json->encode( { title => 'value' } );
35 my $req = POST( $incorrect_url, Content => $test_data );
36 $req->content_type('text/x-json');
39 cmp_ok( $mech->status, '==', 400,
40 'Attempt with invalid track id caught' );
42 my $response = $json->decode( $mech->content );
44 $response->{messages}->[0],
45 qr/No object found for id/,
46 'correct message returned'
49 $track->discard_changes;
51 { $track->get_columns },
58 # validation when no params sent
60 my $test_data = $json->encode( { wrong_param => 'value' } );
61 my $req = POST( $track_update_url, Content => $test_data );
62 $req->content_type('text/x-json');
65 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
67 my $response = $json->decode( $mech->content );
68 is_deeply( $response->{messages}, ['No valid keys passed'],
69 'correct message returned' );
71 $track->discard_changes;
73 { $track->get_columns },
80 my $test_data = $json->encode( { title => undef } );
81 my $req = POST( $track_update_url, Content => $test_data );
82 $req->content_type('text/x-json');
84 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
86 $track->discard_changes;
87 isnt( $track->title, $original_cols{title}, 'Title changed' );
88 is( $track->title, undef, 'Title changed to undef' );
92 my $test_data = $json->encode(
93 { title => 'monkey monkey', 'cd' => { year => 2009 } } );
94 my $req = POST( $track_update_url, Content => $test_data );
95 $req->content_type('text/x-json');
98 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
100 $track->discard_changes;
101 is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
102 is( $track->cd->year, 2009, 'related row updated' );
105 # bulk_update existing objects
108 # order to get a stable order of rows
110 $schema->resultset('Track')
111 ->search( undef, { order_by => 'trackid', rows => 3 } );
112 my $test_data = $json->encode(
114 map +{ id => $_->id, title => 'Track ' . $_->id },
119 my $req = PUT( $tracks_update_url, Content => $test_data );
120 $req->content_type('text/x-json');
121 $mech->request($req);
122 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
125 while ( my $track = $tracks_rs->next ) {
126 is( $track->title, 'Track ' . $track->id, 'Title changed' );
130 # bulk_update nonexisting objects
133 # order to get a stable order of rows
134 my $test_data = $json->encode(
136 map +{ id => $_, title => 'Track ' . $_ },
141 my $req = PUT( $tracks_update_url, Content => $test_data );
142 $req->content_type('text/x-json');
143 $mech->request($req);
144 cmp_ok( $mech->status, '==', 400,
145 'Attempt to update three nonexisting tracks fails' );
146 my $response = $json->decode( $mech->content );
147 is( $response->{success}, JSON::false,
148 'success property returns unquoted false' );
150 $response->{messages}->[0],
151 qr/No object found for id/,
152 'correct message returned'