View | Details | Raw Unified | Return to bug 37711
Collapse All | Expand All

(-)a/Koha/Template/Plugin/AuthClient.pm (-17 / +28 lines)
Lines 20-28 package Koha::Template::Plugin::AuthClient; Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Template::Plugin;
22
use Template::Plugin;
23
use base qw( Template::Plugin );
23
use base      qw( Template::Plugin );
24
use Try::Tiny qw( catch try );
24
25
25
use Koha::Auth::Identity::Providers;
26
use Koha::Auth::Identity::Providers;
27
use Koha::Logger;
26
28
27
=head1 NAME
29
=head1 NAME
28
30
Lines 49-74 sub get_providers { Link Here
49
    $interface = 'staff'
51
    $interface = 'staff'
50
        if $interface eq 'intranet';
52
        if $interface eq 'intranet';
51
53
52
    my $providers =
53
        Koha::Auth::Identity::Providers->search( { "domains.allow_$interface" => 1 }, { prefetch => 'domains' } );
54
    my $base_url = ( $interface eq 'staff' ) ? "/api/v1/oauth/login" : "/api/v1/public/oauth/login";
55
56
    my @urls;
54
    my @urls;
57
55
58
    while ( my $provider = $providers->next ) {
56
    # Handle database upgrade state where schema might be out of sync
57
    try {
58
        my $providers =
59
            Koha::Auth::Identity::Providers->search( { "domains.allow_$interface" => 1 }, { prefetch => 'domains' } );
60
        my $base_url = ( $interface eq 'staff' ) ? "/api/v1/oauth/login" : "/api/v1/public/oauth/login";
61
62
        while ( my $provider = $providers->next ) {
63
64
            my $code = $provider->code;
65
66
            if ( $provider->protocol eq 'OIDC' || $provider->protocol eq 'OAuth' ) {
67
                push @urls,
68
                    {
69
                    code        => $code,
70
                    description => $provider->description,
71
                    icon_url    => $provider->icon_url,
72
                    url         => "$base_url/$code/$interface",
73
                    };
74
            }
75
        }
76
    } catch {
59
77
60
        my $code = $provider->code;
78
        # If database query fails (e.g., during upgrade), return empty array
79
        Koha::Logger->get->warn("AuthClient: Unable to load identity providers (database may need upgrade): $_");
80
        @urls = ();
81
    };
61
82
62
        if ( $provider->protocol eq 'OIDC' || $provider->protocol eq 'OAuth' ) {
63
            push @urls,
64
                {
65
                code        => $code,
66
                description => $provider->description,
67
                icon_url    => $provider->icon_url,
68
                url         => "$base_url/$code/$interface",
69
                };
70
        }
71
    }
72
    return \@urls;
83
    return \@urls;
73
}
84
}
74
85
(-)a/t/db_dependent/Template/Plugin/AuthClient.t (-1 / +86 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, see <https://www.gnu.org/licenses>.
16
17
use Modern::Perl;
18
19
use Test::More tests => 3;
20
use Test::Exception;
21
use Test::MockModule;
22
use Test::NoWarnings;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks::Logger;
26
27
BEGIN {
28
    use_ok('Koha::Template::Plugin::AuthClient');
29
}
30
31
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
my $logger  = t::lib::Mocks::Logger->new();
34
35
subtest 'get_providers() tests' => sub {
36
37
    plan tests => 2;
38
39
    subtest 'Normal operation' => sub {
40
41
        plan tests => 2;
42
43
        $schema->storage->txn_begin;
44
45
        my $plugin = Koha::Template::Plugin::AuthClient->new();
46
        my $providers;
47
48
        lives_ok {
49
            $providers = $plugin->get_providers('staff');
50
        }
51
        'get_providers executes without error';
52
53
        is( ref($providers), 'ARRAY', 'Returns array reference' );
54
55
        $schema->storage->txn_rollback;
56
    };
57
58
    subtest 'Database error handling' => sub {
59
60
        plan tests => 2;
61
62
        $schema->storage->txn_begin;
63
        $logger->clear();
64
65
        # Mock search to simulate database schema mismatch
66
        my $mock = Test::MockModule->new('Koha::Auth::Identity::Providers');
67
        $mock->mock(
68
            'search',
69
            sub {
70
                die "DBD::mysql::st execute failed: Unknown column 'domains.auto_register_opac' in 'field list'";
71
            }
72
        );
73
74
        my $plugin = Koha::Template::Plugin::AuthClient->new();
75
        my $providers;
76
77
        lives_ok {
78
            $providers = $plugin->get_providers('staff');
79
        }
80
        'get_providers handles database errors gracefully';
81
82
        $logger->warn_like( qr/AuthClient: Unable to load identity providers/, 'Logs warning about database issue' );
83
84
        $schema->storage->txn_rollback;
85
    };
86
};

Return to bug 37711