@@ -, +, @@ --- .../api/v1/auth_authenticate_api_request.t | 165 ++++++++++++------ 1 file changed, 112 insertions(+), 53 deletions(-) --- a/t/db_dependent/api/v1/auth_authenticate_api_request.t +++ a/t/db_dependent/api/v1/auth_authenticate_api_request.t @@ -41,80 +41,139 @@ my $tx; # [1] https://metacpan.org/source/CGI::Session::Driver::DBI#L28 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); -subtest 'token-based tests' => sub { +subtest 'stash the user' => sub { - if ( can_load( modules => { 'Net::OAuth2::AuthorizationServer' => undef } ) ) { - plan tests => 10; - } - else { - plan skip_all => 'Net::OAuth2::AuthorizationServer not available'; - } + subtest 'token-based tests' => sub { - $schema->storage->txn_begin; + if ( can_load( modules => { 'Net::OAuth2::AuthorizationServer' => undef } ) ) { + plan tests => 10; + } + else { + plan skip_all => 'Net::OAuth2::AuthorizationServer not available'; + } + + $schema->storage->txn_begin; + + my $patron = $builder->build_object({ + class => 'Koha::Patrons', + value => { + flags => 16 # no permissions + }, + }); + + t::lib::Mocks::mock_preference('RESTOAuth2ClientCredentials', 1); + + my $api_key = Koha::ApiKey->new({ patron_id => $patron->id, description => 'blah' })->store; + + my $formData = { + grant_type => 'client_credentials', + client_id => $api_key->client_id, + client_secret => $api_key->secret + }; + $t->post_ok('/api/v1/oauth/token', form => $formData) + ->status_is(200) + ->json_is('/expires_in' => 3600) + ->json_is('/token_type' => 'Bearer') + ->json_has('/access_token'); + + my $access_token = $t->tx->res->json->{access_token}; + + my $stash; - my $patron = $builder->build_object({ - class => 'Koha::Patrons', - value => { - flags => 16 # no permissions - }, - }); + my $tx = $t->ua->build_tx(GET => '/api/v1/patrons'); + $tx->req->headers->authorization("Bearer $access_token"); - t::lib::Mocks::mock_preference('RESTOAuth2ClientCredentials', 1); + $t->app->hook(after_dispatch => sub { $stash = shift->stash }); + $t->request_ok($tx)->status_is(200); - my $api_key = Koha::ApiKey->new({ patron_id => $patron->id, description => 'blah' })->store; + 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' ); - my $formData = { - grant_type => 'client_credentials', - client_id => $api_key->client_id, - client_secret => $api_key->secret + $schema->storage->txn_rollback; }; - $t->post_ok('/api/v1/oauth/token', form => $formData) - ->status_is(200) - ->json_is('/expires_in' => 3600) - ->json_is('/token_type' => 'Bearer') - ->json_has('/access_token'); - my $access_token = $t->tx->res->json->{access_token}; + subtest 'cookie-based tests' => sub { - # With access token and permissions, it returns 200 - #$patron->flags(2**4)->store; + plan tests => 5; - my $stash; + $schema->storage->txn_begin; - my $tx = $t->ua->build_tx(GET => '/api/v1/patrons'); - $tx->req->headers->authorization("Bearer $access_token"); + my ( $patron, $session_id ) = create_user_and_session({ authorized => 1 }); - $t->app->hook(after_dispatch => sub { $stash = shift->stash }); - $t->request_ok($tx)->status_is(200); + $tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); + $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); + $tx->req->env( { REMOTE_ADDR => $remote_address } ); - 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' ); + my $stash; + $t->app->hook(after_dispatch => sub { $stash = shift->stash }); + $t->request_ok($tx)->status_is(200); - $schema->storage->txn_rollback; + 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' ); + + $schema->storage->txn_rollback; + }; }; -subtest 'cookie-based tests' => sub { +subtest 'stash the reason the user was granted access' => sub { - plan tests => 5; + plan tests => 12; $schema->storage->txn_begin; - my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 1 }); + my ( $patron, $session_id ) = create_user_and_session({ authorized => 0 }); - $tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); + my ( $stash, $reason ); + + my $other_patron = $builder->build_object({ class => 'Koha::Patrons' }); + + $tx = $t->ua->build_tx( GET => "/api/v1/patrons/" . $other_patron->borrowernumber ); + $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); + $tx->req->env( { REMOTE_ADDR => $remote_address } ); + + $t->app->hook(after_dispatch => sub { $stash = shift->stash }); + $t->request_ok($tx)->status_is(403); + + $reason = $stash->{'grant_reason'}; + is( $reason, undef, "no grant_reason is stashed when no permissions" ); + + $tx = $t->ua->build_tx( GET => "/api/v1/patrons/" . $patron->borrowernumber ); + $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); + $tx->req->env( { REMOTE_ADDR => $remote_address } ); + + $t->app->hook(after_dispatch => sub { $stash = shift->stash }); + $t->request_ok($tx)->status_is(200); + + $reason = $stash->{'grant_reason'}; + is( $reason, 'owner', "grant_reason is 'owner'" ); + + my $guarantee = $builder->build_object({ class => 'Koha::Patrons', value => { guarantorid => $patron->borrowernumber } }); + + $tx = $t->ua->build_tx( GET => "/api/v1/patrons/" . $guarantee->borrowernumber ); + $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); + $tx->req->env( { REMOTE_ADDR => $remote_address } ); + + $t->app->hook(after_dispatch => sub { $stash = shift->stash }); + $t->request_ok($tx)->status_is(200); + + $reason = $stash->{'grant_reason'}; + is( $reason, 'guarantor', "grant_reason is 'guarantor'" ); + + $patron->flags(2**4)->store; # + + $tx = $t->ua->build_tx( GET => "/api/v1/patrons/" . $guarantee->borrowernumber ); $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); $tx->req->env( { REMOTE_ADDR => $remote_address } ); - my $stash; $t->app->hook(after_dispatch => sub { $stash = shift->stash }); $t->request_ok($tx)->status_is(200); - 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, $borrowernumber, 'The stashed user is the right one' ); + $reason = $stash->{'grant_reason'}; + is( $reason, 'permissions', "grant_reason is 'permissions'" ); $schema->storage->txn_rollback; }; @@ -124,10 +183,10 @@ sub create_user_and_session { my $args = shift; my $flags = ( $args->{authorized} ) ? 16 : 0; - my $user = $builder->build( + my $patron = $builder->build_object( { - source => 'Borrower', - value => { + class => 'Koha::Patrons', + value => { flags => $flags } } @@ -135,11 +194,11 @@ sub create_user_and_session { # Create a session for the authorized user my $session = C4::Auth::get_session(''); - $session->param( 'number', $user->{borrowernumber} ); - $session->param( 'id', $user->{userid} ); + $session->param( 'number', $patron->borrowernumber ); + $session->param( 'id', $patron->userid ); $session->param( 'ip', '127.0.0.1' ); $session->param( 'lasttime', time() ); $session->flush; - return ( $user->{borrowernumber}, $session->id ); + return ( $patron, $session->id ); } --