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

(-)a/Koha/REST/V1/Stage.pm (+47 lines)
Line 0 Link Here
1
package Koha::REST::V1::Stage;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Stockrotationrotas;
23
use Koha::Stockrotationstages;
24
25
sub move {
26
    my ($c, $args, $cb) = @_;
27
28
    my $rota = Koha::Stockrotationrotas->find($args->{rota_id});
29
    my $stage = Koha::Stockrotationstages->find($args->{stage_id});
30
31
    if ($stage && $rota) {
32
        my $result = $stage->move_to($args->{position});
33
        return $c->$cb({}, 200) if $result;
34
        return $c->$cb(
35
            { error => "Bad request - new position invalid"},
36
            400
37
        );
38
    } else {
39
        return $c->$cb(
40
            { error => "Not found - Invalid rota or stage ID"},
41
            404
42
        );
43
    }
44
45
}
46
47
1;
(-)a/api/v1/swagger/paths.json (+3 lines)
Lines 16-20 Link Here
16
  },
16
  },
17
  "/patrons/{borrowernumber}": {
17
  "/patrons/{borrowernumber}": {
18
    "$ref": "paths/patrons.json#/~1patrons~1{borrowernumber}"
18
    "$ref": "paths/patrons.json#/~1patrons~1{borrowernumber}"
19
  },
20
  "/rotas/{rota_id}/stages/{stage_id}/position": {
21
    "$ref": "paths/rotas.json#/~1rotas~1{rota_id}~1stages~1{stage_id}~1position"
19
  }
22
  }
20
}
23
}
(-)a/api/v1/swagger/paths/rotas.json (+60 lines)
Line 0 Link Here
1
{
2
    "/rotas/{rota_id}/stages/{stage_id}/position": {
3
        "put": {
4
            "operationId": "moveStage",
5
            "tags": ["rotas"],
6
            "parameters": [{
7
                "name": "rota_id",
8
                "in": "path",
9
                "required": true,
10
                "description": "A rotas ID",
11
                "type": "integer"
12
            }, {
13
                "name": "stage_id",
14
                "in": "path",
15
                "required": true,
16
                "description": "A stages ID",
17
                "type": "integer"
18
            }, {
19
                "name": "position",
20
                "in": "body",
21
                "required": true,
22
                "description": "A stages position in the rota",
23
                "schema": {
24
                    "type": "integer"
25
                }
26
            }],
27
            "produces": [
28
                "application/json"
29
            ],
30
            "responses": {
31
                "200": {
32
                    "description": "OK"
33
                },
34
                "400": {
35
                    "description": "Bad request",
36
                    "schema": {
37
                        "$ref": "../definitions.json#/error"
38
                    }
39
                },
40
                "403": {
41
                    "description": "Access forbidden",
42
                    "schema": {
43
                        "$ref": "../definitions.json#/error"
44
                    }
45
                },
46
                "404": {
47
                    "description": "Position not found",
48
                    "schema": {
49
                        "$ref": "../definitions.json#/error"
50
                    }
51
                }
52
            },
53
            "x-koha-authorization": {
54
                "permissions": {
55
                    "borrowers": "1"
56
                }
57
            }
58
        }
59
    }
60
}
(-)a/t/db_dependent/api/v1/stockrotationstage.t (-1 / +172 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 under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Test::More tests => 1;
21
use Test::Mojo;
22
use Test::Warn;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use C4::Auth;
28
use Koha::Stockrotationstages;
29
30
my $schema  = Koha::Database->new->schema;
31
my $builder = t::lib::TestBuilder->new;
32
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' );
36
37
my $remote_address = '127.0.0.1';
38
my $t              = Test::Mojo->new('Koha::REST::V1');
39
40
subtest 'move() tests' => sub {
41
42
    plan tests => 16;
43
44
    $schema->storage->txn_begin;
45
46
    my ( $unauthorized_borrowernumber, $unauthorized_session_id ) =
47
      create_user_and_session( { authorized => 0 } );
48
    my ( $authorized_borrowernumber, $authorized_session_id ) =
49
      create_user_and_session( { authorized => 1 } );
50
51
    my $library1 = $builder->build({ source => 'Branch' });
52
    my $library2 = $builder->build({ source => 'Branch' });
53
    my $rota = $builder->build({ source => 'Stockrotationrota' });
54
    my $stage1 = $builder->build({
55
        source => 'Stockrotationstage',
56
        value  => {
57
            branchcode_id => $library1->{branchcode},
58
            rota_id       => $rota->{rota_id},
59
        }
60
    });
61
    my $stage2 = $builder->build({
62
        source => 'Stockrotationstage',
63
        value  => {
64
            branchcode_id => $library2->{branchcode},
65
            rota_id       => $rota->{rota_id},
66
        }
67
    });
68
    my $rota_id = $rota->{rota_id};
69
    my $stage1_id = $stage1->{stage_id};
70
71
    # Unauthorized attempt to update
72
    my $tx = $t->ua->build_tx(
73
      PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" =>
74
      json => 2
75
    );
76
    $tx->req->cookies(
77
        { name => 'CGISESSID', value => $unauthorized_session_id } );
78
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
79
    $t->request_ok($tx)->status_is(403);
80
81
    # Invalid attempt to move a stage on a non-existant rota
82
    $tx = $t->ua->build_tx(
83
      PUT => "/api/v1/rotas/99999999/stages/$stage1_id/position" =>
84
      json => 2
85
    );
86
    $tx->req->cookies(
87
        { name => 'CGISESSID', value => $authorized_session_id } );
88
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
89
    $t->request_ok($tx)->status_is(404)
90
      ->json_is( '/error' => "Not found - Invalid rota or stage ID" );
91
92
    # Invalid attempt to move an non-existant stage
93
    $tx = $t->ua->build_tx(
94
      PUT => "/api/v1/rotas/$rota_id/stages/999999999/position" =>
95
      json => 2
96
    );
97
    $tx->req->cookies(
98
        { name => 'CGISESSID', value => $authorized_session_id } );
99
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
100
    $t->request_ok($tx)->status_is(404)
101
      ->json_is( '/error' => "Not found - Invalid rota or stage ID" );
102
103
    # Invalid attempt to move stage to current position
104
    my $curr_position = $stage1->{position};
105
    $tx = $t->ua->build_tx(
106
      PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" =>
107
      json => $curr_position
108
    );
109
    $tx->req->cookies(
110
        { name => 'CGISESSID', value => $authorized_session_id } );
111
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
112
    $t->request_ok($tx)->status_is(400)
113
      ->json_is( '/error' => "Bad request - new position invalid" );
114
115
    # Invalid attempt to move stage to invalid position
116
    $tx = $t->ua->build_tx(
117
      PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" =>
118
      json => 99999999
119
    );
120
    $tx->req->cookies(
121
        { name => 'CGISESSID', value => $authorized_session_id } );
122
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
123
    $t->request_ok($tx)->status_is(400)
124
      ->json_is( '/error' => "Bad request - new position invalid" );
125
126
    # Valid, authorised move
127
    $tx = $t->ua->build_tx(
128
      PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" =>
129
      json => 2
130
    );
131
    $tx->req->cookies(
132
        { name => 'CGISESSID', value => $authorized_session_id } );
133
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
134
    $t->request_ok($tx)->status_is(200);
135
136
    $schema->storage->txn_rollback;
137
};
138
139
sub create_user_and_session {
140
141
    my $args  = shift;
142
    my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0;
143
    my $dbh   = C4::Context->dbh;
144
145
    my $user = $builder->build(
146
        {
147
            source => 'Borrower',
148
            value  => {
149
                flags => $flags
150
            }
151
        }
152
    );
153
154
    # Create a session for the authorized user
155
    my $session = C4::Auth::get_session('');
156
    $session->param( 'number',   $user->{borrowernumber} );
157
    $session->param( 'id',       $user->{userid} );
158
    $session->param( 'ip',       '127.0.0.1' );
159
    $session->param( 'lasttime', time() );
160
    $session->flush;
161
162
    if ( $args->{authorized} ) {
163
        $dbh->do( "
164
            INSERT INTO user_permissions (borrowernumber,module_bit,code)
165
            VALUES (?,3,'parameters_remaining_permissions')", undef,
166
            $user->{borrowernumber} );
167
    }
168
169
    return ( $user->{borrowernumber}, $session->id );
170
}
171
172
1;

Return to bug 11897