use JSON instead of JSON::Any to get rid of the CPAN Testers failures when only JSON...
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / update.t
1 use 5.6.0;
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib';
7
8 my $base = 'http://localhost';
9 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
10
11 use RestTest;
12 use DBICTest;
13 use Test::More;
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
16 use JSON;
17
18 my $json = JSON->new->utf8;
19
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
22
23 my $track         = $schema->resultset('Track')->first;
24 my %original_cols = $track->get_columns;
25
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";
30
31 # test invalid track id caught
32 {
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' } );
36
37         $mech->request( $req, $content_type );
38         cmp_ok( $mech->status, '==', 400,
39             'Attempt with invalid track id caught' );
40
41         my $response = $json->decode( $mech->content );
42         like(
43             $response->{messages}->[0],
44             qr/No object found for id/,
45             'correct message returned'
46         );
47
48         $track->discard_changes;
49         is_deeply(
50             { $track->get_columns },
51             \%original_cols,
52             'no update occurred'
53         );
54     }
55 }
56
57 # validation when no params sent
58 {
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' );
62
63     my $response = $json->decode( $mech->content );
64     is_deeply( $response->{messages}, ['No valid keys passed'],
65         'correct message returned' );
66
67     $track->discard_changes;
68     is_deeply(
69         { $track->get_columns },
70         \%original_cols,
71         'no update occurred'
72     );
73 }
74
75 {
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' );
79
80     my $response = $json->decode( $mech->content );
81     is_deeply( $response->{messages}, ['No valid keys passed'],
82         'correct message returned' );
83
84     $track->discard_changes;
85     is_deeply(
86         { $track->get_columns },
87         \%original_cols,
88         'no update occurred'
89     );
90 }
91
92 {
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' );
96
97     $track->discard_changes;
98     isnt( $track->title, $original_cols{title}, 'Title changed' );
99     is( $track->title, '', 'Title changed to undef' );
100 }
101
102 {
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' );
106
107     $track->discard_changes;
108     is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' );
109 }
110
111 {
112     my $req = POST(
113         $track_update_url,
114         {   title     => 'sheep sheep',
115             'cd.year' => '2009'
116         }
117     );
118     $mech->request( $req, $content_type );
119     cmp_ok( $mech->status, '==', 200,
120         'Update with key with value and related key okay' );
121
122     $track->discard_changes;
123     is( $track->title,    'sheep sheep', 'Title changed' );
124     is( $track->cd->year, '2009',        'Related field changed"' );
125 }
126
127 {
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' );
131
132     $track->discard_changes;
133     is( $track->title, 'baa', 'Title changed' );
134 }
135
136 {
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' );
140
141     $track->discard_changes;
142     is( $track->get_column('position'), '14', 'Position changed' );
143 }
144
145 # bulk_update existing objects
146 {
147
148     # order to get a stable order of rows
149     my $tracks_rs =
150         $schema->resultset('Track')
151         ->search( undef, { order_by => 'trackid', rows => 3 } );
152     my $test_data = $json->encode(
153         {   list => [
154                 map +{ id => $_->id, title => 'Track ' . $_->id },
155                 $tracks_rs->all
156             ]
157         }
158     );
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' );
163
164     $tracks_rs->reset;
165     while ( my $track = $tracks_rs->next ) {
166         is( $track->title, 'Track ' . $track->id, 'Title changed' );
167     }
168 }
169
170 # bulk_update nonexisting objects
171 {
172
173     # order to get a stable order of rows
174     my $test_data = $json->encode(
175         {   list => [
176                 map +{ id => $_, title => 'Track ' . $_ },
177                 ( 1000 .. 1002 )
178             ]
179         }
180     );
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' );
189     like(
190         $response->{messages}->[0],
191         qr/No object found for id/,
192         'correct message returned'
193     );
194 }
195
196 done_testing();