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 58-71 my $builder = t::lib::TestBuilder->new; Link Here
58
58
59
subtest 'objects.search helper' => sub {
59
subtest 'objects.search helper' => sub {
60
60
61
    plan tests => 34;
61
    plan tests => 38;
62
62
63
    $schema->storage->txn_begin;
63
    $schema->storage->txn_begin;
64
64
65
    # Remove existing cities to have more control on the search restuls
65
    # Remove existing cities to have more control on the search results
66
    Koha::Cities->delete;
66
    Koha::Cities->delete;
67
67
68
    # Create two sample patrons that match the query
68
    # Create three sample cities that match the query. This makes sure we
69
    # always have a "next" link regardless of Mojolicious::Plugin::OpenAPI version.
69
    $builder->build_object({
70
    $builder->build_object({
70
        class => 'Koha::Cities',
71
        class => 'Koha::Cities',
71
        value => {
72
        value => {
Lines 78-87 subtest 'objects.search helper' => sub { Link Here
78
            city_name => 'Manuela'
79
            city_name => 'Manuela'
79
        }
80
        }
80
    });
81
    });
82
    $builder->build_object({
83
        class => 'Koha::Cities',
84
        value => {
85
            city_name => 'Manuelab'
86
        }
87
    });
81
88
82
    $t->get_ok('/cities?name=manuel&_per_page=1&_page=1')
89
    $t->get_ok('/cities?name=manuel&_per_page=1&_page=1')
83
        ->status_is(200)
90
        ->status_is(200)
84
        ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
91
        ->header_like( 'Link' => qr/<http:\/\/.*[\?&]_page=2.*>; rel="next",/ )
85
        ->json_has('/0')
92
        ->json_has('/0')
86
        ->json_hasnt('/1')
93
        ->json_hasnt('/1')
87
        ->json_is('/0/name' => 'Manuel');
94
        ->json_is('/0/name' => 'Manuel');
Lines 94-109 subtest 'objects.search helper' => sub { Link Here
94
    });
101
    });
95
102
96
    # _match=starts_with
103
    # _match=starts_with
97
    $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=starts_with')
104
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=starts_with')
98
        ->status_is(200)
105
        ->status_is(200)
99
        ->json_has('/0')
106
        ->json_has('/0')
100
        ->json_has('/1')
107
        ->json_has('/1')
101
        ->json_hasnt('/2')
108
        ->json_has('/2')
109
        ->json_hasnt('/3')
102
        ->json_is('/0/name' => 'Manuel')
110
        ->json_is('/0/name' => 'Manuel')
103
        ->json_is('/1/name' => 'Manuela');
111
        ->json_is('/1/name' => 'Manuela')
112
        ->json_is('/2/name' => 'Manuelab');
104
113
105
    # _match=ends_with
114
    # _match=ends_with
106
    $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=ends_with')
115
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=ends_with')
107
        ->status_is(200)
116
        ->status_is(200)
108
        ->json_has('/0')
117
        ->json_has('/0')
109
        ->json_has('/1')
118
        ->json_has('/1')
Lines 112-133 subtest 'objects.search helper' => sub { Link Here
112
        ->json_is('/1/name' => 'Emanuel');
121
        ->json_is('/1/name' => 'Emanuel');
113
122
114
    # _match=exact
123
    # _match=exact
115
    $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=exact')
124
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=exact')
116
        ->status_is(200)
125
        ->status_is(200)
117
        ->json_has('/0')
126
        ->json_has('/0')
118
        ->json_hasnt('/1')
127
        ->json_hasnt('/1')
119
        ->json_is('/0/name' => 'Manuel');
128
        ->json_is('/0/name' => 'Manuel');
120
129
121
    # _match=contains
130
    # _match=contains
122
    $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=contains')
131
    $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=contains')
123
        ->status_is(200)
132
        ->status_is(200)
124
        ->json_has('/0')
133
        ->json_has('/0')
125
        ->json_has('/1')
134
        ->json_has('/1')
126
        ->json_has('/2')
135
        ->json_has('/2')
127
        ->json_hasnt('/3')
136
        ->json_has('/3')
137
        ->json_hasnt('/4')
128
        ->json_is('/0/name' => 'Manuel')
138
        ->json_is('/0/name' => 'Manuel')
129
        ->json_is('/1/name' => 'Manuela')
139
        ->json_is('/1/name' => 'Manuela')
130
        ->json_is('/2/name' => 'Emanuel');
140
        ->json_is('/2/name' => 'Manuelab')
141
        ->json_is('/3/name' => 'Emanuel');
131
142
132
    $schema->storage->txn_rollback;
143
    $schema->storage->txn_rollback;
133
};
144
};
(-)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