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

(-)a/t/db_dependent/api/v1/circulation_rules.t (-1 / +162 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 => 26;
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
    Koha::CirculationRules->delete;
45
46
    my $librarian = $builder->build_object(
47
        {
48
            class => 'Koha::Patrons',
49
            value => { flags => 2 }     # circulate
50
        }
51
    );
52
    my $password = 'thePassword123';
53
    $librarian->set_password( { password => $password, skip_validation => 1 } );
54
    my $userid = $librarian->userid;
55
56
    my $patron = $builder->build_object(
57
        {
58
            class => 'Koha::Patrons',
59
            value => { flags => 0 }
60
        }
61
    );
62
63
    $patron->set_password( { password => $password, skip_validation => 1 } );
64
    my $unauth_userid = $patron->userid;
65
66
    ## Authorized user tests
67
    # No circulation_rules, so empty hash should be returned
68
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( {} );
69
70
    # One rule created, should get returned
71
    ok(
72
        Koha::CirculationRule->new(
73
            {
74
                branchcode   => undef,
75
                categorycode => undef,
76
                itemtype     => undef,
77
                rule_name    => 'fine',
78
                rule_value   => 2,
79
            }
80
        )->store,
81
        'Given I added an issuing rule branchcode => undef,' . ' categorycode => undef, itemtype => undef,'
82
    );
83
84
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)
85
        ->json_is( '' => { 'fine' => 2 }, "Our single rule is returned" );
86
87
    # Two circulation_rules created, they should both be returned
88
    ok(
89
        Koha::CirculationRule->new(
90
            {
91
                branchcode   => undef,
92
                categorycode => undef,
93
                itemtype     => undef,
94
                rule_name    => 'finedays',
95
                rule_value   => 5,
96
            }
97
        )->store,
98
        'Given I added another issuing rule branchcode => undef,' . ' categorycode => undef, itemtype => undef,'
99
    );
100
101
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is(
102
        '' => {
103
            fine     => 2,
104
            finedays => 5,
105
        },
106
        "Two default rules are returned"
107
    );
108
109
    # Specificity works, three circulation_rules stored, one branchcode specific
110
    ok(
111
        Koha::CirculationRule->new(
112
            {
113
                branchcode   => $branchcode,
114
                categorycode => undef,
115
                itemtype     => undef,
116
                rule_name    => 'fine',
117
                rule_value   => 4,
118
            }
119
        )->store,
120
        "Given I added an issuing rule branchcode => $branchcode," . ' categorycode => undef, itemtype => undef,'
121
    );
122
123
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library=$branchcode")->status_is(200)->json_is(
124
        '' => {
125
            fine     => 4,
126
            finedays => 5,
127
        },
128
        "Branch specific rule is returned when library is added to request query"
129
    );
130
131
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is(
132
        '' => {
133
            fine     => 2,
134
            finedays => 5,
135
        },
136
        "Default rules are returned when no library is added to request query"
137
    );
138
139
    # Warn on unsupported query parameter
140
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules?rules_blah=blah")->status_is(400)
141
        ->json_is( [ { path => '/query/rules_blah', message => 'Malformed query string' } ] );
142
143
    # Warn on incorrect query parameter value
144
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library=SMITH")->status_is(400)->json_is(
145
        '' => {
146
            error      => 'Invalid parameter value',
147
            error_code => 'invalid_parameter_value',
148
            path       => '/query/library',
149
            values     => {
150
                uri   => '/api/v1/libraries',
151
                field => 'library_id'
152
            }
153
        },
154
        "Invalid parameter value handled correctly"
155
    );
156
157
    # Unauthorized access
158
    $t->get_ok("//$unauth_userid:$password@/api/v1/circulation_rules")->status_is(403);
159
160
    $schema->storage->txn_rollback;
161
};
162

Return to bug 36641