fa5bc7603880ce9204657873bd390f9e17024e49
[koha.git] / t / db_dependent / AuthUtils.t
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
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
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;
26
27 my $schema  = Koha::Database->schema;
28 my $builder = t::lib::TestBuilder->new;
29
30 $schema->storage->txn_begin;
31
32 my $category1 = $builder->build_object(
33     {
34         class => 'Koha::Patron::Categories',
35         value => { min_password_length => 15, require_strong_password => 1 }
36     }
37 );
38 my $category2 = $builder->build_object(
39     {
40         class => 'Koha::Patron::Categories',
41         value => { min_password_length => 5, require_strong_password => undef }
42     }
43 );
44 my $category3 = $builder->build_object(
45     {
46         class => 'Koha::Patron::Categories',
47         value => { min_password_length => undef, require_strong_password => 1 }
48     }
49 );
50 my $category4 = $builder->build_object(
51     {
52         class => 'Koha::Patron::Categories',
53         value =>
54           { min_password_length => undef, require_strong_password => undef }
55     }
56 );
57
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';
65
66 subtest 'is_password_valid for category' => sub {
67     plan tests => 15;
68
69     my ( $is_valid, $error );
70
71     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
72     t::lib::Mocks::mock_preference( 'minPasswordLength',     3 );
73
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' );
79
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' );
84
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' );
88
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' );
94
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' );
98
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' );
104
105     ( $is_valid, $error ) =
106       Koha::AuthUtils::is_password_valid( $p_3l_strong, $category3 );
107     is( $is_valid, 1, 'password should be ok' );
108
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' );
113
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' );
119
120     throws_ok { Koha::AuthUtils::is_password_valid($p_2l); }
121     'Koha::Exceptions::Password::NoCategoryProvided',
122       'Category should always be provided';
123
124 };
125
126 subtest 'generate_password for category' => sub {
127     plan tests => 5;
128
129     my ( $is_valid, $error );
130
131     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
132     t::lib::Mocks::mock_preference( 'minPasswordLength',     3 );
133
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' );
139
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' );
145
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' );
151
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' );
157
158     throws_ok { Koha::AuthUtils::generate_password(); }
159     'Koha::Exceptions::Password::NoCategoryProvided',
160       'Category should always be provided';
161
162 };
163
164 $schema->storage->txn_rollback;