|
Lines 17-43
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 => 38; |
| 21 |
use Test::Mojo; |
21 |
use Test::Mojo; |
| 22 |
use t::lib::TestBuilder; |
22 |
use t::lib::TestBuilder; |
|
|
23 |
use t::lib::Mocks; |
| 23 |
|
24 |
|
| 24 |
use C4::Auth; |
25 |
use C4::Auth; |
| 25 |
use C4::Context; |
26 |
use C4::Context; |
| 26 |
|
27 |
|
|
|
28 |
use Koha::AuthUtils; |
| 27 |
use Koha::Database; |
29 |
use Koha::Database; |
| 28 |
use Koha::Patron; |
30 |
use Koha::Patron; |
| 29 |
|
31 |
|
| 30 |
my $builder = t::lib::TestBuilder->new(); |
32 |
my $builder = t::lib::TestBuilder->new(); |
| 31 |
|
33 |
|
| 32 |
my $dbh = C4::Context->dbh; |
34 |
my $schema = Koha::Database->new->schema; |
| 33 |
$dbh->{AutoCommit} = 0; |
35 |
$schema->storage->txn_begin; |
| 34 |
$dbh->{RaiseError} = 1; |
|
|
| 35 |
|
36 |
|
| 36 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
37 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
| 37 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
38 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 38 |
|
39 |
|
| 39 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
40 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
| 40 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
41 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
|
|
42 |
|
| 41 |
my $guarantor = $builder->build({ |
43 |
my $guarantor = $builder->build({ |
| 42 |
source => 'Borrower', |
44 |
source => 'Borrower', |
| 43 |
value => { |
45 |
value => { |
|
Lines 46-51
my $guarantor = $builder->build({
Link Here
|
| 46 |
flags => 0, |
48 |
flags => 0, |
| 47 |
} |
49 |
} |
| 48 |
}); |
50 |
}); |
|
|
51 |
|
| 52 |
my $password = "secret"; |
| 49 |
my $borrower = $builder->build({ |
53 |
my $borrower = $builder->build({ |
| 50 |
source => 'Borrower', |
54 |
source => 'Borrower', |
| 51 |
value => { |
55 |
value => { |
|
Lines 81-86
$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 |
|
|
|
88 |
|
| 84 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . ($borrower->{ borrowernumber }-1)); |
89 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . ($borrower->{ borrowernumber }-1)); |
| 85 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
90 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 86 |
$t->request_ok($tx) |
91 |
$t->request_ok($tx) |
|
Lines 100-111
$t->request_ok($tx)
Link Here
|
| 100 |
->status_is(200) |
105 |
->status_is(200) |
| 101 |
->json_is('/guarantorid', $guarantor->{borrowernumber}); |
106 |
->json_is('/guarantorid', $guarantor->{borrowernumber}); |
| 102 |
|
107 |
|
|
|
108 |
my $password_obj = { |
| 109 |
current_password => $password, |
| 110 |
new_password => "new password", |
| 111 |
}; |
| 112 |
|
| 113 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/-100/password' => json => $password_obj); |
| 114 |
$t->request_ok($tx) |
| 115 |
->status_is(401); |
| 116 |
|
| 117 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$borrower->{borrowernumber}.'/password'); |
| 118 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 119 |
$t->request_ok($tx) |
| 120 |
->status_is(400); |
| 121 |
|
| 122 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$guarantor->{borrowernumber}.'/password' => json => $password_obj); |
| 123 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 124 |
$t->request_ok($tx) |
| 125 |
->status_is(403); |
| 126 |
|
| 103 |
my $loggedinuser = $builder->build({ |
127 |
my $loggedinuser = $builder->build({ |
| 104 |
source => 'Borrower', |
128 |
source => 'Borrower', |
| 105 |
value => { |
129 |
value => { |
| 106 |
branchcode => $branchcode, |
130 |
branchcode => $branchcode, |
| 107 |
categorycode => $categorycode, |
131 |
categorycode => $categorycode, |
| 108 |
flags => 16 # borrowers flag |
132 |
flags => 16, # borrowers flag |
|
|
133 |
password => Koha::AuthUtils::hash_password($password), |
| 109 |
} |
134 |
} |
| 110 |
}); |
135 |
}); |
| 111 |
|
136 |
|
|
Lines 129-132
$t->request_ok($tx)
Link Here
|
| 129 |
->json_is('/borrowernumber' => $borrower->{ borrowernumber }) |
154 |
->json_is('/borrowernumber' => $borrower->{ borrowernumber }) |
| 130 |
->json_is('/surname' => $borrower->{ surname }); |
155 |
->json_is('/surname' => $borrower->{ surname }); |
| 131 |
|
156 |
|
| 132 |
$dbh->rollback; |
157 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/-100/password' => json => $password_obj); |
|
|
158 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 159 |
$t->request_ok($tx) |
| 160 |
->status_is(404); |
| 161 |
|
| 162 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$loggedinuser->{borrowernumber}.'/password' => json => $password_obj); |
| 163 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 164 |
$t->request_ok($tx) |
| 165 |
->status_is(200); |
| 166 |
|
| 167 |
ok(C4::Auth::checkpw_hash($password_obj->{'new_password'}, Koha::Patrons->find($loggedinuser->{borrowernumber})->password), "New password in database."); |
| 168 |
is(C4::Auth::checkpw_hash($password_obj->{'current_password'}, Koha::Patrons->find($loggedinuser->{borrowernumber})->password), "", "Old password is gone."); |
| 169 |
|
| 170 |
$password_obj->{'current_password'} = $password_obj->{'new_password'}; |
| 171 |
$password_obj->{'new_password'} = "a"; |
| 172 |
t::lib::Mocks::mock_preference("minPasswordLength", 5); |
| 173 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$loggedinuser->{borrowernumber}.'/password' => json => $password_obj); |
| 174 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 175 |
$t->request_ok($tx) |
| 176 |
->status_is(400) |
| 177 |
->json_like('/error', qr/Password is too short/, "Password too short"); |
| 178 |
|
| 179 |
$password_obj->{'new_password'} = " abcdef "; |
| 180 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$loggedinuser->{borrowernumber}.'/password' => json => $password_obj); |
| 181 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 182 |
$t->request_ok($tx) |
| 183 |
->status_is(400) |
| 184 |
->json_is('/error', "Password cannot contain trailing whitespaces."); |
| 185 |
|
| 186 |
$schema->storage->txn_rollback; |
| 133 |
- |
|
|