Bugzilla – Attachment 83683 Details for
Bug 21198
authenticate_api_request should stash the reason access is granted
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 21198: Unit tests
Bug-21198-Unit-tests.patch (text/plain), 8.88 KB, created by
Tomás Cohen Arazi (tcohen)
on 2019-01-07 13:30:16 UTC
(
hide
)
Description:
Bug 21198: Unit tests
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2019-01-07 13:30:16 UTC
Size:
8.88 KB
patch
obsolete
>From 1c6f302df1bafa49e4e5c9094c720befe41ac062 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Mon, 7 Jan 2019 10:22:17 -0300 >Subject: [PATCH] Bug 21198: Unit tests > >--- > .../api/v1/auth_authenticate_api_request.t | 165 ++++++++++++------ > 1 file changed, 112 insertions(+), 53 deletions(-) > >diff --git a/t/db_dependent/api/v1/auth_authenticate_api_request.t b/t/db_dependent/api/v1/auth_authenticate_api_request.t >index 8583125a9f..16b21fb670 100755 >--- a/t/db_dependent/api/v1/auth_authenticate_api_request.t >+++ b/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 ); > } >-- >2.20.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 21198
: 83683 |
83684