Commit | Line | Data |
d2739840 |
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; |
0b8c7370 |
13 | use Test::More; |
d2739840 |
14 | use Test::WWW::Mechanize::Catalyst 'RestTest'; |
15 | use HTTP::Request::Common; |
0b0bf911 |
16 | use JSON; |
17 | |
18 | my $json = JSON->new->utf8; |
d2739840 |
19 | |
20 | my $mech = Test::WWW::Mechanize::Catalyst->new; |
0b0bf911 |
21 | ok( my $schema = DBICTest->init_schema(), 'got schema' ); |
d2739840 |
22 | |
0b0bf911 |
23 | my $track = $schema->resultset('Track')->first; |
d2739840 |
24 | my %original_cols = $track->get_columns; |
25 | |
0b0bf911 |
26 | my $track_url = "$base/api/rest/track/"; |
27 | my $track_update_url = $track_url . $track->id; |
0b8c7370 |
28 | my $tracks_update_url = $track_url; |
d2739840 |
29 | |
30 | # test invalid track id caught |
31 | { |
0b0bf911 |
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'); |
37 | $mech->request($req); |
38 | |
39 | cmp_ok( $mech->status, '==', 400, |
40 | 'Attempt with invalid track id caught' ); |
41 | |
42 | my $response = $json->decode( $mech->content ); |
43 | like( |
44 | $response->{messages}->[0], |
45 | qr/No object found for id/, |
46 | 'correct message returned' |
47 | ); |
48 | |
49 | $track->discard_changes; |
50 | is_deeply( |
51 | { $track->get_columns }, |
52 | \%original_cols, |
53 | 'no update occurred' |
54 | ); |
55 | } |
d2739840 |
56 | } |
57 | |
58 | # validation when no params sent |
59 | { |
0b0bf911 |
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'); |
63 | $mech->request($req); |
64 | |
65 | cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' ); |
66 | |
67 | my $response = $json->decode( $mech->content ); |
68 | is_deeply( $response->{messages}, ['No valid keys passed'], |
69 | 'correct message returned' ); |
70 | |
71 | $track->discard_changes; |
72 | is_deeply( |
73 | { $track->get_columns }, |
74 | \%original_cols, |
75 | 'no update occurred' |
76 | ); |
d2739840 |
77 | } |
78 | |
79 | { |
0b0bf911 |
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'); |
83 | $mech->request($req); |
84 | cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' ); |
85 | |
86 | $track->discard_changes; |
87 | isnt( $track->title, $original_cols{title}, 'Title changed' ); |
88 | is( $track->title, undef, 'Title changed to undef' ); |
d2739840 |
89 | } |
90 | |
91 | { |
0b0bf911 |
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'); |
96 | $mech->request($req); |
d2739840 |
97 | |
0b0bf911 |
98 | cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' ); |
d2739840 |
99 | |
0b0bf911 |
100 | $track->discard_changes; |
101 | is( $track->title, 'monkey monkey', 'Title changed to "monkey monkey"' ); |
102 | is( $track->cd->year, 2009, 'related row updated' ); |
d2739840 |
103 | } |
0b8c7370 |
104 | |
7c8cb609 |
105 | # bulk_update existing objects |
0b8c7370 |
106 | { |
0b0bf911 |
107 | |
108 | # order to get a stable order of rows |
109 | my $tracks_rs = |
110 | $schema->resultset('Track') |
111 | ->search( undef, { order_by => 'trackid', rows => 3 } ); |
112 | my $test_data = $json->encode( |
113 | { list => [ |
114 | map +{ id => $_->id, title => 'Track ' . $_->id }, |
115 | $tracks_rs->all |
116 | ] |
117 | } |
118 | ); |
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' ); |
123 | |
124 | $tracks_rs->reset; |
125 | while ( my $track = $tracks_rs->next ) { |
126 | is( $track->title, 'Track ' . $track->id, 'Title changed' ); |
127 | } |
0b8c7370 |
128 | } |
129 | |
7c8cb609 |
130 | # bulk_update nonexisting objects |
131 | { |
0b0bf911 |
132 | |
133 | # order to get a stable order of rows |
134 | my $test_data = $json->encode( |
135 | { list => [ |
136 | map +{ id => $_, title => 'Track ' . $_ }, |
137 | ( 1000 .. 1002 ) |
138 | ] |
139 | } |
140 | ); |
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' ); |
149 | like( |
150 | $response->{messages}->[0], |
151 | qr/No object found for id/, |
152 | 'correct message returned' |
153 | ); |
7c8cb609 |
154 | } |
155 | |
0b8c7370 |
156 | done_testing(); |