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::MaybeXS->new(utf8 => 1);
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 diag 'DBIx::Class warns about a non-numeric id which is ok because we test for that too';
31 foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
32 my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
33 my $test_data = $json->encode( { title => 'value' } );
34 my $req = POST( $incorrect_url, Content => $test_data );
35 $req->content_type('text/x-json');
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 $test_data = $json->encode( { wrong_param => 'value' } );
60 my $req = POST( $track_update_url, Content => $test_data );
61 $req->content_type('text/x-json');
64 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
66 my $response = $json->decode( $mech->content );
67 is_deeply( $response->{messages}, ['No valid keys passed'],
68 'correct message returned' );
70 $track->discard_changes;
72 { $track->get_columns },
79 my $test_data = $json->encode( { title => undef } );
80 my $req = POST( $track_update_url, Content => $test_data );
81 $req->content_type('text/x-json');
83 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
85 $track->discard_changes;
86 isnt( $track->title, $original_cols{title}, 'Title changed' );
87 is( $track->title, undef, 'Title changed to undef' );
91 my $test_data = $json->encode(
92 { title => 'monkey monkey', 'cd' => { year => 2009 } } );
93 my $req = POST( $track_update_url, Content => $test_data );
94 $req->content_type('text/x-json');
97 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
99 $track->discard_changes;
100 is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
101 is( $track->cd->year, 2009, 'related row updated' );
104 # bulk update existing objects with database error
107 # order to get a stable order of rows
109 $schema->resultset('Track')
110 ->search( undef, { order_by => 'trackid', rows => 3 } );
111 my @tracks = $tracks_rs->all;
112 my @tracks_new_data = map +{ id => $_->id, title => 'Track ' . $_->id },
114 # set position column to NULL to force error
115 $tracks_new_data[0]->{position} = undef;
116 my $test_data = $json->encode({ list => \@tracks_new_data });
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, '==', 400, 'Attempt to update three tracks fails' );
121 my $response = $json->decode( $mech->content );
122 is( $response->{success}, JSON::MaybeXS::false,
123 'success property returns unquoted false' );
125 $response->{messages}->[0],
126 qr/a database error has occured/,
127 'correct message returned'
131 while ( my $track = $tracks_rs->next ) {
132 isnt( $track->title, 'Track ' . $track->id, 'Title unchanged' );
136 # bulk_update existing objects
139 # order to get a stable order of rows
141 $schema->resultset('Track')
142 ->search( undef, { order_by => 'trackid', rows => 3 } );
143 my $test_data = $json->encode(
145 map +{ id => $_->id, title => 'Track ' . $_->id },
150 my $req = PUT( $tracks_update_url, Content => $test_data );
151 $req->content_type('text/x-json');
152 $mech->request($req);
153 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
156 while ( my $track = $tracks_rs->next ) {
157 is( $track->title, 'Track ' . $track->id, 'Title changed' );
161 # bulk_update nonexisting objects
164 # order to get a stable order of rows
165 my $test_data = $json->encode(
167 map +{ id => $_, title => 'Track ' . $_ },
172 my $req = PUT( $tracks_update_url, Content => $test_data );
173 $req->content_type('text/x-json');
174 $mech->request($req);
175 cmp_ok( $mech->status, '==', 400,
176 'Attempt to update three nonexisting tracks fails' );
177 my $response = $json->decode( $mech->content );
178 is( $response->{success}, JSON::MaybeXS::false,
179 'success property returns unquoted false' );
181 $response->{messages}->[0],
182 qr/No object found for id/,
183 'correct message returned'