Bug 31378: Rename Auth Provider to Identity Provider and add Client.t tests
[koha.git] / t / db_dependent / Koha / REST / Plugin / Auth / IdP.t
1 #!/usr/bin/perl
2
3 # Copyright 2022 Theke Solutions
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Koha::Patrons;
23 use Koha::Auth::Identity::Provider::Domains;
24 use Koha::Patrons;
25 use Try::Tiny;
26
27 # Dummy app for testing the plugin
28 use Mojolicious::Lite;
29
30 plugin 'Koha::REST::Plugin::Auth::IdP';
31
32 post '/register_user' => sub {
33   my $c = shift;
34   my $params = $c->req->json;
35   try {
36     my $domain = Koha::Auth::Identity::Provider::Domains->find($params->{domain_id});
37     my $patron = $c->auth->register({
38       data => $params->{data},
39       domain => $domain,
40       interface => $params->{interface}
41     });
42     $c->render(status => 200, json => $patron->to_api);
43   } catch {
44     if ( ref($_) eq 'Koha::Exceptions::Auth::Unauthorized' ) {
45       $c->render(status => 401, json => {message => 'unauthorized'});
46     } else {
47       $c->render(status => 500, json => {message => 'other error'});
48     }
49   }
50 };
51
52 post '/start_session' => sub {
53   my $c = shift;
54   my $userid = my $params = $c->req->json->{userid};
55
56   try {
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});
60   } catch {
61     if ( ref($_) eq 'Koha::Exceptions::Auth::CannotCreateSession' ) {
62       $c->render(status => 401, json => {message => 'unauthorized'});
63     } else {
64       $c->render(status => 500, json => {message => 'other error'});
65     }
66   }
67 };
68
69 use Test::More tests => 2;
70 use Test::Mojo;
71
72 use t::lib::Mocks;
73 use t::lib::TestBuilder;
74 use Koha::Database;
75
76 my $schema  = Koha::Database->new()->schema();
77 my $builder = t::lib::TestBuilder->new;
78
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' );
82
83 subtest 'auth.register helper' => sub {
84   plan tests => 6;
85
86   $schema->storage->txn_begin;
87
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'});
95   my $user_data = {
96     firstname => 'test',
97     surname => 'test',
98     userid => 'id1',
99     branchcode => $library->branchcode,
100     categorycode => $category->categorycode
101   };
102
103   my $t = Test::Mojo->new;
104
105   $t->post_ok('/register_user' => json => {data => $user_data, domain_id => $domain_with_register->identity_provider_domain_id, interface => 'opac'})
106     ->status_is(200)
107     ->json_has('/firstname', 'test');
108
109   $t->post_ok('/register_user' => json => {data => $user_data, domain_id => $domain_without_register->identity_provider_domain_id, interface => 'opac'})
110     ->status_is(401)
111     ->json_has('/message', 'unauthorized');
112   $schema->storage->txn_rollback;
113 };
114
115 subtest 'auth.session helper' => sub {
116   plan tests => 3;
117
118   $schema->storage->txn_begin;
119
120   # Remove existing patrons
121   Koha::Patrons->delete;
122   my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
123
124
125   my $t = Test::Mojo->new;
126   $t->post_ok('/start_session' => json => {userid => $patron->userid})
127     ->status_is(200)
128     ->json_has('/status', 'ok');
129
130   $schema->storage->txn_rollback;
131 };
132
133 1;