Lines 17-44
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 10; |
|
|
21 |
use Test::Mojo; |
22 |
use t::lib::TestBuilder; |
20 |
use t::lib::TestBuilder; |
23 |
|
21 |
|
|
|
22 |
use Test::More tests => 56; |
23 |
use Test::Mojo; |
24 |
|
24 |
use C4::Auth; |
25 |
use C4::Auth; |
25 |
use C4::Context; |
26 |
use C4::Context; |
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 |
|
32 |
my $dbh = C4::Context->dbh; |
34 |
my $schema = Koha::Database->schema; |
33 |
$dbh->{AutoCommit} = 0; |
35 |
my $dbh = C4::Context->dbh; |
34 |
$dbh->{RaiseError} = 1; |
36 |
my $builder = t::lib::TestBuilder->new; |
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 $borrower = $builder->build({ |
45 |
my $patron = $builder->build({ |
42 |
source => 'Borrower', |
46 |
source => 'Borrower', |
43 |
value => { |
47 |
value => { |
44 |
branchcode => $branchcode, |
48 |
branchcode => $branchcode, |
Lines 46-55
my $borrower = $builder->build({
Link Here
|
46 |
} |
50 |
} |
47 |
}); |
51 |
}); |
48 |
|
52 |
|
|
|
53 |
### GET /api/v1/patrons |
54 |
|
49 |
$t->get_ok('/api/v1/patrons') |
55 |
$t->get_ok('/api/v1/patrons') |
50 |
->status_is(403); |
56 |
->status_is(403); |
51 |
|
57 |
|
52 |
$t->get_ok("/api/v1/patrons/" . $borrower->{ borrowernumber }) |
58 |
$t->get_ok("/api/v1/patrons/" . $patron->{ borrowernumber }) |
53 |
->status_is(403); |
59 |
->status_is(403); |
54 |
|
60 |
|
55 |
my $loggedinuser = $builder->build({ |
61 |
my $loggedinuser = $builder->build({ |
Lines 73-84
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
Link Here
|
73 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
79 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
74 |
$t->request_ok($tx) |
80 |
$t->request_ok($tx) |
75 |
->status_is(200); |
81 |
->status_is(200); |
|
|
82 |
ok(@{$tx->res->json} >= 1, 'Json response lists all when no params given'); |
83 |
|
84 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $patron->{ borrowernumber }); |
85 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
86 |
$t->request_ok($tx) |
87 |
->status_is(200) |
88 |
->json_is('/borrowernumber' => $patron->{ borrowernumber }) |
89 |
->json_is('/surname' => $patron->{ surname }); |
90 |
|
91 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons' => form => {surname => 'nonexistent'}); |
92 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
93 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
94 |
$t->request_ok($tx) |
95 |
->status_is(200); |
96 |
ok(@{$tx->res->json} == 0, 'Json response yields no results when params doesnt match'); |
76 |
|
97 |
|
77 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{ borrowernumber }); |
98 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons' => form => {surname => $patron->{ surname }}); |
78 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
99 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
|
|
100 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
79 |
$t->request_ok($tx) |
101 |
$t->request_ok($tx) |
80 |
->status_is(200) |
102 |
->status_is(200) |
81 |
->json_is('/borrowernumber' => $borrower->{ borrowernumber }) |
103 |
->json_has($patron); |
82 |
->json_is('/surname' => $borrower->{ surname }); |
104 |
ok(@{$tx->res->json} == 1, 'Json response yields expected results when params match'); |
|
|
105 |
|
106 |
### POST /api/v1/patrons |
107 |
|
108 |
my $newpatron = { |
109 |
branchcode => $branchcode, |
110 |
categorycode => $categorycode, |
111 |
surname => "TestUser", |
112 |
cardnumber => "123456", |
113 |
userid => "testuser" |
114 |
}; |
115 |
|
116 |
$newpatron->{ branchcode } = "nonexistent"; # Test invalid branchcode |
117 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
118 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
119 |
$t->request_ok($tx) |
120 |
->status_is(404) |
121 |
->json_is('/error' => "Library with branchcode \"nonexistent\" does not exist"); |
122 |
|
123 |
$newpatron->{ branchcode } = $branchcode; |
124 |
$newpatron->{ categorycode } = "nonexistent"; # Test invalid patron category |
125 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
126 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
127 |
$t->request_ok($tx) |
128 |
->status_is(404) |
129 |
->json_is('/error' => "Patron category \"nonexistent\" does not exist"); |
130 |
$newpatron->{ categorycode } = $categorycode; |
131 |
|
132 |
$newpatron->{ falseproperty } = "Non existent property"; |
133 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
134 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
135 |
$t->request_ok($tx) |
136 |
->status_is(500) |
137 |
->json_is('/error' => "Something went wrong, check Koha logs for details"); |
138 |
|
139 |
delete $newpatron->{ falseproperty }; |
140 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
141 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
142 |
$t->request_ok($tx) |
143 |
->status_is(201, 'Patron created successfully') |
144 |
->json_has('/borrowernumber', 'got a borrowernumber') |
145 |
->json_is('/cardnumber', $newpatron->{ cardnumber }) |
146 |
->json_is('/surname' => $newpatron->{ surname }) |
147 |
->json_is('/firstname' => $newpatron->{ firstname }); |
148 |
$newpatron->{borrowernumber} = $tx->res->json->{borrowernumber}; |
149 |
|
150 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
151 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
152 |
$t->request_ok($tx) |
153 |
->status_is(409) |
154 |
->json_has('/error', 'Fails when trying to POST duplicate cardnumber or userid') |
155 |
->json_has('/conflict', { userid => $newpatron->{ userid }, cardnumber => $newpatron->{ cardnumber } }); |
156 |
|
157 |
### PUT /api/v1/patrons |
158 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/0" => json => {}); |
159 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
160 |
$t->request_ok($tx) |
161 |
->status_is(404) |
162 |
->json_has('/error', 'Fails when trying to PUT nonexistent patron'); |
163 |
|
164 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => {categorycode => "nonexistent"}); |
165 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
166 |
$t->request_ok($tx) |
167 |
->status_is(404) |
168 |
->json_is('/error' => "Patron category \"nonexistent\" does not exist"); |
169 |
|
170 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => {branchcode => "nonexistent"}); |
171 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
172 |
$t->request_ok($tx) |
173 |
->status_is(404) |
174 |
->json_is('/error' => "Library with branchcode \"nonexistent\" does not exist"); |
175 |
|
176 |
$newpatron->{ falseproperty } = "Non existent property"; |
177 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
178 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
179 |
$t->request_ok($tx) |
180 |
->status_is(500) |
181 |
->json_is('/error' => "Something went wrong, check Koha logs for details"); |
182 |
delete $newpatron->{ falseproperty }; |
183 |
|
184 |
$newpatron->{ cardnumber } = $patron-> { cardnumber }; |
185 |
$newpatron->{ userid } = $patron-> { userid }; |
186 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
187 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
188 |
$t->request_ok($tx) |
189 |
->status_is(409) |
190 |
->json_has('/error' => "Fails when trying to update to an existing cardnumber or userid") |
191 |
->json_has('/conflict', { cardnumber => $patron->{ cardnumber }, userid => $patron->{ userid } }); |
192 |
|
193 |
$newpatron->{ cardnumber } = "123456"; |
194 |
$newpatron->{ userid } = "testuser"; |
195 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
196 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
197 |
$t->request_ok($tx) |
198 |
->status_is(204, 'No changes - patron NOT updated'); |
199 |
|
200 |
$newpatron->{ cardnumber } = "234567"; |
201 |
$newpatron->{ userid } = "updatedtestuser"; |
202 |
$newpatron->{ surname } = "UpdatedTestUser"; |
203 |
|
204 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
205 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
206 |
$t->request_ok($tx) |
207 |
->status_is(200, 'Patron updated successfully') |
208 |
->json_has($newpatron); |
209 |
|
210 |
### DELETE /api/v1/patrons |
211 |
|
212 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/0"); |
213 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
214 |
$t->request_ok($tx) |
215 |
->status_is(404, 'Patron not found'); |
216 |
|
217 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/" . $newpatron->{ borrowernumber }); |
218 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
219 |
$t->request_ok($tx) |
220 |
->status_is(200, 'Patron deleted successfully'); |
221 |
|
222 |
$schema->storage->txn_rollback; |
83 |
|
223 |
|
84 |
$dbh->rollback; |
|
|
85 |
- |