|
Lines 19-68
use Modern::Perl;
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Test::More tests => 5; |
20 |
use Test::More tests => 5; |
| 21 |
use Test::Mojo; |
21 |
use Test::Mojo; |
| 22 |
use Test::Warn; |
|
|
| 23 |
|
22 |
|
| 24 |
use t::lib::TestBuilder; |
23 |
use t::lib::TestBuilder; |
| 25 |
use t::lib::Mocks; |
24 |
use t::lib::Mocks; |
| 26 |
|
25 |
|
| 27 |
use C4::Auth; |
|
|
| 28 |
use Koha::Cities; |
26 |
use Koha::Cities; |
| 29 |
use Koha::Database; |
27 |
use Koha::Database; |
| 30 |
|
28 |
|
| 31 |
my $schema = Koha::Database->new->schema; |
29 |
my $schema = Koha::Database->new->schema; |
| 32 |
my $builder = t::lib::TestBuilder->new; |
30 |
my $builder = t::lib::TestBuilder->new; |
| 33 |
|
31 |
|
| 34 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
32 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 35 |
# this affects the other REST api tests |
33 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 36 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
|
|
| 37 |
|
| 38 |
my $remote_address = '127.0.0.1'; |
| 39 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 40 |
|
34 |
|
| 41 |
subtest 'list() tests' => sub { |
35 |
subtest 'list() tests' => sub { |
| 42 |
|
36 |
|
| 43 |
plan tests => 18; |
37 |
plan tests => 20; |
| 44 |
|
38 |
|
| 45 |
$schema->storage->txn_begin; |
39 |
$schema->storage->txn_begin; |
| 46 |
|
40 |
|
| 47 |
Koha::Cities->search->delete; |
41 |
Koha::Cities->search->delete; |
| 48 |
my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 1 }); |
42 |
|
|
|
43 |
my $librarian = $builder->build_object( |
| 44 |
{ |
| 45 |
class => 'Koha::Patrons', |
| 46 |
value => { flags => 2 ** 2 } # catalogue flag = 2 |
| 47 |
} |
| 48 |
); |
| 49 |
my $password = 'thePassword123'; |
| 50 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 51 |
my $userid = $librarian->userid; |
| 52 |
|
| 53 |
my $patron = $builder->build_object( |
| 54 |
{ |
| 55 |
class => 'Koha::Patrons', |
| 56 |
value => { flags => 0 } |
| 57 |
} |
| 58 |
); |
| 59 |
|
| 60 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 61 |
my $unauth_userid = $patron->userid; |
| 49 |
|
62 |
|
| 50 |
## Authorized user tests |
63 |
## Authorized user tests |
| 51 |
# No cities, so empty array should be returned |
64 |
# No cities, so empty array should be returned |
| 52 |
my $tx = $t->ua->build_tx( GET => '/api/v1/cities' ); |
65 |
$t->get_ok("//$userid:$password@/api/v1/cities") |
| 53 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
|
|
| 54 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 55 |
$t->request_ok($tx) |
| 56 |
->status_is(200) |
66 |
->status_is(200) |
| 57 |
->json_is( [] ); |
67 |
->json_is( [] ); |
| 58 |
|
68 |
|
| 59 |
my $city = $builder->build_object({ class => 'Koha::Cities' }); |
69 |
my $city = $builder->build_object({ class => 'Koha::Cities' }); |
| 60 |
|
70 |
|
| 61 |
# One city created, should get returned |
71 |
# One city created, should get returned |
| 62 |
$tx = $t->ua->build_tx( GET => '/api/v1/cities' ); |
72 |
$t->get_ok("//$userid:$password@/api/v1/cities") |
| 63 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
|
|
| 64 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 65 |
$t->request_ok($tx) |
| 66 |
->status_is(200) |
73 |
->status_is(200) |
| 67 |
->json_is( [Koha::REST::V1::Cities::_to_api( $city->TO_JSON )] ); |
74 |
->json_is( [Koha::REST::V1::Cities::_to_api( $city->TO_JSON )] ); |
| 68 |
|
75 |
|
|
Lines 71-137
subtest 'list() tests' => sub {
Link Here
|
| 71 |
my $city_with_another_country = $builder->build_object({ class => 'Koha::Cities' }); |
78 |
my $city_with_another_country = $builder->build_object({ class => 'Koha::Cities' }); |
| 72 |
|
79 |
|
| 73 |
# Two cities created, they should both be returned |
80 |
# Two cities created, they should both be returned |
| 74 |
$tx = $t->ua->build_tx( GET => '/api/v1/cities' ); |
81 |
$t->get_ok("//$userid:$password@/api/v1/cities") |
| 75 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
82 |
->status_is(200) |
| 76 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
|
|
| 77 |
$t->request_ok($tx)->status_is(200) |
| 78 |
->json_is([Koha::REST::V1::Cities::_to_api($city->TO_JSON), |
83 |
->json_is([Koha::REST::V1::Cities::_to_api($city->TO_JSON), |
| 79 |
Koha::REST::V1::Cities::_to_api($another_city->TO_JSON), |
84 |
Koha::REST::V1::Cities::_to_api($another_city->TO_JSON), |
| 80 |
Koha::REST::V1::Cities::_to_api($city_with_another_country->TO_JSON) |
85 |
Koha::REST::V1::Cities::_to_api($city_with_another_country->TO_JSON) |
| 81 |
] ); |
86 |
] ); |
| 82 |
|
87 |
|
| 83 |
# Filtering works, two cities sharing city_country |
88 |
# Filtering works, two cities sharing city_country |
| 84 |
$tx = $t->ua->build_tx( GET => "/api/v1/cities?country=" . $city->city_country ); |
89 |
$t->get_ok("//$userid:$password@/api/v1/cities?country=" . $city->city_country ) |
| 85 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
|
|
| 86 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 87 |
$t->request_ok($tx) |
| 88 |
->status_is(200) |
90 |
->status_is(200) |
| 89 |
->json_is([ Koha::REST::V1::Cities::_to_api($city->TO_JSON), |
91 |
->json_is([ Koha::REST::V1::Cities::_to_api($city->TO_JSON), |
| 90 |
Koha::REST::V1::Cities::_to_api($another_city->TO_JSON) |
92 |
Koha::REST::V1::Cities::_to_api($another_city->TO_JSON) |
| 91 |
]); |
93 |
]); |
| 92 |
|
94 |
|
| 93 |
$tx = $t->ua->build_tx( GET => "/api/v1/cities?name=" . $city->city_name ); |
95 |
$t->get_ok("//$userid:$password@/api/v1/cities?name=" . $city->city_name ) |
| 94 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
|
|
| 95 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 96 |
$t->request_ok($tx) |
| 97 |
->status_is(200) |
96 |
->status_is(200) |
| 98 |
->json_is( [Koha::REST::V1::Cities::_to_api($city->TO_JSON)] ); |
97 |
->json_is( [Koha::REST::V1::Cities::_to_api($city->TO_JSON)] ); |
| 99 |
|
98 |
|
| 100 |
# Warn on unsupported query parameter |
99 |
# Warn on unsupported query parameter |
| 101 |
$tx = $t->ua->build_tx( GET => '/api/v1/cities?city_blah=blah' ); |
100 |
$t->get_ok("//$userid:$password@/api/v1/cities?city_blah=blah" ) |
| 102 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
|
|
| 103 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 104 |
$t->request_ok($tx) |
| 105 |
->status_is(400) |
101 |
->status_is(400) |
| 106 |
->json_is( [{ path => '/query/city_blah', message => 'Malformed query string'}] ); |
102 |
->json_is( [{ path => '/query/city_blah', message => 'Malformed query string'}] ); |
| 107 |
|
103 |
|
|
|
104 |
# Unauthorized access |
| 105 |
$t->get_ok("//$unauth_userid:$password@/api/v1/cities") |
| 106 |
->status_is(403); |
| 107 |
|
| 108 |
$schema->storage->txn_rollback; |
108 |
$schema->storage->txn_rollback; |
| 109 |
}; |
109 |
}; |
| 110 |
|
110 |
|
| 111 |
subtest 'get() tests' => sub { |
111 |
subtest 'get() tests' => sub { |
| 112 |
|
112 |
|
| 113 |
plan tests => 6; |
113 |
plan tests => 8; |
| 114 |
|
114 |
|
| 115 |
$schema->storage->txn_begin; |
115 |
$schema->storage->txn_begin; |
| 116 |
|
116 |
|
| 117 |
my $city = $builder->build_object({ class => 'Koha::Cities' }); |
117 |
my $city = $builder->build_object({ class => 'Koha::Cities' }); |
| 118 |
my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 1 }); |
118 |
my $librarian = $builder->build_object( |
|
|
119 |
{ |
| 120 |
class => 'Koha::Patrons', |
| 121 |
value => { flags => 2**2 } # catalogue flag = 2 |
| 122 |
} |
| 123 |
); |
| 124 |
my $password = 'thePassword123'; |
| 125 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 126 |
my $userid = $librarian->userid; |
| 119 |
|
127 |
|
| 120 |
my $tx = $t->ua->build_tx( GET => "/api/v1/cities/" . $city->id ); |
128 |
my $patron = $builder->build_object( |
| 121 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
129 |
{ |
| 122 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
130 |
class => 'Koha::Patrons', |
| 123 |
$t->request_ok($tx) |
131 |
value => { flags => 0 } |
|
|
132 |
} |
| 133 |
); |
| 134 |
|
| 135 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 136 |
my $unauth_userid = $patron->userid; |
| 137 |
|
| 138 |
$t->get_ok( "//$userid:$password@/api/v1/cities/" . $city->cityid ) |
| 124 |
->status_is(200) |
139 |
->status_is(200) |
| 125 |
->json_is(Koha::REST::V1::Cities::_to_api($city->TO_JSON)); |
140 |
->json_is(Koha::REST::V1::Cities::_to_api($city->TO_JSON)); |
| 126 |
|
141 |
|
|
|
142 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/cities/" . $city->cityid ) |
| 143 |
->status_is(403); |
| 144 |
|
| 127 |
my $city_to_delete = $builder->build_object({ class => 'Koha::Cities' }); |
145 |
my $city_to_delete = $builder->build_object({ class => 'Koha::Cities' }); |
| 128 |
my $non_existent_id = $city_to_delete->id; |
146 |
my $non_existent_id = $city_to_delete->id; |
| 129 |
$city_to_delete->delete; |
147 |
$city_to_delete->delete; |
| 130 |
|
148 |
|
| 131 |
$tx = $t->ua->build_tx( GET => "/api/v1/cities/" . $non_existent_id ); |
149 |
$t->get_ok( "//$userid:$password@/api/v1/cities/$non_existent_id" ) |
| 132 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
|
|
| 133 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 134 |
$t->request_ok($tx) |
| 135 |
->status_is(404) |
150 |
->status_is(404) |
| 136 |
->json_is( '/error' => 'City not found' ); |
151 |
->json_is( '/error' => 'City not found' ); |
| 137 |
|
152 |
|
|
Lines 144-153
subtest 'add() tests' => sub {
Link Here
|
| 144 |
|
159 |
|
| 145 |
$schema->storage->txn_begin; |
160 |
$schema->storage->txn_begin; |
| 146 |
|
161 |
|
| 147 |
my ( $unauthorized_borrowernumber, $unauthorized_session_id ) = |
162 |
my $librarian = $builder->build_object( |
| 148 |
create_user_and_session( { authorized => 0 } ); |
163 |
{ |
| 149 |
my ( $authorized_borrowernumber, $authorized_session_id ) = |
164 |
class => 'Koha::Patrons', |
| 150 |
create_user_and_session( { authorized => 1 } ); |
165 |
value => { flags => 2**3 } # parameters flag = 2 |
|
|
166 |
} |
| 167 |
); |
| 168 |
my $password = 'thePassword123'; |
| 169 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 170 |
my $userid = $librarian->userid; |
| 171 |
|
| 172 |
my $patron = $builder->build_object( |
| 173 |
{ |
| 174 |
class => 'Koha::Patrons', |
| 175 |
value => { flags => 0 } |
| 176 |
} |
| 177 |
); |
| 178 |
|
| 179 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 180 |
my $unauth_userid = $patron->userid; |
| 181 |
|
| 151 |
my $city = { |
182 |
my $city = { |
| 152 |
name => "City Name", |
183 |
name => "City Name", |
| 153 |
state => "City State", |
184 |
state => "City State", |
|
Lines 156-165
subtest 'add() tests' => sub {
Link Here
|
| 156 |
}; |
187 |
}; |
| 157 |
|
188 |
|
| 158 |
# Unauthorized attempt to write |
189 |
# Unauthorized attempt to write |
| 159 |
my $tx = $t->ua->build_tx( POST => "/api/v1/cities/" => json => $city ); |
190 |
$t->post_ok("//$unauth_userid:$password@/api/v1/cities" => json => $city) |
| 160 |
$tx->req->cookies({ name => 'CGISESSID', value => $unauthorized_session_id }); |
|
|
| 161 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 162 |
$t->request_ok($tx) |
| 163 |
->status_is(403); |
191 |
->status_is(403); |
| 164 |
|
192 |
|
| 165 |
# Authorized attempt to write invalid data |
193 |
# Authorized attempt to write invalid data |
|
Lines 170-179
subtest 'add() tests' => sub {
Link Here
|
| 170 |
country => "City Country" |
198 |
country => "City Country" |
| 171 |
}; |
199 |
}; |
| 172 |
|
200 |
|
| 173 |
$tx = $t->ua->build_tx( POST => "/api/v1/cities/" => json => $city_with_invalid_field ); |
201 |
$t->post_ok( "//$userid:$password@/api/v1/cities" => json => $city_with_invalid_field ) |
| 174 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
|
|
| 175 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 176 |
$t->request_ok($tx) |
| 177 |
->status_is(400) |
202 |
->status_is(400) |
| 178 |
->json_is( |
203 |
->json_is( |
| 179 |
"/errors" => [ |
204 |
"/errors" => [ |
|
Lines 185-195
subtest 'add() tests' => sub {
Link Here
|
| 185 |
); |
210 |
); |
| 186 |
|
211 |
|
| 187 |
# Authorized attempt to write |
212 |
# Authorized attempt to write |
| 188 |
$tx = $t->ua->build_tx( POST => "/api/v1/cities/" => json => $city ); |
|
|
| 189 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
| 190 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 191 |
my $city_id = |
213 |
my $city_id = |
| 192 |
$t->request_ok($tx) |
214 |
$t->post_ok( "//$userid:$password@/api/v1/cities" => json => $city ) |
| 193 |
->status_is( 201, 'SWAGGER3.2.1' ) |
215 |
->status_is( 201, 'SWAGGER3.2.1' ) |
| 194 |
->header_like( |
216 |
->header_like( |
| 195 |
Location => qr|^\/api\/v1\/cities/\d*|, |
217 |
Location => qr|^\/api\/v1\/cities/\d*|, |
|
Lines 203-221
subtest 'add() tests' => sub {
Link Here
|
| 203 |
|
225 |
|
| 204 |
# Authorized attempt to create with null id |
226 |
# Authorized attempt to create with null id |
| 205 |
$city->{city_id} = undef; |
227 |
$city->{city_id} = undef; |
| 206 |
$tx = $t->ua->build_tx( POST => "/api/v1/cities/" => json => $city ); |
228 |
$t->post_ok( "//$userid:$password@/api/v1/cities" => json => $city ) |
| 207 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
|
|
| 208 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 209 |
$t->request_ok($tx) |
| 210 |
->status_is(400) |
229 |
->status_is(400) |
| 211 |
->json_has('/errors'); |
230 |
->json_has('/errors'); |
| 212 |
|
231 |
|
| 213 |
# Authorized attempt to create with existing id |
232 |
# Authorized attempt to create with existing id |
| 214 |
$city->{city_id} = $city_id; |
233 |
$city->{city_id} = $city_id; |
| 215 |
$tx = $t->ua->build_tx( POST => "/api/v1/cities/" => json => $city ); |
234 |
$t->post_ok( "//$userid:$password@/api/v1/cities" => json => $city ) |
| 216 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
|
|
| 217 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 218 |
$t->request_ok($tx) |
| 219 |
->status_is(400) |
235 |
->status_is(400) |
| 220 |
->json_is( |
236 |
->json_is( |
| 221 |
"/errors" => [ |
237 |
"/errors" => [ |
|
Lines 235-253
subtest 'update() tests' => sub {
Link Here
|
| 235 |
|
251 |
|
| 236 |
$schema->storage->txn_begin; |
252 |
$schema->storage->txn_begin; |
| 237 |
|
253 |
|
| 238 |
my ( $unauthorized_borrowernumber, $unauthorized_session_id ) = |
254 |
my $librarian = $builder->build_object( |
| 239 |
create_user_and_session( { authorized => 0 } ); |
255 |
{ |
| 240 |
my ( $authorized_borrowernumber, $authorized_session_id ) = |
256 |
class => 'Koha::Patrons', |
| 241 |
create_user_and_session( { authorized => 1 } ); |
257 |
value => { flags => 2**3 } # parameters flag = 2 |
|
|
258 |
} |
| 259 |
); |
| 260 |
my $password = 'thePassword123'; |
| 261 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 262 |
my $userid = $librarian->userid; |
| 263 |
|
| 264 |
my $patron = $builder->build_object( |
| 265 |
{ |
| 266 |
class => 'Koha::Patrons', |
| 267 |
value => { flags => 0 } |
| 268 |
} |
| 269 |
); |
| 270 |
|
| 271 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 272 |
my $unauth_userid = $patron->userid; |
| 242 |
|
273 |
|
| 243 |
my $city_id = $builder->build_object({ class => 'Koha::Cities' } )->id; |
274 |
my $city_id = $builder->build_object({ class => 'Koha::Cities' } )->id; |
| 244 |
|
275 |
|
| 245 |
# Unauthorized attempt to update |
276 |
# Unauthorized attempt to update |
| 246 |
my $tx = $t->ua->build_tx( PUT => "/api/v1/cities/$city_id" => json => |
277 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/cities/$city_id" => json => { name => 'New unauthorized name change' } ) |
| 247 |
{ name => 'New unauthorized name change' } ); |
|
|
| 248 |
$tx->req->cookies({ name => 'CGISESSID', value => $unauthorized_session_id }); |
| 249 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 250 |
$t->request_ok($tx) |
| 251 |
->status_is(403); |
278 |
->status_is(403); |
| 252 |
|
279 |
|
| 253 |
# Attempt partial update on a PUT |
280 |
# Attempt partial update on a PUT |
|
Lines 257-267
subtest 'update() tests' => sub {
Link Here
|
| 257 |
country => 'New country' |
284 |
country => 'New country' |
| 258 |
}; |
285 |
}; |
| 259 |
|
286 |
|
| 260 |
$tx = $t->ua->build_tx( |
287 |
$t->put_ok( "//$userid:$password@/api/v1/cities/$city_id" => json => $city_with_missing_field ) |
| 261 |
PUT => "/api/v1/cities/$city_id" => json => $city_with_missing_field ); |
288 |
->status_is(400) |
| 262 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
|
|
| 263 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 264 |
$t->request_ok($tx)->status_is(400) |
| 265 |
->json_is( "/errors" => |
289 |
->json_is( "/errors" => |
| 266 |
[ { message => "Missing property.", path => "/body/postal_code" } ] |
290 |
[ { message => "Missing property.", path => "/body/postal_code" } ] |
| 267 |
); |
291 |
); |
|
Lines 274-283
subtest 'update() tests' => sub {
Link Here
|
| 274 |
country => "City Country" |
298 |
country => "City Country" |
| 275 |
}; |
299 |
}; |
| 276 |
|
300 |
|
| 277 |
$tx = $t->ua->build_tx( PUT => "/api/v1/cities/$city_id" => json => $city_with_updated_field ); |
301 |
$t->put_ok( "//$userid:$password@/api/v1/cities/$city_id" => json => $city_with_updated_field ) |
| 278 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
|
|
| 279 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 280 |
$t->request_ok($tx) |
| 281 |
->status_is(200) |
302 |
->status_is(200) |
| 282 |
->json_is( '/name' => 'London' ); |
303 |
->json_is( '/name' => 'London' ); |
| 283 |
|
304 |
|
|
Lines 289-298
subtest 'update() tests' => sub {
Link Here
|
| 289 |
country => "City Country" |
310 |
country => "City Country" |
| 290 |
}; |
311 |
}; |
| 291 |
|
312 |
|
| 292 |
$tx = $t->ua->build_tx( PUT => "/api/v1/cities/$city_id" => json => $city_with_invalid_field ); |
313 |
$t->put_ok( "//$userid:$password@/api/v1/cities/$city_id" => json => $city_with_invalid_field ) |
| 293 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
|
|
| 294 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 295 |
$t->request_ok($tx) |
| 296 |
->status_is(400) |
314 |
->status_is(400) |
| 297 |
->json_is( |
315 |
->json_is( |
| 298 |
"/errors" => [ |
316 |
"/errors" => [ |
|
Lines 307-330
subtest 'update() tests' => sub {
Link Here
|
| 307 |
my $non_existent_id = $city_to_delete->id; |
325 |
my $non_existent_id = $city_to_delete->id; |
| 308 |
$city_to_delete->delete; |
326 |
$city_to_delete->delete; |
| 309 |
|
327 |
|
| 310 |
$tx = $t->ua->build_tx( PUT => "/api/v1/cities/$non_existent_id" => json => |
328 |
$t->put_ok( "//$userid:$password@/api/v1/cities/$non_existent_id" => json => $city_with_updated_field ) |
| 311 |
$city_with_updated_field ); |
|
|
| 312 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
| 313 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 314 |
$t->request_ok($tx) |
| 315 |
->status_is(404); |
329 |
->status_is(404); |
| 316 |
|
330 |
|
| 317 |
$schema->storage->txn_rollback; |
|
|
| 318 |
|
| 319 |
# Wrong method (POST) |
331 |
# Wrong method (POST) |
| 320 |
$city_with_updated_field->{city_id} = 2; |
332 |
$city_with_updated_field->{city_id} = 2; |
| 321 |
|
333 |
|
| 322 |
$tx = $t->ua->build_tx( |
334 |
$t->post_ok( "//$userid:$password@/api/v1/cities/$city_id" => json => $city_with_updated_field ) |
| 323 |
POST => "/api/v1/cities/$city_id" => json => $city_with_updated_field ); |
|
|
| 324 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
| 325 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 326 |
$t->request_ok($tx) |
| 327 |
->status_is(404); |
335 |
->status_is(404); |
|
|
336 |
|
| 337 |
$schema->storage->txn_rollback; |
| 328 |
}; |
338 |
}; |
| 329 |
|
339 |
|
| 330 |
subtest 'delete() tests' => sub { |
340 |
subtest 'delete() tests' => sub { |
|
Lines 333-398
subtest 'delete() tests' => sub {
Link Here
|
| 333 |
|
343 |
|
| 334 |
$schema->storage->txn_begin; |
344 |
$schema->storage->txn_begin; |
| 335 |
|
345 |
|
| 336 |
my ( $unauthorized_borrowernumber, $unauthorized_session_id ) = |
346 |
my $librarian = $builder->build_object( |
| 337 |
create_user_and_session( { authorized => 0 } ); |
347 |
{ |
| 338 |
my ( $authorized_borrowernumber, $authorized_session_id ) = |
348 |
class => 'Koha::Patrons', |
| 339 |
create_user_and_session( { authorized => 1 } ); |
349 |
value => { flags => 2**3 } # parameters flag = 2 |
|
|
350 |
} |
| 351 |
); |
| 352 |
my $password = 'thePassword123'; |
| 353 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 354 |
my $userid = $librarian->userid; |
| 355 |
|
| 356 |
my $patron = $builder->build_object( |
| 357 |
{ |
| 358 |
class => 'Koha::Patrons', |
| 359 |
value => { flags => 0 } |
| 360 |
} |
| 361 |
); |
| 362 |
|
| 363 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 364 |
my $unauth_userid = $patron->userid; |
| 340 |
|
365 |
|
| 341 |
my $city_id = $builder->build_object({ class => 'Koha::Cities' })->id; |
366 |
my $city_id = $builder->build_object({ class => 'Koha::Cities' })->id; |
| 342 |
|
367 |
|
| 343 |
# Unauthorized attempt to delete |
368 |
# Unauthorized attempt to delete |
| 344 |
my $tx = $t->ua->build_tx( DELETE => "/api/v1/cities/$city_id" ); |
369 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/cities/$city_id" ) |
| 345 |
$tx->req->cookies({ name => 'CGISESSID', value => $unauthorized_session_id }); |
|
|
| 346 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 347 |
$t->request_ok($tx) |
| 348 |
->status_is(403); |
370 |
->status_is(403); |
| 349 |
|
371 |
|
| 350 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/cities/$city_id" ); |
372 |
$t->delete_ok("//$userid:$password@/api/v1/cities/$city_id") |
| 351 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
|
|
| 352 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 353 |
$t->request_ok($tx) |
| 354 |
->status_is(200) |
373 |
->status_is(200) |
| 355 |
->content_is('""'); |
374 |
->content_is('""'); |
| 356 |
|
375 |
|
| 357 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/cities/$city_id" ); |
376 |
$t->delete_ok("//$userid:$password@/api/v1/cities/$city_id") |
| 358 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
|
|
| 359 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
| 360 |
$t->request_ok($tx) |
| 361 |
->status_is(404); |
377 |
->status_is(404); |
| 362 |
|
378 |
|
| 363 |
$schema->storage->txn_rollback; |
379 |
$schema->storage->txn_rollback; |
| 364 |
}; |
380 |
}; |
| 365 |
|
|
|
| 366 |
sub create_user_and_session { |
| 367 |
|
| 368 |
my $args = shift; |
| 369 |
my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0; |
| 370 |
my $dbh = C4::Context->dbh; |
| 371 |
|
| 372 |
my $user = $builder->build( |
| 373 |
{ |
| 374 |
source => 'Borrower', |
| 375 |
value => { |
| 376 |
flags => $flags |
| 377 |
} |
| 378 |
} |
| 379 |
); |
| 380 |
|
| 381 |
# Create a session for the authorized user |
| 382 |
my $session = C4::Auth::get_session(''); |
| 383 |
$session->param( 'number', $user->{borrowernumber} ); |
| 384 |
$session->param( 'id', $user->{userid} ); |
| 385 |
$session->param( 'ip', '127.0.0.1' ); |
| 386 |
$session->param( 'lasttime', time() ); |
| 387 |
$session->flush; |
| 388 |
|
| 389 |
if ( $args->{authorized} ) { |
| 390 |
$dbh->do( " |
| 391 |
INSERT INTO user_permissions (borrowernumber,module_bit,code) |
| 392 |
VALUES (?,3,'parameters_remaining_permissions')", undef, |
| 393 |
$user->{borrowernumber} ); |
| 394 |
} |
| 395 |
|
| 396 |
return ( $user->{borrowernumber}, $session->id ); |
| 397 |
} |
| 398 |
|
| 399 |
- |