From 52c3e069898618694a786afcab4670ea1f5505df Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 14 May 2020 15:15:52 -0300 Subject: [PATCH] Bug 25411: Regression tests This patch adds tests the new RESTPublicAnonymousRequests syspref doesn't apply to routes added by plugins. It is up to the plugin developer to handle those conditions. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/REST/Plugin/PluginRoutes.t => FAIL: Notice the tests fail --- .../Koha/REST/Plugin/PluginRoutes.t | 61 ++++++++++++++++--- t/lib/Koha/Plugin/Test.pm | 61 ++++++++++++++----- t/lib/Koha/Plugin/Test/Controller.pm | 38 ++++++++++++ 3 files changed, 135 insertions(+), 25 deletions(-) create mode 100644 t/lib/Koha/Plugin/Test/Controller.pm diff --git a/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t b/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t index 75a2f66a860..27af737ab0e 100644 --- a/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t +++ b/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Mojo; use Test::Warn; @@ -69,8 +69,8 @@ subtest 'Bad plugins tests' => sub { my $routes = get_defined_routes($t); # Support placeholders () and <> (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28) # TODO: remove () if minimum version is bumped to at least 1.28. - ok( !exists $routes->{'/contrib/badass/patrons/(:patron_id)/bother_wrong'} && !exists $routes->{'/contrib/badass/patrons/<:patron_id>/bother_wrong'}, 'Route doesn\'t exist' ); - ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'}, 'Route exists' ); + ok( !exists $routes->{'/contrib/badass/patrons/bother_wrong'}, 'Route doesn\'t exist' ); + ok( exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Route exists' ); $schema->storage->txn_rollback; }; @@ -102,7 +102,7 @@ subtest 'Disabled plugins tests' => sub { my $routes = get_defined_routes($t); # Support placeholders () and <> (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28) # TODO: remove () if minimum version is bumped to at least 1.28. - ok( !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} && !exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'}, + ok( !exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Plugin disabled, route not defined' ); $good_plugin->enable; @@ -112,12 +112,57 @@ subtest 'Disabled plugins tests' => sub { # Support placeholders () and <> (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28) # TODO: remove () if minimum version is bumped to at least 1.28. - ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'}, + ok( exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Plugin enabled, route defined' ); $schema->storage->txn_rollback; }; +subtest 'Anonymous access routes plugins tests' => sub { + + plan tests => 9; + + $schema->storage->txn_begin; + + # enable plugins + t::lib::Mocks::mock_config( 'enable_plugins', 1 ); + + # remove any existing plugins that might interfere + Koha::Plugins::Methods->search->delete; + my $plugins = Koha::Plugins->new; + $plugins->InstallPlugins; + + my @plugins = $plugins->GetPlugins( { all => 1 } ); + foreach my $plugin (@plugins) { + $plugin->enable; + } + + # initialize Koha::REST::V1 after mocking + my $t; + warning_is + { $t = Test::Mojo->new('Koha::REST::V1'); } + 'The resulting spec is invalid. Skipping Bad API Route Plugin', + 'Bad plugins raise warning'; + + my $routes = get_defined_routes($t); + ok( exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Route exists' ); + ok( exists $routes->{'/contrib/testplugin/public/patrons/bother'}, 'Route exists' ); + + C4::Context->set_preference( 'RESTPublicAnonymousRequests', 0 ); + + $t->get_ok('/api/v1/contrib/testplugin/public/patrons/bother') + ->status_is(200, 'Plugin routes not affected by RESTPublicAnonymousRequests') + ->json_is( { bothered => Mojo::JSON->true } ); + + C4::Context->set_preference( 'RESTPublicAnonymousRequests', 1 ); + + $t->get_ok('/api/v1/contrib/testplugin/public/patrons/bother') + ->status_is(200, 'Plugin routes not affected by RESTPublicAnonymousRequests') + ->json_is( { bothered => Mojo::JSON->true } ); + + $schema->storage->txn_rollback; +}; + subtest 'needs_install use case tests' => sub { plan tests => 2; @@ -142,8 +187,7 @@ subtest 'needs_install use case tests' => sub { # Support placeholders () and <> (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28) # TODO: remove () if minimum version is bumped to at least 1.28. ok( - !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} - && !exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'}, + !exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Plugin enabled, route not defined as C4::Context->needs_install is true' ); @@ -159,8 +203,7 @@ subtest 'needs_install use case tests' => sub { # Support placeholders () and <> (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28) # TODO: remove () if minimum version is bumped to at least 1.28. ok( - exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} - || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'}, + exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Plugin enabled, route defined as C4::Context->needs_install is false' ); diff --git a/t/lib/Koha/Plugin/Test.pm b/t/lib/Koha/Plugin/Test.pm index b7b85d689e2..8036c1fd5ef 100644 --- a/t/lib/Koha/Plugin/Test.pm +++ b/t/lib/Koha/Plugin/Test.pm @@ -160,18 +160,11 @@ sub api_routes { my $spec = qq{ { - "/patrons/{patron_id}/bother": { - "put": { - "x-mojo-to": "Koha::Plugin::Test#bother", + "/patrons/bother": { + "get": { + "x-mojo-to": "Test::Controller#bother", "operationId": "BotherPatron", "tags": ["patrons"], - "parameters": [{ - "name": "patron_id", - "in": "path", - "description": "Internal patron identifier", - "required": true, - "type": "integer" - }], "produces": [ "application/json" ], @@ -188,16 +181,16 @@ sub api_routes { } } }, - "404": { + "401": { "description": "An error occurred", "schema": { "type": "object", - "properties": { - "error": { - "description": "An explanation for the error", - "type": "string" - } + "properties": { + "error": { + "description": "An explanation for the error", + "type": "string" } + } } } }, @@ -207,6 +200,42 @@ sub api_routes { } } } + }, + "/public/patrons/bother": { + "get": { + "x-mojo-to": "Test::Controller#bother", + "operationId": "PubliclyBotherPatron", + "tags": ["patrons"], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A bothered patron", + "schema": { + "type": "object", + "properties": { + "bothered": { + "description": "If the patron has been bothered", + "type": "boolean" + } + } + } + }, + "401": { + "description": "Authentication required", + "schema": { + "type": "object", + "properties": { + "error": { + "description": "An explanation for the error", + "type": "string" + } + } + } + } + } + } } } }; diff --git a/t/lib/Koha/Plugin/Test/Controller.pm b/t/lib/Koha/Plugin/Test/Controller.pm new file mode 100644 index 00000000000..0b90e48434a --- /dev/null +++ b/t/lib/Koha/Plugin/Test/Controller.pm @@ -0,0 +1,38 @@ +package Koha::Plugin::Test::Controller; + +# 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, see . + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +=head1 API + +=head2 Methods + +=head3 bother + +=cut + +sub bother { + my ($c) = @_; + return $c->render( + status => 200, + openapi => { bothered => Mojo::JSON->true } + ); +} + +1; -- 2.26.2