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 => 2;
23 use t::lib::TestBuilder;
26 use Koha::Cash::Register::Cashups;
27 use Koha::Cash::Register::Cashups;
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
33 my $t = Test::Mojo->new('Koha::REST::V1');
34 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36 subtest 'list() tests' => sub {
40 $schema->storage->txn_begin;
42 Koha::Cash::Register::Cashups->search->delete;
44 my $librarian = $builder->build_object(
46 class => 'Koha::Patrons',
47 value => { flags => 25**2 } # cash_management flag = 25
50 my $password = 'thePassword123';
51 $librarian->set_password( { password => $password, skip_validation => 1 } );
52 my $userid = $librarian->userid;
54 my $patron = $builder->build_object(
56 class => 'Koha::Patrons',
57 value => { flags => 0 }
61 $patron->set_password( { password => $password, skip_validation => 1 } );
62 my $unauth_userid = $patron->userid;
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' );
69 my $register = $builder->build_object(
71 class => 'Koha::Cash::Registers'
74 my $register_id = $register->id;
76 # No cashups, so empty array should be returned
78 "//$userid:$password@/api/v1/cash_registers/$register_id/cashups")
79 ->status_is(200)->json_is( [] );
81 my $cashup = $builder->build_object(
83 class => 'Koha::Cash::Register::Cashups',
85 register_id => $register->id,
87 timestamp => \'NOW() - INTERVAL 15 MINUTE'
92 # One cashup created, should get returned
94 "//$userid:$password@/api/v1/cash_registers/$register_id/cashups")
95 ->status_is(200)->json_is( [ $cashup->to_api ] );
97 my $another_cashup = $builder->build_object(
99 class => 'Koha::Cash::Register::Cashups',
101 register_id => $register->id,
103 timestamp => \'NOW()'
108 # One more cashup created, both should be returned
110 "//$userid:$password@/api/v1/cash_registers/$register_id/cashups")
112 ->json_is( [ $another_cashup->to_api, $cashup->to_api, ] );
114 # Warn on unsupported query parameter
116 "//$userid:$password@/api/v1/cash_registers/$register_id/cashups?cashup_blah=blah"
117 )->status_is(400)->json_is(
120 path => '/query/cashup_blah',
121 message => 'Malformed query string'
126 # Unauthorized access
128 "//$unauth_userid:$password@/api/v1/cash_registers/$register_id/cashups"
131 $schema->storage->txn_rollback;
134 subtest 'get() tests' => sub {
138 $schema->storage->txn_begin;
141 $builder->build_object( { class => 'Koha::Cash::Register::Cashups' } );
142 my $librarian = $builder->build_object(
144 class => 'Koha::Patrons',
145 value => { flags => 25**2 } # cash_management flag = 25
148 my $password = 'thePassword123';
149 $librarian->set_password( { password => $password, skip_validation => 1 } );
150 my $userid = $librarian->userid;
152 my $patron = $builder->build_object(
154 class => 'Koha::Patrons',
155 value => { flags => 0 }
159 $patron->set_password( { password => $password, skip_validation => 1 } );
160 my $unauth_userid = $patron->userid;
162 $t->get_ok( "//$userid:$password@/api/v1/cashups/" . $cashup->id )
163 ->status_is(200)->json_is( $cashup->to_api );
165 $t->get_ok( "//$unauth_userid:$password@/api/v1/cashups/" . $cashup->id )
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;
173 $t->get_ok("//$userid:$password@/api/v1/cashups/$non_existent_id")
174 ->status_is(404)->json_is( '/error' => 'Cashup not found' );
176 $schema->storage->txn_rollback;