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>.
19 use Test::More tests => 5;
23 use Koha::Auth::TwoFactorAuth;
26 use t::lib::TestBuilder;
29 my $pref_value = C4::Context->preference('TwoFactorAuthentication');
32 eval { require Selenium::Remote::Driver; };
33 skip "Selenium::Remote::Driver is needed for selenium tests.", 2 if $@;
35 my $builder = t::lib::TestBuilder->new;
37 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 1 }});
38 $patron->flags(1)->store; # superlibrarian permission
39 my $password = Koha::AuthUtils::generate_password($patron->category);
40 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
41 $patron->set_password({ password => $password });
43 push @data_to_cleanup, $patron, $patron->category, $patron->library;
45 my $s = t::lib::Selenium->new({ login => $patron->userid, password => $password });
46 my $driver = $s->driver;
48 subtest 'Setup' => sub {
51 my $mainpage = $s->base_url . q|mainpage.pl|;
52 $driver->get($mainpage);
53 like( $driver->get_title, qr(Log in to Koha), 'Hitting the main page should redirect to the login form');
55 C4::Context->set_preference('TwoFactorAuthentication', 'disabled');
58 like( $driver->get_title, qr(Koha staff interface), 'Patron with flags superlibrarian should be able to login' );
60 $driver->get($s->base_url . q|members/two_factor_auth.pl|);
61 like( $driver->get_title, qr(Error 404), 'Must be redirected to 404 is the pref is off' );
63 C4::Context->set_preference('TwoFactorAuthentication', 'enabled');
64 $driver->get($s->base_url . q|members/two_factor_auth.pl|);
65 like( $driver->get_title, qr(Two-factor authentication), 'Must be on the page with the pref on' );
68 $driver->find_element( '//div[@id="registration-status-disabled"]/div[@class="two-factor-status"]' )->get_text,
74 $driver->find_element( '//div[@id="registration-status-enabled"]/div[@class="two-factor-status"]' )->get_text,
75 '', # 'Status: Enabled' is not shown
79 $driver->find_element('//*[@id="enable-2FA"]')->click;
81 ok($driver->find_element('//img[@id="qr_code"]'), 'There is a QR code');
83 $driver->find_element('//*[@id="pin_code"]')->send_keys('wrong_code');
84 $driver->find_element('//*[@id="register-2FA"]')->click;
86 ok($driver->find_element('//div[@class="dialog error"][contains(text(), "Invalid PIN code")]'));
87 is( $patron->get_from_storage->secret, undef, 'secret is not set in DB yet' );
89 my $secret32 = $driver->find_element('//*[@id="secret32"]')->get_value();
90 my $auth = Koha::Auth::TwoFactorAuth->new({patron => $patron, secret32 => $secret32});
91 my $code = $auth->code();
92 $driver->find_element('//*[@id="pin_code"]')->clear;
93 $driver->find_element('//*[@id="pin_code"]')->send_keys($code);
94 $driver->find_element('//*[@id="register-2FA"]')->click;
95 # Wait for the response then go to the page, don't wait for the redirect
97 $driver->get($s->base_url . q|members/two_factor_auth.pl|);
99 $driver->find_element( '//div[@id="registration-status-disabled"]/div[@class="two-factor-status"]' )->get_text,
100 '', # 'Status: Disabled' is not shown
105 $driver->find_element( '//div[@id="registration-status-enabled"]/div[@class="two-factor-status"]' )->get_text,
110 $patron = $patron->get_from_storage;
111 is( $patron->decoded_secret, $secret32, 'encrypted secret is set in DB' );
115 subtest 'Login' => sub {
118 my $mainpage = $s->base_url . q|mainpage.pl|;
120 my $secret32 = $patron->decoded_secret;
122 $driver->get($mainpage . q|?logout.x=1|);
123 $driver->get($s->base_url . q|circ/circulation.pl?borrowernumber=|.$patron->borrowernumber);
124 like( $driver->get_title, qr(Log in to Koha), 'Must be on the first auth screen' );
126 like( $driver->get_title, qr(Two-factor authentication), 'Must be on the second auth screen' );
127 is( login_error($s), undef );
129 my $auth = Koha::Auth::TwoFactorAuth->new(
130 { patron => $patron, secret32 => $secret32 } );
131 my $code = $auth->code();
133 $driver->find_element('//form[@id="loginform"]//input[@id="otp_token"]')->send_keys($code);
134 $driver->find_element('//input[@type="submit"]')->click;
135 like( $driver->get_title, qr(Checking out to ), 'Must be redirected to the original page' );
138 { # second try and logout
139 $driver->get($mainpage . q|?logout.x=1|);
140 $driver->get($s->base_url . q|circ/circulation.pl?borrowernumber=|.$patron->borrowernumber);
141 like( $driver->get_title, qr(Log in to Koha), 'Must be on the first auth screen' );
143 like( $driver->get_title, qr(Two-factor authentication), 'Must be on the second auth screen' );
144 is( login_error($s), undef );
145 $driver->find_element('//form[@id="loginform"]//input[@id="otp_token"]')->send_keys('wrong_code');
146 $driver->find_element('//input[@type="submit"]')->click;
147 is( login_error($s), "Invalid two-factor code" );
149 $driver->get($mainpage);
150 like( $driver->get_title, qr(Two-factor authentication), 'Must still be on the second auth screen' );
151 is( login_error($s), undef );
152 $driver->find_element('//a[@id="logout"]')->click();
153 like( $driver->get_title, qr(Log in to Koha), 'Must be on the first auth screen' );
154 is( login_error($s), undef );
157 { # second try and success
159 $driver->get($mainpage . q|?logout.x=1|);
160 $driver->get($s->base_url . q|circ/circulation.pl?borrowernumber=|.$patron->borrowernumber);
161 like( $driver->get_title, qr(Log in to Koha), 'Must be on the first auth screen' );
162 like( login_error($s), qr(Session timed out) );
164 like( $driver->get_title, qr(Two-factor authentication), 'Must be on the second auth screen' );
165 is( login_error($s), undef );
166 $driver->find_element('//form[@id="loginform"]//input[@id="otp_token"]')->send_keys('wrong_code');
167 $driver->find_element('//input[@type="submit"]')->click;
168 is( login_error($s), "Invalid two-factor code" );
170 my $auth = Koha::Auth::TwoFactorAuth->new(
171 { patron => $patron, secret32 => $secret32 } );
172 my $code = $auth->code();
174 $driver->find_element('//form[@id="loginform"]//input[@id="otp_token"]')->send_keys($code);
175 $driver->find_element('//input[@type="submit"]')->click;
176 like( $driver->get_title, qr(Checking out to ), 'Must be redirected to the original page' );
180 subtest "Send OTP code" => sub {
183 # Make sure the send won't fail because of invalid email addresses
184 $patron->library->set(
186 branchemail => 'from@example.org',
187 branchreturnpath => undef,
188 branchreplyto => undef,
191 $patron->auth_method('two-factor');
192 $patron->email(undef);
195 my $mainpage = $s->base_url . q|mainpage.pl|;
196 $driver->get( $mainpage . q|?logout.x=1| );
200 'Must be on the first auth screen'
205 qr(Two-factor authentication),
206 'Must be on the second auth screen'
208 $driver->find_element('//a[@id="send_otp"]')->click;
210 my $error = $driver->find_element('//div[@id="email_error"]')->get_text;
214 'Email not sent will display an error'
217 # This test will only pass if an SMTP server is defined
218 # It cannot be mocked from selenium tests
219 #$patron->email('test@example.org');
221 #$driver->find_element('//a[@id="send_otp"]')->click;
224 # $driver->find_element('//div[@id="email_success"]')->get_text;
227 # "The code has been sent by email, please check your inbox.",
228 # 'The email must have been sent correctly'
232 subtest "Enforce 2FA setup on first login" => sub {
235 C4::Context->set_preference( 'TwoFactorAuthentication', 'enforced' );
237 # Make sure the send won't fail because of invalid email addresses
238 $patron->library->set(
240 branchemail => 'from@example.org',
241 branchreturnpath => undef,
242 branchreplyto => undef,
245 $patron->auth_method('password');
246 $patron->email(undef);
249 my $mainpage = $s->base_url . q|mainpage.pl|;
250 $driver->get( $mainpage . q|?logout.x=1| );
254 'Must be on the first auth screen'
259 qr(Two-factor authentication setup),
260 'Must be on the 2FA auth setup screen'
263 $s->wait_for_ajax; # There is an ajax request to populate the qr_code and the secret
265 isnt( $driver->find_element('//*[@id="qr_code"]')->get_attribute("src"), "" );
266 my $secret32 = $driver->find_element('//*[@id="secret32"]')->get_value;
268 my $auth = Koha::Auth::TwoFactorAuth->new(
269 { patron => $patron, secret32 => $secret32 } );
270 my $pin_code = $auth->code;
272 $driver->find_element('//*[@id="pin_code"]')->send_keys("wrong code");
273 $driver->find_element('//*[@id="register-2FA"]')->click;
275 is( $driver->find_element('//*[@id="errors"]')->get_text,
276 "Invalid PIN code" );
278 $driver->find_element('//*[@id="pin_code"]')->clear;
279 $driver->find_element('//*[@id="pin_code"]')->send_keys($pin_code);
280 $driver->find_element('//*[@id="register-2FA"]')->click;
281 is( $s->get_next_alert_text,
282 "Two-factor authentication correctly configured. You will be redirected to the login screen."
284 $driver->accept_alert;
285 # FIXME How to test the redirect to the mainpage here
287 $patron = $patron->get_from_storage;
288 is( $patron->auth_method, 'two-factor', );
289 isnt( $patron->secret, undef, );
292 subtest "Disable" => sub {
295 my $mainpage = $s->base_url . q|mainpage.pl|;
296 $driver->get( $mainpage . q|?logout.x=1| );
298 my $auth = Koha::Auth::TwoFactorAuth->new( { patron => $patron } );
299 my $code = $auth->code();
301 $driver->find_element('//form[@id="loginform"]//input[@id="otp_token"]')
303 $driver->find_element('//input[@type="submit"]')->click;
305 $driver->get( $s->base_url . q|members/two_factor_auth.pl| );
308 $driver->find_element( '//div[@id="registration-status-disabled"]/div[@class="two-factor-status"]' )->get_text,
309 '', # 'Status: Disabled' is not shown
314 $driver->find_element( '//div[@id="registration-status-enabled"]/div[@class="two-factor-status"]' )->get_text,
319 $driver->find_element('//form[@id="two-factor-auth"]//input[@type="submit"]')->click;
322 $driver->find_element( '//div[@id="registration-status-disabled"]/div[@class="two-factor-status"]' )->get_text,
328 $driver->find_element( '//div[@id="registration-status-enabled"]/div[@class="two-factor-status"]' )->get_text,
329 '', # 'Status: Enabled' is not shown
333 $patron = $patron->get_from_storage;
334 is( $patron->secret, undef, "Secret has been cleared" );
335 is( $patron->auth_method(), 'password', 'auth_method has been reset to "password"' );
342 $_->delete for @data_to_cleanup;
343 C4::Context->set_preference('TwoFactorAuthentication', $pref_value);
349 my $driver = $s->driver;
351 $s->remove_error_handler;
352 my $login_error = eval {
353 my $elt = $driver->find_element('//div[@id="login_error"]');
354 return $elt->get_text if $elt && $elt->id;
356 $s->add_error_handler;
360 # Don't use the usual t::lib::Selenium->auth as we don't want the ->get($mainpage) to test the redirect
361 sub fill_login_form {
363 $s->fill_form({ userid => $s->login, password => $s->password });
364 $s->driver->find_element('//input[@id="submit-button"]')->click;