|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 6 |
# terms of the GNU General Public License as published by the Free Software |
| 7 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 8 |
# version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License along |
| 15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 1; |
| 21 |
|
| 22 |
use Test::Mojo; |
| 23 |
use Test::Warn; |
| 24 |
|
| 25 |
use t::lib::TestBuilder; |
| 26 |
use t::lib::Mocks; |
| 27 |
|
| 28 |
use Koha::Patrons; |
| 29 |
|
| 30 |
my $schema = Koha::Database->new->schema; |
| 31 |
my $builder = t::lib::TestBuilder->new; |
| 32 |
|
| 33 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
| 34 |
# this affects the other REST api tests |
| 35 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
| 36 |
|
| 37 |
my $remote_address = '127.0.0.1'; |
| 38 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 39 |
|
| 40 |
subtest 'set() (authorized user tests)' => sub { |
| 41 |
|
| 42 |
plan tests => 21; |
| 43 |
|
| 44 |
$schema->storage->txn_begin; |
| 45 |
|
| 46 |
my ( $patron, $session ) = create_user_and_session({ authorized => 1 }); |
| 47 |
|
| 48 |
t::lib::Mocks::mock_preference( 'minPasswordLength', 3 ); |
| 49 |
t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); |
| 50 |
|
| 51 |
my $new_password = 'abc'; |
| 52 |
|
| 53 |
my $tx |
| 54 |
= $t->ua->build_tx( POST => "/api/v1/patrons/" |
| 55 |
. $patron->id |
| 56 |
. "/password" => json => { password => $new_password, password_2 => $new_password } ); |
| 57 |
|
| 58 |
$tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); |
| 59 |
$tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); |
| 60 |
$t->request_ok($tx)->status_is(200)->json_is( '' ); |
| 61 |
|
| 62 |
$tx |
| 63 |
= $t->ua->build_tx( POST => "/api/v1/patrons/" |
| 64 |
. $patron->id |
| 65 |
. "/password" => json => { password => $new_password, password_2 => 'cde' } ); |
| 66 |
|
| 67 |
$tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); |
| 68 |
$tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); |
| 69 |
$t->request_ok($tx)->status_is(400)->json_is({ error => 'Passwords don\'t match' }); |
| 70 |
|
| 71 |
t::lib::Mocks::mock_preference( 'minPasswordLength', 5 ); |
| 72 |
$tx |
| 73 |
= $t->ua->build_tx( POST => "/api/v1/patrons/" |
| 74 |
. $patron->id |
| 75 |
. "/password" => json => { password => $new_password, password_2 => $new_password } ); |
| 76 |
|
| 77 |
$tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); |
| 78 |
$tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); |
| 79 |
$t->request_ok($tx)->status_is(400)->json_is({ error => 'Password length (3) is shorter than required (5)' }); |
| 80 |
|
| 81 |
$new_password = 'abc '; |
| 82 |
$tx |
| 83 |
= $t->ua->build_tx( POST => "/api/v1/patrons/" |
| 84 |
. $patron->id |
| 85 |
. "/password" => json => { password => $new_password, password_2 => $new_password } ); |
| 86 |
|
| 87 |
$tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); |
| 88 |
$tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); |
| 89 |
$t->request_ok($tx)->status_is(400)->json_is({ error => 'Password contains trailing spaces, which is forbidden.' }); |
| 90 |
|
| 91 |
$new_password = 'abcdefg'; |
| 92 |
$tx |
| 93 |
= $t->ua->build_tx( POST => "/api/v1/patrons/" |
| 94 |
. $patron->id |
| 95 |
. "/password" => json => { password => $new_password, password_2 => $new_password } ); |
| 96 |
|
| 97 |
$tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); |
| 98 |
$tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); |
| 99 |
$t->request_ok($tx)->status_is(200)->json_is(''); |
| 100 |
|
| 101 |
t::lib::Mocks::mock_preference( 'RequireStrongPassword', 1); |
| 102 |
$tx |
| 103 |
= $t->ua->build_tx( POST => "/api/v1/patrons/" |
| 104 |
. $patron->id |
| 105 |
. "/password" => json => { password => $new_password, password_2 => $new_password } ); |
| 106 |
|
| 107 |
$tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); |
| 108 |
$tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); |
| 109 |
$t->request_ok($tx)->status_is(400)->json_is({ error => 'Password is too weak' }); |
| 110 |
|
| 111 |
$new_password = 'ABcde123%&'; |
| 112 |
$tx |
| 113 |
= $t->ua->build_tx( POST => "/api/v1/patrons/" |
| 114 |
. $patron->id |
| 115 |
. "/password" => json => { password => $new_password, password_2 => $new_password } ); |
| 116 |
|
| 117 |
$tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); |
| 118 |
$tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); |
| 119 |
$t->request_ok($tx)->status_is(200)->json_is(''); |
| 120 |
|
| 121 |
$schema->storage->txn_rollback; |
| 122 |
}; |
| 123 |
|
| 124 |
sub create_user_and_session { |
| 125 |
|
| 126 |
my ( $args ) = @_; |
| 127 |
my $flags = ( $args->{authorized} ) ? 16 : 0; |
| 128 |
|
| 129 |
my $user = $builder->build_object( |
| 130 |
{ |
| 131 |
class => 'Koha::Patrons', |
| 132 |
value => { flags => $flags } |
| 133 |
} |
| 134 |
); |
| 135 |
|
| 136 |
# Create a session for the authorized user |
| 137 |
my $session = C4::Auth::get_session(''); |
| 138 |
$session->param( 'number', $user->borrowernumber ); |
| 139 |
$session->param( 'id', $user->userid ); |
| 140 |
$session->param( 'ip', '127.0.0.1' ); |
| 141 |
$session->param( 'lasttime', time() ); |
| 142 |
$session->flush; |
| 143 |
|
| 144 |
return ( $user, $session ); |
| 145 |
} |