remove trailing newlines from error messages
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / update.t
CommitLineData
d2739840 1use 5.6.0;
2
3use strict;
4use warnings;
5
6use lib 't/lib';
7
8my $base = 'http://localhost';
9my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
10
11use RestTest;
12use DBICTest;
13use Test::More;
14use Test::WWW::Mechanize::Catalyst 'RestTest';
15use HTTP::Request::Common;
16use JSON::Any;
17
18my $mech = Test::WWW::Mechanize::Catalyst->new;
19ok(my $schema = DBICTest->init_schema(), 'got schema');
20
21my $track = $schema->resultset('Track')->first;
22my %original_cols = $track->get_columns;
23
24my $track_update_url = "$base/api/rpc/track/id/" . $track->id . "/update";
25my $any_track_update_url = "$base/api/rpc/any/track/id/" . $track->id . "/update";
0b8c7370 26my $tracks_update_url = "$base/api/rpc/track/update";
d2739840 27
28# test invalid track id caught
29{
30 foreach my $wrong_id ('sdsdsdsd', 3434234) {
31 my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
32 my $req = POST( $incorrect_url, {
33 title => 'value'
34 });
35
36 $mech->request($req, $content_type);
37 cmp_ok( $mech->status, '==', 400, 'Attempt with invalid track id caught' );
38
39 my $response = JSON::Any->Load( $mech->content);
40 like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
41
42 $track->discard_changes;
43 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
44 }
45}
46
47# validation when no params sent
48{
49 my $req = POST( $track_update_url, {
50 wrong_param => 'value'
51 });
52 $mech->request($req, $content_type);
53 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
54
55 my $response = JSON::Any->Load( $mech->content);
56 is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
57
58 $track->discard_changes;
59 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
60}
61
62{
63 my $req = POST( $track_update_url, {
64 wrong_param => 'value'
65 });
66 $mech->request($req, $content_type);
67 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
68
69 my $response = JSON::Any->Load( $mech->content);
70 is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
71
72 $track->discard_changes;
73 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
74}
75
76{
77 my $req = POST( $track_update_url, {
78 title => undef
79 });
80 $mech->request($req, $content_type);
81 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
82
83 $track->discard_changes;
84 isnt($track->title, $original_cols{title}, 'Title changed');
85 is($track->title, '', 'Title changed to undef');
86}
87
88{
89 my $req = POST( $track_update_url, {
90 title => 'monkey monkey'
91 });
92 $mech->request($req, $content_type);
93 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
94
95 $track->discard_changes;
96 is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"');
97}
98
99{
100 my $req = POST( $track_update_url, {
101 title => 'sheep sheep',
102 'cd.year' => '2009'
103 });
104 $mech->request($req, $content_type);
105 cmp_ok( $mech->status, '==', 200, 'Update with key with value and related key okay' );
106
107 $track->discard_changes;
108 is($track->title, 'sheep sheep', 'Title changed');
109 is($track->cd->year, '2009', 'Related field changed"');
110}
111
112{
113 my $req = POST( $any_track_update_url, {
114 title => 'baa'
115 });
116 $mech->request($req, $content_type);
117 cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
118
119 $track->discard_changes;
120 is($track->title, 'baa', 'Title changed');
121}
122
123{
124 my $req = POST( $any_track_update_url, {
125 position => '14'
126 });
127 $mech->request($req, $content_type);
128 cmp_ok( $mech->status, '==', 200, 'Position update okay' );
129
130 $track->discard_changes;
131 is($track->get_column('position'), '14', 'Position changed');
132}
133
7c8cb609 134# bulk_update existing objects
0b8c7370 135{
136 # order to get a stable order of rows
137 my $tracks_rs = $schema->resultset('Track')->search(undef, { order_by => 'trackid', rows => 3 });
138 my $test_data = JSON::Any->Dump({ list => [map +{ id => $_->id, title => 'Track ' . $_->id }, $tracks_rs->all] });
139 my $req = POST( $tracks_update_url, Content => $test_data );
140 $req->content_type('text/x-json');
141 $mech->request($req);
142 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
143
144 $tracks_rs->reset;
145 while (my $track = $tracks_rs->next) {
146 is($track->title, 'Track ' . $track->id, 'Title changed');
147 }
148}
149
7c8cb609 150# bulk_update nonexisting objects
151{
152 # order to get a stable order of rows
153 my $test_data = JSON::Any->Dump({ list => [map +{ id => $_, title => 'Track ' . $_ }, (1000..1002)] });
154 my $req = POST( $tracks_update_url, Content => $test_data );
155 $req->content_type('text/x-json');
156 $mech->request($req);
157 cmp_ok( $mech->status, '==', 400, 'Attempt to update three nonexisting tracks fails' );
158 my $response = JSON::Any->Load( $mech->content);
0cd7ab9e 159 is( $response->{success}, 'false', 'success property returns quoted false' );
7c8cb609 160 like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
161}
162
d2739840 163done_testing();