|
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')->status_is(400)->json_is( '/errors/0/message', 'Missing property.' ) |
| 32 |
->json_is( '/errors/0/path', '/grant_type' ); |
| 33 |
|
| 34 |
$t->post_ok( '/api/v1/oauth/token', form => { grant_type => 'client_credentials' } )->status_is(403) |
| 35 |
->json_is( '/error', 'unauthorized_client' ); |
| 36 |
|
| 37 |
my $patron = $builder->build_object( |
| 38 |
{ |
| 39 |
class => 'Koha::Patrons', |
| 40 |
value => { |
| 41 |
flags => 0 # no permissions |
| 42 |
}, |
| 43 |
} |
| 44 |
); |
| 45 |
my $api_key = Koha::ApiKey->new( { patron_id => $patron->id, description => 'blah' } )->store; |
| 46 |
my $client_id = $api_key->client_id; |
| 47 |
my $client_secret = $api_key->plain_text_secret; |
| 48 |
|
| 49 |
$t->post_ok( |
| 50 |
'/api/v1/oauth/token', |
| 51 |
form => { grant_type => 'client_credentials', client_id => $client_id, client_secret => $client_secret } |
| 52 |
)->status_is(200)->json_is( '/expires_in' => 3600 )->json_is( '/token_type' => 'Bearer' ) |
| 53 |
->json_has('/access_token'); |
| 54 |
|
| 55 |
$schema->storage->txn_rollback; |
| 56 |
} |
| 57 |
} |