Lines 44-56
subtest 'list() tests' => sub {
Link Here
|
44 |
$schema->storage->txn_begin; |
44 |
$schema->storage->txn_begin; |
45 |
|
45 |
|
46 |
# Create test context |
46 |
# Create test context |
47 |
my $library = $builder->build( { source => 'Branch' } ); |
47 |
my $library = $builder->build_object({ class => 'Koha::Libraries' }); |
48 |
my $another_library = { %$library }; # create a copy of $library but make |
48 |
my $another_library = $library->unblessed; # create a copy of $library but make |
49 |
delete $another_library->{branchcode}; # sure branchcode will be regenerated |
49 |
delete $another_library->{branchcode}; # sure branchcode will be regenerated |
50 |
$another_library = $builder->build( |
50 |
$another_library = $builder->build_object({ class => 'Koha::Libraries', value => $another_library }); |
51 |
{ source => 'Branch', value => $another_library } ); |
51 |
my ( $borrowernumber, $session_id ) = create_user_and_session( { authorized => 0 } ); |
52 |
my ( $borrowernumber, $session_id ) = |
|
|
53 |
create_user_and_session( { authorized => 0 } ); |
54 |
|
52 |
|
55 |
## Authorized user tests |
53 |
## Authorized user tests |
56 |
my $count_of_libraries = Koha::Libraries->search->count; |
54 |
my $count_of_libraries = Koha::Libraries->search->count; |
Lines 59-81
subtest 'list() tests' => sub {
Link Here
|
59 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
57 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
60 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
58 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
61 |
$t->request_ok($tx) |
59 |
$t->request_ok($tx) |
62 |
->status_is(200) |
60 |
->status_is( 200, 'SWAGGER3.2.2' ) |
63 |
->json_has('/'.($count_of_libraries-1).'/branchcode') |
61 |
->json_has('/'.($count_of_libraries-1).'/library_id') |
64 |
->json_hasnt('/'.($count_of_libraries).'/branchcode'); |
62 |
->json_hasnt('/'.($count_of_libraries).'/library_id'); |
65 |
|
63 |
|
66 |
subtest 'query parameters' => sub { |
64 |
subtest 'query parameters' => sub { |
67 |
my @fields = qw( |
65 |
|
68 |
branchname branchaddress1 branchaddress2 branchaddress3 |
66 |
my $fields = { |
69 |
branchzip branchcity branchstate branchcountry |
67 |
name => 'branchname', |
70 |
branchphone branchfax branchemail branchreplyto |
68 |
address1 => 'branchaddress1', |
71 |
branchreturnpath branchurl issuing branchip |
69 |
address2 => 'branchaddress2', |
72 |
branchprinter branchnotes opac_info |
70 |
address3 => 'branchaddress3', |
73 |
); |
71 |
postal_code => 'branchzip', |
74 |
plan tests => scalar(@fields)*3; |
72 |
city => 'branchcity', |
75 |
|
73 |
state => 'branchstate', |
76 |
foreach my $field (@fields) { |
74 |
country => 'branchcountry', |
|
|
75 |
phone => 'branchphone', |
76 |
fax => 'branchfax', |
77 |
email => 'branchemail', |
78 |
reply_to_email => 'branchreplyto', |
79 |
return_path_email => 'branchreturnpath', |
80 |
url => 'branchurl', |
81 |
ip => 'branchip', |
82 |
notes => 'branchnotes', |
83 |
opac_info => 'opac_info', |
84 |
}; |
85 |
|
86 |
my $size = keys %{$fields}; |
87 |
|
88 |
plan tests => $size * 3; |
89 |
|
90 |
foreach my $field ( keys %{$fields} ) { |
91 |
my $model_field = $fields->{ $field }; |
77 |
$tx = $t->ua->build_tx( GET => |
92 |
$tx = $t->ua->build_tx( GET => |
78 |
"/api/v1/libraries?$field=$library->{$field}" ); |
93 |
"/api/v1/libraries?$field=" . $library->$model_field ); |
79 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
94 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
80 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
95 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
81 |
my $result = |
96 |
my $result = |
Lines 102-119
subtest 'get() tests' => sub {
Link Here
|
102 |
|
117 |
|
103 |
$schema->storage->txn_begin; |
118 |
$schema->storage->txn_begin; |
104 |
|
119 |
|
105 |
my $library = $builder->build( { source => 'Branch' } ); |
120 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
106 |
my ( $borrowernumber, $session_id ) = |
121 |
my ( $borrowernumber, $session_id ) = |
107 |
create_user_and_session( { authorized => 0 } ); |
122 |
create_user_and_session( { authorized => 0 } ); |
108 |
|
123 |
|
109 |
my $tx = $t->ua->build_tx( GET => "/api/v1/libraries/" . $library->{branchcode} ); |
124 |
my $tx = $t->ua->build_tx( GET => "/api/v1/libraries/" . $library->branchcode ); |
110 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
125 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
111 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
126 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
112 |
$t->request_ok($tx) |
127 |
$t->request_ok($tx) |
113 |
->status_is(200) |
128 |
->status_is( 200, 'SWAGGER3.2.2' ) |
114 |
->json_is($library); |
129 |
->json_is( '' => Koha::REST::V1::Library::_to_api( $library->TO_JSON ), 'SWAGGER3.3.2' ); |
|
|
130 |
|
131 |
my $non_existent_code = $library->branchcode; |
132 |
$library->delete; |
115 |
|
133 |
|
116 |
my $non_existent_code = 'non_existent'.int(rand(10000)); |
|
|
117 |
$tx = $t->ua->build_tx( GET => "/api/v1/libraries/" . $non_existent_code ); |
134 |
$tx = $t->ua->build_tx( GET => "/api/v1/libraries/" . $non_existent_code ); |
118 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
135 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
119 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
136 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
Lines 125-131
subtest 'get() tests' => sub {
Link Here
|
125 |
}; |
142 |
}; |
126 |
|
143 |
|
127 |
subtest 'add() tests' => sub { |
144 |
subtest 'add() tests' => sub { |
128 |
plan tests => 31; |
145 |
|
|
|
146 |
plan tests => 17; |
129 |
|
147 |
|
130 |
$schema->storage->txn_begin; |
148 |
$schema->storage->txn_begin; |
131 |
|
149 |
|
Lines 133-160
subtest 'add() tests' => sub {
Link Here
|
133 |
create_user_and_session( { authorized => 0 } ); |
151 |
create_user_and_session( { authorized => 0 } ); |
134 |
my ( $authorized_borrowernumber, $authorized_session_id ) = |
152 |
my ( $authorized_borrowernumber, $authorized_session_id ) = |
135 |
create_user_and_session( { authorized => 1 } ); |
153 |
create_user_and_session( { authorized => 1 } ); |
136 |
my $library = { |
154 |
|
137 |
branchcode => "LIBRARYBR1", |
155 |
my $library_obj = $builder->build_object({ class => 'Koha::Libraries' }); |
138 |
branchname => "Library Name", |
156 |
my $library = Koha::REST::V1::Library::_to_api( $library_obj->TO_JSON ); |
139 |
branchaddress1 => "Library Address1", |
157 |
$library_obj->delete; |
140 |
branchaddress2 => "Library Address2", |
|
|
141 |
branchaddress3 => "Library Address3", |
142 |
branchzip => "Library Zipcode", |
143 |
branchcity => "Library City", |
144 |
branchstate => "Library State", |
145 |
branchcountry => "Library Country", |
146 |
branchphone => "Library Phone", |
147 |
branchfax => "Library Fax", |
148 |
branchemail => "Library Email", |
149 |
branchreplyto => "Library Reply-To", |
150 |
branchreturnpath => "Library Return-Path", |
151 |
branchurl => "http://library.url", |
152 |
issuing => undef, # unused in Koha |
153 |
branchip => "127.0.0.1", |
154 |
branchprinter => "Library Printer", # unused in Koha |
155 |
branchnotes => "Library Notes", |
156 |
opac_info => "<p>Library OPAC info</p>", |
157 |
}; |
158 |
|
158 |
|
159 |
# Unauthorized attempt to write |
159 |
# Unauthorized attempt to write |
160 |
my $tx = $t->ua->build_tx( POST => "/api/v1/libraries" => json => $library ); |
160 |
my $tx = $t->ua->build_tx( POST => "/api/v1/libraries" => json => $library ); |
Lines 189-218
subtest 'add() tests' => sub {
Link Here
|
189 |
$tx->req->cookies( |
189 |
$tx->req->cookies( |
190 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
190 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
191 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
191 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
192 |
my $branchcode = $t->request_ok($tx) |
192 |
$t->request_ok($tx) |
193 |
->status_is(201) |
193 |
->status_is( 201, 'SWAGGER3.2.1' ) |
194 |
->json_is( '/branchname' => $library->{branchname} ) |
194 |
->json_is( '' => $library, 'SWAGGER3.3.1' ) |
195 |
->json_is( '/branchaddress1' => $library->{branchaddress1} ) |
195 |
->header_is( Location => '/api/v1/libraries/' . $library->{library_id}, 'SWAGGER3.4.1' ); |
196 |
->json_is( '/branchaddress2' => $library->{branchaddress2} ) |
|
|
197 |
->json_is( '/branchaddress3' => $library->{branchaddress3} ) |
198 |
->json_is( '/branchzip' => $library->{branchzip} ) |
199 |
->json_is( '/branchcity' => $library->{branchcity} ) |
200 |
->json_is( '/branchstate' => $library->{branchstate} ) |
201 |
->json_is( '/branchcountry' => $library->{branchcountry} ) |
202 |
->json_is( '/branchphone' => $library->{branchphone} ) |
203 |
->json_is( '/branchfax' => $library->{branchfax} ) |
204 |
->json_is( '/branchemail' => $library->{branchemail} ) |
205 |
->json_is( '/branchreplyto' => $library->{branchreplyto} ) |
206 |
->json_is( '/branchreturnpath' => $library->{branchreturnpath} ) |
207 |
->json_is( '/branchurl' => $library->{branchurl} ) |
208 |
->json_is( '/branchip' => $library->{branchip} ) |
209 |
->json_is( '/branchnotes' => $library->{branchnotes} ) |
210 |
->json_is( '/opac_info' => $library->{opac_info} ) |
211 |
->header_is(Location => "/api/v1/libraries/$library->{branchcode}") |
212 |
->tx->res->json->{branchcode}; |
213 |
|
196 |
|
|
|
197 |
# save the library_id |
198 |
my $library_id = $library->{library_id}; |
214 |
# Authorized attempt to create with null id |
199 |
# Authorized attempt to create with null id |
215 |
$library->{branchcode} = undef; |
200 |
$library->{library_id} = undef; |
216 |
$tx = $t->ua->build_tx( |
201 |
$tx = $t->ua->build_tx( |
217 |
POST => "/api/v1/libraries" => json => $library ); |
202 |
POST => "/api/v1/libraries" => json => $library ); |
218 |
$tx->req->cookies( |
203 |
$tx->req->cookies( |
Lines 223-237
subtest 'add() tests' => sub {
Link Here
|
223 |
->json_has('/errors'); |
208 |
->json_has('/errors'); |
224 |
|
209 |
|
225 |
# Authorized attempt to create with existing id |
210 |
# Authorized attempt to create with existing id |
226 |
$library->{branchcode} = $branchcode; |
211 |
$library->{library_id} = $library_id; |
227 |
$tx = $t->ua->build_tx( |
212 |
$tx = $t->ua->build_tx( |
228 |
POST => "/api/v1/libraries" => json => $library ); |
213 |
POST => "/api/v1/libraries" => json => $library ); |
229 |
$tx->req->cookies( |
214 |
$tx->req->cookies( |
230 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
215 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
231 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
216 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
232 |
$t->request_ok($tx) |
217 |
|
233 |
->status_is(400) |
218 |
warning_like { |
234 |
->json_is('/error' => 'Library already exists'); |
219 |
$t->request_ok($tx) |
|
|
220 |
->status_is(409) |
221 |
->json_has( '/error' => "Fails when trying to add an existing library_id") |
222 |
->json_is( '/conflict', 'PRIMARY' ); } # WTF |
223 |
qr/^DBD::mysql::st execute failed: Duplicate entry '(.*)' for key 'PRIMARY'/; |
235 |
|
224 |
|
236 |
$schema->storage->txn_rollback; |
225 |
$schema->storage->txn_rollback; |
237 |
}; |
226 |
}; |
Lines 246-255
subtest 'update() tests' => sub {
Link Here
|
246 |
my ( $authorized_borrowernumber, $authorized_session_id ) = |
235 |
my ( $authorized_borrowernumber, $authorized_session_id ) = |
247 |
create_user_and_session( { authorized => 1 } ); |
236 |
create_user_and_session( { authorized => 1 } ); |
248 |
|
237 |
|
249 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
238 |
my $library = $builder->build_object({ class => 'Koha::Libraries' }); |
|
|
239 |
my $library_id = $library->branchcode; |
250 |
|
240 |
|
251 |
# Unauthorized attempt to update |
241 |
# Unauthorized attempt to update |
252 |
my $tx = $t->ua->build_tx( PUT => "/api/v1/libraries/$branchcode" |
242 |
my $tx = $t->ua->build_tx( PUT => "/api/v1/libraries/$library_id" |
253 |
=> json => { branchname => 'New unauthorized name change' } ); |
243 |
=> json => { branchname => 'New unauthorized name change' } ); |
254 |
$tx->req->cookies( |
244 |
$tx->req->cookies( |
255 |
{ name => 'CGISESSID', value => $unauthorized_session_id } ); |
245 |
{ name => 'CGISESSID', value => $unauthorized_session_id } ); |
Lines 259-268
subtest 'update() tests' => sub {
Link Here
|
259 |
|
249 |
|
260 |
# Attempt partial update on a PUT |
250 |
# Attempt partial update on a PUT |
261 |
my $library_with_missing_field = { |
251 |
my $library_with_missing_field = { |
262 |
branchaddress1 => "New library address", |
252 |
address1 => "New library address", |
263 |
}; |
253 |
}; |
264 |
|
254 |
|
265 |
$tx = $t->ua->build_tx( PUT => "/api/v1/libraries/$branchcode" => |
255 |
$tx = $t->ua->build_tx( PUT => "/api/v1/libraries/$library_id" => |
266 |
json => $library_with_missing_field ); |
256 |
json => $library_with_missing_field ); |
267 |
$tx->req->cookies( |
257 |
$tx->req->cookies( |
268 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
258 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
Lines 270-317
subtest 'update() tests' => sub {
Link Here
|
270 |
$t->request_ok($tx) |
260 |
$t->request_ok($tx) |
271 |
->status_is(400) |
261 |
->status_is(400) |
272 |
->json_has( "/errors" => |
262 |
->json_has( "/errors" => |
273 |
[ { message => "Missing property.", path => "/body/branchaddress2" } ] |
263 |
[ { message => "Missing property.", path => "/body/address2" } ] |
274 |
); |
264 |
); |
275 |
|
265 |
|
276 |
# Full object update on PUT |
266 |
my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } ); |
277 |
my $library_with_updated_field = { |
267 |
my $library_with_updated_field = Koha::REST::V1::Library::_to_api( $deleted_library->TO_JSON ); |
278 |
branchcode => "LIBRARYBR2", |
268 |
$library_with_updated_field->{library_id} = $library_id; |
279 |
branchname => "Library Name", |
269 |
$deleted_library->delete; |
280 |
branchaddress1 => "Library Address1", |
|
|
281 |
branchaddress2 => "Library Address2", |
282 |
branchaddress3 => "Library Address3", |
283 |
branchzip => "Library Zipcode", |
284 |
branchcity => "Library City", |
285 |
branchstate => "Library State", |
286 |
branchcountry => "Library Country", |
287 |
branchphone => "Library Phone", |
288 |
branchfax => "Library Fax", |
289 |
branchemail => "Library Email", |
290 |
branchreplyto => "Library Reply-To", |
291 |
branchreturnpath => "Library Return-Path", |
292 |
branchurl => "http://library.url", |
293 |
issuing => undef, # unused in Koha |
294 |
branchip => "127.0.0.1", |
295 |
branchprinter => "Library Printer", # unused in Koha |
296 |
branchnotes => "Library Notes", |
297 |
opac_info => "<p>Library OPAC info</p>", |
298 |
}; |
299 |
|
270 |
|
300 |
$tx = $t->ua->build_tx( |
271 |
$tx = $t->ua->build_tx( PUT => "/api/v1/libraries/$library_id" => json => $library_with_updated_field ); |
301 |
PUT => "/api/v1/libraries/$branchcode" => json => $library_with_updated_field ); |
272 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
302 |
$tx->req->cookies( |
|
|
303 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
304 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
273 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
305 |
$t->request_ok($tx) |
274 |
$t->request_ok($tx) |
306 |
->status_is(200) |
275 |
->status_is(200, 'SWAGGER3.2.1') |
307 |
->json_is( '/branchname' => 'Library Name' ); |
276 |
->json_is( '' => $library_with_updated_field, 'SWAGGER3.3.3' ); |
308 |
|
277 |
|
309 |
# Authorized attempt to write invalid data |
278 |
# Authorized attempt to write invalid data |
310 |
my $library_with_invalid_field = { %$library_with_updated_field }; |
279 |
my $library_with_invalid_field = { %$library_with_updated_field }; |
311 |
$library_with_invalid_field->{'branchinvalid'} = 'Library invalid'; |
280 |
$library_with_invalid_field->{'branchinvalid'} = 'Library invalid'; |
312 |
|
281 |
|
313 |
$tx = $t->ua->build_tx( |
282 |
$tx = $t->ua->build_tx( |
314 |
PUT => "/api/v1/libraries/$branchcode" => json => $library_with_invalid_field ); |
283 |
PUT => "/api/v1/libraries/$library_id" => json => $library_with_invalid_field ); |
315 |
$tx->req->cookies( |
284 |
$tx->req->cookies( |
316 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
285 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
317 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
286 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
Lines 364-371
subtest 'delete() tests' => sub {
Link Here
|
364 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
333 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
365 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
334 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
366 |
$t->request_ok($tx) |
335 |
$t->request_ok($tx) |
367 |
->status_is(204) |
336 |
->status_is(204, 'SWAGGER3.2.4') |
368 |
->content_is(''); |
337 |
->content_is('', 'SWAGGER3.3.4'); |
369 |
|
338 |
|
370 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/libraries/$branchcode" ); |
339 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/libraries/$branchcode" ); |
371 |
$tx->req->cookies( |
340 |
$tx->req->cookies( |
372 |
- |
|
|