3 # Copyright 2022 Theke Solutions
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use Koha::Auth::Identity::Provider::Domains;
27 # Dummy app for testing the plugin
28 use Mojolicious::Lite;
30 plugin 'Koha::REST::Plugin::Auth::IdP';
32 post '/register_user' => sub {
34 my $params = $c->req->json;
36 my $domain = Koha::Auth::Identity::Provider::Domains->find($params->{domain_id});
37 my $patron = $c->auth->register({
38 data => $params->{data},
40 interface => $params->{interface}
42 $c->render(status => 200, json => $patron->to_api);
44 if ( ref($_) eq 'Koha::Exceptions::Auth::Unauthorized' ) {
45 $c->render(status => 401, json => {message => 'unauthorized'});
47 $c->render(status => 500, json => {message => 'other error'});
52 post '/start_session' => sub {
54 my $userid = my $params = $c->req->json->{userid};
57 my $patron = Koha::Patrons->search({userid => $userid});
58 my ($status, $cookie, $session_id) = $c->auth->session($patron->next);
59 $c->render(status => 200, json => {status => $status});
61 if ( ref($_) eq 'Koha::Exceptions::Auth::CannotCreateSession' ) {
62 $c->render(status => 401, json => {message => 'unauthorized'});
64 $c->render(status => 500, json => {message => 'other error'});
69 use Test::More tests => 2;
73 use t::lib::TestBuilder;
76 my $schema = Koha::Database->new()->schema();
77 my $builder = t::lib::TestBuilder->new;
79 # FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
80 # this affects the other REST api tests
81 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
83 subtest 'auth.register helper' => sub {
86 $schema->storage->txn_begin;
88 # Remove existing patrons
89 Koha::Patrons->delete;
90 my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
91 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 } } );
92 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 } } );
93 my $library = $builder->build_object({ class => 'Koha::Libraries'});
94 my $category = $builder->build_object( {class => 'Koha::Patron::Categories'});
99 branchcode => $library->branchcode,
100 categorycode => $category->categorycode
103 my $t = Test::Mojo->new;
105 $t->post_ok('/register_user' => json => {data => $user_data, domain_id => $domain_with_register->identity_provider_domain_id, interface => 'opac'})
107 ->json_has('/firstname', 'test');
109 $t->post_ok('/register_user' => json => {data => $user_data, domain_id => $domain_without_register->identity_provider_domain_id, interface => 'opac'})
111 ->json_has('/message', 'unauthorized');
112 $schema->storage->txn_rollback;
115 subtest 'auth.session helper' => sub {
118 $schema->storage->txn_begin;
120 # Remove existing patrons
121 Koha::Patrons->delete;
122 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
125 my $t = Test::Mojo->new;
126 $t->post_ok('/start_session' => json => {userid => $patron->userid})
128 ->json_has('/status', 'ok');
130 $schema->storage->txn_rollback;