cc50b56b90d157b0ae17b509ff41e05f417f11bf
[koha-equinox.git] / 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     my @plugins = Koha::Plugins->new->GetPlugins( { all => 1 } );
54     foreach my $plugin (@plugins) {
55         $plugin->enable;
56     }
57
58     # initialize Koha::REST::V1 after mocking
59     my $t;
60     warning_is
61         { $t = Test::Mojo->new('Koha::REST::V1'); }
62         'The resulting spec is invalid. Skipping Bad API Route Plugin',
63         'Bad plugins raise warning';
64
65     my $routes = get_defined_routes($t);
66     ok( !exists $routes->{'/contrib/badass/patrons/(:patron_id)/bother_wrong'}, 'Route doesn\'t exist' );
67     ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'}, 'Route exists' );
68
69     $schema->storage->txn_rollback;
70 };
71
72 subtest 'Disabled plugins tests' => sub {
73
74     plan tests => 2;
75
76     $schema->storage->txn_begin;
77
78     # enable plugins
79     t::lib::Mocks::mock_config( 'enable_plugins', 1 );
80     t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
81
82     my $good_plugin;
83
84     my @plugins = Koha::Plugins->new->GetPlugins( { all => 1 } );
85     foreach my $plugin (@plugins) {
86         $plugin->disable;
87         $good_plugin = $plugin
88             if $plugin->{metadata}->{description} eq 'Test plugin';
89     }
90
91     # initialize Koha::REST::V1 after mocking
92     my $t = Test::Mojo->new('Koha::REST::V1');
93
94     my $routes = get_defined_routes($t);
95     ok( !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'},
96         'Plugin disabled, route not defined' );
97
98     $good_plugin->enable;
99
100     $t      = Test::Mojo->new('Koha::REST::V1');
101     $routes = get_defined_routes($t);
102
103     ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'},
104         'Plugin enabled, route defined' );
105
106     $schema->storage->txn_rollback;
107 };
108
109 sub get_defined_routes {
110     my ($t) = @_;
111     my $routes = {};
112     traverse_routes( $_, 0, $routes ) for @{ $t->app->routes->children };
113
114     return $routes;
115 }
116
117 sub traverse_routes {
118     my ( $route, $depth, $routes ) = @_;
119
120     # Pattern
121     my $path = $route->pattern->unparsed || '/';
122
123     # Methods
124     my $via = $route->via;
125     my $verb = !$via ? '*' : uc join ',', @$via;
126     $routes->{$path}->{$verb} = 1;
127
128     $depth++;
129     traverse_routes( $_, $depth, $routes ) for @{ $route->children };
130     $depth--;
131 }