Bug 25898: Prohibit indirect object notation
[koha.git] / t / db_dependent / AuthUtils.t
blobfa5bc7603880ce9204657873bd390f9e17024e49
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use Test::More tests => 2;
21 use Test::Exception;
22 use Test::MockModule;
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25 use Koha::AuthUtils;
27 my $schema = Koha::Database->schema;
28 my $builder = t::lib::TestBuilder->new;
30 $schema->storage->txn_begin;
32 my $category1 = $builder->build_object(
34 class => 'Koha::Patron::Categories',
35 value => { min_password_length => 15, require_strong_password => 1 }
38 my $category2 = $builder->build_object(
40 class => 'Koha::Patron::Categories',
41 value => { min_password_length => 5, require_strong_password => undef }
44 my $category3 = $builder->build_object(
46 class => 'Koha::Patron::Categories',
47 value => { min_password_length => undef, require_strong_password => 1 }
50 my $category4 = $builder->build_object(
52 class => 'Koha::Patron::Categories',
53 value =>
54 { min_password_length => undef, require_strong_password => undef }
58 my $p_2l = '1A';
59 my $p_3l_weak = '123';
60 my $p_3l_strong = '1Ab';
61 my $p_5l_weak = 'abcde';
62 my $p_15l_weak = '0123456789abcdf';
63 my $p_5l_strong = 'Abc12';
64 my $p_15l_strong = '0123456789AbCdF';
66 subtest 'is_password_valid for category' => sub {
67 plan tests => 15;
69 my ( $is_valid, $error );
71 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
72 t::lib::Mocks::mock_preference( 'minPasswordLength', 3 );
74 #Category 1 - override=>1, length=>15, strong=>1
75 ( $is_valid, $error ) =
76 Koha::AuthUtils::is_password_valid( $p_5l_strong, $category1 );
77 is( $is_valid, 0, 'min password length for this category is 15' );
78 is( $error, 'too_short', 'min password length for this category is 15' );
80 ( $is_valid, $error ) =
81 Koha::AuthUtils::is_password_valid( $p_15l_weak, $category1 );
82 is( $is_valid, 0, 'password should be strong for this category' );
83 is( $error, 'too_weak', 'password should be strong for this category' );
85 ( $is_valid, $error ) =
86 Koha::AuthUtils::is_password_valid( $p_15l_strong, $category1 );
87 is( $is_valid, 1, 'password should be ok for this category' );
89 #Category 2 - override=>1, length=>5, strong=>0
90 ( $is_valid, $error ) =
91 Koha::AuthUtils::is_password_valid( $p_3l_strong, $category2 );
92 is( $is_valid, 0, 'min password length for this category is 5' );
93 is( $error, 'too_short', 'min password length for this category is 5' );
95 ( $is_valid, $error ) =
96 Koha::AuthUtils::is_password_valid( $p_5l_weak, $category2 );
97 is( $is_valid, 1, 'password should be ok for this category' );
99 #Category 3 - override=>0, length=>20, strong=>0
100 ( $is_valid, $error ) =
101 Koha::AuthUtils::is_password_valid( $p_3l_weak, $category3 );
102 is( $is_valid, 0, 'password should be strong' );
103 is( $error, 'too_weak', 'password should be strong' );
105 ( $is_valid, $error ) =
106 Koha::AuthUtils::is_password_valid( $p_3l_strong, $category3 );
107 is( $is_valid, 1, 'password should be ok' );
109 #Category 4 - default settings - override=>undef, length=>undef, strong=>undef
110 ( $is_valid, $error ) =
111 Koha::AuthUtils::is_password_valid( $p_3l_weak, $category4 );
112 is( $is_valid, 1, 'password should be ok' );
114 t::lib::Mocks::mock_preference( 'minPasswordLength', 0 );
115 ( $is_valid, $error ) =
116 Koha::AuthUtils::is_password_valid( $p_2l, $category4 );
117 is( $is_valid, 0, '3 is absolute minimum password' );
118 is( $error, 'too_short', '3 is absolute minimum password' );
120 throws_ok { Koha::AuthUtils::is_password_valid($p_2l); }
121 'Koha::Exceptions::Password::NoCategoryProvided',
122 'Category should always be provided';
126 subtest 'generate_password for category' => sub {
127 plan tests => 5;
129 my ( $is_valid, $error );
131 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
132 t::lib::Mocks::mock_preference( 'minPasswordLength', 3 );
134 #Category 4
135 my $password = Koha::AuthUtils::generate_password($category4);
136 ( $is_valid, $error ) =
137 Koha::AuthUtils::is_password_valid( $password, $category4 );
138 is( $is_valid, 1, 'password should be ok' );
140 #Category 3
141 $password = Koha::AuthUtils::generate_password($category3);
142 ( $is_valid, $error ) =
143 Koha::AuthUtils::is_password_valid( $password, $category3 );
144 is( $is_valid, 1, 'password should be ok' );
146 #Category 2
147 $password = Koha::AuthUtils::generate_password($category2);
148 ( $is_valid, $error ) =
149 Koha::AuthUtils::is_password_valid( $password, $category2 );
150 is( $is_valid, 1, 'password should be ok' );
152 #Category 1
153 $password = Koha::AuthUtils::generate_password($category1);
154 ( $is_valid, $error ) =
155 Koha::AuthUtils::is_password_valid( $password, $category1 );
156 is( $is_valid, 1, 'password should be ok' );
158 throws_ok { Koha::AuthUtils::generate_password(); }
159 'Koha::Exceptions::Password::NoCategoryProvided',
160 'Category should always be provided';
164 $schema->storage->txn_rollback;