Lines 19-25
Link Here
|
19 |
|
19 |
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
|
21 |
|
22 |
use Test::More tests => 1; |
22 |
use Test::More tests => 5; |
|
|
23 |
|
24 |
use Test::MockModule; |
25 |
use Test::Exception; |
26 |
|
27 |
use JSON qw(encode_json); |
23 |
|
28 |
|
24 |
use Koha::Auth::Providers; |
29 |
use Koha::Auth::Providers; |
25 |
|
30 |
|
Lines 35-50
subtest 'domains() tests' => sub {
Link Here
|
35 |
|
40 |
|
36 |
$schema->storage->txn_begin; |
41 |
$schema->storage->txn_begin; |
37 |
|
42 |
|
38 |
my $provider = $builder->build_object({ class => 'Koha::Auth::Providers' }); |
43 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Providers' } ); |
39 |
my $domains = $provider->domains; |
44 |
my $domains = $provider->domains; |
40 |
|
45 |
|
41 |
is( ref($domains), 'Koha::Auth::Provider::Domains', 'Type is correct' ); |
46 |
is( ref($domains), 'Koha::Auth::Provider::Domains', 'Type is correct' ); |
42 |
is( $domains->count, 0, 'No domains defined' ); |
47 |
is( $domains->count, 0, 'No domains defined' ); |
43 |
|
48 |
|
44 |
$builder->build_object({ class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } }); |
49 |
$builder->build_object( { class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } } ); |
45 |
$builder->build_object({ class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } }); |
50 |
$builder->build_object( { class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } } ); |
46 |
|
51 |
|
47 |
is( $provider->domains->count, 2, 'The provider has 2 domains defined' ); |
52 |
is( $provider->domains->count, 2, 'The provider has 2 domains defined' ); |
48 |
|
53 |
|
49 |
$schema->storage->txn_rollback; |
54 |
$schema->storage->txn_rollback; |
50 |
}; |
55 |
}; |
51 |
- |
56 |
|
|
|
57 |
subtest 'get_config() tests' => sub { |
58 |
|
59 |
plan tests => 2; |
60 |
|
61 |
$schema->storage->txn_begin; |
62 |
|
63 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { config => '{' } } ); |
64 |
|
65 |
throws_ok { $provider->get_config() } |
66 |
'Koha::Exceptions::Object::BadValue', 'Expected exception thrown on bad JSON'; |
67 |
|
68 |
my $config = { some => 'value', and => 'another' }; |
69 |
$provider->config( encode_json($config) )->store; |
70 |
|
71 |
is_deeply( $provider->get_config, $config, 'Config correctly retrieved' ); |
72 |
|
73 |
$schema->storage->txn_rollback; |
74 |
}; |
75 |
|
76 |
subtest 'set_config() tests' => sub { |
77 |
|
78 |
plan tests => 3; |
79 |
|
80 |
$schema->storage->txn_begin; |
81 |
|
82 |
subtest 'OIDC protocol tests' => sub { |
83 |
|
84 |
plan tests => 4; |
85 |
|
86 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { protocol => 'OIDC' } } ); |
87 |
|
88 |
my $config = { |
89 |
key => 'key', |
90 |
secret => 'secret', |
91 |
}; |
92 |
|
93 |
throws_ok { $provider->set_config($config) } |
94 |
'Koha::Exceptions::MissingParameter', 'Exception thrown on missing parameter'; |
95 |
|
96 |
is( $@->error, 'The well_known_url parameter is mandatory', 'Message is correct' ); |
97 |
|
98 |
$config->{well_known_url} = 'https://koha-community.org/auth'; |
99 |
|
100 |
my $return = $provider->set_config($config); |
101 |
is( ref($return), 'Koha::Auth::Provider', 'Return type is correct' ); |
102 |
|
103 |
is_deeply( $provider->get_config, $config, 'Configuration stored correctly' ); |
104 |
}; |
105 |
|
106 |
subtest 'OAuth protocol tests' => sub { |
107 |
|
108 |
plan tests => 4; |
109 |
|
110 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { protocol => 'OAuth' } } ); |
111 |
|
112 |
my $config = { |
113 |
key => 'key', |
114 |
secret => 'secret', |
115 |
token_url => 'https://koha-community.org/auth/token', |
116 |
}; |
117 |
|
118 |
throws_ok { $provider->set_config($config) } |
119 |
'Koha::Exceptions::MissingParameter', 'Exception thrown on missing parameter'; |
120 |
|
121 |
is( $@->error, 'The authorize_url parameter is mandatory', 'Message is correct' ); |
122 |
|
123 |
$config->{authorize_url} = 'https://koha-community.org/auth/authorize'; |
124 |
|
125 |
my $return = $provider->set_config($config); |
126 |
is( ref($return), 'Koha::Auth::Provider', 'Return type is correct' ); |
127 |
|
128 |
is_deeply( $provider->get_config, $config, 'Configuration stored correctly' ); |
129 |
}; |
130 |
|
131 |
subtest 'Unsupported protocol tests' => sub { |
132 |
|
133 |
plan tests => 2; |
134 |
|
135 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { protocol => 'CAS' } } ); |
136 |
|
137 |
throws_ok { $provider->set_config() } |
138 |
'Koha::Exception', 'Exception thrown on unsupported protocol'; |
139 |
|
140 |
like( "$@", qr/Unsupported protocol CAS/, 'Message is correct' ); |
141 |
}; |
142 |
|
143 |
$schema->storage->txn_rollback; |
144 |
}; |
145 |
|
146 |
subtest 'get_mapping() tests' => sub { |
147 |
|
148 |
plan tests => 2; |
149 |
|
150 |
$schema->storage->txn_begin; |
151 |
|
152 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { config => '{' } } ); |
153 |
|
154 |
throws_ok { $provider->get_mapping() } |
155 |
'Koha::Exceptions::Object::BadValue', 'Expected exception thrown on bad JSON'; |
156 |
|
157 |
my $mapping = { some => 'value', and => 'another' }; |
158 |
$provider->mapping( encode_json($mapping) )->store; |
159 |
|
160 |
is_deeply( $provider->get_mapping, $mapping, 'Mapping correctly retrieved' ); |
161 |
|
162 |
$schema->storage->txn_rollback; |
163 |
}; |
164 |
|
165 |
subtest 'set_mapping() tests' => sub { |
166 |
|
167 |
plan tests => 1; |
168 |
|
169 |
$schema->storage->txn_begin; |
170 |
|
171 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Providers' } ); |
172 |
|
173 |
my $mapping = { some => 'value', and => 'another' }; |
174 |
$provider->set_mapping($mapping)->store; |
175 |
|
176 |
is_deeply( $provider->get_mapping, $mapping, 'Mapping correctly retrieved' ); |
177 |
|
178 |
$schema->storage->txn_rollback; |
179 |
}; |