Lines 17-43
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 21; |
20 |
use Test::More tests => 39; |
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 82-87
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
Link Here
|
82 |
$t->request_ok($tx) |
86 |
$t->request_ok($tx) |
83 |
->status_is(403); |
87 |
->status_is(403); |
84 |
|
88 |
|
|
|
89 |
|
85 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . ($borrower->{ borrowernumber }-1)); |
90 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . ($borrower->{ borrowernumber }-1)); |
86 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
91 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
87 |
$t->request_ok($tx) |
92 |
$t->request_ok($tx) |
Lines 101-112
$t->request_ok($tx)
Link Here
|
101 |
->status_is(200) |
106 |
->status_is(200) |
102 |
->json_is('/guarantorid', $guarantor->{borrowernumber}); |
107 |
->json_is('/guarantorid', $guarantor->{borrowernumber}); |
103 |
|
108 |
|
|
|
109 |
my $password_obj = { |
110 |
current_password => $password, |
111 |
new_password => "new password", |
112 |
}; |
113 |
|
114 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/-100/password' => json => $password_obj); |
115 |
$t->request_ok($tx) |
116 |
->status_is(401); |
117 |
|
118 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$borrower->{borrowernumber}.'/password'); |
119 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
120 |
$t->request_ok($tx) |
121 |
->status_is(400); |
122 |
|
123 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$guarantor->{borrowernumber}.'/password' => json => $password_obj); |
124 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
125 |
$t->request_ok($tx) |
126 |
->status_is(403); |
127 |
|
104 |
my $loggedinuser = $builder->build({ |
128 |
my $loggedinuser = $builder->build({ |
105 |
source => 'Borrower', |
129 |
source => 'Borrower', |
106 |
value => { |
130 |
value => { |
107 |
branchcode => $branchcode, |
131 |
branchcode => $branchcode, |
108 |
categorycode => $categorycode, |
132 |
categorycode => $categorycode, |
109 |
flags => 16 # borrowers flag |
133 |
flags => 16, # borrowers flag |
|
|
134 |
password => Koha::AuthUtils::hash_password($password), |
110 |
} |
135 |
} |
111 |
}); |
136 |
}); |
112 |
|
137 |
|
Lines 131-134
$t->request_ok($tx)
Link Here
|
131 |
->json_is('/surname' => $borrower->{ surname }) |
156 |
->json_is('/surname' => $borrower->{ surname }) |
132 |
->json_is('/lost' => 1 ); |
157 |
->json_is('/lost' => 1 ); |
133 |
|
158 |
|
134 |
$dbh->rollback; |
159 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/-100/password' => json => $password_obj); |
|
|
160 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
161 |
$t->request_ok($tx) |
162 |
->status_is(404); |
163 |
|
164 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$loggedinuser->{borrowernumber}.'/password' => json => $password_obj); |
165 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
166 |
$t->request_ok($tx) |
167 |
->status_is(200); |
168 |
|
169 |
ok(C4::Auth::checkpw_hash($password_obj->{'new_password'}, Koha::Patrons->find($loggedinuser->{borrowernumber})->password), "New password in database."); |
170 |
is(C4::Auth::checkpw_hash($password_obj->{'current_password'}, Koha::Patrons->find($loggedinuser->{borrowernumber})->password), "", "Old password is gone."); |
171 |
|
172 |
$password_obj->{'current_password'} = $password_obj->{'new_password'}; |
173 |
$password_obj->{'new_password'} = "a"; |
174 |
t::lib::Mocks::mock_preference("minPasswordLength", 5); |
175 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$loggedinuser->{borrowernumber}.'/password' => json => $password_obj); |
176 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
177 |
$t->request_ok($tx) |
178 |
->status_is(400) |
179 |
->json_like('/error', qr/Password is too short/, "Password too short"); |
180 |
|
181 |
$password_obj->{'new_password'} = " abcdef "; |
182 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$loggedinuser->{borrowernumber}.'/password' => json => $password_obj); |
183 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
184 |
$t->request_ok($tx) |
185 |
->status_is(400) |
186 |
->json_is('/error', "Password cannot contain trailing whitespaces."); |
187 |
|
188 |
$schema->storage->txn_rollback; |
135 |
- |
|
|