Bug 26274: (QA follow-up) Minor fixes
[koha.git] / t / db_dependent / api / v1 / cashups.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 => 2;
21 use Test::Mojo;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use Koha::Cash::Register::Cashups;
27 use Koha::Cash::Register::Cashups;
28 use Koha::Database;
29
30 my $schema  = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 my $t = Test::Mojo->new('Koha::REST::V1');
34 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
36 subtest 'list() tests' => sub {
37
38     plan tests => 17;
39
40     $schema->storage->txn_begin;
41
42     Koha::Cash::Register::Cashups->search->delete;
43
44     my $librarian = $builder->build_object(
45         {
46             class => 'Koha::Patrons',
47             value => { flags => 25**2 }    # cash_management flag = 25
48         }
49     );
50     my $password = 'thePassword123';
51     $librarian->set_password( { password => $password, skip_validation => 1 } );
52     my $userid = $librarian->userid;
53
54     my $patron = $builder->build_object(
55         {
56             class => 'Koha::Patrons',
57             value => { flags => 0 }
58         }
59     );
60
61     $patron->set_password( { password => $password, skip_validation => 1 } );
62     my $unauth_userid = $patron->userid;
63
64     ## Authorized user tests
65     # No cash register, so 404 should be returned
66     $t->get_ok("//$userid:$password@/api/v1/cash_registers/1/cashups")
67       ->status_is(404)->json_is( '/error' => 'Register not found' );
68
69     my $register = $builder->build_object(
70         {
71             class => 'Koha::Cash::Registers'
72         }
73     );
74     my $register_id = $register->id;
75
76     # No cashups, so empty array should be returned
77     $t->get_ok(
78         "//$userid:$password@/api/v1/cash_registers/$register_id/cashups")
79       ->status_is(200)->json_is( [] );
80
81     my $cashup = $builder->build_object(
82         {
83             class => 'Koha::Cash::Register::Cashups',
84             value => {
85                 register_id => $register->id,
86                 code        => 'CASHUP',
87                 timestamp   => \'NOW() - INTERVAL 15 MINUTE'
88             }
89         }
90     );
91
92     # One cashup created, should get returned
93     $t->get_ok(
94         "//$userid:$password@/api/v1/cash_registers/$register_id/cashups")
95       ->status_is(200)->json_is( [ $cashup->to_api ] );
96
97     my $another_cashup = $builder->build_object(
98         {
99             class => 'Koha::Cash::Register::Cashups',
100             value => {
101                 register_id => $register->id,
102                 code        => 'CASHUP',
103                 timestamp   => \'NOW()'
104             }
105         }
106     );
107
108     # One more cashup created, both should be returned
109     $t->get_ok(
110         "//$userid:$password@/api/v1/cash_registers/$register_id/cashups")
111       ->status_is(200)
112       ->json_is( [ $another_cashup->to_api, $cashup->to_api, ] );
113
114     # Warn on unsupported query parameter
115     $t->get_ok(
116 "//$userid:$password@/api/v1/cash_registers/$register_id/cashups?cashup_blah=blah"
117     )->status_is(400)->json_is(
118         [
119             {
120                 path    => '/query/cashup_blah',
121                 message => 'Malformed query string'
122             }
123         ]
124     );
125
126     # Unauthorized access
127     $t->get_ok(
128         "//$unauth_userid:$password@/api/v1/cash_registers/$register_id/cashups"
129     )->status_is(403);
130
131     $schema->storage->txn_rollback;
132 };
133
134 subtest 'get() tests' => sub {
135
136     plan tests => 8;
137
138     $schema->storage->txn_begin;
139
140     my $cashup =
141       $builder->build_object( { class => 'Koha::Cash::Register::Cashups' } );
142     my $librarian = $builder->build_object(
143         {
144             class => 'Koha::Patrons',
145             value => { flags => 25**2 }    # cash_management flag = 25
146         }
147     );
148     my $password = 'thePassword123';
149     $librarian->set_password( { password => $password, skip_validation => 1 } );
150     my $userid = $librarian->userid;
151
152     my $patron = $builder->build_object(
153         {
154             class => 'Koha::Patrons',
155             value => { flags => 0 }
156         }
157     );
158
159     $patron->set_password( { password => $password, skip_validation => 1 } );
160     my $unauth_userid = $patron->userid;
161
162     $t->get_ok( "//$userid:$password@/api/v1/cashups/" . $cashup->id )
163       ->status_is(200)->json_is( $cashup->to_api );
164
165     $t->get_ok( "//$unauth_userid:$password@/api/v1/cashups/" . $cashup->id )
166       ->status_is(403);
167
168     my $cashup_to_delete =
169       $builder->build_object( { class => 'Koha::Cash::Register::Cashups' } );
170     my $non_existent_id = $cashup_to_delete->id;
171     $cashup_to_delete->delete;
172
173     $t->get_ok("//$userid:$password@/api/v1/cashups/$non_existent_id")
174       ->status_is(404)->json_is( '/error' => 'Cashup not found' );
175
176     $schema->storage->txn_rollback;
177 };