Bug 28271: Add the ability to set a new lost status when a claim is resolved
[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
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use Koha::StockRotationStages;
27
28 my $schema  = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
30
31 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
32
33 my $t = Test::Mojo->new('Koha::REST::V1');
34
35 subtest 'move() tests' => sub {
36
37     plan tests => 16;
38
39     $schema->storage->txn_begin;
40
41     my $authorized_patron = $builder->build_object({
42         class => 'Koha::Patrons',
43         value => { flags => 2 ** 24 } # stockrotation => 24
44     });
45     my $password = 'thePassword123';
46     $authorized_patron->set_password({ password => $password, skip_validation => 1 });
47     my $auth_userid = $authorized_patron->userid;
48
49     my $unauthorized_patron = $builder->build_object({
50         class => 'Koha::Patrons',
51         value => { flags => 0 }
52     });
53     $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
54     my $unauth_userid = $unauthorized_patron->userid;
55
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',
61         value  => {
62             branchcode_id => $library1->{branchcode},
63             rota_id       => $rota->{rota_id},
64         }
65     });
66     my $stage2 = $builder->build({
67         source => 'Stockrotationstage',
68         value  => {
69             branchcode_id => $library2->{branchcode},
70             rota_id       => $rota->{rota_id},
71         }
72     });
73     my $rota_id = $rota->{rota_id};
74     my $stage1_id = $stage1->{stage_id};
75
76     # Unauthorized attempt to update
77     $t->put_ok( "//$unauth_userid:$password@/api/v1/rotas/$rota_id/stages/$stage1_id/position"
78           => json => 2 )
79       ->status_is(403);
80
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"
83           => json => 2 )
84       ->status_is(404)
85       ->json_is( '/error' => "Not found - Invalid rota or stage ID" );
86
87     # Invalid attempt to move an non-existant stage
88     $t->put_ok( "//$auth_userid:$password@/api/v1/rotas/$rota_id/stages/999999999/position"
89           => json => 2 )
90       ->status_is(404)
91       ->json_is( '/error' => "Not found - Invalid rota or stage ID" );
92
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 )
97       ->status_is(400)
98       ->json_is( '/error' => "Bad request - new position invalid" );
99
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 )
103       ->status_is(400)
104       ->json_is( '/error' => "Bad request - new position invalid" );
105
106     # Valid, authorised move
107     $t->put_ok( "//$auth_userid:$password@/api/v1/rotas/$rota_id/stages/$stage1_id/position"
108           => json => 2 )
109       ->status_is(200);
110
111     $schema->storage->txn_rollback;
112 };