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

(-)a/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t (-9 / +52 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 3;
20
use Test::More tests => 4;
21
use Test::Mojo;
21
use Test::Mojo;
22
use Test::Warn;
22
use Test::Warn;
23
23
Lines 69-76 subtest 'Bad plugins tests' => sub { Link Here
69
    my $routes = get_defined_routes($t);
69
    my $routes = get_defined_routes($t);
70
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
70
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
71
    # TODO: remove () if minimum version is bumped to at least 1.28.
71
    # TODO: remove () if minimum version is bumped to at least 1.28.
72
    ok( !exists $routes->{'/contrib/badass/patrons/(:patron_id)/bother_wrong'} && !exists $routes->{'/contrib/badass/patrons/<:patron_id>/bother_wrong'}, 'Route doesn\'t exist' );
72
    ok( !exists $routes->{'/contrib/badass/patrons/bother_wrong'}, 'Route doesn\'t exist' );
73
    ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'}, 'Route exists' );
73
    ok( exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Route exists' );
74
74
75
    $schema->storage->txn_rollback;
75
    $schema->storage->txn_rollback;
76
};
76
};
Lines 102-108 subtest 'Disabled plugins tests' => sub { Link Here
102
    my $routes = get_defined_routes($t);
102
    my $routes = get_defined_routes($t);
103
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
103
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
104
    # TODO: remove () if minimum version is bumped to at least 1.28.
104
    # TODO: remove () if minimum version is bumped to at least 1.28.
105
    ok( !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} && !exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
105
    ok( !exists $routes->{'/contrib/testplugin/patrons/bother'},
106
        'Plugin disabled, route not defined' );
106
        'Plugin disabled, route not defined' );
107
107
108
    $good_plugin->enable;
108
    $good_plugin->enable;
Lines 112-123 subtest 'Disabled plugins tests' => sub { Link Here
112
112
113
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
113
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
114
    # TODO: remove () if minimum version is bumped to at least 1.28.
114
    # TODO: remove () if minimum version is bumped to at least 1.28.
115
    ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
115
    ok( exists $routes->{'/contrib/testplugin/patrons/bother'},
116
        'Plugin enabled, route defined' );
116
        'Plugin enabled, route defined' );
117
117
118
    $schema->storage->txn_rollback;
118
    $schema->storage->txn_rollback;
119
};
119
};
120
120
121
subtest 'Anonymous access routes plugins tests' => sub {
122
123
    plan tests => 9;
124
125
    $schema->storage->txn_begin;
126
127
    # enable plugins
128
    t::lib::Mocks::mock_config( 'enable_plugins', 1 );
129
130
    # remove any existing plugins that might interfere
131
    Koha::Plugins::Methods->search->delete;
132
    my $plugins = Koha::Plugins->new;
133
    $plugins->InstallPlugins;
134
135
    my @plugins = $plugins->GetPlugins( { all => 1 } );
136
    foreach my $plugin (@plugins) {
137
        $plugin->enable;
138
    }
139
140
    # initialize Koha::REST::V1 after mocking
141
    my $t;
142
    warning_is
143
        { $t = Test::Mojo->new('Koha::REST::V1'); }
144
        'The resulting spec is invalid. Skipping Bad API Route Plugin',
145
        'Bad plugins raise warning';
146
147
    my $routes = get_defined_routes($t);
148
    ok( exists $routes->{'/contrib/testplugin/patrons/bother'}, 'Route exists' );
149
    ok( exists $routes->{'/contrib/testplugin/public/patrons/bother'}, 'Route exists' );
150
151
    C4::Context->set_preference( 'RESTPublicAnonymousRequests', 0 );
152
153
   $t->get_ok('/api/v1/contrib/testplugin/public/patrons/bother')
154
     ->status_is(200, 'Plugin routes not affected by RESTPublicAnonymousRequests')
155
     ->json_is( { bothered => Mojo::JSON->true } );
156
157
    C4::Context->set_preference( 'RESTPublicAnonymousRequests', 1 );
158
159
    $t->get_ok('/api/v1/contrib/testplugin/public/patrons/bother')
160
      ->status_is(200, 'Plugin routes not affected by RESTPublicAnonymousRequests')
161
      ->json_is( { bothered => Mojo::JSON->true } );
162
163
    $schema->storage->txn_rollback;
164
};
165
121
subtest 'needs_install use case tests' => sub {
166
subtest 'needs_install use case tests' => sub {
122
167
123
    plan tests => 2;
168
    plan tests => 2;
Lines 142-149 subtest 'needs_install use case tests' => sub { Link Here
142
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
187
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
143
    # TODO: remove () if minimum version is bumped to at least 1.28.
188
    # TODO: remove () if minimum version is bumped to at least 1.28.
144
    ok(
189
    ok(
145
        !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'}
190
        !exists $routes->{'/contrib/testplugin/patrons/bother'},
146
          && !exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
147
        'Plugin enabled, route not defined as C4::Context->needs_install is true'
191
        'Plugin enabled, route not defined as C4::Context->needs_install is true'
148
    );
192
    );
149
193
Lines 159-166 subtest 'needs_install use case tests' => sub { Link Here
159
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
203
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
160
    # TODO: remove () if minimum version is bumped to at least 1.28.
204
    # TODO: remove () if minimum version is bumped to at least 1.28.
161
    ok(
205
    ok(
162
        exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'}
206
        exists $routes->{'/contrib/testplugin/patrons/bother'},
163
          || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
164
        'Plugin enabled, route defined as C4::Context->needs_install is false'
207
        'Plugin enabled, route defined as C4::Context->needs_install is false'
165
    );
208
    );
166
209
(-)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