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

(-)a/t/db_dependent/api/v1/identity_provider_hostnames.t (+417 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 => 12;
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
    };
261
262
    # Unauthorized
263
    $t->post_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname )
264
        ->status_is(403);
265
266
    # Non-existent provider
267
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/999999/hostnames" => json => $new_hostname )
268
        ->status_is(404);
269
270
    # Valid creation using hostname string (find-or-create)
271
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname )
272
        ->status_is(201)
273
        ->json_is( '/hostname',             'add.example.com' )
274
        ->json_is( '/identity_provider_id', $pid )
275
        ->json_has('/identity_provider_hostname_id')
276
        ->header_like( 'Location', qr|/api/v1/auth/identity_providers/$pid/hostnames/\d+| );
277
278
    # Duplicate should return 409 (suppress the expected Koha::Object DuplicateID warn)
279
    {
280
        local $SIG{__WARN__} = sub { };
281
        $t->post_ok(
282
            "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname )
283
            ->status_is(409);
284
    }
285
286
    $schema->storage->txn_rollback;
287
};
288
289
subtest 'add() tests - hostname_id' => sub {
290
291
    plan tests => 6;
292
293
    $schema->storage->txn_begin;
294
295
    my $auth_patron = authorized_patron();
296
    my $auth_userid = $auth_patron->userid;
297
298
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
299
    my $pid      = $provider->identity_provider_id;
300
301
    my $hostname =
302
        $builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'byid.example.com' } } );
303
304
    my $new_hostname = {
305
        hostname_id => $hostname->hostname_id,
306
        is_enabled  => JSON::true,
307
        force_sso   => JSON::false,
308
    };
309
310
    # Valid creation using hostname_id
311
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname )
312
        ->status_is(201)
313
        ->json_is( '/hostname',             'byid.example.com' )
314
        ->json_is( '/hostname_id',          $hostname->hostname_id )
315
        ->json_is( '/identity_provider_id', $pid )
316
        ->json_has('/identity_provider_hostname_id');
317
318
    $schema->storage->txn_rollback;
319
};
320
321
subtest 'update() tests' => sub {
322
323
    plan tests => 9;
324
325
    $schema->storage->txn_begin;
326
327
    my $auth_patron   = authorized_patron();
328
    my $unauth_patron = unauthorized_patron();
329
330
    my $auth_userid   = $auth_patron->userid;
331
    my $unauth_userid = $unauth_patron->userid;
332
333
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
334
    my $pid      = $provider->identity_provider_id;
335
336
    my $hostname =
337
        $builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'update.example.com' } } );
338
339
    my $ph = $builder->build_object(
340
        {
341
            class => 'Koha::Auth::Identity::Provider::Hostnames',
342
            value => {
343
                identity_provider_id => $pid,
344
                hostname_id          => $hostname->hostname_id,
345
                is_enabled           => 1,
346
                force_sso            => 0,
347
            }
348
        }
349
    );
350
    my $phid = $ph->identity_provider_hostname_id;
351
352
    my $update = {
353
        hostname_id => $hostname->hostname_id,
354
        is_enabled  => JSON::false,
355
        force_sso   => JSON::true,
356
    };
357
358
    # Unauthorized
359
    $t->put_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid" => json => $update )
360
        ->status_is(403);
361
362
    # Not found
363
    $t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/999999" => json => $update )
364
        ->status_is(404);
365
366
    # Valid update
367
    $t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid" => json => $update )
368
        ->status_is(200)
369
        ->json_is( '/identity_provider_hostname_id', $phid )
370
        ->json_is( '/is_enabled',                    JSON::false )
371
        ->json_is( '/force_sso',                     JSON::true );
372
373
    $schema->storage->txn_rollback;
374
};
375
376
subtest 'delete() tests' => sub {
377
378
    plan tests => 6;
379
380
    $schema->storage->txn_begin;
381
382
    my $auth_patron   = authorized_patron();
383
    my $unauth_patron = unauthorized_patron();
384
385
    my $auth_userid   = $auth_patron->userid;
386
    my $unauth_userid = $unauth_patron->userid;
387
388
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
389
    my $pid      = $provider->identity_provider_id;
390
391
    my $hostname =
392
        $builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'delete.example.com' } } );
393
394
    my $ph = $builder->build_object(
395
        {
396
            class => 'Koha::Auth::Identity::Provider::Hostnames',
397
            value => {
398
                identity_provider_id => $pid,
399
                hostname_id          => $hostname->hostname_id,
400
                is_enabled           => 1,
401
                force_sso            => 0,
402
            }
403
        }
404
    );
405
    my $phid = $ph->identity_provider_hostname_id;
406
407
    # Unauthorized
408
    $t->delete_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid")->status_is(403);
409
410
    # Not found
411
    $t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/999999")->status_is(404);
412
413
    # Valid deletion
414
    $t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid")->status_is(204);
415
416
    $schema->storage->txn_rollback;
417
};
(-)a/t/db_dependent/api/v1/identity_provider_mappings.t (+298 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
                is_matchpoint        => 0,
107
            }
108
        }
109
    );
110
111
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings")
112
        ->status_is(200)
113
        ->json_has("/0/mapping_id");
114
115
    $schema->storage->txn_rollback;
116
};
117
118
subtest 'get() tests' => sub {
119
120
    plan tests => 10;
121
122
    $schema->storage->txn_begin;
123
124
    my $auth_patron   = authorized_patron();
125
    my $unauth_patron = unauthorized_patron();
126
127
    my $auth_userid   = $auth_patron->userid;
128
    my $unauth_userid = $unauth_patron->userid;
129
130
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
131
    my $pid      = $provider->identity_provider_id;
132
133
    my $mapping = $builder->build_object(
134
        {
135
            class => 'Koha::Auth::Identity::Provider::Mappings',
136
            value => {
137
                identity_provider_id => $pid,
138
                koha_field           => 'email',
139
                provider_field       => 'mail',
140
                is_matchpoint        => 0,
141
            }
142
        }
143
    );
144
    my $mid = $mapping->mapping_id;
145
146
    # Unauthorized
147
    $t->get_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid")->status_is(403);
148
149
    # Non-existent provider
150
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/999999/mappings/$mid")->status_is(404);
151
152
    # Non-existent mapping
153
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/999999")->status_is(404);
154
155
    # Found
156
    $t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid")
157
        ->status_is(200)
158
        ->json_is( '/mapping_id', $mid )
159
        ->json_is( '/koha_field', 'email' );
160
161
    $schema->storage->txn_rollback;
162
};
163
164
subtest 'add() tests' => sub {
165
166
    plan tests => 12;
167
168
    $schema->storage->txn_begin;
169
170
    my $auth_patron   = authorized_patron();
171
    my $unauth_patron = unauthorized_patron();
172
173
    my $auth_userid   = $auth_patron->userid;
174
    my $unauth_userid = $unauth_patron->userid;
175
176
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
177
    my $pid      = $provider->identity_provider_id;
178
179
    my $new_mapping = {
180
        koha_field     => 'email',
181
        provider_field => 'mail',
182
        is_matchpoint  => JSON::false,
183
    };
184
185
    # Unauthorized
186
    $t->post_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings" => json => $new_mapping )
187
        ->status_is(403);
188
189
    # Non-existent provider
190
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/999999/mappings" => json => $new_mapping )
191
        ->status_is(404);
192
193
    # Missing required field
194
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings" => json =>
195
            { is_matchpoint => JSON::false } )->status_is(400);
196
197
    # Valid creation
198
    $t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings" => json => $new_mapping )
199
        ->status_is(201)
200
        ->json_is( '/koha_field',           'email' )
201
        ->json_is( '/identity_provider_id', $pid )
202
        ->json_has('/mapping_id')
203
        ->header_like( 'Location', qr|/api/v1/auth/identity_providers/$pid/mappings/\d+| );
204
205
    $schema->storage->txn_rollback;
206
};
207
208
subtest 'update() tests' => sub {
209
210
    plan tests => 9;
211
212
    $schema->storage->txn_begin;
213
214
    my $auth_patron   = authorized_patron();
215
    my $unauth_patron = unauthorized_patron();
216
217
    my $auth_userid   = $auth_patron->userid;
218
    my $unauth_userid = $unauth_patron->userid;
219
220
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
221
    my $pid      = $provider->identity_provider_id;
222
223
    my $mapping = $builder->build_object(
224
        {
225
            class => 'Koha::Auth::Identity::Provider::Mappings',
226
            value => {
227
                identity_provider_id => $pid,
228
                koha_field           => 'email',
229
                provider_field       => 'mail',
230
                is_matchpoint        => 0,
231
            }
232
        }
233
    );
234
    my $mid = $mapping->mapping_id;
235
236
    my $update = {
237
        koha_field     => 'surname',
238
        provider_field => 'family_name',
239
        is_matchpoint  => JSON::false,
240
    };
241
242
    # Unauthorized
243
    $t->put_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid" => json => $update )
244
        ->status_is(403);
245
246
    # Not found
247
    $t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/999999" => json => $update )
248
        ->status_is(404);
249
250
    # Valid update
251
    $t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid" => json => $update )
252
        ->status_is(200)
253
        ->json_is( '/mapping_id',     $mid )
254
        ->json_is( '/koha_field',     'surname' )
255
        ->json_is( '/provider_field', 'family_name' );
256
257
    $schema->storage->txn_rollback;
258
};
259
260
subtest 'delete() tests' => sub {
261
262
    plan tests => 6;
263
264
    $schema->storage->txn_begin;
265
266
    my $auth_patron   = authorized_patron();
267
    my $unauth_patron = unauthorized_patron();
268
269
    my $auth_userid   = $auth_patron->userid;
270
    my $unauth_userid = $unauth_patron->userid;
271
272
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
273
    my $pid      = $provider->identity_provider_id;
274
275
    my $mapping = $builder->build_object(
276
        {
277
            class => 'Koha::Auth::Identity::Provider::Mappings',
278
            value => {
279
                identity_provider_id => $pid,
280
                koha_field           => 'email',
281
                provider_field       => 'mail',
282
                is_matchpoint        => 0,
283
            }
284
        }
285
    );
286
    my $mid = $mapping->mapping_id;
287
288
    # Unauthorized
289
    $t->delete_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid")->status_is(403);
290
291
    # Not found
292
    $t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/999999")->status_is(404);
293
294
    # Valid deletion
295
    $t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/mappings/$mid")->status_is(204);
296
297
    $schema->storage->txn_rollback;
298
};
(-)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