Lines 17-48
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 10; |
20 |
use Test::More tests => 26; |
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 |
my $password = "secret"; |
41 |
my $borrower = $builder->build({ |
43 |
my $borrower = $builder->build({ |
42 |
source => 'Borrower', |
44 |
source => 'Borrower', |
43 |
value => { |
45 |
value => { |
44 |
branchcode => $branchcode, |
46 |
branchcode => $branchcode, |
45 |
categorycode => $categorycode |
47 |
categorycode => $categorycode, |
46 |
} |
48 |
} |
47 |
}); |
49 |
}); |
48 |
|
50 |
|
Lines 52-63
$t->get_ok('/api/v1/patrons')
Link Here
|
52 |
$t->get_ok("/api/v1/patrons/" . $borrower->{ borrowernumber }) |
54 |
$t->get_ok("/api/v1/patrons/" . $borrower->{ borrowernumber }) |
53 |
->status_is(403); |
55 |
->status_is(403); |
54 |
|
56 |
|
|
|
57 |
$t->patch_ok("/api/v1/patrons/-100/password") |
58 |
->status_is(400); |
59 |
|
60 |
my $password_obj = { |
61 |
current_password => $password, |
62 |
new_password => "new password", |
63 |
}; |
64 |
|
65 |
my $tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/-100/password' => json => $password_obj); |
66 |
$t->request_ok($tx) |
67 |
->status_is(403); |
68 |
|
55 |
my $loggedinuser = $builder->build({ |
69 |
my $loggedinuser = $builder->build({ |
56 |
source => 'Borrower', |
70 |
source => 'Borrower', |
57 |
value => { |
71 |
value => { |
58 |
branchcode => $branchcode, |
72 |
branchcode => $branchcode, |
59 |
categorycode => $categorycode, |
73 |
categorycode => $categorycode, |
60 |
flags => 16 # borrowers flag |
74 |
flags => 16, # borrowers flag |
|
|
75 |
password => Koha::AuthUtils::hash_password($password), |
61 |
} |
76 |
} |
62 |
}); |
77 |
}); |
63 |
|
78 |
|
Lines 68-74
$session->param('ip', '127.0.0.1');
Link Here
|
68 |
$session->param('lasttime', time()); |
83 |
$session->param('lasttime', time()); |
69 |
$session->flush; |
84 |
$session->flush; |
70 |
|
85 |
|
71 |
my $tx = $t->ua->build_tx(GET => '/api/v1/patrons'); |
86 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons'); |
72 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
87 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
73 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
88 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
74 |
$t->request_ok($tx) |
89 |
$t->request_ok($tx) |
Lines 81-84
$t->request_ok($tx)
Link Here
|
81 |
->json_is('/borrowernumber' => $borrower->{ borrowernumber }) |
96 |
->json_is('/borrowernumber' => $borrower->{ borrowernumber }) |
82 |
->json_is('/surname' => $borrower->{ surname }); |
97 |
->json_is('/surname' => $borrower->{ surname }); |
83 |
|
98 |
|
84 |
$dbh->rollback; |
99 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/-100/password' => json => $password_obj); |
|
|
100 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
101 |
$t->request_ok($tx) |
102 |
->status_is(404); |
103 |
|
104 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$loggedinuser->{borrowernumber}.'/password' => json => $password_obj); |
105 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
106 |
$t->request_ok($tx) |
107 |
->status_is(200); |
108 |
|
109 |
ok(C4::Auth::checkpw_hash($password_obj->{'new_password'}, Koha::Patrons->find($loggedinuser->{borrowernumber})->password), "New password in database."); |
110 |
is(C4::Auth::checkpw_hash($password_obj->{'current_password'}, Koha::Patrons->find($loggedinuser->{borrowernumber})->password), "", "Old password is gone."); |
111 |
|
112 |
$password_obj->{'current_password'} = $password_obj->{'new_password'}; |
113 |
$password_obj->{'new_password'} = "a"; |
114 |
t::lib::Mocks::mock_preference("minPasswordLength", 5); |
115 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$loggedinuser->{borrowernumber}.'/password' => json => $password_obj); |
116 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
117 |
$t->request_ok($tx) |
118 |
->status_is(400) |
119 |
->json_like('/error', qr/Password is too short/, "Password too short"); |
120 |
|
121 |
$password_obj->{'new_password'} = " abcdef "; |
122 |
$tx = $t->ua->build_tx(PATCH => '/api/v1/patrons/'.$loggedinuser->{borrowernumber}.'/password' => json => $password_obj); |
123 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
124 |
$t->request_ok($tx) |
125 |
->status_is(400) |
126 |
->json_is('/error', "Password cannot contain trailing whitespaces."); |
127 |
|
128 |
$schema->storage->txn_rollback; |
85 |
- |
|
|