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

(-)a/Koha/REST/Plugin/Responses.pm (+82 lines)
Line 0 Link Here
1
package Koha::REST::Plugin::Responses;
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::Plugin';
21
22
=head1 NAME
23
24
Koha::REST::Plugin::Responses
25
26
=head1 API
27
28
=head2 Helper methods
29
30
=cut
31
32
sub register {
33
    my ( $self, $app ) = @_;
34
35
=head3 render_resource_deleted
36
37
    $c->render_resource_deleted
38
39
Provides a generic method rendering the standard response for resource deletion.
40
41
=cut
42
43
    $app->helper(
44
        'render_resource_deleted' => sub {
45
            my ($c) = @_;
46
47
            $c->render(
48
                status  => 204,
49
                openapi => q{},
50
            );
51
        }
52
    );
53
54
=head3 render_resource_not_found
55
56
    $c->render_resource_not_found
57
58
Provides a generic method rendering the standard response for resource not found.
59
60
=cut
61
62
    $app->helper(
63
        'render_resource_not_found' => sub {
64
            my ( $c, $name ) = @_;
65
66
            my $message =
67
                ($name)
68
                ? "$name not found"
69
                : "Resource not found";
70
71
            $c->render(
72
                status  => 404,
73
                openapi => {
74
                    error      => $message,
75
                    error_code => 'not_found',
76
                },
77
            );
78
        }
79
    );
80
}
81
82
1;
(-)a/Koha/REST/V1.pm (-5 / +6 lines)
Lines 150-160 sub startup { Link Here
150
        $self->app->log->warn( "Warning: Failed to fetch oauth configuration: " . $_ );
150
        $self->app->log->warn( "Warning: Failed to fetch oauth configuration: " . $_ );
151
    };
151
    };
152
152
153
    $self->plugin( 'Koha::REST::Plugin::Pagination' );
153
    $self->plugin('Koha::REST::Plugin::Pagination');
154
    $self->plugin( 'Koha::REST::Plugin::Query' );
154
    $self->plugin('Koha::REST::Plugin::Query');
155
    $self->plugin( 'Koha::REST::Plugin::Objects' );
155
    $self->plugin('Koha::REST::Plugin::Objects');
156
    $self->plugin( 'Koha::REST::Plugin::Exceptions' );
156
    $self->plugin('Koha::REST::Plugin::Exceptions');
157
    $self->plugin( 'Koha::REST::Plugin::Auth::IdP' );
157
    $self->plugin('Koha::REST::Plugin::Responses');
158
    $self->plugin('Koha::REST::Plugin::Auth::IdP');
158
    $self->plugin( 'Mojolicious::Plugin::OAuth2' => $oauth_configuration );
159
    $self->plugin( 'Mojolicious::Plugin::OAuth2' => $oauth_configuration );
159
}
160
}
160
161
(-)a/t/db_dependent/api/v1/responses.t (-1 / +117 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
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 Test::More tests => 2;
21
use Test::MockModule;
22
use Test::Mojo;
23
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
26
27
use Koha::Database;
28
use Koha::Exceptions;
29
30
my $schema  = Koha::Database->new->schema;
31
my $builder = t::lib::TestBuilder->new;
32
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35
subtest 'render_resource_not_found() tests' => sub {
36
37
    plan tests => 6;
38
39
    $schema->storage->txn_begin;
40
41
    my $authorized_patron = $builder->build_object(
42
        {
43
            class => 'Koha::Patrons',
44
            value => { flags => 1 },
45
        }
46
    );
47
    my $password = 'thePassword123';
48
    $authorized_patron->set_password( { password => $password, skip_validation => 1 } );
49
    my $userid = $authorized_patron->userid;
50
51
    my $mock_cities = Test::MockModule->new('Koha::REST::V1::Cities');
52
    $mock_cities->mock(
53
        'get',
54
        sub {
55
            my $c = shift->openapi->valid_input or return;
56
            $c->render_resource_not_found;
57
        }
58
    );
59
60
    my $t = Test::Mojo->new('Koha::REST::V1');
61
62
    $t->get_ok("//$userid:$password@/api/v1/cities/1")->status_is('404')->json_is(
63
        {
64
            error      => 'Resource not found',
65
            error_code => 'not_found',
66
        }
67
    );
68
69
    $mock_cities->mock(
70
        'get',
71
        sub {
72
            my $c = shift->openapi->valid_input or return;
73
            $c->render_resource_not_found("Thing");
74
        }
75
    );
76
77
    $t->get_ok("//$userid:$password@/api/v1/cities/1")->status_is('404')->json_is(
78
        {
79
            error      => 'Thing not found',
80
            error_code => 'not_found',
81
        }
82
    );
83
84
    $schema->storage->txn_rollback;
85
};
86
87
subtest 'render_resource_deleted() tests' => sub {
88
89
    plan tests => 3;
90
91
    $schema->storage->txn_begin;
92
93
    my $authorized_patron = $builder->build_object(
94
        {
95
            class => 'Koha::Patrons',
96
            value => { flags => 1 },
97
        }
98
    );
99
    my $password = 'thePassword123';
100
    $authorized_patron->set_password( { password => $password, skip_validation => 1 } );
101
    my $userid = $authorized_patron->userid;
102
103
    my $mock_cities = Test::MockModule->new('Koha::REST::V1::Cities');
104
    $mock_cities->mock(
105
        'delete',
106
        sub {
107
            my $c = shift->openapi->valid_input or return;
108
            return $c->render_resource_deleted;
109
        }
110
    );
111
112
    my $t = Test::Mojo->new('Koha::REST::V1');
113
114
    $t->delete_ok("//$userid:$password@/api/v1/cities/1")->status_is('204')->content_is(q{});
115
116
    $schema->storage->txn_rollback;
117
};

Return to bug 36495