|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2025 Koha Development team |
| 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 => 5; |
| 23 |
use Test::Exception; |
| 24 |
use Test::NoWarnings; |
| 25 |
|
| 26 |
use t::lib::TestBuilder; |
| 27 |
use t::lib::Mocks; |
| 28 |
|
| 29 |
use Koha::Database; |
| 30 |
use Koha::Auth::Identity::Providers; |
| 31 |
|
| 32 |
BEGIN { |
| 33 |
use_ok('Koha::Auth::Identity::Provider::Shibboleth'); |
| 34 |
} |
| 35 |
|
| 36 |
my $schema = Koha::Database->schema; |
| 37 |
my $builder = t::lib::TestBuilder->new; |
| 38 |
|
| 39 |
subtest 'Shibboleth provider instantiation' => sub { |
| 40 |
plan tests => 3; |
| 41 |
|
| 42 |
$schema->storage->txn_begin; |
| 43 |
|
| 44 |
# Test direct instantiation |
| 45 |
my $provider_data = { |
| 46 |
code => 'shibboleth_test', |
| 47 |
description => 'Test Shibboleth Provider', |
| 48 |
protocol => 'Shibboleth', |
| 49 |
config => |
| 50 |
'{"sso_url": "https://idp.example.com/sso", "slo_url": "https://idp.example.com/slo", "entity_id": "https://idp.example.com"}', |
| 51 |
mapping => '{"userid": "REMOTE_USER", "email": "mail"}', |
| 52 |
matchpoint => 'userid' |
| 53 |
}; |
| 54 |
|
| 55 |
my $provider = Koha::Auth::Identity::Provider::Shibboleth->new($provider_data); |
| 56 |
isa_ok( $provider, 'Koha::Auth::Identity::Provider::Shibboleth' ); |
| 57 |
isa_ok( $provider, 'Koha::Auth::Identity::Provider' ); |
| 58 |
is( $provider->protocol, 'Shibboleth', 'Protocol is set correctly' ); |
| 59 |
|
| 60 |
$schema->storage->txn_rollback; |
| 61 |
}; |
| 62 |
|
| 63 |
subtest 'mandatory_config_attributes() tests' => sub { |
| 64 |
plan tests => 5; |
| 65 |
|
| 66 |
$schema->storage->txn_begin; |
| 67 |
|
| 68 |
my $provider = Koha::Auth::Identity::Provider::Shibboleth->new(); |
| 69 |
my @mandatory = $provider->mandatory_config_attributes(); |
| 70 |
|
| 71 |
is( scalar @mandatory, 3, 'Returns correct number of mandatory attributes' ); |
| 72 |
|
| 73 |
my %mandatory_hash = map { $_ => 1 } @mandatory; |
| 74 |
ok( exists $mandatory_hash{'sso_url'}, 'sso_url is mandatory' ); |
| 75 |
ok( exists $mandatory_hash{'slo_url'}, 'slo_url is mandatory' ); |
| 76 |
ok( exists $mandatory_hash{'entity_id'}, 'entity_id is mandatory' ); |
| 77 |
|
| 78 |
# Verify order (should be consistent) |
| 79 |
is_deeply( \@mandatory, [qw(sso_url slo_url entity_id)], 'Mandatory attributes in expected order' ); |
| 80 |
|
| 81 |
$schema->storage->txn_rollback; |
| 82 |
}; |
| 83 |
|
| 84 |
subtest 'Configuration validation' => sub { |
| 85 |
plan tests => 8; |
| 86 |
|
| 87 |
$schema->storage->txn_begin; |
| 88 |
|
| 89 |
my $provider = $builder->build_object( |
| 90 |
{ |
| 91 |
class => 'Koha::Auth::Identity::Providers', |
| 92 |
value => { |
| 93 |
protocol => 'Shibboleth', |
| 94 |
config => '{}', |
| 95 |
mapping => '{}', |
| 96 |
} |
| 97 |
} |
| 98 |
); |
| 99 |
|
| 100 |
# Upgrade to Shibboleth provider |
| 101 |
my $shibboleth_provider = $provider->upgrade_class(); |
| 102 |
isa_ok( $shibboleth_provider, 'Koha::Auth::Identity::Provider::Shibboleth' ); |
| 103 |
|
| 104 |
# Test valid configuration |
| 105 |
my $valid_config = { |
| 106 |
sso_url => 'https://idp.example.com/sso', |
| 107 |
slo_url => 'https://idp.example.com/slo', |
| 108 |
entity_id => 'https://idp.example.com' |
| 109 |
}; |
| 110 |
|
| 111 |
lives_ok { $shibboleth_provider->set_config($valid_config) } 'Valid config is accepted'; |
| 112 |
|
| 113 |
# Test invalid configuration (missing sso_url) |
| 114 |
my $invalid_config1 = { |
| 115 |
slo_url => 'https://idp.example.com/slo', |
| 116 |
entity_id => 'https://idp.example.com' |
| 117 |
}; |
| 118 |
|
| 119 |
throws_ok { |
| 120 |
$shibboleth_provider->set_config($invalid_config1) |
| 121 |
} |
| 122 |
'Koha::Exceptions::MissingParameter', 'Missing sso_url throws exception'; |
| 123 |
|
| 124 |
is( $@->parameter, 'sso_url', 'Exception indicates missing sso_url parameter' ); |
| 125 |
|
| 126 |
# Test invalid configuration (missing slo_url) |
| 127 |
my $invalid_config2 = { |
| 128 |
sso_url => 'https://idp.example.com/sso', |
| 129 |
entity_id => 'https://idp.example.com' |
| 130 |
}; |
| 131 |
|
| 132 |
throws_ok { |
| 133 |
$shibboleth_provider->set_config($invalid_config2) |
| 134 |
} |
| 135 |
'Koha::Exceptions::MissingParameter', 'Missing slo_url throws exception'; |
| 136 |
|
| 137 |
is( $@->parameter, 'slo_url', 'Exception indicates missing slo_url parameter' ); |
| 138 |
|
| 139 |
# Test invalid configuration (missing entity_id) |
| 140 |
my $invalid_config3 = { |
| 141 |
sso_url => 'https://idp.example.com/sso', |
| 142 |
slo_url => 'https://idp.example.com/slo' |
| 143 |
}; |
| 144 |
|
| 145 |
throws_ok { |
| 146 |
$shibboleth_provider->set_config($invalid_config3) |
| 147 |
} |
| 148 |
'Koha::Exceptions::MissingParameter', 'Missing entity_id throws exception'; |
| 149 |
|
| 150 |
is( $@->parameter, 'entity_id', 'Exception indicates missing entity_id parameter' ); |
| 151 |
|
| 152 |
$schema->storage->txn_rollback; |
| 153 |
}; |