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>.
20 use Test::More tests => 1;
23 use t::lib::TestBuilder;
26 use Koha::StockRotationStages;
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
31 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
33 my $t = Test::Mojo->new('Koha::REST::V1');
35 subtest 'move() tests' => sub {
39 $schema->storage->txn_begin;
41 my $authorized_patron = $builder->build_object({
42 class => 'Koha::Patrons',
43 value => { flags => 2 ** 24 } # stockrotation => 24
45 my $password = 'thePassword123';
46 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
47 my $auth_userid = $authorized_patron->userid;
49 my $unauthorized_patron = $builder->build_object({
50 class => 'Koha::Patrons',
51 value => { flags => 0 }
53 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
54 my $unauth_userid = $unauthorized_patron->userid;
56 my $library1 = $builder->build({ source => 'Branch' });
57 my $library2 = $builder->build({ source => 'Branch' });
58 my $rota = $builder->build({ source => 'Stockrotationrota' });
59 my $stage1 = $builder->build({
60 source => 'Stockrotationstage',
62 branchcode_id => $library1->{branchcode},
63 rota_id => $rota->{rota_id},
66 my $stage2 = $builder->build({
67 source => 'Stockrotationstage',
69 branchcode_id => $library2->{branchcode},
70 rota_id => $rota->{rota_id},
73 my $rota_id = $rota->{rota_id};
74 my $stage1_id = $stage1->{stage_id};
76 # Unauthorized attempt to update
77 $t->put_ok( "//$unauth_userid:$password@/api/v1/rotas/$rota_id/stages/$stage1_id/position"
81 # Invalid attempt to move a stage on a non-existant rota
82 $t->put_ok( "//$auth_userid:$password@/api/v1/rotas/99999999/stages/$stage1_id/position"
85 ->json_is( '/error' => "Not found - Invalid rota or stage ID" );
87 # Invalid attempt to move an non-existant stage
88 $t->put_ok( "//$auth_userid:$password@/api/v1/rotas/$rota_id/stages/999999999/position"
91 ->json_is( '/error' => "Not found - Invalid rota or stage ID" );
93 # Invalid attempt to move stage to current position
94 my $curr_position = $stage1->{position};
95 $t->put_ok( "//$auth_userid:$password@/api/v1/rotas/$rota_id/stages/$stage1_id/position"
96 => json => $curr_position )
98 ->json_is( '/error' => "Bad request - new position invalid" );
100 # Invalid attempt to move stage to invalid position
101 $t->put_ok( "//$auth_userid:$password@/api/v1/rotas/$rota_id/stages/$stage1_id/position"
102 => json => 99999999 )
104 ->json_is( '/error' => "Bad request - new position invalid" );
106 # Valid, authorised move
107 $t->put_ok( "//$auth_userid:$password@/api/v1/rotas/$rota_id/stages/$stage1_id/position"
111 $schema->storage->txn_rollback;