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

(-)a/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t (-5 / +50 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 2;
20
use Test::More tests => 3;
21
use Test::Mojo;
21
use Test::Mojo;
22
use Test::Warn;
22
use Test::Warn;
23
23
Lines 70-77 subtest 'Bad plugins tests' => sub { Link Here
70
    my $routes = get_defined_routes($t);
70
    my $routes = get_defined_routes($t);
71
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
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.
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' );
73
    ok( !exists $routes->{'/contrib/badass/patrons/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' );
74
    ok( exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Route exists' );
75
75
76
    $schema->storage->txn_rollback;
76
    $schema->storage->txn_rollback;
77
};
77
};
Lines 104-110 subtest 'Disabled plugins tests' => sub { Link Here
104
    my $routes = get_defined_routes($t);
104
    my $routes = get_defined_routes($t);
105
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
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.
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'},
107
    ok( !exists $routes->{'/contrib/testplugin/patrons/bother'},
108
        'Plugin disabled, route not defined' );
108
        'Plugin disabled, route not defined' );
109
109
110
    $good_plugin->enable;
110
    $good_plugin->enable;
Lines 114-125 subtest 'Disabled plugins tests' => sub { Link Here
114
114
115
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
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.
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'},
117
    ok( exists $routes->{'/contrib/testplugin/patrons/bother'},
118
        'Plugin enabled, route defined' );
118
        'Plugin enabled, route defined' );
119
119
120
    $schema->storage->txn_rollback;
120
    $schema->storage->txn_rollback;
121
};
121
};
122
122
123
subtest 'Anonymous access routes plugins tests' => sub {
124
125
    plan tests => 9;
126
127
    $schema->storage->txn_begin;
128
129
    # enable plugins
130
    t::lib::Mocks::mock_config( 'enable_plugins', 1 );
131
132
    # remove any existing plugins that might interfere
133
    Koha::Plugins::Methods->search->delete;
134
    my $plugins = Koha::Plugins->new;
135
    $plugins->InstallPlugins;
136
137
    my @plugins = $plugins->GetPlugins( { all => 1 } );
138
    foreach my $plugin (@plugins) {
139
        $plugin->enable;
140
    }
141
142
    # initialize Koha::REST::V1 after mocking
143
    my $t;
144
    warning_is
145
        { $t = Test::Mojo->new('Koha::REST::V1'); }
146
        'The resulting spec is invalid. Skipping Bad API Route Plugin',
147
        'Bad plugins raise warning';
148
149
    my $routes = get_defined_routes($t);
150
    ok( exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Route exists' );
151
    ok( exists $routes->{'/contrib/testplugin/public/patrons/bother'}, 'Route exists' );
152
153
    C4::Context->set_preference( 'RESTPublicAnonymousRequests', 0 );
154
155
   $t->get_ok('/api/v1/contrib/testplugin/public/patrons/bother')
156
     ->status_is(200, 'Plugin routes not affected by RESTPublicAnonymousRequests')
157
     ->json_is( { bothered => Mojo::JSON->true } );
158
159
    C4::Context->set_preference( 'RESTPublicAnonymousRequests', 1 );
160
161
    $t->get_ok('/api/v1/contrib/testplugin/public/patrons/bother')
162
      ->status_is(200, 'Plugin routes not affected by RESTPublicAnonymousRequests')
163
      ->json_is( { bothered => Mojo::JSON->true } );
164
165
    $schema->storage->txn_rollback;
166
};
167
123
sub get_defined_routes {
168
sub get_defined_routes {
124
    my ($t) = @_;
169
    my ($t) = @_;
125
    my $routes = {};
170
    my $routes = {};
(-)a/t/lib/Koha/Plugin/Test.pm (-16 / +45 lines)
Lines 160-177 sub api_routes { Link Here
160
160
161
    my $spec = qq{
161
    my $spec = qq{
162
{
162
{
163
  "/patrons/{patron_id}/bother": {
163
  "/patrons/bother": {
164
    "put": {
164
    "get": {
165
      "x-mojo-to": "Koha::Plugin::Test#bother",
165
      "x-mojo-to": "Test::Controller#bother",
166
      "operationId": "BotherPatron",
166
      "operationId": "BotherPatron",
167
      "tags": ["patrons"],
167
      "tags": ["patrons"],
168
      "parameters": [{
169
        "name": "patron_id",
170
        "in": "path",
171
        "description": "Internal patron identifier",
172
        "required": true,
173
        "type": "integer"
174
      }],
175
      "produces": [
168
      "produces": [
176
        "application/json"
169
        "application/json"
177
      ],
170
      ],
Lines 188-203 sub api_routes { Link Here
188
                }
181
                }
189
          }
182
          }
190
        },
183
        },
191
        "404": {
184
        "401": {
192
          "description": "An error occurred",
185
          "description": "An error occurred",
193
          "schema": {
186
          "schema": {
194
              "type": "object",
187
              "type": "object",
195
                "properties": {
188
              "properties": {
196
                  "error": {
189
                "error": {
197
                    "description": "An explanation for the error",
190
                  "description": "An explanation for the error",
198
                    "type": "string"
191
                  "type": "string"
199
                  }
200
                }
192
                }
193
              }
201
          }
194
          }
202
        }
195
        }
203
      },
196
      },
Lines 207-212 sub api_routes { Link Here
207
        }
200
        }
208
      }
201
      }
209
    }
202
    }
203
  },
204
  "/public/patrons/bother": {
205
    "get": {
206
      "x-mojo-to": "Test::Controller#bother",
207
      "operationId": "PubliclyBotherPatron",
208
      "tags": ["patrons"],
209
      "produces": [
210
        "application/json"
211
      ],
212
      "responses": {
213
        "200": {
214
          "description": "A bothered patron",
215
          "schema": {
216
              "type": "object",
217
              "properties": {
218
                "bothered": {
219
                  "description": "If the patron has been bothered",
220
                  "type": "boolean"
221
                }
222
              }
223
          }
224
        },
225
        "401": {
226
          "description": "Authentication required",
227
          "schema": {
228
            "type": "object",
229
            "properties": {
230
              "error": {
231
                "description": "An explanation for the error",
232
                "type": "string"
233
              }
234
            }
235
          }
236
        }
237
      }
238
    }
210
  }
239
  }
211
}
240
}
212
    };
241
    };
(-)a/t/lib/Koha/Plugin/Test/Controller.pm (-1 / +38 lines)
Line 0 Link Here
0
- 
1
package Koha::Plugin::Test::Controller;
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 Mojo::Base 'Mojolicious::Controller';
21
22
=head1 API
23
24
=head2 Methods
25
26
=head3 bother
27
28
=cut
29
30
sub bother {
31
    my ($c) = @_;
32
    return $c->render(
33
        status  => 200,
34
        openapi => { bothered => Mojo::JSON->true }
35
    );
36
}
37
38
1;

Return to bug 25411