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