|
Lines 20-32
Link Here
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::NoWarnings; |
22 |
use Test::NoWarnings; |
| 23 |
use Test::More tests => 7; |
23 |
use Test::More tests => 8; |
| 24 |
|
24 |
|
| 25 |
use Test::MockModule; |
25 |
use Test::MockModule; |
| 26 |
use Test::Exception; |
26 |
use Test::Exception; |
| 27 |
|
27 |
|
| 28 |
use JSON qw(encode_json); |
28 |
use JSON qw(encode_json); |
| 29 |
|
29 |
|
|
|
30 |
use Koha::Auth::Hostnames; |
| 31 |
use Koha::Auth::Identity::Provider; |
| 32 |
use Koha::Auth::Identity::Provider::Hostnames; |
| 33 |
use Koha::Auth::Identity::Provider::Mappings; |
| 30 |
use Koha::Auth::Identity::Providers; |
34 |
use Koha::Auth::Identity::Providers; |
| 31 |
|
35 |
|
| 32 |
use t::lib::TestBuilder; |
36 |
use t::lib::TestBuilder; |
|
Lines 88-94
subtest 'set_config() tests' => sub {
Link Here
|
| 88 |
|
92 |
|
| 89 |
my $provider = |
93 |
my $provider = |
| 90 |
$builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { protocol => 'OIDC' } } ); |
94 |
$builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { protocol => 'OIDC' } } ); |
| 91 |
$provider = $provider->upgrade_class; |
|
|
| 92 |
|
95 |
|
| 93 |
my $config = { |
96 |
my $config = { |
| 94 |
key => 'key', |
97 |
key => 'key', |
|
Lines 114-120
subtest 'set_config() tests' => sub {
Link Here
|
| 114 |
|
117 |
|
| 115 |
my $provider = |
118 |
my $provider = |
| 116 |
$builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { protocol => 'OAuth' } } ); |
119 |
$builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { protocol => 'OAuth' } } ); |
| 117 |
$provider = $provider->upgrade_class; |
|
|
| 118 |
|
120 |
|
| 119 |
my $config = { |
121 |
my $config = { |
| 120 |
key => 'key', |
122 |
key => 'key', |
|
Lines 135-149
subtest 'set_config() tests' => sub {
Link Here
|
| 135 |
is_deeply( $provider->get_config, $config, 'Configuration stored correctly' ); |
137 |
is_deeply( $provider->get_config, $config, 'Configuration stored correctly' ); |
| 136 |
}; |
138 |
}; |
| 137 |
|
139 |
|
| 138 |
subtest 'Unsupported protocol tests' => sub { |
140 |
subtest 'Base class (unsupported protocol) tests' => sub { |
| 139 |
|
141 |
|
| 140 |
plan tests => 2; |
142 |
plan tests => 2; |
| 141 |
|
143 |
|
| 142 |
my $provider = |
144 |
# Cannot build in DB with invalid protocol; instantiate base class directly |
| 143 |
$builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { protocol => 'CAS' } } ); |
145 |
my $provider = Koha::Auth::Identity::Provider->new( { protocol => 'OAuth' } ); |
| 144 |
|
146 |
|
| 145 |
throws_ok { $provider->set_config() } |
147 |
throws_ok { $provider->set_config( {} ) } |
| 146 |
'Koha::Exception', 'Exception thrown on unsupported protocol'; |
148 |
'Koha::Exception', 'Exception thrown when calling set_config on base class'; |
| 147 |
|
149 |
|
| 148 |
like( "$@", qr/This method needs to be subclassed/, 'Message is correct' ); |
150 |
like( "$@", qr/This method needs to be subclassed/, 'Message is correct' ); |
| 149 |
}; |
151 |
}; |
|
Lines 151-198
subtest 'set_config() tests' => sub {
Link Here
|
| 151 |
$schema->storage->txn_rollback; |
153 |
$schema->storage->txn_rollback; |
| 152 |
}; |
154 |
}; |
| 153 |
|
155 |
|
| 154 |
subtest 'get_mapping() tests' => sub { |
156 |
subtest 'mappings() tests' => sub { |
| 155 |
|
157 |
|
| 156 |
plan tests => 2; |
158 |
plan tests => 3; |
| 157 |
|
159 |
|
| 158 |
$schema->storage->txn_begin; |
160 |
$schema->storage->txn_begin; |
| 159 |
|
161 |
|
| 160 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { config => '{' } } ); |
162 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); |
| 161 |
|
163 |
my $mappings = $provider->mappings; |
| 162 |
throws_ok { $provider->get_mapping() } |
164 |
|
| 163 |
'Koha::Exceptions::Object::BadValue', 'Expected exception thrown on bad JSON'; |
165 |
is( ref($mappings), 'Koha::Auth::Identity::Provider::Mappings', 'Type is correct' ); |
| 164 |
|
166 |
is( $mappings->count, 0, 'No mappings defined' ); |
| 165 |
my $mapping = { some => 'value', and => 'another' }; |
167 |
|
| 166 |
$provider->mapping( encode_json($mapping) )->store; |
168 |
$builder->build( |
| 167 |
|
169 |
{ |
| 168 |
is_deeply( $provider->get_mapping, $mapping, 'Mapping correctly retrieved' ); |
170 |
source => 'IdentityProviderMapping', |
|
|
171 |
value => { identity_provider_id => $provider->id, koha_field => 'userid', provider_field => 'uid' } |
| 172 |
} |
| 173 |
); |
| 174 |
$builder->build( |
| 175 |
{ |
| 176 |
source => 'IdentityProviderMapping', |
| 177 |
value => { identity_provider_id => $provider->id, koha_field => 'email', provider_field => 'mail' } |
| 178 |
} |
| 179 |
); |
| 180 |
|
| 181 |
is( $provider->mappings->count, 2, 'The provider has 2 mappings defined' ); |
| 169 |
|
182 |
|
| 170 |
$schema->storage->txn_rollback; |
183 |
$schema->storage->txn_rollback; |
| 171 |
}; |
184 |
}; |
| 172 |
|
185 |
|
| 173 |
subtest 'set_mapping() tests' => sub { |
186 |
subtest 'hostnames() tests' => sub { |
| 174 |
|
187 |
|
| 175 |
plan tests => 1; |
188 |
plan tests => 3; |
| 176 |
|
189 |
|
| 177 |
$schema->storage->txn_begin; |
190 |
$schema->storage->txn_begin; |
| 178 |
|
191 |
|
| 179 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); |
192 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); |
|
|
193 |
my $hostnames = $provider->hostnames; |
| 180 |
|
194 |
|
| 181 |
my $mapping = { some => 'value', and => 'another' }; |
195 |
is( ref($hostnames), 'Koha::Auth::Identity::Provider::Hostnames', 'Type is correct' ); |
| 182 |
$provider->set_mapping($mapping)->store; |
196 |
is( $hostnames->count, 0, 'No hostnames defined' ); |
|
|
197 |
|
| 198 |
$builder->build_object( |
| 199 |
{ class => 'Koha::Auth::Identity::Provider::Hostnames', value => { identity_provider_id => $provider->id } } ); |
| 200 |
$builder->build_object( |
| 201 |
{ class => 'Koha::Auth::Identity::Provider::Hostnames', value => { identity_provider_id => $provider->id } } ); |
| 183 |
|
202 |
|
| 184 |
is_deeply( $provider->get_mapping, $mapping, 'Mapping correctly retrieved' ); |
203 |
is( $provider->hostnames->count, 2, 'The provider has 2 hostnames defined' ); |
| 185 |
|
204 |
|
| 186 |
$schema->storage->txn_rollback; |
205 |
$schema->storage->txn_rollback; |
| 187 |
}; |
206 |
}; |
| 188 |
|
207 |
|
| 189 |
subtest 'upgrade_class() tests' => sub { |
208 |
subtest 'polymorphic_retrieval() tests' => sub { |
| 190 |
|
209 |
|
| 191 |
plan tests => 5; |
210 |
plan tests => 7; |
| 192 |
|
211 |
|
| 193 |
$schema->storage->txn_begin; |
212 |
$schema->storage->txn_begin; |
| 194 |
|
213 |
|
| 195 |
my $mapping = Koha::Auth::Identity::Provider::protocol_to_class_mapping; |
214 |
my $providers = Koha::Auth::Identity::Providers->new; |
|
|
215 |
my $mapping = $providers->_polymorphic_map; |
| 196 |
my @protocols = keys %{$mapping}; |
216 |
my @protocols = keys %{$mapping}; |
| 197 |
|
217 |
|
| 198 |
foreach my $protocol (@protocols) { |
218 |
foreach my $protocol (@protocols) { |
|
Lines 204-223
subtest 'upgrade_class() tests' => sub {
Link Here
|
| 204 |
} |
224 |
} |
| 205 |
); |
225 |
); |
| 206 |
|
226 |
|
| 207 |
is( ref($provider), 'Koha::Auth::Identity::Provider', "Base class used for $protocol" ); |
227 |
is( ref($provider), $mapping->{$protocol}, "build_object returns correct subclass for $protocol" ); |
| 208 |
|
228 |
|
| 209 |
# upgrade |
229 |
my $found = Koha::Auth::Identity::Providers->find( $provider->id ); |
| 210 |
$provider = $provider->upgrade_class; |
230 |
is( ref($found), $mapping->{$protocol}, "Providers->find returns correct subclass for $protocol" ); |
| 211 |
is( |
|
|
| 212 |
ref($provider), $mapping->{$protocol}, |
| 213 |
"Class upgraded to " . $mapping->{$protocol} . "for protocol $protocol" |
| 214 |
); |
| 215 |
} |
231 |
} |
| 216 |
|
232 |
|
| 217 |
my $provider = Koha::Auth::Identity::Provider->new( { protocol => 'Invalid' } ); |
233 |
my $provider = Koha::Auth::Identity::Provider->new( { protocol => 'Invalid' } ); |
| 218 |
throws_ok { $provider->upgrade } |
234 |
throws_ok { $provider->set_config( {} ) } 'Koha::Exception', 'Exception thrown calling set_config on base class'; |
| 219 |
'Koha::Exception', |
235 |
|
| 220 |
'Exception throw on invalid protocol'; |
236 |
$schema->storage->txn_rollback; |
|
|
237 |
}; |
| 238 |
|
| 239 |
subtest 'find_forced_provider() tests' => sub { |
| 240 |
|
| 241 |
plan tests => 8; |
| 242 |
|
| 243 |
$schema->storage->txn_begin; |
| 244 |
|
| 245 |
# No hostname argument returns undef |
| 246 |
is( Koha::Auth::Identity::Providers->find_forced_provider(undef), undef, 'Returns undef when hostname is undef' ); |
| 247 |
is( |
| 248 |
Koha::Auth::Identity::Providers->find_forced_provider(''), undef, |
| 249 |
'Returns undef when hostname is empty string' |
| 250 |
); |
| 251 |
|
| 252 |
my $provider = $builder->build_object( |
| 253 |
{ class => 'Koha::Auth::Identity::Providers', value => { protocol => 'OIDC', enabled => 1 } } ); |
| 254 |
|
| 255 |
my $hostname_record = |
| 256 |
$builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'sso.example.com' } } ); |
| 257 |
|
| 258 |
# Hostname linked but force_sso=0 (default) |
| 259 |
$builder->build_object( |
| 260 |
{ |
| 261 |
class => 'Koha::Auth::Identity::Provider::Hostnames', |
| 262 |
value => { |
| 263 |
identity_provider_id => $provider->id, |
| 264 |
hostname_id => $hostname_record->hostname_id, |
| 265 |
is_enabled => 1, |
| 266 |
force_sso => 0, |
| 267 |
} |
| 268 |
} |
| 269 |
); |
| 270 |
is( |
| 271 |
Koha::Auth::Identity::Providers->find_forced_provider('sso.example.com'), |
| 272 |
undef, 'Returns undef when hostname is linked but force_sso is 0' |
| 273 |
); |
| 274 |
|
| 275 |
# Update to force_sso=1 but is_enabled=0 on the hostname link |
| 276 |
Koha::Auth::Identity::Provider::Hostnames->search( |
| 277 |
{ identity_provider_id => $provider->id, hostname_id => $hostname_record->hostname_id } ) |
| 278 |
->update( { force_sso => 1, is_enabled => 0 } ); |
| 279 |
is( |
| 280 |
Koha::Auth::Identity::Providers->find_forced_provider('sso.example.com'), |
| 281 |
undef, 'Returns undef when hostname link has is_enabled=0' |
| 282 |
); |
| 283 |
|
| 284 |
# Enable the hostname link but disable the provider itself |
| 285 |
Koha::Auth::Identity::Provider::Hostnames->search( |
| 286 |
{ identity_provider_id => $provider->id, hostname_id => $hostname_record->hostname_id } ) |
| 287 |
->update( { is_enabled => 1 } ); |
| 288 |
$provider->update( { enabled => 0 } ); |
| 289 |
is( |
| 290 |
Koha::Auth::Identity::Providers->find_forced_provider('sso.example.com'), |
| 291 |
undef, 'Returns undef when provider itself is disabled' |
| 292 |
); |
| 293 |
|
| 294 |
# Re-enable the provider - now all conditions met |
| 295 |
$provider->update( { enabled => 1 } ); |
| 296 |
my $forced = Koha::Auth::Identity::Providers->find_forced_provider('sso.example.com'); |
| 297 |
ok( $forced, 'Returns provider when hostname linked with force_sso=1, is_enabled=1, and provider enabled=1' ); |
| 298 |
is( |
| 299 |
ref($forced), 'Koha::Auth::Identity::Provider::OIDC', |
| 300 |
'Returned object is the correct Identity Provider subclass' |
| 301 |
); |
| 302 |
is( $forced->id, $provider->id, 'Returns the correct provider' ); |
| 221 |
|
303 |
|
| 222 |
$schema->storage->txn_rollback; |
304 |
$schema->storage->txn_rollback; |
| 223 |
}; |
305 |
}; |