Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
use Modern::Perl; |
4 |
|
5 |
use Test::More tests => 2; |
6 |
use Test::Mojo; |
7 |
|
8 |
use Koha::Database; |
9 |
|
10 |
use t::lib::Mocks; |
11 |
use t::lib::TestBuilder; |
12 |
|
13 |
my $schema = Koha::Database->schema; |
14 |
my $builder = t::lib::TestBuilder->new(); |
15 |
|
16 |
t::lib::Mocks::mock_preference('RESTOAuth2ClientCredentials', 1); |
17 |
|
18 |
subtest 'REST Authentication - Intranet' => rest_auth_subtest('Koha::App::Intranet'); |
19 |
subtest 'REST Authentication - OPAC' => rest_auth_subtest('Koha::App::Opac'); |
20 |
|
21 |
sub rest_auth_subtest { |
22 |
my $app = shift; |
23 |
|
24 |
return sub { |
25 |
plan tests => 12; |
26 |
|
27 |
$schema->storage->txn_begin; |
28 |
|
29 |
my $t = Test::Mojo->new($app); |
30 |
|
31 |
$t->post_ok('/api/v1/oauth/token') |
32 |
->status_is(400) |
33 |
->json_is('/errors/0/message', 'Missing property.') |
34 |
->json_is('/errors/0/path', '/grant_type') |
35 |
; |
36 |
|
37 |
$t->post_ok('/api/v1/oauth/token', form => { grant_type => 'client_credentials' }) |
38 |
->status_is(403) |
39 |
->json_is('/error', 'unauthorized_client') |
40 |
; |
41 |
|
42 |
my $patron = $builder->build_object({ |
43 |
class => 'Koha::Patrons', |
44 |
value => { |
45 |
flags => 0 # no permissions |
46 |
}, |
47 |
}); |
48 |
my $api_key = Koha::ApiKey->new({ patron_id => $patron->id, description => 'blah' })->store; |
49 |
my $client_id = $api_key->client_id; |
50 |
my $client_secret = $api_key->plain_text_secret; |
51 |
|
52 |
$t->post_ok('/api/v1/oauth/token', form => { grant_type => 'client_credentials', client_id => $client_id, client_secret => $client_secret }) |
53 |
->status_is(200) |
54 |
->json_is('/expires_in' => 3600) |
55 |
->json_is('/token_type' => 'Bearer') |
56 |
->json_has('/access_token'); |
57 |
; |
58 |
|
59 |
$schema->storage->txn_rollback; |
60 |
} |
61 |
} |