From fa768d5ed4a1b273d049679017c2ef0c4c7bdc20 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 27 Mar 2024 12:00:48 +0000 Subject: [PATCH] Bug 36420: Unit tests Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer (cherry picked from commit 5c3ad38157f483aeb4ff26048a838b3ba6ad34fc) Signed-off-by: Fridolin Somers --- t/db_dependent/api/v1/auth_basic.t | 84 +++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 25 deletions(-) diff --git a/t/db_dependent/api/v1/auth_basic.t b/t/db_dependent/api/v1/auth_basic.t index 78c7572041..808dc31bb9 100755 --- a/t/db_dependent/api/v1/auth_basic.t +++ b/t/db_dependent/api/v1/auth_basic.t @@ -32,7 +32,7 @@ my $t = Test::Mojo->new('Koha::REST::V1'); subtest 'success tests' => sub { - plan tests => 10; + plan tests => 2; $schema->storage->txn_begin; @@ -40,39 +40,73 @@ subtest 'success tests' => sub { my $password = 'AbcdEFG123'; - my $patron = $builder->build_object( - { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); - $patron->set_password({ password => $password }); - my $userid = $patron->userid; + my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { userid => 'tomasito' } } ); + $patron->set_password( { password => $password } ); + + my $userid = $patron->userid; + my $cardnumber = $patron->cardnumber; my $stash; my $interface; my $userenv; - $t->app->hook(after_dispatch => sub { - $stash = shift->stash; - $interface = C4::Context->interface; - $userenv = C4::Context->userenv; - }); + $t->app->hook( + after_dispatch => sub { + $stash = shift->stash; + $interface = C4::Context->interface; + $userenv = C4::Context->userenv; + } + ); - $t->get_ok("//$userid:$password@/api/v1/patrons") - ->status_is( 200, 'Successful authentication and permissions check' ); + subtest '`userid` login' => sub { - my $user = $stash->{'koha.user'}; - ok( defined $user, 'The \'koha.user\' object is defined in the stash') and - is( ref($user), 'Koha::Patron', 'Stashed koha.user object type is Koha::Patron') and - is( $user->borrowernumber, $patron->borrowernumber, 'The stashed user is the right one' ); - is( $userenv->{number}, $patron->borrowernumber, 'userenv set correctly' ); - is( $interface, 'api', "Interface correctly set to \'api\'" ); + plan tests => 10; - $patron->flags(undef)->store; + $patron->flags( 2**4 )->store; - $t->get_ok("//$userid:$password@/api/v1/patrons") - ->status_is( 403, 'Successful authentication and not enough permissions' ) - ->json_is( - '/error' => 'Authorization failure. Missing required permission(s).', - 'Error message returned' - ); + $t->get_ok("//$userid:$password@/api/v1/patrons") + ->status_is( 200, 'Successful authentication and permissions check' ); + + my $user = $stash->{'koha.user'}; + ok( defined $user, 'The \'koha.user\' object is defined in the stash' ) + and is( ref($user), 'Koha::Patron', 'Stashed koha.user object type is Koha::Patron' ) + and is( $user->borrowernumber, $patron->borrowernumber, 'The stashed user is the right one' ); + is( $userenv->{number}, $patron->borrowernumber, 'userenv set correctly' ); + is( $interface, 'api', "Interface correctly set to \'api\'" ); + + $patron->flags(undef)->store; + + $t->get_ok("//$userid:$password@/api/v1/patrons") + ->status_is( 403, 'Successful authentication and not enough permissions' )->json_is( + '/error' => 'Authorization failure. Missing required permission(s).', + 'Error message returned' + ); + }; + + subtest '`cardnumber` login' => sub { + + plan tests => 10; + + $patron->flags( 2**4 )->store; + + $t->get_ok("//$cardnumber:$password@/api/v1/patrons") + ->status_is( 200, 'Successful authentication and permissions check' ); + + my $user = $stash->{'koha.user'}; + ok( defined $user, 'The \'koha.user\' object is defined in the stash' ) + and is( ref($user), 'Koha::Patron', 'Stashed koha.user object type is Koha::Patron' ) + and is( $user->borrowernumber, $patron->borrowernumber, 'The stashed user is the right one' ); + is( $userenv->{number}, $patron->borrowernumber, 'userenv set correctly' ); + is( $interface, 'api', "Interface correctly set to \'api\'" ); + + $patron->flags(undef)->store; + + $t->get_ok("//$cardnumber:$password@/api/v1/patrons") + ->status_is( 403, 'Successful authentication and not enough permissions' )->json_is( + '/error' => 'Authorization failure. Missing required permission(s).', + 'Error message returned' + ); + }; $schema->storage->txn_rollback; };