Bugzilla – Attachment 82625 Details for
Bug 17006
Add route to change patron's password (authenticated)
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 17006: Unit tests
Bug-17006-Unit-tests.patch (text/plain), 8.49 KB, created by
Tomás Cohen Arazi (tcohen)
on 2018-11-24 12:42:15 UTC
(
hide
)
Description:
Bug 17006: Unit tests
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2018-11-24 12:42:15 UTC
Size:
8.49 KB
patch
obsolete
>From f07d9322e2c3870364fd75b2e1b201b8369985b0 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Thu, 16 Aug 2018 07:00:38 -0300 >Subject: [PATCH] Bug 17006: Unit tests > >Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> > >Signed-off-by: Josef Moravec <josef.moravec@gmail.com> >--- > t/db_dependent/api/v1/patrons_password.t | 224 +++++++++++++++++++++++ > 1 file changed, 224 insertions(+) > create mode 100644 t/db_dependent/api/v1/patrons_password.t > >diff --git a/t/db_dependent/api/v1/patrons_password.t b/t/db_dependent/api/v1/patrons_password.t >new file mode 100644 >index 0000000000..0443354ed1 >--- /dev/null >+++ b/t/db_dependent/api/v1/patrons_password.t >@@ -0,0 +1,224 @@ >+#!/usr/bin/perl >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it under the >+# terms of the GNU General Public License as published by the Free Software >+# Foundation; either version 3 of the License, or (at your option) any later >+# version. >+# >+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY >+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR >+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License along >+# with Koha; if not, write to the Free Software Foundation, Inc., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+use Modern::Perl; >+ >+use Test::More tests => 2; >+ >+use Test::Mojo; >+use Test::Warn; >+ >+use t::lib::TestBuilder; >+use t::lib::Mocks; >+ >+use Koha::Patrons; >+ >+my $schema = Koha::Database->new->schema; >+my $builder = t::lib::TestBuilder->new; >+ >+# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling >+# this affects the other REST api tests >+t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); >+ >+my $remote_address = '127.0.0.1'; >+my $t = Test::Mojo->new('Koha::REST::V1'); >+ >+subtest 'set_password() (authorized user tests)' => sub { >+ >+ plan tests => 18; >+ >+ $schema->storage->txn_begin; >+ >+ my ( $patron, $session ) = create_user_and_session({ authorized => 1 }); >+ >+ t::lib::Mocks::mock_preference( 'OpacPasswordChange', 0 ); >+ t::lib::Mocks::mock_preference( 'minPasswordLength', 3 ); >+ t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); >+ >+ my $new_password = 'abc'; >+ >+ my $tx >+ = $t->ua->build_tx( POST => "/api/v1/patrons/" >+ . $patron->id >+ . "/password" => json => { password => $new_password } ); >+ >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx)->status_is(200)->json_is( '' ); >+ >+ t::lib::Mocks::mock_preference( 'minPasswordLength', 5 ); >+ $tx >+ = $t->ua->build_tx( POST => "/api/v1/patrons/" >+ . $patron->id >+ . "/password" => json => { password => $new_password } ); >+ >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx)->status_is(400)->json_is({ error => 'Password length (3) is shorter than required (5)' }); >+ >+ $new_password = 'abc '; >+ $tx >+ = $t->ua->build_tx( POST => "/api/v1/patrons/" >+ . $patron->id >+ . "/password" => json => { password => $new_password } ); >+ >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx)->status_is(400)->json_is({ error => 'Password contains trailing spaces, which is forbidden.' }); >+ >+ $new_password = 'abcdefg'; >+ $tx >+ = $t->ua->build_tx( POST => "/api/v1/patrons/" >+ . $patron->id >+ . "/password" => json => { password => $new_password } ); >+ >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx)->status_is(200)->json_is(''); >+ >+ t::lib::Mocks::mock_preference( 'RequireStrongPassword', 1); >+ $tx >+ = $t->ua->build_tx( POST => "/api/v1/patrons/" >+ . $patron->id >+ . "/password" => json => { password => $new_password } ); >+ >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx)->status_is(400)->json_is({ error => 'Password is too weak' }); >+ >+ $new_password = 'ABcde123%&'; >+ $tx >+ = $t->ua->build_tx( POST => "/api/v1/patrons/" >+ . $patron->id >+ . "/password" => json => { password => $new_password } ); >+ >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx)->status_is(200)->json_is(''); >+ >+ $schema->storage->txn_rollback; >+}; >+ >+subtest 'set_password() (unauthorized user tests)' => sub { >+ >+ plan tests => 15; >+ >+ $schema->storage->txn_begin; >+ >+ my ( $patron, $session ) = create_user_and_session({ authorized => 0 }); >+ my $other_patron = $builder->build_object({ class => 'Koha::Patrons' }); >+ >+ t::lib::Mocks::mock_preference( 'OpacPasswordChange', 0 ); >+ t::lib::Mocks::mock_preference( 'minPasswordLength', 3 ); >+ t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); >+ >+ my $new_password = 'abc'; >+ >+ my $tx >+ = $t->ua->build_tx( POST => "/api/v1/patrons/" >+ . $other_patron->id >+ . "/password" => json => { password => $new_password } ); >+ >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx) >+ ->status_is(403) >+ ->json_is({ >+ error => 'Authorization failure. Missing required permission(s).', >+ required_permissions => { borrowers => 1 } >+ }); >+ >+ my $password = 'holapassword'; >+ $patron->update_password( $patron->userid, $password ); >+ $tx >+ = $t->ua->build_tx( POST => "/api/v1/patrons/" >+ . $other_patron->id >+ . "/password" => json => { password => $new_password, old_password => $password } ); >+ >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx) >+ ->status_is(403) >+ ->json_is({ >+ error => 'Authorization failure. Missing required permission(s).', >+ required_permissions => { borrowers => 1 } >+ }); >+ >+ $tx >+ = $t->ua->build_tx( POST => "/api/v1/patrons/" >+ . $patron->id >+ . "/password" => json => { password => $new_password, old_password => 'badpassword' } ); >+ >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx) >+ ->status_is(400) >+ ->json_is({ >+ error => 'Configuration prevents password changes by unauthorized users' >+ }); >+ >+ t::lib::Mocks::mock_preference( 'OpacPasswordChange', 1 ); >+ >+ $tx >+ = $t->ua->build_tx( POST => "/api/v1/patrons/" >+ . $patron->id >+ . "/password" => json => { password => $new_password, old_password => 'badpassword' } ); >+ >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx) >+ ->status_is(400) >+ ->json_is({ >+ error => 'Invalid password' >+ }); >+ >+ $tx >+ = $t->ua->build_tx( POST => "/api/v1/patrons/" >+ . $patron->id >+ . "/password" => json => { password => $new_password, old_password => $password } ); >+ >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx) >+ ->status_is(200) >+ ->json_is(''); >+ >+ $schema->storage->txn_rollback; >+}; >+ >+sub create_user_and_session { >+ >+ my ( $args ) = @_; >+ my $flags = ( $args->{authorized} ) ? 16 : 0; >+ >+ my $user = $builder->build_object( >+ { >+ class => 'Koha::Patrons', >+ value => { flags => $flags } >+ } >+ ); >+ >+ # Create a session for the authorized user >+ my $session = C4::Auth::get_session(''); >+ $session->param( 'number', $user->borrowernumber ); >+ $session->param( 'id', $user->userid ); >+ $session->param( 'ip', '127.0.0.1' ); >+ $session->param( 'lasttime', time() ); >+ $session->flush; >+ >+ return ( $user, $session ); >+} >-- >2.19.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 17006
:
54575
|
54576
|
54629
|
54630
|
54716
|
54717
|
54718
|
56155
|
56156
|
56157
|
56158
|
56283
|
56284
|
56399
|
56400
|
56401
|
56409
|
57356
|
57357
|
57358
|
57359
|
57360
|
63329
|
63330
|
63331
|
63332
|
63333
|
67128
|
67129
|
67130
|
67131
|
67132
|
70260
|
70261
|
70262
|
70263
|
70264
|
77882
|
77883
|
77884
|
77923
|
77924
|
77925
|
78837
|
82625
|
82626
|
82627
|
82628
|
83579
|
83580
|
83581
|
83749
|
83750
|
83751
|
84285
|
84286
|
84287
|
84488