|
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 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 |
$newpatron->{ categorycode } = $categorycode; |
| 113 |
|
| 114 |
$newpatron->{ falseproperty } = "Non existent property"; |
| 115 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
| 116 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 117 |
$t->request_ok($tx) |
| 118 |
->status_is(500) |
| 119 |
->json_is('/error' => "Something went wrong, check Koha logs for details"); |
| 120 |
|
| 121 |
delete $newpatron->{ falseproperty }; |
| 122 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
| 123 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 124 |
$t->request_ok($tx) |
| 125 |
->status_is(201, 'Patron created successfully') |
| 126 |
->json_has('/borrowernumber', 'got a borrowernumber') |
| 127 |
->json_is('/cardnumber', $newpatron->{ cardnumber }) |
| 128 |
->json_is('/surname' => $newpatron->{ surname }) |
| 129 |
->json_is('/firstname' => $newpatron->{ firstname }); |
| 130 |
$newpatron->{borrowernumber} = $tx->res->json->{borrowernumber}; |
| 131 |
|
| 132 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
| 133 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 134 |
$t->request_ok($tx) |
| 135 |
->status_is(409) |
| 136 |
->json_has('/error', 'Fails when trying to POST duplicate cardnumber or userid') |
| 137 |
->json_has('/conflict', { userid => $newpatron->{ userid }, cardnumber => $newpatron->{ cardnumber } }); |
| 138 |
|
| 139 |
### PUT /api/v1/patrons |
| 140 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/0" => json => {}); |
| 141 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 142 |
$t->request_ok($tx) |
| 143 |
->status_is(404) |
| 144 |
->json_has('/error', 'Fails when trying to PUT nonexistent patron'); |
| 145 |
|
| 146 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => {categorycode => "nonexistent"}); |
| 147 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 148 |
$t->request_ok($tx) |
| 149 |
->status_is(404) |
| 150 |
->json_is('/error' => "Patron category \"nonexistent\" does not exist"); |
| 151 |
|
| 152 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => {branchcode => "nonexistent"}); |
| 153 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 154 |
$t->request_ok($tx) |
| 155 |
->status_is(404) |
| 156 |
->json_is('/error' => "Library with branchcode \"nonexistent\" does not exist"); |
| 157 |
|
| 158 |
$newpatron->{ falseproperty } = "Non existent property"; |
| 159 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
| 160 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 161 |
$t->request_ok($tx) |
| 162 |
->status_is(500) |
| 163 |
->json_is('/error' => "Something went wrong, check Koha logs for details"); |
| 164 |
delete $newpatron->{ falseproperty }; |
| 165 |
|
| 166 |
$newpatron->{ cardnumber } = $patron-> { cardnumber }; |
| 167 |
$newpatron->{ userid } = $patron-> { userid }; |
| 168 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $newpatron->{ borrowernumber } => json => $newpatron); |
| 169 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 170 |
$t->request_ok($tx) |
| 171 |
->status_is(409) |
| 172 |
->json_has('/error' => "Fails when trying to update to an existing cardnumber or userid") |
| 173 |
->json_has('/conflict', { cardnumber => $patron->{ cardnumber }, userid => $patron->{ userid } }); |
| 174 |
|
| 175 |
$newpatron->{ cardnumber } = "123456"; |
| 176 |
$newpatron->{ userid } = "testuser"; |
| 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(204, 'No changes - patron NOT updated'); |
| 181 |
|
| 182 |
$newpatron->{ cardnumber } = "234567"; |
| 183 |
$newpatron->{ userid } = "updatedtestuser"; |
| 184 |
$newpatron->{ surname } = "UpdatedTestUser"; |
| 185 |
|
| 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(200, 'Patron updated successfully') |
| 190 |
->json_has($newpatron); |
| 191 |
|
| 192 |
### DELETE /api/v1/patrons |
| 193 |
|
| 194 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/0"); |
| 195 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 196 |
$t->request_ok($tx) |
| 197 |
->status_is(404, 'Patron not found'); |
| 198 |
|
| 199 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/" . $newpatron->{ borrowernumber }); |
| 200 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 201 |
$t->request_ok($tx) |
| 202 |
->status_is(200, 'Patron deleted successfully'); |
| 203 |
|
| 204 |
$schema->storage->txn_rollback; |
| 83 |
|
205 |
|
| 84 |
$dbh->rollback; |
|
|
| 85 |
- |