Lines 24-37
use Test::Warn;
Link Here
|
24 |
use t::lib::TestBuilder; |
24 |
use t::lib::TestBuilder; |
25 |
use t::lib::Mocks; |
25 |
use t::lib::Mocks; |
26 |
|
26 |
|
27 |
use Data::Printer colored => 1; |
|
|
28 |
|
29 |
use C4::Auth; |
27 |
use C4::Auth; |
30 |
use Koha::Cities; |
28 |
use Koha::Cities; |
31 |
use Koha::Database; |
29 |
use Koha::Database; |
32 |
|
30 |
|
33 |
my $schema = Koha::Database->new->schema; |
31 |
my $schema = Koha::Database->new->schema; |
34 |
my $builder = t::lib::TestBuilder->new; |
32 |
my $builder = t::lib::TestBuilder->new; |
|
|
33 |
|
35 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
34 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
36 |
# this affects the other REST api tests |
35 |
# this affects the other REST api tests |
37 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
36 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
Lines 41-47
my $t = Test::Mojo->new('Koha::REST::V1');
Link Here
|
41 |
|
40 |
|
42 |
subtest 'list() tests' => sub { |
41 |
subtest 'list() tests' => sub { |
43 |
|
42 |
|
44 |
plan tests => 19; |
43 |
plan tests => 18; |
45 |
|
44 |
|
46 |
$schema->storage->txn_begin; |
45 |
$schema->storage->txn_begin; |
47 |
|
46 |
|
Lines 82-88
subtest 'list() tests' => sub {
Link Here
|
82 |
$t->ua->build_tx( GET => "/api/v1/cities?city_country=" . $city_country ); |
81 |
$t->ua->build_tx( GET => "/api/v1/cities?city_country=" . $city_country ); |
83 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
82 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
84 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
83 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
85 |
$t->request_ok($tx)->status_is(200)->json_is( [ $city, $another_city ] ); |
84 |
my $result = |
|
|
85 |
$t->request_ok($tx)->status_is(200)->json_is( [ $city, $another_city ] ); |
86 |
|
86 |
|
87 |
$tx = $t->ua->build_tx( |
87 |
$tx = $t->ua->build_tx( |
88 |
GET => "/api/v1/cities?city_name=" . $city->{city_name} ); |
88 |
GET => "/api/v1/cities?city_name=" . $city->{city_name} ); |
Lines 90-104
subtest 'list() tests' => sub {
Link Here
|
90 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
90 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
91 |
$t->request_ok($tx)->status_is(200)->json_is( [$city] ); |
91 |
$t->request_ok($tx)->status_is(200)->json_is( [$city] ); |
92 |
|
92 |
|
|
|
93 |
# Warn on unsupported query parameter |
93 |
$tx = $t->ua->build_tx( GET => '/api/v1/cities?city_blah=blah' ); |
94 |
$tx = $t->ua->build_tx( GET => '/api/v1/cities?city_blah=blah' ); |
94 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
95 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
95 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
96 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
96 |
|
97 |
$t->request_ok($tx)->status_is(400) |
97 |
warning_like { |
98 |
->json_is( [{ path => '/query/city_blah', message => 'Malformed query string'}] ); |
98 |
$t->request_ok($tx)->status_is(500) |
|
|
99 |
->json_like( '/error' => qr/Unknown column/ ); |
100 |
} |
101 |
qr/Unknown column/, 'Wrong parameters raise warnings'; |
102 |
|
99 |
|
103 |
$schema->storage->txn_rollback; |
100 |
$schema->storage->txn_rollback; |
104 |
}; |
101 |
}; |
Lines 130-136
subtest 'get() tests' => sub {
Link Here
|
130 |
|
127 |
|
131 |
subtest 'add() tests' => sub { |
128 |
subtest 'add() tests' => sub { |
132 |
|
129 |
|
133 |
plan tests => 10; |
130 |
plan tests => 17; |
134 |
|
131 |
|
135 |
$schema->storage->txn_begin; |
132 |
$schema->storage->txn_begin; |
136 |
|
133 |
|
Lines 152-189
subtest 'add() tests' => sub {
Link Here
|
152 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
149 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
153 |
$t->request_ok($tx)->status_is(403); |
150 |
$t->request_ok($tx)->status_is(403); |
154 |
|
151 |
|
|
|
152 |
# Authorized attempt to write invalid data |
153 |
my $city_with_invalid_field = { |
154 |
city_blah => "City Blah", |
155 |
city_state => "City State", |
156 |
city_zipcode => "City Zipcode", |
157 |
city_country => "City Country" |
158 |
}; |
159 |
|
160 |
$tx = $t->ua->build_tx( |
161 |
POST => "/api/v1/cities/" => json => $city_with_invalid_field ); |
162 |
$tx->req->cookies( |
163 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
164 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
165 |
$t->request_ok($tx)->status_is(400)->json_is( |
166 |
"/errors" => [ |
167 |
{ |
168 |
message => "Properties not allowed: city_blah.", |
169 |
path => "/body" |
170 |
} |
171 |
] |
172 |
); |
173 |
|
155 |
# Authorized attempt to write |
174 |
# Authorized attempt to write |
156 |
$tx = $t->ua->build_tx( POST => "/api/v1/cities/" => json => $city ); |
175 |
$tx = $t->ua->build_tx( POST => "/api/v1/cities/" => json => $city ); |
157 |
$tx->req->cookies( |
176 |
$tx->req->cookies( |
158 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
177 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
159 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
178 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
160 |
$t->request_ok($tx)->status_is(200) |
179 |
my $cityid = $t->request_ok($tx)->status_is(200) |
161 |
->json_is( '/city_name' => $city->{city_name} ) |
180 |
->json_is( '/city_name' => $city->{city_name} ) |
162 |
->json_is( '/city_state' => $city->{city_state} ) |
181 |
->json_is( '/city_state' => $city->{city_state} ) |
163 |
->json_is( '/city_zipcode' => $city->{city_zipcode} ) |
182 |
->json_is( '/city_zipcode' => $city->{city_zipcode} ) |
164 |
->json_is( '/city_country' => $city->{city_country} ); |
183 |
->json_is( '/city_country' => $city->{city_country} ) |
|
|
184 |
->tx->res->json->{cityid}; |
165 |
|
185 |
|
166 |
my $city_with_invalid_field = { |
186 |
# Authorized attempt to create with null id |
167 |
city_blah => "City Blah", |
187 |
$city->{cityid} = undef; |
168 |
city_state => "City State", |
188 |
$tx = $t->ua->build_tx( |
169 |
city_zipcode => "City Zipcode", |
189 |
POST => "/api/v1/cities/" => json => $city ); |
170 |
city_country => "City Country" |
190 |
$tx->req->cookies( |
171 |
}; |
191 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
|
|
192 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
193 |
$t->request_ok($tx)->status_is(400)->json_has('/errors'); |
172 |
|
194 |
|
173 |
# Authorized attempt to write invalid data |
195 |
# Authorized attempt to create with existing id |
|
|
196 |
$city->{cityid} = $cityid; |
174 |
$tx = $t->ua->build_tx( |
197 |
$tx = $t->ua->build_tx( |
175 |
POST => "/api/v1/cities/" => json => $city_with_invalid_field ); |
198 |
POST => "/api/v1/cities/" => json => $city ); |
176 |
$tx->req->cookies( |
199 |
$tx->req->cookies( |
177 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
200 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
178 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
201 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
179 |
$t->request_ok($tx)->status_is(500); |
202 |
$t->request_ok($tx)->status_is(400)->json_is( |
|
|
203 |
"/errors" => [ |
204 |
{ |
205 |
message => "Read-only.", |
206 |
path => "/body/cityid" |
207 |
} |
208 |
] |
209 |
); |
180 |
|
210 |
|
181 |
$schema->storage->txn_rollback; |
211 |
$schema->storage->txn_rollback; |
182 |
}; |
212 |
}; |
183 |
|
213 |
|
184 |
subtest 'update() tests' => sub { |
214 |
subtest 'update() tests' => sub { |
185 |
|
215 |
|
186 |
plan tests => 10; |
216 |
plan tests => 15; |
187 |
|
217 |
|
188 |
$schema->storage->txn_begin; |
218 |
$schema->storage->txn_begin; |
189 |
|
219 |
|
Lines 202-233
subtest 'update() tests' => sub {
Link Here
|
202 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
232 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
203 |
$t->request_ok($tx)->status_is(403); |
233 |
$t->request_ok($tx)->status_is(403); |
204 |
|
234 |
|
|
|
235 |
# Attempt partial update on a PUT |
236 |
my $city_with_missing_field = { |
237 |
city_name => 'New name', |
238 |
city_state => 'New state', |
239 |
city_country => 'New country' |
240 |
}; |
241 |
|
205 |
$tx = $t->ua->build_tx( |
242 |
$tx = $t->ua->build_tx( |
206 |
PUT => "/api/v1/cities/$city_id" => json => { city_name => 'New name' } |
243 |
PUT => "/api/v1/cities/$city_id" => json => $city_with_missing_field ); |
207 |
); |
|
|
208 |
$tx->req->cookies( |
244 |
$tx->req->cookies( |
209 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
245 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
210 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
246 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
211 |
$t->request_ok($tx)->status_is(200)->json_is( '/city_name' => 'New name' ); |
247 |
$t->request_ok($tx)->status_is(400) |
|
|
248 |
->json_is( "/errors" => |
249 |
[ { message => "Missing property.", path => "/body/city_zipcode" } ] |
250 |
); |
251 |
|
252 |
# Full object update on PUT |
253 |
my $city_with_updated_field = { |
254 |
city_name => "London", |
255 |
city_state => "City State", |
256 |
city_zipcode => "City Zipcode", |
257 |
city_country => "City Country" |
258 |
}; |
212 |
|
259 |
|
213 |
$tx = $t->ua->build_tx( |
260 |
$tx = $t->ua->build_tx( |
214 |
PUT => "/api/v1/cities/$city_id" => json => { city_blah => 'New blah' } |
261 |
PUT => "/api/v1/cities/$city_id" => json => $city_with_updated_field ); |
215 |
); |
|
|
216 |
$tx->req->cookies( |
262 |
$tx->req->cookies( |
217 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
263 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
218 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
264 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
219 |
$t->request_ok($tx)->status_is(500) |
265 |
$t->request_ok($tx)->status_is(200)->json_is( '/city_name' => 'London' ); |
220 |
->json_is( '/error' => "No method city_blah for Koha::City" ); |
266 |
|
|
|
267 |
# Authorized attempt to write invalid data |
268 |
my $city_with_invalid_field = { |
269 |
city_blah => "City Blah", |
270 |
city_state => "City State", |
271 |
city_zipcode => "City Zipcode", |
272 |
city_country => "City Country" |
273 |
}; |
274 |
|
275 |
$tx = $t->ua->build_tx( |
276 |
PUT => "/api/v1/cities/$city_id" => json => $city_with_invalid_field ); |
277 |
$tx->req->cookies( |
278 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
279 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
280 |
$t->request_ok($tx)->status_is(400)->json_is( |
281 |
"/errors" => [ |
282 |
{ |
283 |
message => "Properties not allowed: city_blah.", |
284 |
path => "/body" |
285 |
} |
286 |
] |
287 |
); |
221 |
|
288 |
|
222 |
my $non_existent_id = $city_id + 1; |
289 |
my $non_existent_id = $city_id + 1; |
223 |
$tx = $t->ua->build_tx( PUT => "/api/v1/cities/$non_existent_id" => json => |
290 |
$tx = |
224 |
{ city_name => 'New name' } ); |
291 |
$t->ua->build_tx( PUT => "/api/v1/cities/$non_existent_id" => json => |
|
|
292 |
$city_with_updated_field ); |
225 |
$tx->req->cookies( |
293 |
$tx->req->cookies( |
226 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
294 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
227 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
295 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
228 |
$t->request_ok($tx)->status_is(404); |
296 |
$t->request_ok($tx)->status_is(404); |
229 |
|
297 |
|
230 |
$schema->storage->txn_rollback; |
298 |
$schema->storage->txn_rollback; |
|
|
299 |
|
300 |
# Wrong mathod (POST) |
301 |
$city_with_updated_field->{cityid} = 2; |
302 |
|
303 |
$tx = $t->ua->build_tx( |
304 |
POST => "/api/v1/cities/$city_id" => json => $city_with_updated_field ); |
305 |
$tx->req->cookies( |
306 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
307 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
308 |
$t->request_ok($tx)->status_is(404); |
231 |
}; |
309 |
}; |
232 |
|
310 |
|
233 |
subtest 'delete() tests' => sub { |
311 |
subtest 'delete() tests' => sub { |
234 |
- |
|
|