Bugzilla – Attachment 138164 Details for
Bug 30588
Add the option to require 2FA setup on first staff login
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 30588: Add tests for the REST API routes
Bug-30588-Add-tests-for-the-REST-API-routes.patch (text/plain), 5.77 KB, created by
Jonathan Druart
on 2022-07-27 08:05:35 UTC
(
hide
)
Description:
Bug 30588: Add tests for the REST API routes
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2022-07-27 08:05:35 UTC
Size:
5.77 KB
patch
obsolete
>From e1ae484a7bdba7cb23626f043d51e318ecbf3aff Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Tue, 26 Jul 2022 11:41:14 +0200 >Subject: [PATCH] Bug 30588: Add tests for the REST API routes > >Sponsored-by: Rijksmuseum, Netherlands >--- > t/db_dependent/api/v1/two_factor_auth.t | 114 +++++++++++++++++++++++- > 1 file changed, 113 insertions(+), 1 deletion(-) > >diff --git a/t/db_dependent/api/v1/two_factor_auth.t b/t/db_dependent/api/v1/two_factor_auth.t >index 8278e8d5e08..63075a6925a 100755 >--- a/t/db_dependent/api/v1/two_factor_auth.t >+++ b/t/db_dependent/api/v1/two_factor_auth.t >@@ -17,7 +17,7 @@ > > use Modern::Perl; > >-use Test::More tests => 1; >+use Test::More tests => 2; > use Test::Mojo; > use Test::MockModule; > >@@ -41,12 +41,123 @@ $mocked_koha_email->mock( 'send_or_die', sub { > return 1; > }); > >+subtest 'registration and verification' => sub { >+ >+ plan tests => 22; >+ >+ $schema->storage->txn_begin; >+ >+ t::lib::Mocks::mock_preference('TwoFactorAuthentication', 'enabled'); >+ >+ my $patron = $builder->build_object( >+ { >+ class => 'Koha::Patrons', >+ value => { >+ flags => 20, # Staff access and Patron info >+ } >+ } >+ ); >+ >+ # Not authenticated yet - 401 >+ my $session = C4::Auth::get_session(''); >+ $session->param( 'ip', '127.0.0.1' ); >+ $session->param( 'lasttime', time() ); >+ $session->flush; >+ >+ my $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration" ); >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(401); >+ >+ $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration/verification" ); >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(401); >+ >+ # Authenticated - can register >+ $session->param( 'number', $patron->borrowernumber ); >+ $session->param( 'id', $patron->userid ); >+ $session->flush; >+ >+ $patron->auth_method('password'); >+ $patron->secret(undef); >+ $patron->store; >+ >+ $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration" ); >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(201); >+ my $secret32 = $t->tx->res->json->{secret32}; >+ >+ $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration/verification" ); >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(400); # Missing parameter >+ >+ my $auth = Koha::Auth::TwoFactorAuth->new({patron => $patron, secret32 => $secret32}); >+ my $pin_code = $auth->code; >+ $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration/verification" => form => {secret32 => $secret32, pin_code => $pin_code} ); >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(204); >+ >+ $patron = $patron->get_from_storage; >+ is($patron->auth_method, 'two-factor'); >+ isnt($patron->secret, undef); >+ >+ $patron->auth_method('password'); >+ $patron->secret(undef); >+ $patron->store; >+ >+ # Setting up 2FA - can register >+ $session->param('waiting-for-2FA-setup', 1); >+ $session->flush; >+ $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration" ); >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(201); >+ $secret32 = $t->tx->res->json->{secret32}; >+ >+ $auth = Koha::Auth::TwoFactorAuth->new({patron => $patron, secret32 => $secret32}); >+ $pin_code = $auth->code; >+ $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration/verification" => form => {secret32 => $secret32, pin_code => $pin_code} ); >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(204); >+ >+ $patron = $patron->get_from_storage; >+ is($patron->auth_method, 'two-factor'); >+ isnt($patron->secret, undef); >+ >+ # 2FA already enabled - cannot register again >+ $patron->auth_method('two-factor'); >+ $patron->encode_secret("nv4v65dpobpxgzldojsxiii"); >+ $patron->store; >+ >+ $session->param('waiting-for-2FA-setup', undef); >+ $session->flush; >+ >+ $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration" ); >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(401); >+ >+ $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration/verification" ); >+ $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(401); >+ >+ $schema->storage->txn_rollback; >+}; >+ > subtest 'send_otp_token' => sub { > > plan tests => 11; > > $schema->storage->txn_begin; > >+ t::lib::Mocks::mock_preference('TwoFactorAuthentication', 'enabled'); >+ > my $patron = $builder->build_object( > { > class => 'Koha::Patrons', >@@ -65,6 +176,7 @@ subtest 'send_otp_token' => sub { > $tx->req->cookies( { name => 'CGISESSID', value => $session->id } ); > $tx->req->env( { REMOTE_ADDR => $remote_address } ); > >+ # FIXME This is not correct They are authenticated! > # Patron is not authenticated yet > $t->request_ok($tx)->status_is(401); > >-- >2.25.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 30588
:
138160
|
138161
|
138162
|
138163
|
138164
|
138165
|
138166
|
138191
|
138193
|
138953
|
138954
|
138955
|
138956
|
138957
|
138958
|
138959
|
138960
|
138961
|
138962
|
140365
|
140366
|
140367
|
140368
|
140369
|
140416
|
140417
|
140418
|
140419
|
140420
|
140421
|
140422
|
140423
|
140424
|
140425
|
140426
|
140427
|
140428
|
140429
|
140430
|
141978
|
141979
|
141980
|
141981
|
141982
|
141983
|
141984
|
141985
|
141986
|
141987
|
141988
|
141989
|
141990
|
141991
|
141992
|
142344
|
142345
|
142346
|
142347
|
142348
|
142349
|
142350
|
142351
|
142352
|
142353
|
142354
|
142355
|
142356
|
142357
|
142358