Bug 25898: Prohibit indirect object notation
[koha.git] / t / db_dependent / api / v1 / stockrotationstage.t
blob2c184ee303df1100f720ba74085e290b1c664b7f
1 #!/usr/bin/env perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More tests => 1;
21 use Test::Mojo;
22 use Test::Warn;
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
27 use C4::Auth;
28 use Koha::StockRotationStages;
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
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' );
37 my $remote_address = '127.0.0.1';
38 my $t = Test::Mojo->new('Koha::REST::V1');
40 subtest 'move() tests' => sub {
42 plan tests => 16;
44 $schema->storage->txn_begin;
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 } );
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},
60 });
61 my $stage2 = $builder->build({
62 source => 'Stockrotationstage',
63 value => {
64 branchcode_id => $library2->{branchcode},
65 rota_id => $rota->{rota_id},
67 });
68 my $rota_id = $rota->{rota_id};
69 my $stage1_id = $stage1->{stage_id};
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
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);
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
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" );
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
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" );
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
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" );
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
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" );
126 # Valid, authorised move
127 $tx = $t->ua->build_tx(
128 PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" =>
129 json => 2
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);
136 $schema->storage->txn_rollback;
139 sub create_user_and_session {
141 my $args = shift;
142 my $flags = ( $args->{authorized} ) ? 2 ** 24 : 0; # stockrotation == 24
143 my $dbh = C4::Context->dbh;
145 my $user = $builder->build(
147 source => 'Borrower',
148 value => {
149 flags => $flags
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;
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} );
169 return ( $user->{borrowernumber}, $session->id );