Lines 23-28
use Test::Mojo;
Link Here
|
23 |
use t::lib::TestBuilder; |
23 |
use t::lib::TestBuilder; |
24 |
use t::lib::Mocks; |
24 |
use t::lib::Mocks; |
25 |
|
25 |
|
|
|
26 |
use JSON qw(encode_json); |
27 |
|
26 |
use Koha::Illbatch; |
28 |
use Koha::Illbatch; |
27 |
use Koha::Illbatches; |
29 |
use Koha::Illbatches; |
28 |
use Koha::Illrequests; |
30 |
use Koha::Illrequests; |
Lines 37-48
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
Link Here
|
37 |
|
39 |
|
38 |
subtest 'list() tests' => sub { |
40 |
subtest 'list() tests' => sub { |
39 |
|
41 |
|
40 |
plan tests => 19; |
42 |
plan tests => 21; |
41 |
|
43 |
|
42 |
$schema->storage->txn_begin; |
44 |
$schema->storage->txn_begin; |
43 |
|
45 |
|
44 |
Koha::Illbatches->search->delete; |
|
|
45 |
|
46 |
my $librarian = $builder->build_object( |
46 |
my $librarian = $builder->build_object( |
47 |
{ |
47 |
{ |
48 |
class => 'Koha::Patrons', |
48 |
class => 'Koha::Patrons', |
Lines 52-106
subtest 'list() tests' => sub {
Link Here
|
52 |
} |
52 |
} |
53 |
); |
53 |
); |
54 |
|
54 |
|
55 |
my $branch = $builder->build_object( { class => 'Koha::Libraries' } ); |
55 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
56 |
|
56 |
|
57 |
my $password = 'sheev_is_da_boss!'; |
57 |
my $password = 'sheev_is_da_boss!'; |
58 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
58 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
59 |
my $userid = $librarian->userid; |
59 |
my $userid = $librarian->userid; |
60 |
|
60 |
|
|
|
61 |
my $batch_to_delete = $builder->build_object( { class => 'Koha::Illbatches' } ); |
62 |
my $deleted_batch_id = $batch_to_delete->id; |
63 |
$batch_to_delete->delete; |
64 |
|
65 |
my $query = { ill_batch_id => [$deleted_batch_id] }; |
66 |
|
61 |
## Authorized user tests |
67 |
## Authorized user tests |
62 |
# No batches, so empty array should be returned |
68 |
# No batches, so empty array should be returned |
63 |
$t->get_ok("//$userid:$password@/api/v1/ill/batches")->status_is(200)->json_is( [] ); |
69 |
$t->get_ok( "//$userid:$password@/api/v1/ill/batches?q=" . encode_json($query) )->status_is(200)->json_is( [] ); |
64 |
|
70 |
|
65 |
my $batch = $builder->build_object( |
71 |
my $batch_1 = $builder->build_object( |
66 |
{ |
72 |
{ |
67 |
class => 'Koha::Illbatches', |
73 |
class => 'Koha::Illbatches', |
68 |
value => { |
74 |
value => { |
69 |
name => "PapaPalpatine", |
75 |
backend => "Mock", |
70 |
backend => "Mock", |
76 |
patron_id => $librarian->id, |
71 |
borrowernumber => $librarian->borrowernumber, |
77 |
library_id => $library->id, |
72 |
branchcode => $branch->branchcode |
|
|
73 |
} |
78 |
} |
74 |
} |
79 |
} |
75 |
); |
80 |
); |
76 |
|
81 |
|
77 |
my $illrq = $builder->build( |
82 |
my $illrq = $builder->build_object( |
78 |
{ |
83 |
{ |
79 |
source => 'Illrequest', |
84 |
class => 'Koha::Illrequests', |
80 |
value => { |
85 |
value => { |
81 |
borrowernumber => $librarian->borrowernumber, |
86 |
batch_id => $batch_1->id, |
82 |
batch_id => $batch->id |
87 |
borrowernumber => $librarian->id, |
83 |
} |
88 |
} |
84 |
} |
89 |
} |
85 |
); |
90 |
); |
86 |
|
91 |
|
87 |
# One batch created, should get returned |
92 |
$query = { ill_batch_id => [ $batch_1->id ] }; |
88 |
$t->get_ok( |
|
|
89 |
"//$userid:$password@/api/v1/ill/batches" => { 'x-koha-embed' => '+strings,requests+count,patron,branch' } ) |
90 |
->status_is(200)->json_has( '/0/batch_id', 'Batch ID' )->json_has( '/0/name', 'Batch name' ) |
91 |
->json_has( '/0/backend', 'Backend name' )->json_has( '/0/patron_id', 'Borrowernumber' ) |
92 |
->json_has( '/0/library_id', 'Branchcode' )->json_has( '/0/patron', 'patron embedded' ) |
93 |
->json_has( '/0/branch', 'branch embedded' )->json_has( '/0/requests_count', 'request count' ); |
94 |
|
93 |
|
95 |
# Try to create a second batch with the same name, this should fail |
94 |
# One batch created, should get returned |
96 |
my $another_batch = $builder->build_object( { class => 'Koha::Illbatches', value => { name => $batch->name } } ); |
95 |
$t->get_ok( "//$userid:$password@/api/v1/ill/batches?q=" |
|
|
96 |
. encode_json($query) => { 'x-koha-embed' => '+strings,requests+count,patron,library' } )->status_is(200) |
97 |
->json_has( '/0/ill_batch_id', 'Batch ID' )->json_has( '/0/name', 'Batch name' ) |
98 |
->json_has( '/0/backend', 'Backend name' )->json_has( '/0/patron_id', 'Borrowernumber' ) |
99 |
->json_has( '/0/library_id', 'Branchcode' )->json_has( '/0/patron', 'patron embedded' ) |
100 |
->json_has( '/0/library', 'branch embedded' )->json_has( '/0/requests_count', 'request count' ); |
97 |
|
101 |
|
98 |
# Create a second batch with a different name |
102 |
# Create a second batch with a different name |
99 |
my $batch_with_another_name = $builder->build_object( { class => 'Koha::Illbatches' } ); |
103 |
my $batch_2 = $builder->build_object( { class => 'Koha::Illbatches' } ); |
|
|
104 |
|
105 |
$query = { ill_batch_id => [ $batch_1->id, $batch_2->id ] }; |
100 |
|
106 |
|
101 |
# Two batches created, they should both be returned |
107 |
# Two batches created, they should both be returned |
102 |
$t->get_ok("//$userid:$password@/api/v1/ill/batches")->status_is(200)->json_has( '/0', 'has first batch' ) |
108 |
$t->get_ok( "//$userid:$password@/api/v1/ill/batches?q=" . encode_json($query) )->status_is(200) |
103 |
->json_has( '/1', 'has second batch' ); |
109 |
->json_has( '/0', 'has first batch' )->json_is( '/0/ill_batch_id', $batch_1->id ) |
|
|
110 |
->json_has( '/1', 'has second batch' )->json_is( '/1/ill_batch_id', $batch_2->id ); |
104 |
|
111 |
|
105 |
my $patron = $builder->build_object( |
112 |
my $patron = $builder->build_object( |
106 |
{ |
113 |
{ |
Lines 144-159
subtest 'get() tests' => sub {
Link Here
|
144 |
} |
151 |
} |
145 |
); |
152 |
); |
146 |
|
153 |
|
147 |
my $branch = $builder->build_object( { class => 'Koha::Libraries' } ); |
154 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
148 |
|
155 |
|
149 |
my $batch = $builder->build_object( |
156 |
my $batch = $builder->build_object( |
150 |
{ |
157 |
{ |
151 |
class => 'Koha::Illbatches', |
158 |
class => 'Koha::Illbatches', |
152 |
value => { |
159 |
value => { |
153 |
name => "LeiaOrgana", |
160 |
backend => "Mock", |
154 |
backend => "Mock", |
161 |
patron_id => $librarian->id, |
155 |
borrowernumber => $librarian->borrowernumber, |
162 |
library_id => $library->id, |
156 |
branchcode => $branch->branchcode |
|
|
157 |
} |
163 |
} |
158 |
} |
164 |
} |
159 |
); |
165 |
); |
Lines 162-172
subtest 'get() tests' => sub {
Link Here
|
162 |
my $unauth_userid = $patron->userid; |
168 |
my $unauth_userid = $patron->userid; |
163 |
|
169 |
|
164 |
$t->get_ok( "//$userid:$password@/api/v1/ill/batches/" |
170 |
$t->get_ok( "//$userid:$password@/api/v1/ill/batches/" |
165 |
. $batch->id => { 'x-koha-embed' => '+strings,requests+count,patron,branch' } )->status_is(200) |
171 |
. $batch->id => { 'x-koha-embed' => '+strings,requests+count,patron,library' } )->status_is(200) |
166 |
->json_has( '/batch_id', 'Batch ID' )->json_has( '/name', 'Batch name' ) |
172 |
->json_has( '/ill_batch_id', 'Batch ID' )->json_has( '/name', 'Batch name' ) |
167 |
->json_has( '/backend', 'Backend name' )->json_has( '/patron_id', 'Borrowernumber' ) |
173 |
->json_has( '/backend', 'Backend name' )->json_has( '/patron_id', 'Borrowernumber' ) |
168 |
->json_has( '/library_id', 'Branchcode' )->json_has( '/patron', 'patron embedded' ) |
174 |
->json_has( '/library_id', 'Branchcode' )->json_has( '/patron', 'patron embedded' ) |
169 |
->json_has( '/branch', 'branch embedded' )->json_has( '/requests_count', 'request count' ); |
175 |
->json_has( '/library', 'library embedded' )->json_has( '/requests_count', 'request count' ); |
170 |
|
176 |
|
171 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/ill/batches/" . $batch->id )->status_is(403); |
177 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/ill/batches/" . $batch->id )->status_is(403); |
172 |
|
178 |
|
Lines 182-188
subtest 'get() tests' => sub {
Link Here
|
182 |
|
188 |
|
183 |
subtest 'add() tests' => sub { |
189 |
subtest 'add() tests' => sub { |
184 |
|
190 |
|
185 |
plan tests => 19; |
191 |
plan tests => 20; |
186 |
|
192 |
|
187 |
$schema->storage->txn_begin; |
193 |
$schema->storage->txn_begin; |
188 |
|
194 |
|
Lines 206-221
subtest 'add() tests' => sub {
Link Here
|
206 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
212 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
207 |
my $unauth_userid = $patron->userid; |
213 |
my $unauth_userid = $patron->userid; |
208 |
|
214 |
|
209 |
my $branch = $builder->build_object( { class => 'Koha::Libraries' } ); |
215 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
210 |
|
216 |
|
211 |
my $batch_status = $builder->build_object( { class => 'Koha::IllbatchStatuses' } ); |
217 |
my $batch_status = $builder->build_object( { class => 'Koha::IllbatchStatuses' } ); |
212 |
|
218 |
|
213 |
my $batch_metadata = { |
219 |
my $batch_metadata = { |
214 |
name => "Anakin's requests", |
220 |
name => "Anakin's requests", |
215 |
backend => "Mock", |
221 |
backend => "Mock", |
216 |
cardnumber => $librarian->cardnumber, |
222 |
cardnumber => $librarian->cardnumber, |
217 |
library_id => $branch->branchcode, |
223 |
library_id => $library->branchcode, |
218 |
statuscode => $batch_status->code |
224 |
status_code => $batch_status->code |
219 |
}; |
225 |
}; |
220 |
|
226 |
|
221 |
# Unauthorized attempt to write |
227 |
# Unauthorized attempt to write |
Lines 239-251
subtest 'add() tests' => sub {
Link Here
|
239 |
|
245 |
|
240 |
# Authorized attempt to write |
246 |
# Authorized attempt to write |
241 |
my $batch_id = |
247 |
my $batch_id = |
242 |
$t->post_ok( "//$userid:$password@/api/v1/ill/batches" => |
248 |
$t->post_ok( |
243 |
{ 'x-koha-embed' => '+strings,requests+count,patron,branch' } => json => $batch_metadata ) |
249 |
"//$userid:$password@/api/v1/ill/batches" => { 'x-koha-embed' => '+strings,requests+count,patron,library' } => |
244 |
->status_is(201) |
250 |
json => $batch_metadata )->status_is(201)->json_is( '/name' => $batch_metadata->{name} ) |
245 |
->json_is( '/name' => $batch_metadata->{name} )->json_is( '/backend' => $batch_metadata->{backend} ) |
251 |
->json_is( '/backend' => $batch_metadata->{backend} )->json_is( '/patron_id' => $librarian->borrowernumber ) |
246 |
->json_is( '/patron_id' => $librarian->borrowernumber ) |
252 |
->json_is( '/library_id' => $batch_metadata->{library_id} )->json_is( '/status_code' => $batch_status->code ) |
247 |
->json_is( '/library_id' => $batch_metadata->{library_id} )->json_is( '/statuscode' => $batch_status->code ) |
253 |
->json_has('/patron')->json_has('/_strings/status_code')->json_has('/_strings/library_id') |
248 |
->json_has('/patron')->json_has('/_strings/status')->json_has('/requests_count')->json_has('/branch'); |
254 |
->json_has('/requests_count')->json_has('/library'); |
249 |
|
255 |
|
250 |
# Authorized attempt to create with null id |
256 |
# Authorized attempt to create with null id |
251 |
$batch_metadata->{id} = undef; |
257 |
$batch_metadata->{id} = undef; |
Lines 281-288
subtest 'update() tests' => sub {
Link Here
|
281 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
287 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
282 |
my $unauth_userid = $patron->userid; |
288 |
my $unauth_userid = $patron->userid; |
283 |
|
289 |
|
284 |
my $branch = $builder->build_object( { class => 'Koha::Libraries' } ); |
290 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
285 |
|
|
|
286 |
my $batch_id = $builder->build_object( { class => 'Koha::Illbatches' } )->id; |
291 |
my $batch_id = $builder->build_object( { class => 'Koha::Illbatches' } )->id; |
287 |
|
292 |
|
288 |
# Unauthorized attempt to update |
293 |
# Unauthorized attempt to update |
Lines 293-302
subtest 'update() tests' => sub {
Link Here
|
293 |
|
298 |
|
294 |
# Attempt partial update on a PUT |
299 |
# Attempt partial update on a PUT |
295 |
my $batch_with_missing_field = { |
300 |
my $batch_with_missing_field = { |
296 |
backend => "Mock", |
301 |
backend => "Mock", |
297 |
patron_id => $librarian->borrowernumber, |
302 |
patron_id => $librarian->borrowernumber, |
298 |
library_id => $branch->branchcode, |
303 |
library_id => $library->branchcode, |
299 |
statuscode => $batch_status->code |
304 |
status_code => $batch_status->code |
300 |
}; |
305 |
}; |
301 |
|
306 |
|
302 |
$t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_missing_field ) |
307 |
$t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_missing_field ) |
Lines 304-314
subtest 'update() tests' => sub {
Link Here
|
304 |
|
309 |
|
305 |
# Full object update on PUT |
310 |
# Full object update on PUT |
306 |
my $batch_with_updated_field = { |
311 |
my $batch_with_updated_field = { |
307 |
name => "Master Ploo Koon", |
312 |
name => "Master Ploo Koon", |
308 |
backend => "Mock", |
313 |
backend => "Mock", |
309 |
patron_id => $librarian->borrowernumber, |
314 |
patron_id => $librarian->borrowernumber, |
310 |
library_id => $branch->branchcode, |
315 |
library_id => $library->branchcode, |
311 |
statuscode => $batch_status->code |
316 |
status_code => $batch_status->code |
312 |
}; |
317 |
}; |
313 |
|
318 |
|
314 |
$t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field ) |
319 |
$t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field ) |
315 |
- |
|
|