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

(-)a/t/db_dependent/api/v1/identity_provider_hostnames.t (+421 lines)
Line 0 Link Here
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::NoWarnings;
21
use Test::More tests => 9;
22
use Test::Mojo;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use JSON;
28
29
use Koha::Auth::Hostname;
30
use Koha::Auth::Hostnames;
31
use Koha::Auth::Identity::Provider::Hostnames;
32
use Koha::Auth::Identity::Providers;
33
use Koha::Database;
34
35
my $schema  = Koha::Database->new->schema;
36
my $builder = t::lib::TestBuilder->new;
37
38
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
39
40
my $t = Test::Mojo->new('Koha::REST::V1');
41
42
my $password = 'thePassword123';
43
44
sub authorized_patron {
45
    my $patron = $builder->build_object(
46
        {
47
            class => 'Koha::Patrons',
48
            value => { flags => 0 }
49
        }
50
    );
51
    $builder->build(
52
        {
53
            source => 'UserPermission',
54
            value  => {
55
                borrowernumber => $patron->borrowernumber,
56
                module_bit     => 3,
57
                code           => 'manage_identity_providers',
58
            },
59
        }
60
    );
61
    $patron->set_password( { password => $password, skip_validation => 1 } );
62
    return $patron;
63
}
64
65
sub unauthorized_patron {
66
    my $patron = $builder->build_object(
67
        {
68
            class => 'Koha::Patrons',
69
            value => { flags => 0 }
70
        }
71
    );
72
    $patron->set_password( { password => $password, skip_validation => 1 } );
73
    return $patron;
74
}
75
76
subtest 'list() tests for /auth/hostnames' => sub {
77
78
    plan tests => 8;
79
80
    $schema->storage->txn_begin;
81
82
    Koha::Auth::Hostnames->search->delete;
83
84
    my $auth_patron   = authorized_patron();
85
    my $unauth_patron = unauthorized_patron();
86
87
    my $auth_userid   = $auth_patron->userid;
88
    my $unauth_userid = $unauth_patron->userid;
89
90
    # Unauthorized
91
    $t->get_ok("//$unauth_userid:$password@/api/v1/auth/hostnames")->status_is(403);
92
93
    # Empty list
94
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/hostnames")->status_is(200)->json_is( [] );
95
96
    # With a hostname
97
    $builder->build_object(
98
        {
99
            class => 'Koha::Auth::Hostnames',
100
            value => { hostname => 'library.example.com' }
101
        }
102
    );
103
104
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/hostnames")->status_is(200)->json_has("/0/hostname_id");
105
106
    $schema->storage->txn_rollback;
107
};
108
109
subtest 'get() tests for /auth/hostnames/{hostname_id}' => sub {
110
111
    plan tests => 8;
112
113
    $schema->storage->txn_begin;
114
115
    my $auth_patron   = authorized_patron();
116
    my $unauth_patron = unauthorized_patron();
117
118
    my $auth_userid   = $auth_patron->userid;
119
    my $unauth_userid = $unauth_patron->userid;
120
121
    my $hostname = $builder->build_object(
122
        {
123
            class => 'Koha::Auth::Hostnames',
124
            value => { hostname => 'lib.example.org' }
125
        }
126
    );
127
    my $hid = $hostname->hostname_id;
128
129
    # Unauthorized
130
    $t->get_ok("//$unauth_userid:$password@/api/v1/auth/hostnames/$hid")->status_is(403);
131
132
    # Not found
133
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/hostnames/999999")->status_is(404);
134
135
    # Found
136
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/hostnames/$hid")
137
        ->status_is(200)
138
        ->json_is( '/hostname_id', $hid )
139
        ->json_is( '/hostname',    'lib.example.org' );
140
141
    $schema->storage->txn_rollback;
142
};
143
144
subtest 'list() tests for /auth/identity_providers/{id}/hostnames' => sub {
145
146
    plan tests => 10;
147
148
    $schema->storage->txn_begin;
149
150
    my $auth_patron   = authorized_patron();
151
    my $unauth_patron = unauthorized_patron();
152
153
    my $auth_userid   = $auth_patron->userid;
154
    my $unauth_userid = $unauth_patron->userid;
155
156
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
157
    my $pid      = $provider->identity_provider_id;
158
159
    # Unauthorized
160
    $t->get_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames")->status_is(403);
161
162
    # Non-existent provider
163
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/999999/hostnames")->status_is(404);
164
165
    # Empty list
166
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames")
167
        ->status_is(200)
168
        ->json_is( [] );
169
170
    # With a hostname association
171
    my $hostname =
172
        $builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'provider.example.com' } } );
173
    $builder->build_object(
174
        {
175
            class => 'Koha::Auth::Identity::Provider::Hostnames',
176
            value => {
177
                identity_provider_id => $pid,
178
                hostname_id          => $hostname->hostname_id,
179
                is_enabled           => 1,
180
                force_sso            => 0,
181
            }
182
        }
183
    );
184
185
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames")
186
        ->status_is(200)
187
        ->json_has("/0/identity_provider_hostname_id");
188
189
    $schema->storage->txn_rollback;
190
};
191
192
subtest 'get() tests for /auth/identity_providers/{id}/hostnames/{hid}' => sub {
193
194
    plan tests => 10;
195
196
    $schema->storage->txn_begin;
197
198
    my $auth_patron   = authorized_patron();
199
    my $unauth_patron = unauthorized_patron();
200
201
    my $auth_userid   = $auth_patron->userid;
202
    my $unauth_userid = $unauth_patron->userid;
203
204
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
205
    my $pid      = $provider->identity_provider_id;
206
207
    my $hostname =
208
        $builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'get.example.com' } } );
209
210
    my $ph = $builder->build_object(
211
        {
212
            class => 'Koha::Auth::Identity::Provider::Hostnames',
213
            value => {
214
                identity_provider_id => $pid,
215
                hostname_id          => $hostname->hostname_id,
216
                is_enabled           => 1,
217
                force_sso            => 0,
218
            }
219
        }
220
    );
221
    my $phid = $ph->identity_provider_hostname_id;
222
223
    # Unauthorized
224
    $t->get_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid")->status_is(403);
225
226
    # Non-existent provider
227
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/999999/hostnames/$phid")->status_is(404);
228
229
    # Non-existent hostname association
230
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/999999")->status_is(404);
231
232
    # Found
233
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid")
234
        ->status_is(200)
235
        ->json_is( '/identity_provider_hostname_id', $phid )
236
        ->json_is( '/hostname',                      'get.example.com' );
237
238
    $schema->storage->txn_rollback;
239
};
240
241
subtest 'add() tests - hostname string' => sub {
242
243
    plan tests => 13;
244
245
    $schema->storage->txn_begin;
246
247
    my $auth_patron   = authorized_patron();
248
    my $unauth_patron = unauthorized_patron();
249
250
    my $auth_userid   = $auth_patron->userid;
251
    my $unauth_userid = $unauth_patron->userid;
252
253
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
254
    my $pid      = $provider->identity_provider_id;
255
256
    my $new_hostname = {
257
        hostname   => 'add.example.com',
258
        is_enabled => JSON::true,
259
        force_sso  => JSON::false,
260
        matchpoint => 'userid',
261
    };
262
263
    # Unauthorized
264
    $t->post_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname )
265
        ->status_is(403);
266
267
    # Non-existent provider
268
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/999999/hostnames" => json => $new_hostname )
269
        ->status_is(404);
270
271
    # Valid creation using hostname string (find-or-create)
272
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname )
273
        ->status_is(201)
274
        ->json_is( '/hostname',             'add.example.com' )
275
        ->json_is( '/identity_provider_id', $pid )
276
        ->json_is( '/matchpoint',           'userid' )
277
        ->json_has('/identity_provider_hostname_id')
278
        ->header_like( 'Location', qr|/api/v1/auth/identity_providers/$pid/hostnames/\d+| );
279
280
    # Duplicate should return 409 (suppress the expected Koha::Object DuplicateID warn)
281
    {
282
        local $SIG{__WARN__} = sub { };
283
        $t->post_ok(
284
            "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname )
285
            ->status_is(409);
286
    }
287
288
    $schema->storage->txn_rollback;
289
};
290
291
subtest 'add() tests - hostname_id' => sub {
292
293
    plan tests => 6;
294
295
    $schema->storage->txn_begin;
296
297
    my $auth_patron = authorized_patron();
298
    my $auth_userid = $auth_patron->userid;
299
300
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
301
    my $pid      = $provider->identity_provider_id;
302
303
    my $hostname =
304
        $builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'byid.example.com' } } );
305
306
    my $new_hostname = {
307
        hostname_id => $hostname->hostname_id,
308
        is_enabled  => JSON::true,
309
        force_sso   => JSON::false,
310
    };
311
312
    # Valid creation using hostname_id
313
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname )
314
        ->status_is(201)
315
        ->json_is( '/hostname',             'byid.example.com' )
316
        ->json_is( '/hostname_id',          $hostname->hostname_id )
317
        ->json_is( '/identity_provider_id', $pid )
318
        ->json_has('/identity_provider_hostname_id');
319
320
    $schema->storage->txn_rollback;
321
};
322
323
subtest 'update() tests' => sub {
324
325
    plan tests => 10;
326
327
    $schema->storage->txn_begin;
328
329
    my $auth_patron   = authorized_patron();
330
    my $unauth_patron = unauthorized_patron();
331
332
    my $auth_userid   = $auth_patron->userid;
333
    my $unauth_userid = $unauth_patron->userid;
334
335
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
336
    my $pid      = $provider->identity_provider_id;
337
338
    my $hostname =
339
        $builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'update.example.com' } } );
340
341
    my $ph = $builder->build_object(
342
        {
343
            class => 'Koha::Auth::Identity::Provider::Hostnames',
344
            value => {
345
                identity_provider_id => $pid,
346
                hostname_id          => $hostname->hostname_id,
347
                is_enabled           => 1,
348
                force_sso            => 0,
349
            }
350
        }
351
    );
352
    my $phid = $ph->identity_provider_hostname_id;
353
354
    my $update = {
355
        hostname_id => $hostname->hostname_id,
356
        is_enabled  => JSON::false,
357
        force_sso   => JSON::true,
358
        matchpoint  => 'userid',
359
    };
360
361
    # Unauthorized
362
    $t->put_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid" => json => $update )
363
        ->status_is(403);
364
365
    # Not found
366
    $t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/999999" => json => $update )
367
        ->status_is(404);
368
369
    # Valid update
370
    $t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid" => json => $update )
371
        ->status_is(200)
372
        ->json_is( '/identity_provider_hostname_id', $phid )
373
        ->json_is( '/is_enabled',                    JSON::false )
374
        ->json_is( '/force_sso',                     JSON::true )
375
        ->json_is( '/matchpoint',                    'userid' );
376
377
    $schema->storage->txn_rollback;
378
};
379
380
subtest 'delete() tests' => sub {
381
382
    plan tests => 6;
383
384
    $schema->storage->txn_begin;
385
386
    my $auth_patron   = authorized_patron();
387
    my $unauth_patron = unauthorized_patron();
388
389
    my $auth_userid   = $auth_patron->userid;
390
    my $unauth_userid = $unauth_patron->userid;
391
392
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
393
    my $pid      = $provider->identity_provider_id;
394
395
    my $hostname =
396
        $builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'delete.example.com' } } );
397
398
    my $ph = $builder->build_object(
399
        {
400
            class => 'Koha::Auth::Identity::Provider::Hostnames',
401
            value => {
402
                identity_provider_id => $pid,
403
                hostname_id          => $hostname->hostname_id,
404
                is_enabled           => 1,
405
                force_sso            => 0,
406
            }
407
        }
408
    );
409
    my $phid = $ph->identity_provider_hostname_id;
410
411
    # Unauthorized
412
    $t->delete_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid")->status_is(403);
413
414
    # Not found
415
    $t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/999999")->status_is(404);
416
417
    # Valid deletion
418
    $t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid")->status_is(204);
419
420
    $schema->storage->txn_rollback;
421
};
(-)a/t/db_dependent/api/v1/identity_provider_mappings.t (+292 lines)
Line 0 Link Here
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::NoWarnings;
21
use Test::More tests => 6;
22
use Test::Mojo;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use JSON;
28
29
use Koha::Auth::Identity::Providers;
30
use Koha::Auth::Identity::Provider::Mappings;
31
use Koha::Database;
32
33
my $schema  = Koha::Database->new->schema;
34
my $builder = t::lib::TestBuilder->new;
35
36
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37
38
my $t = Test::Mojo->new('Koha::REST::V1');
39
40
my $password = 'thePassword123';
41
42
sub authorized_patron {
43
    my $patron = $builder->build_object(
44
        {
45
            class => 'Koha::Patrons',
46
            value => { flags => 0 }
47
        }
48
    );
49
    $builder->build(
50
        {
51
            source => 'UserPermission',
52
            value  => {
53
                borrowernumber => $patron->borrowernumber,
54
                module_bit     => 3,
55
                code           => 'manage_identity_providers',
56
            },
57
        }
58
    );
59
    $patron->set_password( { password => $password, skip_validation => 1 } );
60
    return $patron;
61
}
62
63
sub unauthorized_patron {
64
    my $patron = $builder->build_object(
65
        {
66
            class => 'Koha::Patrons',
67
            value => { flags => 0 }
68
        }
69
    );
70
    $patron->set_password( { password => $password, skip_validation => 1 } );
71
    return $patron;
72
}
73
74
subtest 'list() tests' => sub {
75
76
    plan tests => 10;
77
78
    $schema->storage->txn_begin;
79
80
    my $auth_patron   = authorized_patron();
81
    my $unauth_patron = unauthorized_patron();
82
83
    my $auth_userid   = $auth_patron->userid;
84
    my $unauth_userid = $unauth_patron->userid;
85
86
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
87
    my $pid      = $provider->identity_provider_id;
88
89
    # Unauthorized
90
    $t->get_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings")->status_is(403);
91
92
    # Non-existent provider
93
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/999999/mappings")->status_is(404);
94
95
    # Empty list
96
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings")->status_is(200)->json_is( [] );
97
98
    # With a mapping
99
    $builder->build_object(
100
        {
101
            class => 'Koha::Auth::Identity::Provider::Mappings',
102
            value => {
103
                identity_provider_id => $pid,
104
                koha_field           => 'email',
105
                provider_field       => 'mail',
106
            }
107
        }
108
    );
109
110
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings")
111
        ->status_is(200)
112
        ->json_has("/0/mapping_id");
113
114
    $schema->storage->txn_rollback;
115
};
116
117
subtest 'get() tests' => sub {
118
119
    plan tests => 10;
120
121
    $schema->storage->txn_begin;
122
123
    my $auth_patron   = authorized_patron();
124
    my $unauth_patron = unauthorized_patron();
125
126
    my $auth_userid   = $auth_patron->userid;
127
    my $unauth_userid = $unauth_patron->userid;
128
129
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
130
    my $pid      = $provider->identity_provider_id;
131
132
    my $mapping = $builder->build_object(
133
        {
134
            class => 'Koha::Auth::Identity::Provider::Mappings',
135
            value => {
136
                identity_provider_id => $pid,
137
                koha_field           => 'email',
138
                provider_field       => 'mail',
139
            }
140
        }
141
    );
142
    my $mid = $mapping->mapping_id;
143
144
    # Unauthorized
145
    $t->get_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid")->status_is(403);
146
147
    # Non-existent provider
148
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/999999/mappings/$mid")->status_is(404);
149
150
    # Non-existent mapping
151
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/999999")->status_is(404);
152
153
    # Found
154
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid")
155
        ->status_is(200)
156
        ->json_is( '/mapping_id', $mid )
157
        ->json_is( '/koha_field', 'email' );
158
159
    $schema->storage->txn_rollback;
160
};
161
162
subtest 'add() tests' => sub {
163
164
    plan tests => 12;
165
166
    $schema->storage->txn_begin;
167
168
    my $auth_patron   = authorized_patron();
169
    my $unauth_patron = unauthorized_patron();
170
171
    my $auth_userid   = $auth_patron->userid;
172
    my $unauth_userid = $unauth_patron->userid;
173
174
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
175
    my $pid      = $provider->identity_provider_id;
176
177
    my $new_mapping = {
178
        koha_field     => 'email',
179
        provider_field => 'mail',
180
    };
181
182
    # Unauthorized
183
    $t->post_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings" => json => $new_mapping )
184
        ->status_is(403);
185
186
    # Non-existent provider
187
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/999999/mappings" => json => $new_mapping )
188
        ->status_is(404);
189
190
    # Missing required field
191
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings" => json =>
192
            { provider_field => 'mail' } )->status_is(400);
193
194
    # Valid creation
195
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings" => json => $new_mapping )
196
        ->status_is(201)
197
        ->json_is( '/koha_field',           'email' )
198
        ->json_is( '/identity_provider_id', $pid )
199
        ->json_has('/mapping_id')
200
        ->header_like( 'Location', qr|/api/v1/auth/identity_providers/$pid/mappings/\d+| );
201
202
    $schema->storage->txn_rollback;
203
};
204
205
subtest 'update() tests' => sub {
206
207
    plan tests => 9;
208
209
    $schema->storage->txn_begin;
210
211
    my $auth_patron   = authorized_patron();
212
    my $unauth_patron = unauthorized_patron();
213
214
    my $auth_userid   = $auth_patron->userid;
215
    my $unauth_userid = $unauth_patron->userid;
216
217
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
218
    my $pid      = $provider->identity_provider_id;
219
220
    my $mapping = $builder->build_object(
221
        {
222
            class => 'Koha::Auth::Identity::Provider::Mappings',
223
            value => {
224
                identity_provider_id => $pid,
225
                koha_field           => 'email',
226
                provider_field       => 'mail',
227
            }
228
        }
229
    );
230
    my $mid = $mapping->mapping_id;
231
232
    my $update = {
233
        koha_field     => 'surname',
234
        provider_field => 'family_name',
235
    };
236
237
    # Unauthorized
238
    $t->put_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid" => json => $update )
239
        ->status_is(403);
240
241
    # Not found
242
    $t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/999999" => json => $update )
243
        ->status_is(404);
244
245
    # Valid update
246
    $t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid" => json => $update )
247
        ->status_is(200)
248
        ->json_is( '/mapping_id',     $mid )
249
        ->json_is( '/koha_field',     'surname' )
250
        ->json_is( '/provider_field', 'family_name' );
251
252
    $schema->storage->txn_rollback;
253
};
254
255
subtest 'delete() tests' => sub {
256
257
    plan tests => 6;
258
259
    $schema->storage->txn_begin;
260
261
    my $auth_patron   = authorized_patron();
262
    my $unauth_patron = unauthorized_patron();
263
264
    my $auth_userid   = $auth_patron->userid;
265
    my $unauth_userid = $unauth_patron->userid;
266
267
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
268
    my $pid      = $provider->identity_provider_id;
269
270
    my $mapping = $builder->build_object(
271
        {
272
            class => 'Koha::Auth::Identity::Provider::Mappings',
273
            value => {
274
                identity_provider_id => $pid,
275
                koha_field           => 'email',
276
                provider_field       => 'mail',
277
            }
278
        }
279
    );
280
    my $mid = $mapping->mapping_id;
281
282
    # Unauthorized
283
    $t->delete_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid")->status_is(403);
284
285
    # Not found
286
    $t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/999999")->status_is(404);
287
288
    # Valid deletion
289
    $t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid")->status_is(204);
290
291
    $schema->storage->txn_rollback;
292
};
(-)a/t/db_dependent/api/v1/identity_providers.t (-1 / +273 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::NoWarnings;
21
use Test::More tests => 6;
22
use Test::Mojo;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use JSON;
28
29
use Koha::Auth::Identity::Providers;
30
use Koha::Database;
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37
my $t = Test::Mojo->new('Koha::REST::V1');
38
39
my $password = 'thePassword123';
40
41
sub authorized_patron {
42
    my $patron = $builder->build_object(
43
        {
44
            class => 'Koha::Patrons',
45
            value => { flags => 0 }
46
        }
47
    );
48
    $builder->build(
49
        {
50
            source => 'UserPermission',
51
            value  => {
52
                borrowernumber => $patron->borrowernumber,
53
                module_bit     => 3,
54
                code           => 'manage_identity_providers',
55
            },
56
        }
57
    );
58
    $patron->set_password( { password => $password, skip_validation => 1 } );
59
    return $patron;
60
}
61
62
sub unauthorized_patron {
63
    my $patron = $builder->build_object(
64
        {
65
            class => 'Koha::Patrons',
66
            value => { flags => 0 }
67
        }
68
    );
69
    $patron->set_password( { password => $password, skip_validation => 1 } );
70
    return $patron;
71
}
72
73
my $valid_oidc_config =
74
    '{"key":"client_key","secret":"client_secret","well_known_url":"https://example.com/.well-known/openid-configuration"}';
75
76
subtest 'list() tests' => sub {
77
78
    plan tests => 8;
79
80
    $schema->storage->txn_begin;
81
82
    Koha::Auth::Identity::Providers->search->delete;
83
84
    my $auth_patron   = authorized_patron();
85
    my $unauth_patron = unauthorized_patron();
86
87
    my $auth_userid   = $auth_patron->userid;
88
    my $unauth_userid = $unauth_patron->userid;
89
90
    # Unauthorized
91
    $t->get_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers")->status_is(403);
92
93
    # Empty list
94
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers")->status_is(200)->json_is( [] );
95
96
    # With a provider
97
    $builder->build_object(
98
        {
99
            class => 'Koha::Auth::Identity::Providers',
100
            value => { protocol => 'OIDC', config => $valid_oidc_config }
101
        }
102
    );
103
104
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers")
105
        ->status_is(200)
106
        ->json_has("/0/identity_provider_id");
107
108
    $schema->storage->txn_rollback;
109
};
110
111
subtest 'get() tests' => sub {
112
113
    plan tests => 7;
114
115
    $schema->storage->txn_begin;
116
117
    my $auth_patron   = authorized_patron();
118
    my $unauth_patron = unauthorized_patron();
119
120
    my $auth_userid   = $auth_patron->userid;
121
    my $unauth_userid = $unauth_patron->userid;
122
123
    my $provider = $builder->build_object(
124
        {
125
            class => 'Koha::Auth::Identity::Providers',
126
            value => { protocol => 'OIDC', config => $valid_oidc_config }
127
        }
128
    );
129
    my $id = $provider->identity_provider_id;
130
131
    # Unauthorized
132
    $t->get_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$id")->status_is(403);
133
134
    # Not found
135
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/999999")->status_is(404);
136
137
    # Found
138
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$id")
139
        ->status_is(200)
140
        ->json_is( '/identity_provider_id', $id );
141
142
    $schema->storage->txn_rollback;
143
};
144
145
subtest 'add() tests' => sub {
146
147
    plan tests => 12;
148
149
    $schema->storage->txn_begin;
150
151
    my $auth_patron   = authorized_patron();
152
    my $unauth_patron = unauthorized_patron();
153
154
    my $auth_userid   = $auth_patron->userid;
155
    my $unauth_userid = $unauth_patron->userid;
156
157
    my $new_provider = {
158
        code        => 'test_oidc',
159
        description => 'Test OIDC Provider',
160
        protocol    => 'OIDC',
161
        config      => {
162
            key            => 'client_key',
163
            secret         => 'client_secret',
164
            well_known_url => 'https://example.com/.well-known/openid-configuration',
165
        },
166
        enabled => JSON::true,
167
    };
168
169
    # Unauthorized
170
    $t->post_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers" => json => $new_provider )
171
        ->status_is(403);
172
173
    # Missing required field (no config)
174
    my $bad_provider = { code => 'bad', protocol => 'OIDC' };
175
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers" => json => $bad_provider )->status_is(400);
176
177
    # Invalid protocol (caught by OpenAPI validation)
178
    my $invalid_protocol_provider = { %$new_provider, protocol => 'INVALID', code => 'inv' };
179
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers" => json => $invalid_protocol_provider )
180
        ->status_is(400);
181
182
    # Valid creation
183
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers" => json => $new_provider )
184
        ->status_is(201)
185
        ->json_is( '/code',     'test_oidc' )
186
        ->json_is( '/protocol', 'OIDC' )
187
        ->json_has('/identity_provider_id')
188
        ->header_like( 'Location', qr|/api/v1/auth/identity_providers/\d+| );
189
190
    $schema->storage->txn_rollback;
191
};
192
193
subtest 'update() tests' => sub {
194
195
    plan tests => 8;
196
197
    $schema->storage->txn_begin;
198
199
    my $auth_patron   = authorized_patron();
200
    my $unauth_patron = unauthorized_patron();
201
202
    my $auth_userid   = $auth_patron->userid;
203
    my $unauth_userid = $unauth_patron->userid;
204
205
    my $provider = $builder->build_object(
206
        {
207
            class => 'Koha::Auth::Identity::Providers',
208
            value => {
209
                protocol => 'OIDC',
210
                config   => $valid_oidc_config,
211
            }
212
        }
213
    );
214
    my $id = $provider->identity_provider_id;
215
216
    my $update = {
217
        code        => $provider->code,
218
        description => 'Updated description',
219
        protocol    => 'OIDC',
220
        config      => {
221
            key            => 'k',
222
            secret         => 's',
223
            well_known_url => 'https://example.com/.well-known/openid-configuration',
224
        },
225
        enabled => JSON::true,
226
    };
227
228
    # Unauthorized
229
    $t->put_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers/$id" => json => $update )->status_is(403);
230
231
    # Not found
232
    $t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/999999" => json => $update )->status_is(404);
233
234
    # Valid update
235
    $t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$id" => json => $update )
236
        ->status_is(200)
237
        ->json_is( '/identity_provider_id', $id )
238
        ->json_is( '/description',          'Updated description' );
239
240
    $schema->storage->txn_rollback;
241
};
242
243
subtest 'delete() tests' => sub {
244
245
    plan tests => 6;
246
247
    $schema->storage->txn_begin;
248
249
    my $auth_patron   = authorized_patron();
250
    my $unauth_patron = unauthorized_patron();
251
252
    my $auth_userid   = $auth_patron->userid;
253
    my $unauth_userid = $unauth_patron->userid;
254
255
    my $provider = $builder->build_object(
256
        {
257
            class => 'Koha::Auth::Identity::Providers',
258
            value => { protocol => 'OIDC', config => $valid_oidc_config }
259
        }
260
    );
261
    my $id = $provider->identity_provider_id;
262
263
    # Unauthorized
264
    $t->delete_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$id")->status_is(403);
265
266
    # Not found
267
    $t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/999999")->status_is(404);
268
269
    # Valid deletion
270
    $t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$id")->status_is(204);
271
272
    $schema->storage->txn_rollback;
273
};

Return to bug 39224