From 62f07b301283882251155a89f11df76c22474a6a Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 18 Aug 2022 06:57:42 +0000 Subject: [PATCH] Bug 31380: Add unit tests --- t/db_dependent/Koha/Auth.t | 92 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 t/db_dependent/Koha/Auth.t diff --git a/t/db_dependent/Koha/Auth.t b/t/db_dependent/Koha/Auth.t new file mode 100755 index 0000000000..0df31ddc1a --- /dev/null +++ b/t/db_dependent/Koha/Auth.t @@ -0,0 +1,92 @@ +#!/usr/bin/perl + +use Modern::Perl; +use C4::Auth qw( get_session ); +use Test::More tests => 5; +use t::lib::TestBuilder; + +use Koha::Database; + +use_ok('Koha::Auth'); + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +subtest 'Successful authentication' => sub { + plan tests => 2; + + $ENV{REMOTE_ADDR} = '127.0.0.1'; + my $builder = t::lib::TestBuilder->new; + my $borrower = $builder->build({ source => 'Borrower' }); + my $session = C4::Auth::get_session; + $session->param( 'id', $borrower->{userid} ); + $session->param( 'lasttime', time() ); + $session->param( 'ip', '127.0.0.1' ); + $session->flush; + my $sessionID = $session->id; + + my ($user,$user_session) = Koha::Auth->authenticate({ + sessionID => $sessionID, + }); + is(ref $user, 'Koha::Patron', 'User found'); + is(ref $user_session, 'CGI::Session', 'User session found'); + + $session->delete; +}; + +subtest 'Failed authentication' => sub { + plan tests => 2; + + $ENV{REMOTE_ADDR} = '127.0.0.1'; + my $session = C4::Auth::get_session; + $session->flush; + $session->param( 'ip', '127.0.0.1' ); + my $sessionID = $session->id; + + my ($user,$user_session) = Koha::Auth->authenticate({ + sessionID => $sessionID, + }); + is($user, undef, 'User not found'); + is($user_session, undef, 'User session not found'); + + $session->delete; +}; + +subtest 'Superlibrarian authorization' => sub { + plan tests => 1; + my $builder = t::lib::TestBuilder->new; + my $borrower = $builder->build({ source => 'Borrower', value => {flags => 1,} }); + + my $session = C4::Auth::get_session; + $session->param( 'id', $borrower->{userid} ); + $session->param( 'lasttime', time() ); + $session->param( 'ip', '127.0.0.1' ); + $session->flush; + + my $flags = Koha::Auth->authorize({ + session => $session, + flagsrequired => { circulate => 1 }, + }); + is($flags->{superlibrarian},1,'Got superlibrarian authorization'); +}; + +subtest 'Circulation staff authorization' => sub { + plan tests => 2; + my $builder = t::lib::TestBuilder->new; + my $borrower = $builder->build({ source => 'Borrower', value => {flags => 2,} }); + + my $session = C4::Auth::get_session; + $session->param( 'id', $borrower->{userid} ); + $session->param( 'lasttime', time() ); + $session->param( 'ip', '127.0.0.1' ); + $session->flush; + + my $flags = Koha::Auth->authorize({ + session => $session, + flagsrequired => { circulate => 1 }, + }); + is($flags->{superlibrarian},0,'Did not get superlibrarian authorization'); + is($flags->{circulate},1,'Did get circulate authorization'); +}; + +$schema->storage->txn_rollback; -- 2.30.2