|
Lines 32-37
use Koha::Database;
Link Here
|
| 32 |
|
32 |
|
| 33 |
my $schema = Koha::Database->new->schema; |
33 |
my $schema = Koha::Database->new->schema; |
| 34 |
my $builder = t::lib::TestBuilder->new; |
34 |
my $builder = t::lib::TestBuilder->new; |
|
|
35 |
|
| 35 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
36 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
| 36 |
# this affects the other REST api tests |
37 |
# this affects the other REST api tests |
| 37 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
38 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
|
Lines 41-47
my $t = Test::Mojo->new('Koha::REST::V1');
Link Here
|
| 41 |
|
42 |
|
| 42 |
subtest 'list() tests' => sub { |
43 |
subtest 'list() tests' => sub { |
| 43 |
|
44 |
|
| 44 |
plan tests => 19; |
45 |
plan tests => 18; |
| 45 |
|
46 |
|
| 46 |
$schema->storage->txn_begin; |
47 |
$schema->storage->txn_begin; |
| 47 |
|
48 |
|
|
Lines 82-88
subtest 'list() tests' => sub {
Link Here
|
| 82 |
$t->ua->build_tx( GET => "/api/v1/cities?city_country=" . $city_country ); |
83 |
$t->ua->build_tx( GET => "/api/v1/cities?city_country=" . $city_country ); |
| 83 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
84 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 84 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
85 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 85 |
$t->request_ok($tx)->status_is(200)->json_is( [ $city, $another_city ] ); |
86 |
my $result = |
|
|
87 |
$t->request_ok($tx)->status_is(200)->json_is( [ $city, $another_city ] ); |
| 86 |
|
88 |
|
| 87 |
$tx = $t->ua->build_tx( |
89 |
$tx = $t->ua->build_tx( |
| 88 |
GET => "/api/v1/cities?city_name=" . $city->{city_name} ); |
90 |
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 } ); |
92 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 91 |
$t->request_ok($tx)->status_is(200)->json_is( [$city] ); |
93 |
$t->request_ok($tx)->status_is(200)->json_is( [$city] ); |
| 92 |
|
94 |
|
|
|
95 |
# Warn on unsupported query parameter |
| 93 |
$tx = $t->ua->build_tx( GET => '/api/v1/cities?city_blah=blah' ); |
96 |
$tx = $t->ua->build_tx( GET => '/api/v1/cities?city_blah=blah' ); |
| 94 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
97 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 95 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
98 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 96 |
|
99 |
$t->request_ok($tx)->status_is(400) |
| 97 |
warning_like { |
100 |
->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 |
|
101 |
|
| 103 |
$schema->storage->txn_rollback; |
102 |
$schema->storage->txn_rollback; |
| 104 |
}; |
103 |
}; |
|
Lines 130-136
subtest 'get() tests' => sub {
Link Here
|
| 130 |
|
129 |
|
| 131 |
subtest 'add() tests' => sub { |
130 |
subtest 'add() tests' => sub { |
| 132 |
|
131 |
|
| 133 |
plan tests => 10; |
132 |
plan tests => 11; |
| 134 |
|
133 |
|
| 135 |
$schema->storage->txn_begin; |
134 |
$schema->storage->txn_begin; |
| 136 |
|
135 |
|
|
Lines 163-168
subtest 'add() tests' => sub {
Link Here
|
| 163 |
->json_is( '/city_zipcode' => $city->{city_zipcode} ) |
162 |
->json_is( '/city_zipcode' => $city->{city_zipcode} ) |
| 164 |
->json_is( '/city_country' => $city->{city_country} ); |
163 |
->json_is( '/city_country' => $city->{city_country} ); |
| 165 |
|
164 |
|
|
|
165 |
# Authorized attempt to write invalid data |
| 166 |
my $city_with_invalid_field = { |
166 |
my $city_with_invalid_field = { |
| 167 |
city_blah => "City Blah", |
167 |
city_blah => "City Blah", |
| 168 |
city_state => "City State", |
168 |
city_state => "City State", |
|
Lines 170-189
subtest 'add() tests' => sub {
Link Here
|
| 170 |
city_country => "City Country" |
170 |
city_country => "City Country" |
| 171 |
}; |
171 |
}; |
| 172 |
|
172 |
|
| 173 |
# Authorized attempt to write invalid data |
|
|
| 174 |
$tx = $t->ua->build_tx( |
173 |
$tx = $t->ua->build_tx( |
| 175 |
POST => "/api/v1/cities/" => json => $city_with_invalid_field ); |
174 |
POST => "/api/v1/cities/" => json => $city_with_invalid_field ); |
| 176 |
$tx->req->cookies( |
175 |
$tx->req->cookies( |
| 177 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
176 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
| 178 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
177 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 179 |
$t->request_ok($tx)->status_is(500); |
178 |
$t->request_ok($tx)->status_is(400)->json_is( |
|
|
179 |
"/errors" => [ |
| 180 |
{ |
| 181 |
message => "Properties not allowed: city_blah.", |
| 182 |
path => "/body" |
| 183 |
} |
| 184 |
] |
| 185 |
); |
| 180 |
|
186 |
|
| 181 |
$schema->storage->txn_rollback; |
187 |
$schema->storage->txn_rollback; |
| 182 |
}; |
188 |
}; |
| 183 |
|
189 |
|
| 184 |
subtest 'update() tests' => sub { |
190 |
subtest 'update() tests' => sub { |
| 185 |
|
191 |
|
| 186 |
plan tests => 10; |
192 |
plan tests => 13; |
| 187 |
|
193 |
|
| 188 |
$schema->storage->txn_begin; |
194 |
$schema->storage->txn_begin; |
| 189 |
|
195 |
|
|
Lines 202-227
subtest 'update() tests' => sub {
Link Here
|
| 202 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
208 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 203 |
$t->request_ok($tx)->status_is(403); |
209 |
$t->request_ok($tx)->status_is(403); |
| 204 |
|
210 |
|
|
|
211 |
# Attempt partial update on a PUT |
| 212 |
my $city_with_missing_field = { |
| 213 |
city_name => 'New name', |
| 214 |
city_state => 'New state', |
| 215 |
city_country => 'New country' |
| 216 |
}; |
| 217 |
|
| 205 |
$tx = $t->ua->build_tx( |
218 |
$tx = $t->ua->build_tx( |
| 206 |
PUT => "/api/v1/cities/$city_id" => json => { city_name => 'New name' } |
219 |
PUT => "/api/v1/cities/$city_id" => json => $city_with_missing_field ); |
| 207 |
); |
|
|
| 208 |
$tx->req->cookies( |
220 |
$tx->req->cookies( |
| 209 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
221 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
| 210 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
222 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 211 |
$t->request_ok($tx)->status_is(200)->json_is( '/city_name' => 'New name' ); |
223 |
$t->request_ok($tx)->status_is(400) |
|
|
224 |
->json_is( "/errors" => |
| 225 |
[ { message => "Missing property.", path => "/body/city_zipcode" } ] |
| 226 |
); |
| 227 |
|
| 228 |
# Full object update on PUT |
| 229 |
my $city_with_updated_field = { |
| 230 |
city_name => "London", |
| 231 |
city_state => "City State", |
| 232 |
city_zipcode => "City Zipcode", |
| 233 |
city_country => "City Country" |
| 234 |
}; |
| 212 |
|
235 |
|
| 213 |
$tx = $t->ua->build_tx( |
236 |
$tx = $t->ua->build_tx( |
| 214 |
PUT => "/api/v1/cities/$city_id" => json => { city_blah => 'New blah' } |
237 |
PUT => "/api/v1/cities/$city_id" => json => $city_with_updated_field ); |
| 215 |
); |
|
|
| 216 |
$tx->req->cookies( |
238 |
$tx->req->cookies( |
| 217 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
239 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
| 218 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
240 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 219 |
$t->request_ok($tx)->status_is(500) |
241 |
$t->request_ok($tx)->status_is(200)->json_is( '/city_name' => 'London' ); |
| 220 |
->json_is( '/error' => "No method city_blah for Koha::City" ); |
242 |
|
|
|
243 |
# Authorized attempt to write invalid data |
| 244 |
my $city_with_invalid_field = { |
| 245 |
city_blah => "City Blah", |
| 246 |
city_state => "City State", |
| 247 |
city_zipcode => "City Zipcode", |
| 248 |
city_country => "City Country" |
| 249 |
}; |
| 250 |
|
| 251 |
$tx = $t->ua->build_tx( |
| 252 |
PUT => "/api/v1/cities/$city_id" => json => $city_with_invalid_field ); |
| 253 |
$tx->req->cookies( |
| 254 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
| 255 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 256 |
$t->request_ok($tx)->status_is(400)->json_is( |
| 257 |
"/errors" => [ |
| 258 |
{ |
| 259 |
message => "Properties not allowed: city_blah.", |
| 260 |
path => "/body" |
| 261 |
} |
| 262 |
] |
| 263 |
); |
| 221 |
|
264 |
|
| 222 |
my $non_existent_id = $city_id + 1; |
265 |
my $non_existent_id = $city_id + 1; |
| 223 |
$tx = $t->ua->build_tx( PUT => "/api/v1/cities/$non_existent_id" => json => |
266 |
$tx = |
| 224 |
{ city_name => 'New name' } ); |
267 |
$t->ua->build_tx( PUT => "/api/v1/cities/$non_existent_id" => json => |
|
|
268 |
$city_with_updated_field ); |
| 225 |
$tx->req->cookies( |
269 |
$tx->req->cookies( |
| 226 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
270 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
| 227 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
271 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 228 |
- |
|
|