From f9ffeb8ca3aefd306e8f7516ed96e34c9fea4f82 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Fri, 24 Jan 2025 10:58:12 +0100 Subject: [PATCH] Bug 37286: Add tests for REST authentication through Mojo apps --- t/db_dependent/mojo/rest-auth.t | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 t/db_dependent/mojo/rest-auth.t diff --git a/t/db_dependent/mojo/rest-auth.t b/t/db_dependent/mojo/rest-auth.t new file mode 100644 index 0000000000..a83486c5b9 --- /dev/null +++ b/t/db_dependent/mojo/rest-auth.t @@ -0,0 +1,61 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use Test::More tests => 2; +use Test::Mojo; + +use Koha::Database; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +my $schema = Koha::Database->schema; +my $builder = t::lib::TestBuilder->new(); + +t::lib::Mocks::mock_preference('RESTOAuth2ClientCredentials', 1); + +subtest 'REST Authentication - Intranet' => rest_auth_subtest('Koha::App::Intranet'); +subtest 'REST Authentication - OPAC' => rest_auth_subtest('Koha::App::Opac'); + +sub rest_auth_subtest { + my $app = shift; + + return sub { + plan tests => 12; + + $schema->storage->txn_begin; + + my $t = Test::Mojo->new($app); + + $t->post_ok('/api/v1/oauth/token') + ->status_is(400) + ->json_is('/errors/0/message', 'Missing property.') + ->json_is('/errors/0/path', '/grant_type') + ; + + $t->post_ok('/api/v1/oauth/token', form => { grant_type => 'client_credentials' }) + ->status_is(403) + ->json_is('/error', 'unauthorized_client') + ; + + my $patron = $builder->build_object({ + class => 'Koha::Patrons', + value => { + flags => 0 # no permissions + }, + }); + my $api_key = Koha::ApiKey->new({ patron_id => $patron->id, description => 'blah' })->store; + my $client_id = $api_key->client_id; + my $client_secret = $api_key->plain_text_secret; + + $t->post_ok('/api/v1/oauth/token', form => { grant_type => 'client_credentials', client_id => $client_id, client_secret => $client_secret }) + ->status_is(200) + ->json_is('/expires_in' => 3600) + ->json_is('/token_type' => 'Bearer') + ->json_has('/access_token'); + ; + + $schema->storage->txn_rollback; + } +} -- 2.39.2