View | Details | Raw Unified | Return to bug 36641
Collapse All | Expand All

(-)a/t/db_dependent/api/v1/circulation_rules.t (-1 / +108 lines)
Line 0 Link Here
0
- 
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::CirculationRules;
27
use Koha::Database;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
my $t = Test::Mojo->new('Koha::REST::V1');
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35
subtest 'list_effective_rules() tests' => sub {
36
37
    plan tests => 20;
38
39
    $schema->storage->txn_begin;
40
41
    my $categorycode = $builder->build( { source => 'Category' } )->{'categorycode'};
42
    my $itemtype     = $builder->build( { source => 'Itemtype' } )->{'itemtype'};
43
    my $branchcode   = $builder->build( { source => 'Branch' } )->{'branchcode'};
44
45
    my $librarian = $builder->build_object(
46
        {
47
            class => 'Koha::Patrons',
48
            value => { flags => 2**2 }    # catalogue flag = 2
49
        }
50
    );
51
    my $password = 'thePassword123';
52
    $librarian->set_password( { password => $password, skip_validation => 1 } );
53
    my $userid = $librarian->userid;
54
55
    my $patron = $builder->build_object(
56
        {
57
            class => 'Koha::Patrons',
58
            value => { flags => 0 }
59
        }
60
    );
61
62
    $patron->set_password( { password => $password, skip_validation => 1 } );
63
    my $unauth_userid = $patron->userid;
64
65
    ## Authorized user tests
66
    # No circulation_rules, so empty array should be returned
67
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( [] );
68
69
    my $rules = $builder->build_object( { class => 'Koha::CirculationRules' } );
70
71
    # One rules created, should get returned
72
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( [ $rules->to_api ] );
73
74
    my $another_rules = $builder->build_object(
75
        { class => 'Koha::CirculationRules', value => { rules_country => $rules->rules_country } } );
76
    my $rules_with_another_country = $builder->build_object( { class => 'Koha::CirculationRules' } );
77
78
    # Two circulation_rules created, they should both be returned
79
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is(
80
        [
81
            $rules->to_api,
82
            $another_rules->to_api,
83
            $rules_with_another_country->to_api
84
        ]
85
    );
86
87
    # Filtering works, two circulation_rules sharing rules_country
88
    $t->get_ok( "//$userid:$password@/api/v1/circulation_rules?country=" . $rules->rules_country )->status_is(200)
89
        ->json_is(
90
        [
91
            $rules->to_api,
92
            $another_rules->to_api
93
        ]
94
        );
95
96
    $t->get_ok( "//$userid:$password@/api/v1/circulation_rules?name=" . $rules->rules_name )->status_is(200)
97
        ->json_is( [ $rules->to_api ] );
98
99
    # Warn on unsupported query parameter
100
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules?rules_blah=blah")->status_is(400)
101
        ->json_is( [ { path => '/query/rules_blah', message => 'Malformed query string' } ] );
102
103
    # Unauthorized access
104
    $t->get_ok("//$unauth_userid:$password@/api/v1/circulation_rules")->status_is(403);
105
106
    $schema->storage->txn_rollback;
107
};
108

Return to bug 36641