Bug 27587: Use Basic auth on API tests - patrons_password.t
[srvgit] / t / db_dependent / api / v1 / patrons_password.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
22 use Test::Mojo;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use Koha::Patrons;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
33
34 my $t = Test::Mojo->new('Koha::REST::V1');
35
36 subtest 'set() (authorized user tests)' => sub {
37
38     plan tests => 21;
39
40     $schema->storage->txn_begin;
41
42     my $privileged_patron = $builder->build_object(
43         {
44             class => 'Koha::Patrons',
45             value => { flags => 1 }
46         }
47     );
48     my $password = 'thePassword123';
49     $privileged_patron->set_password(
50         { password => $password, skip_validation => 1 } );
51     my $userid = $privileged_patron->userid;
52
53     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
54
55     t::lib::Mocks::mock_preference( 'minPasswordLength',     3 );
56     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
57
58     my $new_password = 'abc';
59
60     $t->post_ok( "//$userid:$password@/api/v1/patrons/"
61           . $patron->id
62           . "/password" => json =>
63           { password => $new_password, password_2 => $new_password } )
64       ->status_is(200)->json_is('');
65
66     $t->post_ok( "//$userid:$password@/api/v1/patrons/"
67           . $patron->id
68           . "/password" => json =>
69           { password => $new_password, password_2 => 'cde' } )->status_is(400)
70       ->json_is( { error => 'Passwords don\'t match' } );
71
72     t::lib::Mocks::mock_preference( 'minPasswordLength', 5 );
73
74     $t->post_ok( "//$userid:$password@/api/v1/patrons/"
75           . $patron->id
76           . "/password" => json =>
77           { password => $new_password, password_2 => $new_password } )
78       ->status_is(400)
79       ->json_is(
80         { error => 'Password length (3) is shorter than required (5)' } );
81
82     $new_password = 'abc   ';
83     $t->post_ok( "//$userid:$password@/api/v1/patrons/"
84           . $patron->id
85           . "/password" => json =>
86           { password => $new_password, password_2 => $new_password } )
87       ->status_is(400)->json_is(
88         {
89             error =>
90               '[Password contains leading/trailing whitespace character(s)]'
91         }
92       );
93
94     $new_password = 'abcdefg';
95     $t->post_ok( "//$userid:$password@/api/v1/patrons/"
96           . $patron->id
97           . "/password" => json =>
98           { password => $new_password, password_2 => $new_password } )
99       ->status_is(200)->json_is('');
100
101     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 1 );
102     $t->post_ok( "//$userid:$password@/api/v1/patrons/"
103           . $patron->id
104           . "/password" => json =>
105           { password => $new_password, password_2 => $new_password } )
106       ->status_is(400)->json_is( { error => '[Password is too weak]' } );
107
108     $new_password = 'ABcde123%&';
109     $t->post_ok( "//$userid:$password@/api/v1/patrons/"
110           . $patron->id
111           . "/password" => json =>
112           { password => $new_password, password_2 => $new_password } )
113       ->status_is(200)->json_is('');
114
115     $schema->storage->txn_rollback;
116 };
117
118 subtest 'set_public() (unprivileged user tests)' => sub {
119
120     plan tests => 15;
121
122     $schema->storage->txn_begin;
123
124     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
125     my $password = 'thePassword123';
126     $patron->set_password( { password => $password, skip_validation => 1 } );
127     my $userid       = $patron->userid;
128     my $other_patron = $builder->build_object( { class => 'Koha::Patrons' } );
129
130     # Enable the public API
131     t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 );
132
133     t::lib::Mocks::mock_preference( 'OpacPasswordChange',    0 );
134     t::lib::Mocks::mock_preference( 'minPasswordLength',     3 );
135     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
136
137     my $new_password = 'abc';
138
139     $t->post_ok(
140             "//$userid:$password@/api/v1/public/patrons/"
141           . $patron->id
142           . "/password" => json => {
143             password          => $new_password,
144             password_repeated => $new_password,
145             old_password      => 'blah'
146           }
147     )->status_is(403)->json_is(
148         {
149             error =>
150               'Configuration prevents password changes by unprivileged users'
151         }
152     );
153
154     t::lib::Mocks::mock_preference( 'OpacPasswordChange', 1 );
155
156     $t->post_ok(
157             "//$userid:$password@/api/v1/public/patrons/"
158           . $other_patron->id
159           . "/password" => json => {
160             password          => $new_password,
161             password_repeated => $new_password,
162             old_password      => $password
163           }
164     )->status_is(403)
165       ->json_is( '/error',
166         "Authorization failure. Missing required permission(s)." );
167
168     $t->post_ok(
169             "//$userid:$password@/api/v1/public/patrons/"
170           . $patron->id
171           . "/password" => json => {
172             password          => $new_password,
173             password_repeated => 'wrong_password',
174             old_password      => $password
175           }
176     )->status_is(400)->json_is(
177         {
178             error => "Passwords don't match"
179         }
180     );
181
182     $t->post_ok(
183             "//$userid:$password@/api/v1/public/patrons/"
184           . $patron->id
185           . "/password" => json => {
186             password          => $new_password,
187             password_repeated => $new_password,
188             old_password      => 'badpassword'
189           }
190     )->status_is(400)->json_is(
191         {
192             error => "Invalid password"
193         }
194     );
195
196     $t->post_ok(
197             "//$userid:$password@/api/v1/public/patrons/"
198           . $patron->id
199           . "/password" => json => {
200             password          => $new_password,
201             password_repeated => $new_password,
202             old_password      => $password
203           }
204     )->status_is(200)->json_is('');
205
206     $schema->storage->txn_rollback;
207 };