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 => 50; |
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 49-55
my $borrower = $builder->build({
Link Here
|
49 |
$t->get_ok('/api/v1/patrons') |
53 |
$t->get_ok('/api/v1/patrons') |
50 |
->status_is(403); |
54 |
->status_is(403); |
51 |
|
55 |
|
52 |
$t->get_ok("/api/v1/patrons/" . $borrower->{ borrowernumber }) |
56 |
$t->get_ok("/api/v1/patrons/" . $patron->{ borrowernumber }) |
53 |
->status_is(403); |
57 |
->status_is(403); |
54 |
|
58 |
|
55 |
my $loggedinuser = $builder->build({ |
59 |
my $loggedinuser = $builder->build({ |
Lines 74-84
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
Link Here
|
74 |
$t->request_ok($tx) |
78 |
$t->request_ok($tx) |
75 |
->status_is(200); |
79 |
->status_is(200); |
76 |
|
80 |
|
77 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{ borrowernumber }); |
81 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $patron->{ borrowernumber }); |
78 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
82 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
79 |
$t->request_ok($tx) |
83 |
$t->request_ok($tx) |
80 |
->status_is(200) |
84 |
->status_is(200) |
81 |
->json_is('/borrowernumber' => $borrower->{ borrowernumber }) |
85 |
->json_is('/borrowernumber' => $patron->{ borrowernumber }) |
82 |
->json_is('/surname' => $borrower->{ surname }); |
86 |
->json_is('/surname' => $patron->{ surname }); |
|
|
87 |
|
88 |
### POST /api/v1/patrons |
89 |
|
90 |
my $newpatron = { |
91 |
branchcode => $branchcode, |
92 |
categorycode => $categorycode, |
93 |
surname => "TestUser", |
94 |
cardnumber => "123456", |
95 |
userid => "testuser" |
96 |
}; |
97 |
|
98 |
$newpatron->{ branchcode } = "nonexistent"; # Test invalid branchcode |
99 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
100 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
101 |
$t->request_ok($tx) |
102 |
->status_is(404) |
103 |
->json_is('/error' => "Library with branchcode \"nonexistent\" does not exist"); |
104 |
|
105 |
$newpatron->{ branchcode } = $branchcode; |
106 |
$newpatron->{ categorycode } = "nonexistent"; # Test invalid patron category |
107 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
108 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
109 |
$t->request_ok($tx) |
110 |
->status_is(404) |
111 |
->json_is('/error' => "Patron category \"nonexistent\" does not exist"); |
112 |
|
113 |
$newpatron->{ categorycode } = $categorycode; |
114 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
115 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
116 |
$t->request_ok($tx) |
117 |
->status_is(201, 'Patron created successfully') |
118 |
->json_has('/borrowernumber', 'got a borrowernumber') |
119 |
->json_is('/cardnumber', $newpatron->{ cardnumber }) |
120 |
->json_is('/surname' => $newpatron->{ surname }) |
121 |
->json_is('/firstname' => $newpatron->{ firstname }); |
122 |
$newpatron->{borrowernumber} = $tx->res->json->{borrowernumber}; |
123 |
|
124 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
125 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
126 |
$t->request_ok($tx) |
127 |
->status_is(409) |
128 |
->json_has('/error', 'Fails when trying to POST duplicate cardnumber or userid') |
129 |
->json_has('/conflict', { userid => $newpatron->{ userid }, cardnumber => $newpatron->{ cardnumber } }); |
130 |
|
131 |
### PUT /api/v1/patrons |
132 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/0" => json => {}); |
133 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
134 |
$t->request_ok($tx) |
135 |
->status_is(404) |
136 |
->json_has('/error', 'Fails when trying to PUT nonexistent patron'); |
137 |
|
138 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => {categorycode => "nonexistent"}); |
139 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
140 |
$t->request_ok($tx) |
141 |
->status_is(404) |
142 |
->json_is('/error' => "Patron category \"nonexistent\" does not exist"); |
143 |
|
144 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => {branchcode => "nonexistent"}); |
145 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
146 |
$t->request_ok($tx) |
147 |
->status_is(404) |
148 |
->json_is('/error' => "Library with branchcode \"nonexistent\" does not exist"); |
149 |
|
150 |
$newpatron->{ cardnumber } = $patron-> { cardnumber }; |
151 |
$newpatron->{ userid } = $patron-> { userid }; |
152 |
|
153 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
154 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
155 |
$t->request_ok($tx) |
156 |
->status_is(409) |
157 |
->json_has('/error' => "Fails when trying to update to an existing cardnumber or userid") |
158 |
->json_has('/conflict', { cardnumber => $patron->{ cardnumber }, userid => $patron->{ userid } }); |
159 |
|
160 |
$newpatron->{ cardnumber } = "123456"; |
161 |
$newpatron->{ userid } = "testuser"; |
162 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
163 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
164 |
$t->request_ok($tx) |
165 |
->status_is(204, 'No changes - patron NOT updated'); |
166 |
|
167 |
$newpatron->{ cardnumber } = "234567"; |
168 |
$newpatron->{ userid } = "updatedtestuser"; |
169 |
$newpatron->{ surname } = "UpdatedTestUser"; |
170 |
|
171 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
172 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
173 |
$t->request_ok($tx) |
174 |
->status_is(200, 'Patron updated successfully') |
175 |
->json_has($newpatron); |
176 |
|
177 |
### DELETE /api/v1/patrons |
178 |
|
179 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/0"); |
180 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
181 |
$t->request_ok($tx) |
182 |
->status_is(404, 'Patron not found'); |
183 |
|
184 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/" . $newpatron->{ borrowernumber }); |
185 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
186 |
$t->request_ok($tx) |
187 |
->status_is(200, 'Patron deleted successfully'); |
188 |
|
189 |
$schema->storage->txn_rollback; |
83 |
|
190 |
|
84 |
$dbh->rollback; |
|
|
85 |
- |