From 776ea02d177d31b6c3461d55bf1cfaae7b9df4aa 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 Amended-by: Jonathan Druart Add +x Signed-off-by: Martin Renvoize --- t/db_dependent/mojo/rest-auth.t | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 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 100755 index 00000000000..75384b95f7e --- /dev/null +++ b/t/db_dependent/mojo/rest-auth.t @@ -0,0 +1,57 @@ +#!/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.48.1