Bugzilla – Attachment 73086 Details for
Bug 20402
OAuth2 client credentials grant for REST API
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 20402: Add unit tests
Bug-20402-Add-unit-tests.patch (text/plain), 5.80 KB, created by
Julian Maurice
on 2018-03-19 09:47:37 UTC
(
hide
)
Description:
Bug 20402: Add unit tests
Filename:
MIME Type:
Creator:
Julian Maurice
Created:
2018-03-19 09:47:37 UTC
Size:
5.80 KB
patch
obsolete
>From 1b30fa231447dc99319521190c046f76959e11be Mon Sep 17 00:00:00 2001 >From: Julian Maurice <julian.maurice@biblibre.com> >Date: Mon, 19 Mar 2018 10:32:20 +0100 >Subject: [PATCH] Bug 20402: Add unit tests > >Also, avoid some Perl warnings about undefined variables, and check that >grant_type parameter is set correctly > >Test plan: >1. prove t/db_dependent/api/v1/oauth.t >--- > Koha/OAuth.pm | 2 ++ > Koha/REST/V1/OAuth.pm | 10 ++++-- > api/v1/swagger/paths/oauth.json | 12 +++++++ > t/db_dependent/api/v1/oauth.t | 79 +++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 101 insertions(+), 2 deletions(-) > create mode 100755 t/db_dependent/api/v1/oauth.t > >diff --git a/Koha/OAuth.pm b/Koha/OAuth.pm >index 24546bd9b3..b984226b22 100644 >--- a/Koha/OAuth.pm >+++ b/Koha/OAuth.pm >@@ -21,6 +21,8 @@ sub _verify_client_cb { > my ($client_id, $scopes_ref, $client_secret) > = @args{ qw/ client_id scopes client_secret / }; > >+ return (0, 'unauthorized_client') unless $client_id; >+ > my $clients = C4::Context->config('api_client'); > $clients = [ $clients ] unless ref $clients eq 'ARRAY'; > my ($client) = grep { $_->{client_id} eq $client_id } @$clients; >diff --git a/Koha/REST/V1/OAuth.pm b/Koha/REST/V1/OAuth.pm >index d9e84386d8..78a2d1e5b0 100644 >--- a/Koha/REST/V1/OAuth.pm >+++ b/Koha/REST/V1/OAuth.pm >@@ -10,10 +10,16 @@ use C4::Context; > sub token { > my $c = shift->openapi->valid_input or return; > >- my $grant = Koha::OAuth->grant; >+ my $grant_type = $c->validation->param('grant_type'); >+ unless ($grant_type eq 'client_credentials') { >+ return $c->render(status => 400, openapi => {error => 'Unimplemented grant type'}); >+ } >+ > my $client_id = $c->validation->param('client_id'); > my $client_secret = $c->validation->param('client_secret'); >- my $scope = [ split / /, $c->validation->param('scope') ]; >+ my $scope = [ split / /, ($c->validation->param('scope') // '') ]; >+ >+ my $grant = Koha::OAuth->grant; > > # verify a client against known clients > my ( $is_valid, $error, $scopes ) = $grant->verify_client( >diff --git a/api/v1/swagger/paths/oauth.json b/api/v1/swagger/paths/oauth.json >index 5f4d16e42b..f2c0c5a718 100644 >--- a/api/v1/swagger/paths/oauth.json >+++ b/api/v1/swagger/paths/oauth.json >@@ -42,10 +42,22 @@ > "properties": { > "access_token": { > "type": "string" >+ }, >+ "token_type": { >+ "type": "string" >+ }, >+ "expires_in": { >+ "type": "integer" > } > } > } > }, >+ "400": { >+ "description": "Bad Request", >+ "schema": { >+ "$ref": "../definitions.json#/error" >+ } >+ }, > "403": { > "description": "Access forbidden", > "schema": { >diff --git a/t/db_dependent/api/v1/oauth.t b/t/db_dependent/api/v1/oauth.t >new file mode 100755 >index 0000000000..e81118d408 >--- /dev/null >+++ b/t/db_dependent/api/v1/oauth.t >@@ -0,0 +1,79 @@ >+#!/usr/bin/env perl >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it under the >+# terms of the GNU General Public License as published by the Free Software >+# Foundation; either version 3 of the License, or (at your option) any later >+# version. >+# >+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY >+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR >+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License along >+# with Koha; if not, write to the Free Software Foundation, Inc., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+use Modern::Perl; >+ >+use Test::More tests => 1; >+use Test::Mojo; >+ >+use Koha::Database; >+ >+use t::lib::Mocks; >+ >+my $t = Test::Mojo->new('Koha::REST::V1'); >+my $schema = Koha::Database->new->schema; >+ >+subtest '/oauth/token tests' => sub { >+ plan tests => 17; >+ >+ $schema->storage->txn_begin; >+ >+ # Missing parameter grant_type >+ $t->post_ok('/api/v1/oauth/token') >+ ->status_is(400); >+ >+ # Wrong grant type >+ $t->post_ok('/api/v1/oauth/token', form => { grant_type => 'password' }) >+ ->status_is(400) >+ ->json_is({error => 'Unimplemented grant type'}); >+ >+ # No client_id/client_secret >+ $t->post_ok('/api/v1/oauth/token', form => { grant_type => 'client_credentials' }) >+ ->status_is(403) >+ ->json_is({error => 'unauthorized_client'}); >+ >+ my ($client_id, $client_secret) = ('client1', 'secr3t'); >+ t::lib::Mocks::mock_config('api_client', { >+ 'client_id' => $client_id, >+ 'client_secret' => $client_secret, >+ 'scope' => ['patrons.read'], >+ }); >+ >+ my $formData = { >+ grant_type => 'client_credentials', >+ client_id => $client_id, >+ client_secret => $client_secret, >+ scope => 'patrons.read', >+ }; >+ $t->post_ok('/api/v1/oauth/token', form => $formData) >+ ->status_is(200) >+ ->json_is('/expires_in' => 3600) >+ ->json_is('/token_type' => 'Bearer') >+ ->json_has('/access_token'); >+ >+ my $access_token = $t->tx->res->json->{access_token}; >+ >+ # Without access token, it returns 401 >+ $t->get_ok('/api/v1/patrons')->status_is(401); >+ >+ # With access token, it returns 200 >+ my $tx = $t->ua->build_tx(GET => '/api/v1/patrons'); >+ $tx->req->headers->authorization("Bearer $access_token"); >+ $t->request_ok($tx)->status_is(200); >+ >+ $schema->storage->txn_rollback; >+}; >-- >2.14.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 20402
:
72865
|
72874
|
72886
|
72887
|
72888
|
72892
|
72893
|
72894
|
73086
|
73087
|
74009
|
74018
|
74050
|
74051
|
74069
|
74070
|
74088
|
74365
|
74366
|
74367
|
74368
|
74369
|
74380
|
74381
|
74382
|
74383
|
74384
|
74385
|
74386
|
74600
|
75026
|
75027
|
75028
|
75029
|
75030
|
75031
|
75032
|
75033
|
75189