|
Line 0
Link Here
|
|
|
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::MockModule; |
| 24 |
use Test::MockObject; |
| 25 |
use Test::NoWarnings; |
| 26 |
|
| 27 |
use t::lib::TestBuilder; |
| 28 |
use t::lib::Mocks; |
| 29 |
|
| 30 |
use Koha::Database; |
| 31 |
use Koha::Auth::Identity::Providers; |
| 32 |
|
| 33 |
BEGIN { |
| 34 |
use_ok('Koha::Auth::Client::CAS'); |
| 35 |
} |
| 36 |
|
| 37 |
my $schema = Koha::Database->schema; |
| 38 |
my $builder = t::lib::TestBuilder->new; |
| 39 |
|
| 40 |
subtest 'CAS client instantiation' => sub { |
| 41 |
plan tests => 2; |
| 42 |
|
| 43 |
$schema->storage->txn_begin; |
| 44 |
|
| 45 |
my $client = Koha::Auth::Client::CAS->new(); |
| 46 |
isa_ok( $client, 'Koha::Auth::Client::CAS' ); |
| 47 |
isa_ok( $client, 'Koha::Auth::Client' ); |
| 48 |
|
| 49 |
$schema->storage->txn_rollback; |
| 50 |
}; |
| 51 |
|
| 52 |
subtest '_get_data_and_patron() tests' => sub { |
| 53 |
plan tests => 8; |
| 54 |
|
| 55 |
$schema->storage->txn_begin; |
| 56 |
|
| 57 |
# Create a CAS provider |
| 58 |
my $provider = $builder->build_object( |
| 59 |
{ |
| 60 |
class => 'Koha::Auth::Identity::Providers', |
| 61 |
value => { |
| 62 |
protocol => 'CAS', |
| 63 |
config => '{"cas_url": "https://cas.example.com/cas"}', |
| 64 |
mapping => '{"userid": "user", "email": "mail"}', |
| 65 |
matchpoint => 'userid' |
| 66 |
} |
| 67 |
} |
| 68 |
); |
| 69 |
|
| 70 |
# Create a patron to match against |
| 71 |
my $patron = $builder->build_object( |
| 72 |
{ |
| 73 |
class => 'Koha::Patrons', |
| 74 |
value => { userid => 'testuser' } |
| 75 |
} |
| 76 |
); |
| 77 |
|
| 78 |
my $client = Koha::Auth::Client::CAS->new(); |
| 79 |
|
| 80 |
# Mock Authen::CAS::Client |
| 81 |
my $cas_mock = Test::MockModule->new('Authen::CAS::Client'); |
| 82 |
my $response_mock = Test::MockObject->new(); |
| 83 |
|
| 84 |
# Test successful ticket validation |
| 85 |
$response_mock->mock( 'is_success', sub { return 1; } ); |
| 86 |
$response_mock->mock( 'user', sub { return 'testuser'; } ); |
| 87 |
|
| 88 |
my $cas_client_mock = Test::MockObject->new(); |
| 89 |
$cas_client_mock->mock( 'service_validate', sub { return $response_mock; } ); |
| 90 |
|
| 91 |
$cas_mock->mock( 'new', sub { return $cas_client_mock; } ); |
| 92 |
|
| 93 |
# Mock the client methods that depend on provider |
| 94 |
my $client_mock = Test::MockModule->new('Koha::Auth::Client::CAS'); |
| 95 |
$client_mock->mock( 'provider', sub { return $provider; } ); |
| 96 |
$client_mock->mock( '_get_patron', sub { return $patron; } ); |
| 97 |
|
| 98 |
my ( $user_data, $returned_patron ) = $client->_get_data_and_patron( |
| 99 |
{ |
| 100 |
ticket => 'ST-123456', |
| 101 |
service => 'https://koha.example.com/cgi-bin/koha/opac-main.pl' |
| 102 |
} |
| 103 |
); |
| 104 |
|
| 105 |
is( ref($user_data), 'HASH', 'Returns user data as hashref' ); |
| 106 |
is( $user_data->{userid}, 'testuser', 'User data contains correct userid' ); |
| 107 |
is( $user_data->{email}, 'testuser', 'User data contains email (mapped from userid)' ); |
| 108 |
isa_ok( $returned_patron, 'Koha::Patron', 'Returns patron object' ); |
| 109 |
is( $returned_patron->id, $patron->id, 'Returns correct patron' ); |
| 110 |
|
| 111 |
# Test failed ticket validation |
| 112 |
$response_mock->mock( 'is_success', sub { return 0; } ); |
| 113 |
$response_mock->mock( 'code', sub { return 'INVALID_TICKET'; } ); |
| 114 |
|
| 115 |
( $user_data, $returned_patron ) = $client->_get_data_and_patron( |
| 116 |
{ |
| 117 |
ticket => 'ST-invalid', |
| 118 |
service => 'https://koha.example.com/cgi-bin/koha/opac-main.pl' |
| 119 |
} |
| 120 |
); |
| 121 |
|
| 122 |
is( ref($user_data), 'HASH', 'Returns empty hashref on failure' ); |
| 123 |
is( scalar keys %$user_data, 0, 'User data is empty on failure' ); |
| 124 |
is( $returned_patron, undef, 'Returns undef patron on failure' ); |
| 125 |
|
| 126 |
$schema->storage->txn_rollback; |
| 127 |
}; |
| 128 |
|
| 129 |
subtest 'CAS URL generation methods' => sub { |
| 130 |
plan tests => 4; |
| 131 |
|
| 132 |
$schema->storage->txn_begin; |
| 133 |
|
| 134 |
my $client = Koha::Auth::Client::CAS->new(); |
| 135 |
my $config = { cas_url => 'https://cas.example.com/cas' }; |
| 136 |
|
| 137 |
# Mock Authen::CAS::Client |
| 138 |
my $cas_mock = Test::MockModule->new('Authen::CAS::Client'); |
| 139 |
my $cas_client_mock = Test::MockObject->new(); |
| 140 |
|
| 141 |
$cas_client_mock->mock( |
| 142 |
'login_url', |
| 143 |
sub { |
| 144 |
my ( $self, %params ) = @_; |
| 145 |
return 'https://cas.example.com/cas/login?service=' . $params{service}; |
| 146 |
} |
| 147 |
); |
| 148 |
|
| 149 |
$cas_client_mock->mock( |
| 150 |
'logout_url', |
| 151 |
sub { |
| 152 |
my ( $self, %params ) = @_; |
| 153 |
return 'https://cas.example.com/cas/logout?url=' . $params{url}; |
| 154 |
} |
| 155 |
); |
| 156 |
|
| 157 |
$cas_mock->mock( 'new', sub { return $cas_client_mock; } ); |
| 158 |
|
| 159 |
# Test login URL generation |
| 160 |
my $service_url = 'https://koha.example.com/cgi-bin/koha/opac-main.pl'; |
| 161 |
my $login_url = $client->get_cas_login_url( $config, $service_url ); |
| 162 |
|
| 163 |
like( $login_url, qr/https:\/\/cas\.example\.com\/cas\/login/, 'Login URL contains CAS login endpoint' ); |
| 164 |
like( $login_url, qr/service=/, 'Login URL contains service parameter' ); |
| 165 |
|
| 166 |
# Test logout URL generation |
| 167 |
my $return_url = 'https://koha.example.com/cgi-bin/koha/opac-main.pl'; |
| 168 |
my $logout_url = $client->get_cas_logout_url( $config, $return_url ); |
| 169 |
|
| 170 |
like( $logout_url, qr/https:\/\/cas\.example\.com\/cas\/logout/, 'Logout URL contains CAS logout endpoint' ); |
| 171 |
like( $logout_url, qr/url=/, 'Logout URL contains return URL parameter' ); |
| 172 |
|
| 173 |
$schema->storage->txn_rollback; |
| 174 |
}; |