View | Details | Raw Unified | Return to bug 21116
Collapse All | Expand All

(-)a/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t (+83 lines)
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
}
(-)a/t/lib/Koha/Plugin/BadAPIRoute.pm (+92 lines)
Line 0 Link Here
1
package Koha::Plugin::BadAPIRoute;
2
3
use Modern::Perl;
4
5
use Mojo::JSON qw(decode_json);
6
7
use base qw(Koha::Plugins::Base);
8
9
our $VERSION = 0.01;
10
our $metadata = {
11
    name            => 'Bad API Route Plugin',
12
    author          => 'John Doe',
13
    description     => 'Test plugin for bad API route',
14
    date_authored   => '2018-',
15
    date_updated    => '2013-01-14',
16
    minimum_version => '17.11',
17
    maximum_version => undef,
18
    version         => $VERSION,
19
    my_example_tag  => 'find_me',
20
};
21
22
sub new {
23
    my ( $class, $args ) = @_;
24
    $args->{'metadata'} = $metadata;
25
    my $self = $class->SUPER::new($args);
26
    return $self;
27
}
28
29
sub api_namespace {
30
    return "badass";
31
}
32
33
sub api_routes {
34
    my ( $self, $args ) = @_;
35
36
    my $spec = qq{
37
{
38
  "/patrons/{patron_id}/bother_wrong": {
39
    "put": {
40
      "x-mojo-to": "Koha::Plugin::BadAPIRoute#bother",
41
      "operationId": "BotherPatron",
42
      "tags": ["patrons"],
43
      "parameters": [{
44
        "name": "patron_id",
45
        "in": "nowhere",
46
        "description": "Internal patron identifier",
47
        "required": true,
48
        "type": "integer"
49
      }],
50
      "produces": [
51
        "application/json"
52
      ],
53
      "responses": {
54
        "200": {
55
          "description": "A bothered patron",
56
          "schema": {
57
              "type": "object",
58
                "properties": {
59
                  "bothered": {
60
                    "description": "If the patron has been bothered",
61
                    "type": "boolean"
62
                  }
63
                }
64
          }
65
        },
66
        "404": {
67
          "description": "An error occurred",
68
          "schema": {
69
              "type": "object",
70
                "properties": {
71
                  "error": {
72
                    "description": "An explanation for the error",
73
                    "type": "string"
74
                  }
75
                }
76
          }
77
        }
78
      },
79
      "x-koha-authorization": {
80
        "permissions": {
81
          "borrowers": "1"
82
        }
83
      }
84
    }
85
  }
86
}
87
    };
88
89
    return decode_json($spec);
90
}
91
92
1;
(-)a/t/lib/Koha/Plugin/Test.pm (-1 / +65 lines)
Lines 3-8 package Koha::Plugin::Test; Link Here
3
## It's good practice to use Modern::Perl
3
## It's good practice to use Modern::Perl
4
use Modern::Perl;
4
use Modern::Perl;
5
5
6
use Mojo::JSON qw(decode_json);
7
6
## Required for all plugins
8
## Required for all plugins
7
use base qw(Koha::Plugins::Base);
9
use base qw(Koha::Plugins::Base);
8
10
Lines 92-94 sub test_output_html { Link Here
92
    my ( $self ) = @_;
94
    my ( $self ) = @_;
93
    $self->output_html( '¡Hola output_html!' );
95
    $self->output_html( '¡Hola output_html!' );
94
}
96
}
95
- 
97
98
sub api_namespace {
99
    return "testplugin";
100
}
101
102
sub api_routes {
103
    my ( $self, $args ) = @_;
104
105
    my $spec = qq{
106
{
107
  "/patrons/{patron_id}/bother": {
108
    "put": {
109
      "x-mojo-to": "Koha::Plugin::Test#bother",
110
      "operationId": "BotherPatron",
111
      "tags": ["patrons"],
112
      "parameters": [{
113
        "name": "patron_id",
114
        "in": "path",
115
        "description": "Internal patron identifier",
116
        "required": true,
117
        "type": "integer"
118
      }],
119
      "produces": [
120
        "application/json"
121
      ],
122
      "responses": {
123
        "200": {
124
          "description": "A bothered patron",
125
          "schema": {
126
              "type": "object",
127
                "properties": {
128
                  "bothered": {
129
                    "description": "If the patron has been bothered",
130
                    "type": "boolean"
131
                  }
132
                }
133
          }
134
        },
135
        "404": {
136
          "description": "An error occurred",
137
          "schema": {
138
              "type": "object",
139
                "properties": {
140
                  "error": {
141
                    "description": "An explanation for the error",
142
                    "type": "string"
143
                  }
144
                }
145
          }
146
        }
147
      },
148
      "x-koha-authorization": {
149
        "permissions": {
150
          "borrowers": "1"
151
        }
152
      }
153
    }
154
  }
155
}
156
    };
157
158
    return decode_json($spec);
159
}

Return to bug 21116