Bug 32030: ERM - Add more API tests
[koha-ffzg.git] / 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 => 18;
121
122     $schema->storage->txn_begin;
123
124     my $category = $builder->build_object(
125         {
126             class => 'Koha::Patron::Categories',
127             value => { change_password => 0 } # disallow changing password for the patron category
128         }
129     );
130     my $patron = $builder->build_object(
131         {
132             class => 'Koha::Patrons',
133             value => { categorycode => $category->id }
134         }
135     );
136
137     my $password = 'thePassword123';
138     $patron->set_password( { password => $password, skip_validation => 1 } );
139     my $userid       = $patron->userid;
140     my $other_patron = $builder->build_object( { class => 'Koha::Patrons' } );
141
142     # Enable the public API
143     t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 );
144
145     t::lib::Mocks::mock_preference( 'OpacPasswordChange',    0 );
146     t::lib::Mocks::mock_preference( 'minPasswordLength',     3 );
147     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
148
149     my $new_password = 'abc';
150
151     $t->post_ok(
152             "//$userid:$password@/api/v1/public/patrons/"
153           . $patron->id
154           . "/password" => json => {
155             password          => $new_password,
156             password_repeated => $new_password,
157             old_password      => 'blah'
158           }
159     )->status_is(403)->json_is(
160         {
161             error => 'Changing password is forbidden'
162         }
163     );
164
165     t::lib::Mocks::mock_preference( 'OpacPasswordChange', 1 );
166
167     $t->post_ok(
168             "//$userid:$password@/api/v1/public/patrons/"
169           . $other_patron->id
170           . "/password" => json => {
171             password          => $new_password,
172             password_repeated => $new_password,
173             old_password      => $password
174           }
175     )->status_is(403)
176       ->json_is( '/error',
177         "Authorization failure. Missing required permission(s)." );
178
179     $t->post_ok(
180             "//$userid:$password@/api/v1/public/patrons/"
181           . $patron->id
182           . "/password" => json => {
183             password          => $new_password,
184             password_repeated => $new_password,
185             old_password      => $password
186           }
187     )->status_is(403)->json_is({ error => 'Changing password is forbidden' });
188
189     # Allow password changing to the patron category
190     $category->change_password(1)->store;
191
192     $t->post_ok(
193             "//$userid:$password@/api/v1/public/patrons/"
194           . $patron->id
195           . "/password" => json => {
196             password          => $new_password,
197             password_repeated => 'wrong_password',
198             old_password      => $password
199           }
200     )->status_is(400)->json_is(
201         {
202             error => "Passwords don't match"
203         }
204     );
205
206     $t->post_ok(
207             "//$userid:$password@/api/v1/public/patrons/"
208           . $patron->id
209           . "/password" => json => {
210             password          => $new_password,
211             password_repeated => $new_password,
212             old_password      => 'badpassword'
213           }
214     )->status_is(400)->json_is(
215         {
216             error => "Invalid password"
217         }
218     );
219
220     $t->post_ok(
221             "//$userid:$password@/api/v1/public/patrons/"
222           . $patron->id
223           . "/password" => json => {
224             password          => $new_password,
225             password_repeated => $new_password,
226             old_password      => $password
227           }
228     )->status_is(200)->json_is('');
229
230     $schema->storage->txn_rollback;
231 };