use Modern::Perl;
-use Test::More tests => 3;
+use Test::More tests => 4;
use Test::Mojo;
use Test::Warn;
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;
};
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;
# 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;
# 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'
);
# 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'
);
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"
],
}
}
},
- "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"
}
+ }
}
}
},
}
}
}
+ },
+ "/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"
+ }
+ }
+ }
+ }
+ }
+ }
}
}
};
--- /dev/null
+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;