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

(-)a/Koha/REST/V1/Illrequests.pm (-38 / +12 lines)
Lines 20-32 use Modern::Perl; Link Here
20
use Mojo::Base 'Mojolicious::Controller';
20
use Mojo::Base 'Mojolicious::Controller';
21
21
22
use Koha::Illrequests;
22
use Koha::Illrequests;
23
use Koha::Library;
23
use Koha::Libraries;
24
24
25
sub list {
25
sub list {
26
    my ($c, $args, $cb) = @_;
26
    my $c = shift->openapi->valid_input or return;
27
27
28
    my $args = $c->req->params->to_hash // {};
28
    my $filter;
29
    my $filter;
29
    $args //= {};
30
    my $output = [];
30
    my $output = [];
31
31
32
    # Create a hash where all keys are embedded values
32
    # Create a hash where all keys are embedded values
Lines 44-85 sub list { Link Here
44
44
45
    my $requests = Koha::Illrequests->search($filter);
45
    my $requests = Koha::Illrequests->search($filter);
46
46
47
    while (my $request = $requests->next) {
47
    if ( scalar (keys %embed) )
48
        my $unblessed = $request->unblessed;
48
    {
49
        # Add the request's id_prefix
49
        # Need to embed stuff
50
        $unblessed->{id_prefix} = $request->id_prefix;
50
        my @results = map { $_->TO_JSON(\%embed) } $requests->as_list;
51
        # Augment the request response with patron details
51
        return $c->render( status => 200, openapi => \@results );
52
        # if appropriate
52
    }
53
        if (defined $embed{patron}) {
53
    else
54
            my $patron = $request->patron;
54
    {
55
            $unblessed->{patron} = {
55
        return $c->render( status => 200, openapi => $requests );
56
                firstname  => $patron->firstname,
57
                surname    => $patron->surname,
58
                cardnumber => $patron->cardnumber
59
            };
60
        }
61
        # Augment the request response with metadata details
62
        # if appropriate
63
        if (defined $embed{metadata}) {
64
            $unblessed->{metadata} = $request->metadata;
65
        }
66
        # Augment the request response with status details
67
        # if appropriate
68
        if (defined $embed{capabilities}) {
69
            $unblessed->{capabilities} = $request->capabilities;
70
        }
71
        # Augment the request response with branch details
72
        # if appropriate
73
        if (defined $embed{branch}) {
74
            $unblessed->{branch} = Koha::Libraries->find(
75
                $request->branchcode
76
            )->unblessed;
77
        }
78
        push @{$output}, $unblessed
79
    }
56
    }
80
81
    return $c->$cb( $output, 200 );
82
83
}
57
}
84
58
85
1;
59
1;
(-)a/api/v1/swagger/paths/illrequests.json (-5 / +36 lines)
Lines 1-8 Link Here
1
{
1
{
2
    "/illrequests": {
2
    "/illrequests": {
3
        "get": {
3
        "get": {
4
            "x-mojo-controller": "Koha::REST::V1::Illrequests",
4
            "x-mojo-to": "Illrequests#list",
5
            "operationId": "list",
5
            "operationId": "listIllrequests",
6
            "tags": ["illrequests"],
6
            "tags": ["illrequests"],
7
            "parameters": [{
7
            "parameters": [{
8
                "name": "embed",
8
                "name": "embed",
Lines 16-22 Link Here
16
                    "enum": [
16
                    "enum": [
17
                        "patron",
17
                        "patron",
18
                        "branch",
18
                        "branch",
19
                        "capabilities"
19
                        "capabilities",
20
                        "metadata"
20
                    ]
21
                    ]
21
                }
22
                }
22
            }, {
23
            }, {
Lines 85-96 Link Here
85
            ],
86
            ],
86
            "responses": {
87
            "responses": {
87
                "200": {
88
                "200": {
88
                    "description": "OK"
89
                    "description": "A list of ILL requests"
90
                },
91
                "401": {
92
                  "description": "Authentication required",
93
                  "schema": {
94
                    "$ref": "../definitions.json#/error"
95
                  }
96
                },
97
                "403": {
98
                  "description": "Access forbidden",
99
                  "schema": {
100
                    "$ref": "../definitions.json#/error"
101
                  }
102
                },
103
                "404": {
104
                  "description": "ILL requests not found",
105
                  "schema": {
106
                    "$ref": "../definitions.json#/error"
107
                  }
108
                },
109
                "500": {
110
                  "description": "Internal server error",
111
                  "schema": {
112
                    "$ref": "../definitions.json#/error"
113
                  }
114
                },
115
                "503": {
116
                  "description": "Under maintenance",
117
                  "schema": {
118
                    "$ref": "../definitions.json#/error"
119
                  }
89
                }
120
                }
90
            },
121
            },
91
            "x-koha-authorization": {
122
            "x-koha-authorization": {
92
                "permissions": {
123
                "permissions": {
93
                    "borrowers": "1"
124
                    "ill": "1"
94
                }
125
                }
95
            }
126
            }
96
        }
127
        }
(-)a/t/db_dependent/api/v1/illrequests.t (-50 / +64 lines)
Lines 18-23 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 1;
20
use Test::More tests => 1;
21
use Test::MockModule;
21
use Test::Mojo;
22
use Test::Mojo;
22
use Test::Warn;
23
use Test::Warn;
23
24
Lines 30-37 use Koha::Illrequests; Link Here
30
my $schema  = Koha::Database->new->schema;
31
my $schema  = Koha::Database->new->schema;
31
my $builder = t::lib::TestBuilder->new;
32
my $builder = t::lib::TestBuilder->new;
32
33
33
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
34
# this affects the other REST api tests
35
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
34
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
36
35
37
my $remote_address = '127.0.0.1';
36
my $remote_address = '127.0.0.1';
Lines 39-51 my $t = Test::Mojo->new('Koha::REST::V1'); Link Here
39
38
40
subtest 'list() tests' => sub {
39
subtest 'list() tests' => sub {
41
40
42
    plan tests => 6;
41
    plan tests => 15;
42
43
    my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
44
    # Mock ->capabilities
45
    $illreqmodule->mock( 'capabilities', sub { return 'capable'; } );
46
    # Mock ->metadata
47
    $illreqmodule->mock( 'metadata', sub { return 'metawhat?'; } );
43
48
44
    $schema->storage->txn_begin;
49
    $schema->storage->txn_begin;
45
50
46
    Koha::Illrequests->search->delete;
51
    Koha::Illrequests->search->delete;
47
    my ( $borrowernumber, $session_id ) =
52
    # ill => 22 (userflags.sql)
48
      create_user_and_session( { authorized => 1 } );
53
    my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 22 });
49
54
50
    ## Authorized user tests
55
    ## Authorized user tests
51
    # No requests, so empty array should be returned
56
    # No requests, so empty array should be returned
Lines 54-93 subtest 'list() tests' => sub { Link Here
54
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
59
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
55
    $t->request_ok($tx)->status_is(200)->json_is( [] );
60
    $t->request_ok($tx)->status_is(200)->json_is( [] );
56
61
57
#    my $city_country = 'France';
62
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
58
#    my $city         = $builder->build(
63
    my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
59
#        { source => 'City', value => { city_country => $city_country } } );
64
60
#
65
    # Create an ILL request
61
#    # One city created, should get returned
66
    my $illrequest = $builder->build_object(
62
#    $tx = $t->ua->build_tx( GET => '/api/v1/cities' );
67
        {
63
#    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
68
            class => 'Koha::Illrequests',
64
#    $tx->req->env( { REMOTE_ADDR => $remote_address } );
69
            value => {
65
#    $t->request_ok($tx)->status_is(200)->json_is( [$city] );
70
                branchcode     => $library->branchcode,
66
#
71
                borrowernumber => $patron->borrowernumber
67
#    my $another_city = $builder->build(
72
            }
68
#        { source => 'City', value => { city_country => $city_country } } );
73
        }
69
#    my $city_with_another_country = $builder->build( { source => 'City' } );
74
    );
70
#
75
71
#    # Two cities created, they should both be returned
76
    # One illrequest created, should get returned
72
#    $tx = $t->ua->build_tx( GET => '/api/v1/cities' );
77
    $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
73
#    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
78
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
74
#    $tx->req->env( { REMOTE_ADDR => $remote_address } );
79
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
75
#    $t->request_ok($tx)->status_is(200)
80
    $t->request_ok($tx)->status_is(200)->json_is( [ $illrequest->TO_JSON ] );
76
#      ->json_is( [ $city, $another_city, $city_with_another_country ] );
81
77
#
82
    # One illrequest created, returned with augmented data
78
#    # Filtering works, two cities sharing city_country
83
    $tx = $t->ua->build_tx( GET =>
79
#    $tx =
84
          '/api/v1/illrequests?embed=patron,branch,capabilities,metadata' );
80
#      $t->ua->build_tx( GET => "/api/v1/cities?city_country=" . $city_country );
85
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
81
#    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
86
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
82
#    $tx->req->env( { REMOTE_ADDR => $remote_address } );
87
    $t->request_ok($tx)->status_is(200)->json_is(
83
#    my $result =
88
        [
84
#      $t->request_ok($tx)->status_is(200)->json_is( [ $city, $another_city ] );
89
            $illrequest->TO_JSON(
85
#
90
                { patron => 1, branch => 1, capabilities => 1, metadata => 1 }
86
#    $tx = $t->ua->build_tx(
91
            )
87
#        GET => "/api/v1/cities?city_name=" . $city->{city_name} );
92
        ]
88
#    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
93
    );
89
#    $tx->req->env( { REMOTE_ADDR => $remote_address } );
94
90
#    $t->request_ok($tx)->status_is(200)->json_is( [$city] );
95
    # Create another ILL request
96
    my $illrequest2 = $builder->build_object(
97
        {
98
            class => 'Koha::Illrequests',
99
            value => {
100
                branchcode     => $library->branchcode,
101
                borrowernumber => $patron->borrowernumber
102
            }
103
        }
104
    );
105
106
    # Two illrequest created, should get returned
107
    $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
108
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
109
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
110
    $t->request_ok($tx)->status_is(200)
111
      ->json_is( [ $illrequest->TO_JSON, $illrequest2->TO_JSON ] );
91
112
92
    # Warn on unsupported query parameter
113
    # Warn on unsupported query parameter
93
    $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
114
    $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
Lines 102-110 subtest 'list() tests' => sub { Link Here
102
123
103
sub create_user_and_session {
124
sub create_user_and_session {
104
125
105
    my $args  = shift;
126
    my $args = shift;
106
    my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0;
127
    my $dbh  = C4::Context->dbh;
107
    my $dbh   = C4::Context->dbh;
128
129
    my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
108
130
109
    my $user = $builder->build(
131
    my $user = $builder->build(
110
        {
132
        {
Lines 123-135 sub create_user_and_session { Link Here
123
    $session->param( 'lasttime', time() );
145
    $session->param( 'lasttime', time() );
124
    $session->flush;
146
    $session->flush;
125
147
126
    if ( $args->{authorized} ) {
127
        $dbh->do( "
128
            INSERT INTO user_permissions (borrowernumber,module_bit,code)
129
            VALUES (?,3,'parameters_remaining_permissions')", undef,
130
            $user->{borrowernumber} );
131
    }
132
133
    return ( $user->{borrowernumber}, $session->id );
148
    return ( $user->{borrowernumber}, $session->id );
134
}
149
}
135
150
136
- 

Return to bug 7317