278137fda088eda5fcdecd40d3c0e10a638ff16e
[srvgit] / t / db_dependent / Koha / REST / Plugin / PluginRoutes.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 2;
21 use Test::Mojo;
22 use Test::Warn;
23
24 use File::Basename;
25 use t::lib::Mocks;
26
27 use JSON::Validator::OpenAPI::Mojolicious;
28
29 # Dummy app for testing the plugin
30 use Mojolicious::Lite;
31
32 BEGIN {
33     # Mock pluginsdir before loading Plugins module
34     my $path = dirname(__FILE__) . '/../../../../lib';
35     t::lib::Mocks::mock_config( 'pluginsdir', $path );
36 }
37
38 use Koha::Database;
39 use Koha::Plugins;
40
41 my $schema = Koha::Database->new->schema;
42
43 subtest 'Bad plugins tests' => sub {
44
45     plan tests => 3;
46
47     $schema->storage->txn_begin;
48
49     # enable plugins
50     t::lib::Mocks::mock_config( 'enable_plugins', 1 );
51     t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
52
53     # remove any existing plugins that might interfere
54     Koha::Plugins::Methods->search->delete;
55     my $plugins = Koha::Plugins->new;
56     $plugins->InstallPlugins;
57
58     my @plugins = $plugins->GetPlugins( { all => 1 } );
59     foreach my $plugin (@plugins) {
60         $plugin->enable;
61     }
62
63     # initialize Koha::REST::V1 after mocking
64     my $t;
65     warning_is
66         { $t = Test::Mojo->new('Koha::REST::V1'); }
67         'The resulting spec is invalid. Skipping Bad API Route Plugin',
68         'Bad plugins raise warning';
69
70     my $routes = get_defined_routes($t);
71     # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
72     # TODO: remove () if minimum version is bumped to at least 1.28.
73     ok( !exists $routes->{'/contrib/badass/patrons/(:patron_id)/bother_wrong'} && !exists $routes->{'/contrib/badass/patrons/<:patron_id>/bother_wrong'}, 'Route doesn\'t exist' );
74     ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'}, 'Route exists' );
75
76     $schema->storage->txn_rollback;
77 };
78
79 subtest 'Disabled plugins tests' => sub {
80
81     plan tests => 2;
82
83     $schema->storage->txn_begin;
84
85     # enable plugins
86     t::lib::Mocks::mock_config( 'enable_plugins', 1 );
87     t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
88
89     my $good_plugin;
90
91     my $plugins = Koha::Plugins->new;
92     $plugins->InstallPlugins;
93
94     my @plugins = $plugins->GetPlugins( { all => 1 } );
95     foreach my $plugin (@plugins) {
96         $plugin->disable;
97         $good_plugin = $plugin
98             if $plugin->{metadata}->{description} eq 'Test plugin';
99     }
100
101     # initialize Koha::REST::V1 after mocking
102     my $t = Test::Mojo->new('Koha::REST::V1');
103
104     my $routes = get_defined_routes($t);
105     # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
106     # TODO: remove () if minimum version is bumped to at least 1.28.
107     ok( !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} && !exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
108         'Plugin disabled, route not defined' );
109
110     $good_plugin->enable;
111
112     $t      = Test::Mojo->new('Koha::REST::V1');
113     $routes = get_defined_routes($t);
114
115     # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
116     # TODO: remove () if minimum version is bumped to at least 1.28.
117     ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
118         'Plugin enabled, route defined' );
119
120     $schema->storage->txn_rollback;
121 };
122
123 sub get_defined_routes {
124     my ($t) = @_;
125     my $routes = {};
126     traverse_routes( $_, 0, $routes ) for @{ $t->app->routes->children };
127
128     return $routes;
129 }
130
131 sub traverse_routes {
132     my ( $route, $depth, $routes ) = @_;
133
134     # Pattern
135     my $path = $route->pattern->unparsed || '/';
136
137     # Methods
138     my $via = $route->via;
139     my $verb = !$via ? '*' : uc join ',', @$via;
140     $routes->{$path}->{$verb} = 1;
141
142     $depth++;
143     traverse_routes( $_, $depth, $routes ) for @{ $route->children };
144     $depth--;
145 }