Bug 32739: Unit tests
[koha.git] / t / db_dependent / api / v1 / password_validation.t
1 #!/usr/bin/perl
2
3 #
4 # This file is part of Koha
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use Test::More tests => 4;
22 use Test::Mojo;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use Koha::Database;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 my $t = Test::Mojo->new('Koha::REST::V1');
33 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35 $schema->storage->txn_begin;
36
37 # create a privileged user
38 my $librarian = $builder->build_object(
39     {
40         class => 'Koha::Patrons',
41         value => { flags => 2 ** 4 } # borrowers flag = 4
42     }
43 );
44 my $password = 'thePassword123';
45 $librarian->set_password( { password => $password, skip_validation => 1 } );
46 my $userid = $librarian->userid;
47
48 subtest 'password validation - account lock out' => sub {
49
50     plan tests => 6;
51
52     $schema->storage->txn_begin;
53
54     t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 1 );
55
56     my $json = {
57         identifier => $userid,
58         password   => "bad",
59     };
60
61     $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => $json )
62       ->status_is(400)
63       ->json_is({ error => q{Validation failed} });
64
65     $json->{password} = $password;
66
67     $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => $json )
68       ->status_is(400)
69       ->json_is({ error => q{Validation failed} });
70
71     $schema->storage->txn_rollback;
72 };
73
74 subtest 'password validation - unauthorized user' => sub {
75
76     plan tests => 3;
77
78     $schema->storage->txn_begin;
79
80     my $patron = $builder->build_object(
81         {
82             class => 'Koha::Patrons',
83             value => { flags => 2 ** 2 } # catalogue flag = 2
84         }
85     );
86     my $password = 'thePassword123';
87     $patron->set_password( { password => $password, skip_validation => 1 } );
88     my $userid = $patron->userid;
89
90     my $json = {
91         identifier => $userid,
92         password   => "test",
93     };
94
95     $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => $json )
96       ->status_is(403)
97       ->json_is('/error' => 'Authorization failure. Missing required permission(s).');
98
99     $schema->storage->txn_rollback;
100 };
101
102 subtest 'password validation - unauthenticated user' => sub {
103     plan tests => 3;
104
105     $schema->storage->txn_begin;
106
107     my $json = {
108         identifier => "banana",
109         password   => "test",
110     };
111
112     $t->post_ok( "/api/v1/auth/password/validation" => json => $json )
113       ->json_is( '/error' => 'Authentication failure.' )
114       ->status_is(401);
115
116     $schema->storage->txn_rollback;
117 };
118
119 subtest 'Password validation - authorized requests tests' => sub {
120
121     plan tests => 24;
122
123     $schema->storage->txn_begin;
124
125     # generate a random unused userid
126     my $patron_to_delete = $builder->build_object( { class => 'Koha::Patrons' } );
127
128     my $deleted_userid     = $patron_to_delete->userid;
129     my $deleted_cardnumber = $patron_to_delete->cardnumber;
130
131     $patron_to_delete->delete;
132
133     my $json = {
134         identifier => $librarian->userid,
135         password   => $password,
136     };
137
138     $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => $json )
139       ->status_is(204, 'Validating using `cardnumber` works')
140       ->content_is(q{});
141
142     $json = {
143         identifier => $librarian->cardnumber,
144         password   => $password,
145     };
146
147     $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => $json )
148       ->status_is(204, 'Validating using `cardnumber` works')
149       ->content_is(q{});
150
151     $json = {
152         identifier => $deleted_cardnumber,
153         password   => $password,
154     };
155
156     $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => $json )
157       ->status_is(400, 'Validating using and invalid identifier fails')
158       ->json_is({ error => 'Validation failed' });
159
160     $json = {
161         identifier => $deleted_userid,
162         password   => $password,
163     };
164
165     $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => $json )
166       ->status_is(400, 'Validating using and invalid identifier fails')
167       ->json_is({ error => 'Validation failed' });
168
169     $json = {
170         password => $password,
171         userid   => $deleted_userid,
172     };
173
174     $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => $json )
175       ->status_is(400, 'Validating using and invalid userid fails')
176       ->json_is({ error => 'Validation failed' });
177
178     $json = {
179         password => $password,
180         userid   => $userid,
181     };
182
183     $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => $json )
184       ->status_is(204, 'Validating using the `userid` attribute works')
185       ->content_is(q{});
186
187     $json = {
188         password => $password,
189         userid   => $librarian->cardnumber,
190     };
191
192     $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => $json )
193         ->status_is( 400, 'Validating using a cardnumber as userid fails' )->json_is( { error => 'Validation failed' } );
194
195     $json = {
196         identifier => $userid,
197         password   => $password,
198         userid     => $userid,
199     };
200
201     $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => $json )
202       ->status_is(400, 'Passing both parameters forbidden')
203       ->json_is({ error => 'Bad request. Only one identifier attribute can be passed.' });
204
205     $schema->storage->txn_rollback;
206 };
207
208 $schema->storage->txn_rollback;