16e273cedd7b37526c7068b29b8a77978ebae4bd
[koha-equinox.git] / t / db_dependent / api / v1 / oauth.t
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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Test::More;
21 use Test::MockModule;
22 use Test::Mojo;
23
24 use Module::Load::Conditional qw(can_load);
25
26 use Koha::ApiKeys;
27 use Koha::Database;
28 use Koha::Patrons;
29
30 use t::lib::Mocks;
31 use t::lib::TestBuilder;
32
33 my $t = Test::Mojo->new('Koha::REST::V1');
34 my $schema  = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new();
36
37 if ( can_load( modules => { 'Net::OAuth2::AuthorizationServer' => undef } ) ) {
38     plan tests => 2;
39 }
40 else {
41     plan skip_all => 'Net::OAuth2::AuthorizationServer not available';
42 }
43
44 subtest '/oauth/token tests' => sub {
45
46     plan tests => 27;
47
48     $schema->storage->txn_begin;
49
50     my $patron = $builder->build_object({
51         class => 'Koha::Patrons',
52         value  => {
53             flags => 0 # no permissions
54         },
55     });
56
57     # Missing parameter grant_type
58     $t->post_ok('/api/v1/oauth/token')
59         ->status_is(400);
60
61     # Wrong grant type
62     $t->post_ok('/api/v1/oauth/token', form => { grant_type => 'password' })
63         ->status_is(400)
64         ->json_is({error => 'Unimplemented grant type'});
65
66     t::lib::Mocks::mock_preference('RESTOAuth2ClientCredentials', 1);
67
68     # No client_id/client_secret
69     $t->post_ok('/api/v1/oauth/token', form => { grant_type => 'client_credentials' })
70         ->status_is(403)
71         ->json_is({error => 'unauthorized_client'});
72
73     my $api_key = Koha::ApiKey->new({ patron_id => $patron->id, description => 'blah' })->store;
74
75     my $formData = {
76         grant_type    => 'client_credentials',
77         client_id     => $api_key->client_id,
78         client_secret => $api_key->secret
79     };
80     $t->post_ok('/api/v1/oauth/token', form => $formData)
81         ->status_is(200)
82         ->json_is('/expires_in' => 3600)
83         ->json_is('/token_type' => 'Bearer')
84         ->json_has('/access_token');
85
86     my $access_token = $t->tx->res->json->{access_token};
87
88     # Without access token, it returns 401
89     $t->get_ok('/api/v1/patrons')->status_is(401);
90
91     # With access token, but without permissions, it returns 403
92     my $tx = $t->ua->build_tx(GET => '/api/v1/patrons');
93     $tx->req->headers->authorization("Bearer $access_token");
94     $t->request_ok($tx)->status_is(403);
95
96     # With access token and permissions, it returns 200
97     $patron->flags(2**4)->store;
98     $tx = $t->ua->build_tx(GET => '/api/v1/patrons');
99     $tx->req->headers->authorization("Bearer $access_token");
100     $t->request_ok($tx)->status_is(200);
101
102     # expire token
103     my $token = Koha::OAuthAccessTokens->find($access_token);
104     $token->expires( time - 1 )->store;
105     $tx = $t->ua->build_tx( GET => '/api/v1/patrons' );
106     $tx->req->headers->authorization("Bearer $access_token");
107     $t->request_ok($tx)
108       ->status_is(401);
109
110     # revoke key
111     $api_key->active(0)->store;
112     $t->post_ok('/api/v1/oauth/token', form => $formData)
113         ->status_is(403)
114         ->json_is({ error => 'unauthorized_client' });
115
116     # disable client credentials grant
117     t::lib::Mocks::mock_preference('RESTOAuth2ClientCredentials', 0);
118
119     # enable API key
120     $api_key->active(1)->store;
121     # Wrong grant type
122     $t->post_ok('/api/v1/oauth/token', form => $formData )
123         ->status_is(400)
124         ->json_is({ error => 'Unimplemented grant type' });
125
126     $schema->storage->txn_rollback;
127 };
128
129 subtest 'Net::OAuth2::AuthorizationServer missing tests' => sub {
130
131     plan tests => 10;
132
133     my $load_conditional = Test::MockModule->new('Module::Load::Conditional');
134
135     # Enable the client credentials grant syspref
136     t::lib::Mocks::mock_preference( 'RESTOAuth2ClientCredentials', 1 );
137
138     my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 2**4 } });
139     my $api_key = Koha::ApiKey->new({ patron_id => $patron->id, description => 'blah' })->store;
140
141     my $form_data = {
142         grant_type    => 'client_credentials',
143         client_id     => $api_key->client_id,
144         client_secret => $api_key->secret
145     };
146
147     $t->post_ok( '/api/v1/oauth/token', form => $form_data )->status_is(200)
148       ->json_is( '/expires_in' => 3600 )->json_is( '/token_type' => 'Bearer' )
149       ->json_has('/access_token');
150
151     my $access_token = $t->tx->res->json->{access_token};
152
153     $load_conditional->mock( 'can_load', sub { return 0; } );
154
155     my $tx = $t->ua->build_tx( GET => '/api/v1/patrons' );
156     $tx->req->headers->authorization("Bearer $access_token");
157     $t->request_ok($tx)
158       ->status_is(403);
159
160     $t->post_ok( '/api/v1/oauth/token', form => $form_data )
161       ->status_is(400)
162       ->json_is( { error => 'Unimplemented grant type' } );
163
164 };