Lines 17-23
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 20; |
20 |
use Test::More tests => 74; |
21 |
use Test::Mojo; |
21 |
use Test::Mojo; |
22 |
use t::lib::TestBuilder; |
22 |
use t::lib::TestBuilder; |
23 |
|
23 |
|
Lines 25-41
use C4::Auth;
Link Here
|
25 |
use C4::Context; |
25 |
use C4::Context; |
26 |
|
26 |
|
27 |
use Koha::Database; |
27 |
use Koha::Database; |
28 |
use Koha::Patron; |
|
|
29 |
|
28 |
|
30 |
my $builder = t::lib::TestBuilder->new(); |
29 |
BEGIN { |
|
|
30 |
use_ok('Koha::Object'); |
31 |
use_ok('Koha::Patron'); |
32 |
} |
31 |
|
33 |
|
|
|
34 |
my $builder = t::lib::TestBuilder->new(); |
32 |
my $dbh = C4::Context->dbh; |
35 |
my $dbh = C4::Context->dbh; |
33 |
$dbh->{AutoCommit} = 0; |
36 |
my $schema = Koha::Database->schema; |
34 |
$dbh->{RaiseError} = 1; |
|
|
35 |
|
37 |
|
36 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
38 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
37 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
39 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
38 |
|
40 |
|
|
|
41 |
$schema->storage->txn_begin; |
42 |
|
39 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
43 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
40 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
44 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
41 |
my $guarantor = $builder->build({ |
45 |
my $guarantor = $builder->build({ |
Lines 46-52
my $guarantor = $builder->build({
Link Here
|
46 |
flags => 0, |
50 |
flags => 0, |
47 |
} |
51 |
} |
48 |
}); |
52 |
}); |
49 |
my $borrower = $builder->build({ |
53 |
my $patron = $builder->build({ |
50 |
source => 'Borrower', |
54 |
source => 'Borrower', |
51 |
value => { |
55 |
value => { |
52 |
branchcode => $branchcode, |
56 |
branchcode => $branchcode, |
Lines 59-70
my $borrower = $builder->build({
Link Here
|
59 |
$t->get_ok('/api/v1/patrons') |
63 |
$t->get_ok('/api/v1/patrons') |
60 |
->status_is(401); |
64 |
->status_is(401); |
61 |
|
65 |
|
62 |
$t->get_ok("/api/v1/patrons/" . $borrower->{ borrowernumber }) |
66 |
$t->get_ok("/api/v1/patrons/" . $patron->{ borrowernumber }) |
63 |
->status_is(401); |
67 |
->status_is(401); |
64 |
|
68 |
|
65 |
my $session = C4::Auth::get_session(''); |
69 |
my $session = C4::Auth::get_session(''); |
66 |
$session->param('number', $borrower->{ borrowernumber }); |
70 |
$session->param('number', $patron->{ borrowernumber }); |
67 |
$session->param('id', $borrower->{ userid }); |
71 |
$session->param('id', $patron->{ userid }); |
68 |
$session->param('ip', '127.0.0.1'); |
72 |
$session->param('ip', '127.0.0.1'); |
69 |
$session->param('lasttime', time()); |
73 |
$session->param('lasttime', time()); |
70 |
$session->flush; |
74 |
$session->flush; |
Lines 81-100
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
Link Here
|
81 |
$t->request_ok($tx) |
85 |
$t->request_ok($tx) |
82 |
->status_is(403); |
86 |
->status_is(403); |
83 |
|
87 |
|
84 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . ($borrower->{ borrowernumber }-1)); |
88 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . ($patron->{ borrowernumber }-1)); |
85 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
89 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
86 |
$t->request_ok($tx) |
90 |
$t->request_ok($tx) |
87 |
->status_is(403) |
91 |
->status_is(403) |
88 |
->json_is('/required_permissions', {"borrowers" => "1"}); |
92 |
->json_is('/required_permissions', {"borrowers" => "1"}); |
89 |
|
93 |
|
90 |
# User without permissions, but is the owner of the object |
94 |
# User without permissions, but is the owner of the object |
91 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{borrowernumber}); |
95 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $patron->{borrowernumber}); |
92 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
96 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
93 |
$t->request_ok($tx) |
97 |
$t->request_ok($tx) |
94 |
->status_is(200); |
98 |
->status_is(200); |
95 |
|
99 |
|
96 |
# User without permissions, but is the guarantor of the owner of the object |
100 |
# User without permissions, but is the guarantor of the owner of the object |
97 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{borrowernumber}); |
101 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $patron->{borrowernumber}); |
98 |
$tx->req->cookies({name => 'CGISESSID', value => $session2->id}); |
102 |
$tx->req->cookies({name => 'CGISESSID', value => $session2->id}); |
99 |
$t->request_ok($tx) |
103 |
$t->request_ok($tx) |
100 |
->status_is(200) |
104 |
->status_is(200) |
Lines 121-132
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
Link Here
|
121 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
125 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
122 |
$t->request_ok($tx) |
126 |
$t->request_ok($tx) |
123 |
->status_is(200); |
127 |
->status_is(200); |
|
|
128 |
ok(@{$tx->res->json} >= 1, 'Json response lists all when no params given'); |
124 |
|
129 |
|
125 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{ borrowernumber }); |
130 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $patron->{ borrowernumber }); |
126 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
131 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
127 |
$t->request_ok($tx) |
132 |
$t->request_ok($tx) |
128 |
->status_is(200) |
133 |
->status_is(200) |
129 |
->json_is('/borrowernumber' => $borrower->{ borrowernumber }) |
134 |
->json_is('/borrowernumber' => $patron->{ borrowernumber }) |
130 |
->json_is('/surname' => $borrower->{ surname }); |
135 |
->json_is('/surname' => $patron->{ surname }); |
|
|
136 |
|
137 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons' => form => {surname => 'nonexistent'}); |
138 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
139 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
140 |
$t->request_ok($tx) |
141 |
->status_is(200); |
142 |
ok(@{$tx->res->json} == 0, "Json response yields no results when params doesn't match"); |
143 |
|
144 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons' => form => {surname => $patron->{ surname }}); |
145 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
146 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
147 |
$t->request_ok($tx) |
148 |
->status_is(200) |
149 |
->json_has($patron); |
150 |
ok(@{$tx->res->json} == 1, 'Json response yields expected results when params match'); |
151 |
|
152 |
### POST /api/v1/patrons |
153 |
|
154 |
my $newpatron = { |
155 |
branchcode => $branchcode, |
156 |
categorycode => $categorycode, |
157 |
surname => "TestUser", |
158 |
cardnumber => "123456", |
159 |
userid => "testuser" |
160 |
}; |
161 |
|
162 |
$newpatron->{ branchcode } = "nonexistent"; # Test invalid branchcode |
163 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
164 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
165 |
$t->request_ok($tx) |
166 |
->status_is(404) |
167 |
->json_is('/error' => "Library with branchcode \"nonexistent\" does not exist"); |
168 |
|
169 |
$newpatron->{ branchcode } = $branchcode; |
170 |
$newpatron->{ categorycode } = "nonexistent"; # Test invalid patron category |
171 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
172 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
173 |
$t->request_ok($tx) |
174 |
->status_is(404) |
175 |
->json_is('/error' => "Patron category \"nonexistent\" does not exist"); |
176 |
$newpatron->{ categorycode } = $categorycode; |
177 |
|
178 |
$newpatron->{ falseproperty } = "Non existent property"; |
179 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
180 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
181 |
$t->request_ok($tx) |
182 |
->status_is(500) |
183 |
->json_is('/error' => "Something went wrong, check Koha logs for details"); |
184 |
|
185 |
delete $newpatron->{ falseproperty }; |
186 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
187 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
188 |
$t->request_ok($tx) |
189 |
->status_is(201, 'Patron created successfully') |
190 |
->json_has('/borrowernumber', 'got a borrowernumber') |
191 |
->json_is('/cardnumber', $newpatron->{ cardnumber }) |
192 |
->json_is('/surname' => $newpatron->{ surname }) |
193 |
->json_is('/firstname' => $newpatron->{ firstname }); |
194 |
$newpatron->{borrowernumber} = $tx->res->json->{borrowernumber}; |
195 |
|
196 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
197 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
198 |
$t->request_ok($tx) |
199 |
->status_is(409) |
200 |
->json_has('/error', 'Fails when trying to POST duplicate cardnumber or userid') |
201 |
->json_has('/conflict', { userid => $newpatron->{ userid }, cardnumber => $newpatron->{ cardnumber } }); |
202 |
|
203 |
### PUT /api/v1/patrons |
204 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/0" => json => {}); |
205 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
206 |
$t->request_ok($tx) |
207 |
->status_is(404) |
208 |
->json_has('/error', 'Fails when trying to PUT nonexistent patron'); |
209 |
|
210 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => {categorycode => "nonexistent"}); |
211 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
212 |
$t->request_ok($tx) |
213 |
->status_is(404) |
214 |
->json_is('/error' => "Patron category \"nonexistent\" does not exist"); |
215 |
|
216 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => {branchcode => "nonexistent"}); |
217 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
218 |
$t->request_ok($tx) |
219 |
->status_is(404) |
220 |
->json_is('/error' => "Library with branchcode \"nonexistent\" does not exist"); |
221 |
|
222 |
$newpatron->{ falseproperty } = "Non existent property"; |
223 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
224 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
225 |
$t->request_ok($tx) |
226 |
->status_is(500) |
227 |
->json_is('/error' => "Something went wrong, check Koha logs for details"); |
228 |
delete $newpatron->{ falseproperty }; |
229 |
|
230 |
$newpatron->{ cardnumber } = $patron-> { cardnumber }; |
231 |
$newpatron->{ userid } = $patron-> { userid }; |
232 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
233 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
234 |
$t->request_ok($tx) |
235 |
->status_is(409) |
236 |
->json_has('/error' => "Fails when trying to update to an existing cardnumber or userid") |
237 |
->json_has('/conflict', { cardnumber => $patron->{ cardnumber }, userid => $patron->{ userid } }); |
238 |
|
239 |
$newpatron->{ cardnumber } = "123456"; |
240 |
$newpatron->{ userid } = "testuser"; |
241 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
242 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
243 |
$t->request_ok($tx) |
244 |
->status_is(204, 'No changes - patron NOT updated'); |
245 |
|
246 |
$newpatron->{ cardnumber } = "234567"; |
247 |
$newpatron->{ userid } = "updatedtestuser"; |
248 |
$newpatron->{ surname } = "UpdatedTestUser"; |
249 |
|
250 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
251 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
252 |
$t->request_ok($tx) |
253 |
->status_is(200, 'Patron updated successfully') |
254 |
->json_has($newpatron); |
255 |
|
256 |
### DELETE /api/v1/patrons |
257 |
|
258 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/0"); |
259 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
260 |
$t->request_ok($tx) |
261 |
->status_is(404, 'Patron not found'); |
262 |
|
263 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/" . $newpatron->{ borrowernumber }); |
264 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
265 |
$t->request_ok($tx) |
266 |
->status_is(200, 'Patron deleted successfully'); |
131 |
|
267 |
|
132 |
$dbh->rollback; |
268 |
$schema->storage->txn_rollback; |
133 |
- |
|
|