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

(-)a/Koha/REST/V1/Auth.pm (-4 / +6 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'};
140
143
141
    $c->stash_embed({ spec => $spec });
144
    $c->stash_embed({ spec => $spec });
142
145
Lines 205-211 sub authenticate_api_request { Link Here
205
        if ($status eq "ok") {
208
        if ($status eq "ok") {
206
            my $session = get_session($sessionID);
209
            my $session = get_session($sessionID);
207
            $user = Koha::Patrons->find($session->param('number'));
210
            $user = Koha::Patrons->find($session->param('number'));
208
            # $c->stash('koha.user' => $user);
209
        }
211
        }
210
        elsif ($status eq "maintenance") {
212
        elsif ($status eq "maintenance") {
211
            Koha::Exceptions::UnderMaintenance->throw(
213
            Koha::Exceptions::UnderMaintenance->throw(
(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-12 / +23 lines)
Lines 69-82 my $builder = t::lib::TestBuilder->new; Link Here
69
69
70
subtest 'objects.search helper' => sub {
70
subtest 'objects.search helper' => sub {
71
71
72
    plan tests => 34;
72
    plan tests => 38;
73
73
74
    $schema->storage->txn_begin;
74
    $schema->storage->txn_begin;
75
75
76
    # Remove existing cities to have more control on the search restuls
76
    # Remove existing cities to have more control on the search results
77
    Koha::Cities->delete;
77
    Koha::Cities->delete;
78
78
79
    # Create two sample patrons that match the query
79
    # Create three sample cities that match the query. This makes sure we
80
    # always have a "next" link regardless of Mojolicious::Plugin::OpenAPI version.
80
    $builder->build_object({
81
    $builder->build_object({
81
        class => 'Koha::Cities',
82
        class => 'Koha::Cities',
82
        value => {
83
        value => {
Lines 89-98 subtest 'objects.search helper' => sub { Link Here
89
            city_name => 'Manuela'
90
            city_name => 'Manuela'
90
        }
91
        }
91
    });
92
    });
93
    $builder->build_object({
94
        class => 'Koha::Cities',
95
        value => {
96
            city_name => 'Manuelab'
97
        }
98
    });
92
99
93
    $t->get_ok('/cities?name=manuel&_per_page=1&_page=1')
100
    $t->get_ok('/cities?name=manuel&_per_page=1&_page=1')
94
        ->status_is(200)
101
        ->status_is(200)
95
        ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
102
        ->header_like( 'Link' => qr/<http:\/\/.*[\?&]_page=2.*>; rel="next",/ )
96
        ->json_has('/0')
103
        ->json_has('/0')
97
        ->json_hasnt('/1')
104
        ->json_hasnt('/1')
98
        ->json_is('/0/name' => 'Manuel');
105
        ->json_is('/0/name' => 'Manuel');
Lines 105-120 subtest 'objects.search helper' => sub { Link Here
105
    });
112
    });
106
113
107
    # _match=starts_with
114
    # _match=starts_with
108
    $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=starts_with')
115
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=starts_with')
109
        ->status_is(200)
116
        ->status_is(200)
110
        ->json_has('/0')
117
        ->json_has('/0')
111
        ->json_has('/1')
118
        ->json_has('/1')
112
        ->json_hasnt('/2')
119
        ->json_has('/2')
120
        ->json_hasnt('/3')
113
        ->json_is('/0/name' => 'Manuel')
121
        ->json_is('/0/name' => 'Manuel')
114
        ->json_is('/1/name' => 'Manuela');
122
        ->json_is('/1/name' => 'Manuela')
123
        ->json_is('/2/name' => 'Manuelab');
115
124
116
    # _match=ends_with
125
    # _match=ends_with
117
    $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=ends_with')
126
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=ends_with')
118
        ->status_is(200)
127
        ->status_is(200)
119
        ->json_has('/0')
128
        ->json_has('/0')
120
        ->json_has('/1')
129
        ->json_has('/1')
Lines 123-144 subtest 'objects.search helper' => sub { Link Here
123
        ->json_is('/1/name' => 'Emanuel');
132
        ->json_is('/1/name' => 'Emanuel');
124
133
125
    # _match=exact
134
    # _match=exact
126
    $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=exact')
135
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=exact')
127
        ->status_is(200)
136
        ->status_is(200)
128
        ->json_has('/0')
137
        ->json_has('/0')
129
        ->json_hasnt('/1')
138
        ->json_hasnt('/1')
130
        ->json_is('/0/name' => 'Manuel');
139
        ->json_is('/0/name' => 'Manuel');
131
140
132
    # _match=contains
141
    # _match=contains
133
    $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=contains')
142
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=contains')
134
        ->status_is(200)
143
        ->status_is(200)
135
        ->json_has('/0')
144
        ->json_has('/0')
136
        ->json_has('/1')
145
        ->json_has('/1')
137
        ->json_has('/2')
146
        ->json_has('/2')
138
        ->json_hasnt('/3')
147
        ->json_has('/3')
148
        ->json_hasnt('/4')
139
        ->json_is('/0/name' => 'Manuel')
149
        ->json_is('/0/name' => 'Manuel')
140
        ->json_is('/1/name' => 'Manuela')
150
        ->json_is('/1/name' => 'Manuela')
141
        ->json_is('/2/name' => 'Emanuel');
151
        ->json_is('/2/name' => 'Manuelab')
152
        ->json_is('/3/name' => 'Emanuel');
142
153
143
    $schema->storage->txn_rollback;
154
    $schema->storage->txn_rollback;
144
};
155
};
(-)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