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

Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Tomas Cohen Arazi 2020-05-14 15:15:52 -03:00 committed by Martin Renvoize
parent 5f9cc8ada9
commit 84bd1ac370
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
3 changed files with 135 additions and 25 deletions

View File

@ -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'
);

View File

@ -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"
}
}
}
}
}
}
}
}
};

View File

@ -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 <http://www.gnu.org/licenses>.
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;