Line 0
Link Here
|
|
|
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 => 1; |
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 |
subtest 'Bad plugins tests' => sub { |
39 |
|
40 |
plan tests => 3; |
41 |
|
42 |
# enable plugins |
43 |
t::lib::Mocks::mock_config( 'enable_plugins', 1 ); |
44 |
t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 ); |
45 |
|
46 |
# initialize Koha::REST::V1 after mocking |
47 |
my $remote_address = '127.0.0.1'; |
48 |
my $t; |
49 |
|
50 |
warning_is |
51 |
{ $t = Test::Mojo->new('Koha::REST::V1'); } |
52 |
'The resulting spec is invalid. Skipping Bad API Route Plugin', |
53 |
'Bad plugins raise warning'; |
54 |
|
55 |
my $routes = get_defined_routes($t); |
56 |
ok( !exists $routes->{'/contrib/badass/patrons/(:patron_id)/bother_wrong'}, 'Route doesn\'t exist' ); |
57 |
ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'}, 'Route exists' ); |
58 |
|
59 |
}; |
60 |
|
61 |
sub get_defined_routes { |
62 |
my ($t) = @_; |
63 |
my $routes = {}; |
64 |
traverse_routes( $_, 0, $routes ) for @{ $t->app->routes->children }; |
65 |
|
66 |
return $routes; |
67 |
} |
68 |
|
69 |
sub traverse_routes { |
70 |
my ( $route, $depth, $routes ) = @_; |
71 |
|
72 |
# Pattern |
73 |
my $path = $route->pattern->unparsed || '/'; |
74 |
|
75 |
# Methods |
76 |
my $via = $route->via; |
77 |
my $verb = !$via ? '*' : uc join ',', @$via; |
78 |
$routes->{$path}->{$verb} = 1; |
79 |
|
80 |
$depth++; |
81 |
traverse_routes( $_, $depth, $routes ) for @{ $route->children }; |
82 |
$depth--; |
83 |
} |