Line 0
Link Here
|
|
|
1 |
#!/usr/bin/env 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 => 1; |
21 |
use Test::Mojo; |
22 |
use Test::MockModule; |
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 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
33 |
# this affects the other REST api tests |
34 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
35 |
|
36 |
my $remote_address = '127.0.0.1'; |
37 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
38 |
|
39 |
subtest 'send_otp_token' => sub { |
40 |
|
41 |
plan tests => 7; |
42 |
|
43 |
$schema->storage->txn_begin; |
44 |
|
45 |
my $patron = $builder->build_object( |
46 |
{ |
47 |
class => 'Koha::Patrons', |
48 |
value => { |
49 |
flags => 16 |
50 |
} |
51 |
} |
52 |
); |
53 |
|
54 |
my $session = C4::Auth::get_session(''); |
55 |
$session->param( 'number', $patron->borrowernumber ); |
56 |
$session->param( 'id', $patron->userid ); |
57 |
$session->param( 'ip', '127.0.0.1' ); |
58 |
$session->param( 'lasttime', time() ); |
59 |
$session->flush; |
60 |
|
61 |
my $tx = $t->ua->build_tx( POST => "/api/v1/auth/send_otp_token" ); |
62 |
$tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); |
63 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
64 |
|
65 |
# Patron is not authenticated yet |
66 |
$t->request_ok($tx)->status_is(403); |
67 |
|
68 |
$session->param('waiting-for-2FA', 1); |
69 |
$session->flush; |
70 |
|
71 |
$session = C4::Auth::get_session($session->id); |
72 |
|
73 |
my $auth = Test::MockModule->new("C4::Auth"); |
74 |
$auth->mock('check_cookie_auth', sub { return 'additional-auth-needed'}); |
75 |
|
76 |
$patron->library->set( |
77 |
{ |
78 |
branchemail => 'from@example.org', |
79 |
branchreturnpath => undef, |
80 |
branchreplyto => undef, |
81 |
} |
82 |
)->store; |
83 |
$patron->auth_method('two-factor'); |
84 |
$patron->encode_secret("nv4v65dpobpxgzldojsxiii"); |
85 |
$patron->email(undef); |
86 |
$patron->store; |
87 |
|
88 |
$tx = $t->ua->build_tx( POST => "/api/v1/auth/send_otp_token" ); |
89 |
$tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); |
90 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
91 |
|
92 |
# Invalid email |
93 |
$t->request_ok($tx)->status_is(400)->json_is({ error => 'email_not_sent' }); |
94 |
|
95 |
$patron->email('to@example.org')->store; |
96 |
$tx = $t->ua->build_tx( POST => "/api/v1/auth/send_otp_token" ); |
97 |
$tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); |
98 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
99 |
|
100 |
# Everything is ok, the email will be sent |
101 |
$t->request_ok($tx)->status_is(200); |
102 |
|
103 |
$schema->storage->txn_rollback; |
104 |
}; |
105 |
|
106 |
1; |