Bug 24545: Fix license statements
[koha.git] / t / db_dependent / api / v1 / stockrotationstage.t
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 => 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} ) ? 2 ** 24 : 0; # stockrotation == 24
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;