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

(-)a/Koha/REST/V1/Auth.pm (-4 / +7 lines)
Lines 55-68 This subroutine is called before every request to API. Link Here
55
=cut
55
=cut
56
56
57
sub under {
57
sub under {
58
    my $c = shift->openapi->valid_input or return;;
58
    my ( $c ) = @_;
59
59
60
    my $status = 0;
60
    my $status = 0;
61
61
62
    try {
62
    try {
63
63
64
        # /api/v1/{namespace}
64
        # /api/v1/{namespace}
65
        my $namespace = $c->req->url->to_abs->path->[2];
65
        my $namespace = $c->req->url->to_abs->path->[2] // '';
66
66
67
        if ( $namespace eq 'public'
67
        if ( $namespace eq 'public'
68
            and !C4::Context->preference('RESTPublicAPI') )
68
            and !C4::Context->preference('RESTPublicAPI') )
Lines 136-142 sub authenticate_api_request { Link Here
136
136
137
    my $user;
137
    my $user;
138
138
139
    my $spec = $c->match->endpoint->pattern->defaults->{'openapi.op_spec'};
139
    # The following supports retrieval of spec with Mojolicious::Plugin::OpenAPI@1.17 and later (first one)
140
    # and older versions (second one).
141
    # TODO: remove the latter 'openapi.op_spec' if minimum version is bumped to at least 1.17.
142
    my $spec = $c->openapi->spec || $c->match->endpoint->pattern->defaults->{'openapi.op_spec'};
143
140
    my $authorization = $spec->{'x-koha-authorization'};
144
    my $authorization = $spec->{'x-koha-authorization'};
141
145
142
    my $authorization_header = $c->req->headers->authorization;
146
    my $authorization_header = $c->req->headers->authorization;
Lines 202-208 sub authenticate_api_request { Link Here
202
        if ($status eq "ok") {
206
        if ($status eq "ok") {
203
            my $session = get_session($sessionID);
207
            my $session = get_session($sessionID);
204
            $user = Koha::Patrons->find($session->param('number'));
208
            $user = Koha::Patrons->find($session->param('number'));
205
            # $c->stash('koha.user' => $user);
206
        }
209
        }
207
        elsif ($status eq "maintenance") {
210
        elsif ($status eq "maintenance") {
208
            Koha::Exceptions::UnderMaintenance->throw(
211
            Koha::Exceptions::UnderMaintenance->throw(
(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-87 / +24 lines)
Lines 109-122 my $t = Test::Mojo->new; Link Here
109
109
110
subtest 'objects.search helper' => sub {
110
subtest 'objects.search helper' => sub {
111
111
112
    plan tests => 90;
112
    plan tests => 38;
113
113
114
    $schema->storage->txn_begin;
114
    $schema->storage->txn_begin;
115
115
116
    # Remove existing cities to have more control on the search restuls
116
    # Remove existing cities to have more control on the search results
117
    Koha::Cities->delete;
117
    Koha::Cities->delete;
118
118
119
    # Create two sample patrons that match the query
119
    # Create three sample cities that match the query. This makes sure we
120
    # always have a "next" link regardless of Mojolicious::Plugin::OpenAPI version.
120
    $builder->build_object({
121
    $builder->build_object({
121
        class => 'Koha::Cities',
122
        class => 'Koha::Cities',
122
        value => {
123
        value => {
Lines 129-138 subtest 'objects.search helper' => sub { Link Here
129
            city_name => 'Manuela'
130
            city_name => 'Manuela'
130
        }
131
        }
131
    });
132
    });
133
    $builder->build_object({
134
        class => 'Koha::Cities',
135
        value => {
136
            city_name => 'Manuelab'
137
        }
138
    });
132
139
133
    $t->get_ok('/cities?city_name=manuel&_per_page=1&_page=1')
140
    $t->get_ok('/cities?city_name=manuel&_per_page=1&_page=1')
134
        ->status_is(200)
141
        ->status_is(200)
135
        ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
142
        ->header_like( 'Link' => qr/<http:\/\/.*[\?&]_page=2.*>; rel="next",/ )
136
        ->json_has('/0')
143
        ->json_has('/0')
137
        ->json_hasnt('/1')
144
        ->json_hasnt('/1')
138
        ->json_is('/0/city_name' => 'Manuel');
145
        ->json_is('/0/city_name' => 'Manuel');
Lines 145-197 subtest 'objects.search helper' => sub { Link Here
145
    });
152
    });
146
153
147
    # _match=starts_with
154
    # _match=starts_with
148
    $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=starts_with')
155
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=starts_with')
149
        ->status_is(200)
150
        ->json_has('/0')
151
        ->json_has('/1')
152
        ->json_hasnt('/2')
153
        ->json_is('/0/city_name' => 'Manuel')
154
        ->json_is('/1/city_name' => 'Manuela');
155
156
    # _match=ends_with
157
    $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=ends_with')
158
        ->status_is(200)
159
        ->json_has('/0')
160
        ->json_has('/1')
161
        ->json_hasnt('/2')
162
        ->json_is('/0/city_name' => 'Manuel')
163
        ->json_is('/1/city_name' => 'Emanuel');
164
165
    # _match=exact
166
    $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=exact')
167
        ->status_is(200)
168
        ->json_has('/0')
169
        ->json_hasnt('/1')
170
        ->json_is('/0/city_name' => 'Manuel');
171
172
    # _match=contains
173
    $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=contains')
174
        ->status_is(200)
156
        ->status_is(200)
175
        ->json_has('/0')
157
        ->json_has('/0')
176
        ->json_has('/1')
158
        ->json_has('/1')
177
        ->json_has('/2')
159
        ->json_has('/2')
178
        ->json_hasnt('/3')
160
        ->json_hasnt('/3')
179
        ->json_is('/0/city_name' => 'Manuel')
161
        ->json_is('/0/name' => 'Manuel')
180
        ->json_is('/1/city_name' => 'Manuela')
162
        ->json_is('/1/name' => 'Manuela')
181
        ->json_is('/2/city_name' => 'Emanuel');
163
        ->json_is('/2/name' => 'Manuelab');
182
183
    ## _to_model tests
184
    # _match=starts_with
185
    $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
186
        ->status_is(200)
187
        ->json_has('/0')
188
        ->json_has('/1')
189
        ->json_hasnt('/2')
190
        ->json_is('/0/city_name' => 'Manuel')
191
        ->json_is('/1/city_name' => 'Manuela');
192
164
193
    # _match=ends_with
165
    # _match=ends_with
194
    $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
166
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=ends_with')
195
        ->status_is(200)
167
        ->status_is(200)
196
        ->json_has('/0')
168
        ->json_has('/0')
197
        ->json_has('/1')
169
        ->json_has('/1')
Lines 200-258 subtest 'objects.search helper' => sub { Link Here
200
        ->json_is('/1/city_name' => 'Emanuel');
172
        ->json_is('/1/city_name' => 'Emanuel');
201
173
202
    # _match=exact
174
    # _match=exact
203
    $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=exact')
175
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=exact')
204
        ->status_is(200)
176
        ->status_is(200)
205
        ->json_has('/0')
177
        ->json_has('/0')
206
        ->json_hasnt('/1')
178
        ->json_hasnt('/1')
207
        ->json_is('/0/city_name' => 'Manuel');
179
        ->json_is('/0/city_name' => 'Manuel');
208
180
209
    # _match=contains
181
    # _match=contains
210
    $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=contains')
182
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=contains')
211
        ->status_is(200)
212
        ->json_has('/0')
213
        ->json_has('/1')
214
        ->json_has('/2')
215
        ->json_hasnt('/3')
216
        ->json_is('/0/city_name' => 'Manuel')
217
        ->json_is('/1/city_name' => 'Manuela')
218
        ->json_is('/2/city_name' => 'Emanuel');
219
220
    ## _to_model && _to_api tests
221
    # _match=starts_with
222
    $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
223
        ->status_is(200)
224
        ->json_has('/0')
225
        ->json_has('/1')
226
        ->json_hasnt('/2')
227
        ->json_is('/0/nombre' => 'Manuel')
228
        ->json_is('/1/nombre' => 'Manuela');
229
230
    # _match=ends_with
231
    $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
232
        ->status_is(200)
233
        ->json_has('/0')
234
        ->json_has('/1')
235
        ->json_hasnt('/2')
236
        ->json_is('/0/nombre' => 'Manuel')
237
        ->json_is('/1/nombre' => 'Emanuel');
238
239
    # _match=exact
240
    $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=exact')
241
        ->status_is(200)
242
        ->json_has('/0')
243
        ->json_hasnt('/1')
244
        ->json_is('/0/nombre' => 'Manuel');
245
246
    # _match=contains
247
    $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=contains')
248
        ->status_is(200)
183
        ->status_is(200)
249
        ->json_has('/0')
184
        ->json_has('/0')
250
        ->json_has('/1')
185
        ->json_has('/1')
251
        ->json_has('/2')
186
        ->json_has('/2')
252
        ->json_hasnt('/3')
187
        ->json_has('/3')
253
        ->json_is('/0/nombre' => 'Manuel')
188
        ->json_hasnt('/4')
254
        ->json_is('/1/nombre' => 'Manuela')
189
        ->json_is('/0/name' => 'Manuel')
255
        ->json_is('/2/nombre' => 'Emanuel');
190
        ->json_is('/1/name' => 'Manuela')
191
        ->json_is('/2/name' => 'Manuelab')
192
        ->json_is('/3/name' => 'Emanuel');
256
193
257
    $schema->storage->txn_rollback;
194
    $schema->storage->txn_rollback;
258
};
195
};
(-)a/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t (-5 / +12 lines)
Lines 50-55 subtest 'Bad plugins tests' => sub { Link Here
50
    t::lib::Mocks::mock_config( 'enable_plugins', 1 );
50
    t::lib::Mocks::mock_config( 'enable_plugins', 1 );
51
    t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
51
    t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
52
52
53
    # remove any existing plugins that might interfere
54
    Koha::Plugins::Methods->search->delete;
53
    my $plugins = Koha::Plugins->new;
55
    my $plugins = Koha::Plugins->new;
54
    $plugins->InstallPlugins;
56
    $plugins->InstallPlugins;
55
57
Lines 66-73 subtest 'Bad plugins tests' => sub { Link Here
66
        'Bad plugins raise warning';
68
        'Bad plugins raise warning';
67
69
68
    my $routes = get_defined_routes($t);
70
    my $routes = get_defined_routes($t);
69
    ok( !exists $routes->{'/contrib/badass/patrons/(:patron_id)/bother_wrong'}, 'Route doesn\'t exist' );
71
    # Support placeholders () and <>  (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
70
    ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'}, 'Route exists' );
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' );
74
    ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id>)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'}, 'Route exists' );
71
75
72
    $schema->storage->txn_rollback;
76
    $schema->storage->txn_rollback;
73
};
77
};
Lines 98-104 subtest 'Disabled plugins tests' => sub { Link Here
98
    my $t = Test::Mojo->new('Koha::REST::V1');
102
    my $t = Test::Mojo->new('Koha::REST::V1');
99
103
100
    my $routes = get_defined_routes($t);
104
    my $routes = get_defined_routes($t);
101
    ok( !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'},
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.
107
    ok( !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} && !exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
102
        'Plugin disabled, route not defined' );
108
        'Plugin disabled, route not defined' );
103
109
104
    $good_plugin->enable;
110
    $good_plugin->enable;
Lines 106-112 subtest 'Disabled plugins tests' => sub { Link Here
106
    $t      = Test::Mojo->new('Koha::REST::V1');
112
    $t      = Test::Mojo->new('Koha::REST::V1');
107
    $routes = get_defined_routes($t);
113
    $routes = get_defined_routes($t);
108
114
109
    ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'},
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.
117
    ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
110
        'Plugin enabled, route defined' );
118
        'Plugin enabled, route defined' );
111
119
112
    $schema->storage->txn_rollback;
120
    $schema->storage->txn_rollback;
113
- 

Return to bug 22522