Bug 36503: Move and update unit test
[koha.git] / t / db_dependent / Koha / Auth / Client.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 Test::More tests => 4;
23
24 use Test::MockModule;
25 use Test::MockObject;
26 use Test::Exception;
27
28 use JSON qw(encode_json);
29 use MIME::Base64 qw{ encode_base64url };
30
31 use Koha::Auth::Client;
32 use Koha::Auth::Client::OAuth;
33 use Koha::Patrons;
34
35 use t::lib::TestBuilder;
36 use t::lib::Mocks;
37
38 my $schema  = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new;
40
41 subtest 'get_user() tests' => sub {
42     plan tests => 5;
43
44     $schema->storage->txn_begin;
45
46     my $client   = Koha::Auth::Client::OAuth->new;
47     my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
48     my $domain   = $builder->build_object(
49         {   class => 'Koha::Auth::Identity::Provider::Domains',
50             value => { identity_provider_id => $provider->id, domain => '', update_on_auth => 0, allow_opac => 1, allow_staff => 0 }
51         }
52     );
53     my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { email => 'patron@test.com' } } );
54     my $mapping = {
55         email     => 'electronic_mail',
56         firstname => 'given_name',
57         surname   => 'family_name'
58     };
59     $provider->set_mapping($mapping)->store;
60
61     my $id_token = 'header.'
62       . encode_base64url(
63         encode_json(
64             {   electronic_mail => 'patron@test.com',
65                 given_name      => 'test name'
66             }
67         )
68       ) . '.footer';
69
70     my $data = { id_token => $id_token };
71
72     my ( $resolved_patron, $mapped_data, $resolved_domain ) = $client->get_user( { provider => $provider->code, data => $data, interface => 'opac' } );
73     is_deeply(
74         $resolved_patron->to_api( { user => $patron } ), $patron->to_api( { user => $patron } ),
75         'Patron correctly retrieved'
76     );
77     is( $mapped_data->{firstname},            'test name',                                   'Data mapped correctly' );
78     is( $mapped_data->{surname},              undef,                                         'No surname mapped' );
79     is( $domain->identity_provider_domain_id, $resolved_domain->identity_provider_domain_id, 'Is the same domain' );
80
81     $schema->storage->txn_rollback;
82 };
83
84 subtest 'get_valid_domain_config() tests' => sub {
85     plan tests => 10;
86
87     $schema->storage->txn_begin;
88
89     my $client   = Koha::Auth::Client->new;
90     my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
91     my $domain1  = $builder->build_object(
92         { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '', allow_opac => 0, allow_staff => 0 } } );
93     my $domain2 = $builder->build_object(
94         { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '*library.com', allow_opac => 1, allow_staff => 0 } } );
95     my $domain3 = $builder->build_object(
96         { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '*.library.com', allow_opac => 1, allow_staff => 0 } }
97     );
98     my $domain4 = $builder->build_object(
99         {   class => 'Koha::Auth::Identity::Provider::Domains',
100             value => { identity_provider_id => $provider->id, domain => 'student.library.com', allow_opac => 1, allow_staff => 0 }
101         }
102     );
103     my $domain5 = $builder->build_object(
104         {   class => 'Koha::Auth::Identity::Provider::Domains',
105             value => { identity_provider_id => $provider->id, domain => 'staff.library.com', allow_opac => 1, allow_staff => 1 }
106         }
107     );
108
109     my $retrieved_domain;
110
111     # Test @gmail.com
112     $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@gmail.com', interface => 'opac' } );
113     is( $retrieved_domain, undef, 'gmail user cannot enter opac' );
114     $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@gmail.com', interface => 'staff' } );
115     is( $retrieved_domain, undef, 'gmail user cannot enter staff' );
116
117     # Test @otherlibrary.com
118     $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@otherlibrary.com', interface => 'opac' } );
119     is( $retrieved_domain->identity_provider_domain_id, $domain2->identity_provider_domain_id, 'otherlibaray user can enter opac with domain2' );
120     $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@otherlibrary.com', interface => 'staff' } );
121     is( $retrieved_domain, undef, 'otherlibrary user cannot enter staff' );
122
123     # Test @provider.library.com
124     $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@provider.library.com', interface => 'opac' } );
125     is( $retrieved_domain->identity_provider_domain_id, $domain3->identity_provider_domain_id, 'provider.library user can enter opac with domain3' );
126     $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@provider.library.com', interface => 'staff' } );
127     is( $retrieved_domain, undef, 'provider.library user cannot enter staff' );
128
129     # Test @student.library.com
130     $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@student.library.com', interface => 'opac' } );
131     is( $retrieved_domain->identity_provider_domain_id, $domain4->identity_provider_domain_id, 'student.library user can enter opac with domain4' );
132     $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@student.library.com', interface => 'staff' } );
133     is( $retrieved_domain, undef, 'student.library user cannot enter staff' );
134
135     # Test @staff.library.com
136     $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@staff.library.com', interface => 'opac' } );
137     is( $retrieved_domain->identity_provider_domain_id, $domain5->identity_provider_domain_id, 'staff.library user can enter opac with domain5' );
138     $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@staff.library.com', interface => 'staff' } );
139     is( $retrieved_domain->identity_provider_domain_id, $domain5->identity_provider_domain_id, 'staff.library user can enter staff with domain5' );
140
141     $schema->storage->txn_rollback;
142 };
143
144 subtest 'has_valid_domain_config() tests' => sub {
145     plan tests => 2;
146     $schema->storage->txn_begin;
147
148     my $client   = Koha::Auth::Client->new;
149     my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
150     my $domain1  = $builder->build_object(
151         { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '', allow_opac => 1, allow_staff => 0 } } );
152
153     # Test @gmail.com
154     my $retrieved_domain = $client->has_valid_domain_config( { provider => $provider, email => 'user@gmail.com', interface => 'opac' } );
155     is( $retrieved_domain->identity_provider_domain_id, $domain1->identity_provider_domain_id, 'gmail user can enter opac with domain1' );
156     throws_ok { $client->has_valid_domain_config( { provider => $provider, email => 'user@gmail.com', interface => 'staff' } ) } 'Koha::Exceptions::Auth::NoValidDomain',
157       'gmail user cannot enter staff';
158
159     $schema->storage->txn_rollback;
160 };
161
162 subtest '_traverse_hash() tests' => sub {
163     plan tests => 3;
164
165     my $client = Koha::Auth::Client->new;
166
167     my $hash = {
168         a  => { hash  => { with => 'complicated structure' } },
169         an => { array => [ { inside => 'a hash' }, { inside => 'second element' } ] }
170     };
171
172     my $first_result = $client->_traverse_hash(
173         {   base => $hash,
174             keys => 'a.hash.with'
175         }
176     );
177     is( $first_result, 'complicated structure', 'get the value within a hash structure' );
178
179     my $second_result = $client->_traverse_hash(
180         {   base => $hash,
181             keys => 'an.array.0.inside'
182         }
183     );
184     is( $second_result, 'a hash', 'get the value of the first element of an array within a hash structure' );
185
186     my $third_result = $client->_traverse_hash(
187         {   base => $hash,
188             keys => 'an.array.1.inside'
189         }
190     );
191     is( $third_result, 'second element', 'get the value of the second element of an array within a hash structure' );
192 };