From d8d03c8efa31860a30449fe75fd2fafe3dc2879d Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 18 Nov 2022 11:59:23 -0300 Subject: [PATCH] Bug 32178: Adapt tests to new auth.session params Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Koha/REST/Plugin/Auth/IdP.t | 138 ++++++++++----------- 1 file changed, 68 insertions(+), 70 deletions(-) diff --git a/t/db_dependent/Koha/REST/Plugin/Auth/IdP.t b/t/db_dependent/Koha/REST/Plugin/Auth/IdP.t index 0e624bfc4e..006415f07f 100755 --- a/t/db_dependent/Koha/REST/Plugin/Auth/IdP.t +++ b/t/db_dependent/Koha/REST/Plugin/Auth/IdP.t @@ -30,40 +30,41 @@ use Mojolicious::Lite; plugin 'Koha::REST::Plugin::Auth::IdP'; post '/register_user' => sub { - my $c = shift; - my $params = $c->req->json; - try { - my $domain = Koha::Auth::Identity::Provider::Domains->find($params->{domain_id}); - my $patron = $c->auth->register({ - data => $params->{data}, - domain => $domain, - interface => $params->{interface} - }); - $c->render(status => 200, json => $patron->to_api); - } catch { - if ( ref($_) eq 'Koha::Exceptions::Auth::Unauthorized' ) { - $c->render(status => 401, json => {message => 'unauthorized'}); - } else { - $c->render(status => 500, json => {message => 'other error'}); + my $c = shift; + my $params = $c->req->json; + try { + my $domain = Koha::Auth::Identity::Provider::Domains->find( $params->{domain_id} ); + my $patron = $c->auth->register( + { data => $params->{data}, + domain => $domain, + interface => $params->{interface} + } + ); + $c->render( status => 200, json => $patron->to_api ); + } catch { + if ( ref($_) eq 'Koha::Exceptions::Auth::Unauthorized' ) { + $c->render( status => 401, json => { message => 'unauthorized' } ); + } else { + $c->render( status => 500, json => { message => 'other error' } ); + } } - } }; post '/start_session' => sub { - my $c = shift; - my $userid = my $params = $c->req->json->{userid}; - - try { - my $patron = Koha::Patrons->search({userid => $userid}); - my ($status, $cookie, $session_id) = $c->auth->session($patron->next); - $c->render(status => 200, json => {status => $status}); - } catch { - if ( ref($_) eq 'Koha::Exceptions::Auth::CannotCreateSession' ) { - $c->render(status => 401, json => {message => 'unauthorized'}); - } else { - $c->render(status => 500, json => {message => 'other error'}); + my $c = shift; + my $userid = my $params = $c->req->json->{userid}; + + try { + my $patron = Koha::Patrons->search( { userid => $userid } ); + my ( $status, $cookie, $session_id ) = $c->auth->session({ patron => $patron->next, interface => 'opac' }); + $c->render( status => 200, json => { status => $status } ); + } catch { + if ( ref($_) eq 'Koha::Exceptions::Auth::CannotCreateSession' ) { + $c->render( status => 401, json => { message => 'unauthorized' } ); + } else { + $c->render( status => 500, json => { message => 'other error' } ); + } } - } }; use Test::More tests => 2; @@ -81,53 +82,50 @@ my $builder = t::lib::TestBuilder->new; t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); subtest 'auth.register helper' => sub { - plan tests => 6; - - $schema->storage->txn_begin; - - # Remove existing patrons - Koha::Patrons->delete; - my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } ); - my $domain_with_register = $builder->build_object( { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => 'domain1.com', auto_register => 1 } } ); - my $domain_without_register = $builder->build_object( { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => 'domain2.com', auto_register => 0 } } ); - my $library = $builder->build_object({ class => 'Koha::Libraries'}); - my $category = $builder->build_object( {class => 'Koha::Patron::Categories'}); - my $user_data = { - firstname => 'test', - surname => 'test', - userid => 'id1', - branchcode => $library->branchcode, - categorycode => $category->categorycode - }; - - my $t = Test::Mojo->new; - - $t->post_ok('/register_user' => json => {data => $user_data, domain_id => $domain_with_register->identity_provider_domain_id, interface => 'opac'}) - ->status_is(200) - ->json_has('/firstname', 'test'); - - $t->post_ok('/register_user' => json => {data => $user_data, domain_id => $domain_without_register->identity_provider_domain_id, interface => 'opac'}) - ->status_is(401) - ->json_has('/message', 'unauthorized'); - $schema->storage->txn_rollback; + plan tests => 6; + + $schema->storage->txn_begin; + + # Remove existing patrons + Koha::Patrons->delete; + my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } ); + my $domain_with_register = $builder->build_object( + { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => 'domain1.com', auto_register => 1 } } ); + my $domain_without_register = $builder->build_object( + { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => 'domain2.com', auto_register => 0 } } ); + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $category = $builder->build_object( { class => 'Koha::Patron::Categories' } ); + my $user_data = { + firstname => 'test', + surname => 'test', + userid => 'id1', + branchcode => $library->branchcode, + categorycode => $category->categorycode + }; + + my $t = Test::Mojo->new; + + $t->post_ok( '/register_user' => json => { data => $user_data, domain_id => $domain_with_register->identity_provider_domain_id, interface => 'opac' } )->status_is(200) + ->json_has( '/firstname', 'test' ); + + $t->post_ok( '/register_user' => json => { data => $user_data, domain_id => $domain_without_register->identity_provider_domain_id, interface => 'opac' } )->status_is(401) + ->json_has( '/message', 'unauthorized' ); + $schema->storage->txn_rollback; }; subtest 'auth.session helper' => sub { - plan tests => 3; + plan tests => 3; - $schema->storage->txn_begin; + $schema->storage->txn_begin; - # Remove existing patrons - Koha::Patrons->delete; - my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + # Remove existing patrons + Koha::Patrons->delete; + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $t = Test::Mojo->new; + $t->post_ok( '/start_session' => json => { userid => $patron->userid } )->status_is(200)->json_has( '/status', 'ok' ); - my $t = Test::Mojo->new; - $t->post_ok('/start_session' => json => {userid => $patron->userid}) - ->status_is(200) - ->json_has('/status', 'ok'); - - $schema->storage->txn_rollback; + $schema->storage->txn_rollback; }; -1; \ No newline at end of file +1; -- 2.39.5