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

(-)a/t/db_dependent/api/v1/circulation_rules.t (-26 / +98 lines)
Lines 33-39 my $t = Test::Mojo->new('Koha::REST::V1'); Link Here
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
34
35
subtest 'list_rules() tests' => sub {
35
subtest 'list_rules() tests' => sub {
36
    plan tests => 32;
36
37
    my $expected_rules = [ keys %{ Koha::CirculationRules->rule_kinds } ];
38
39
    plan tests => ( scalar( @{$expected_rules} ) * 2 ) + 36;
37
40
38
    $schema->storage->txn_begin;
41
    $schema->storage->txn_begin;
39
42
Lines 62-70 subtest 'list_rules() tests' => sub { Link Here
62
    $patron->set_password( { password => $password, skip_validation => 1 } );
65
    $patron->set_password( { password => $password, skip_validation => 1 } );
63
    my $unauth_userid = $patron->userid;
66
    my $unauth_userid = $patron->userid;
64
67
68
    note("Effective rules by default");
65
    ## Authorized user tests
69
    ## Authorized user tests
66
    # No circulation_rules, so empty hash should be returned
70
    # No circulation_rules, so all keys in the returned hash should be undefined
67
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( '/0' => {} );
71
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200);
72
73
    # Extract and decode the JSON response
74
    my $json = $t->tx->res->json;
75
    note("No rules defined");
76
    foreach my $key ( @{$expected_rules} ) {
77
        ok( exists $json->[0]->{$key}, "Key '$key' exists in the JSON response" );
78
        is( $json->[0]->{$key}, undef, "'$key' is undefined" );
79
    }
68
80
69
    # One rule created, should get returned
81
    # One rule created, should get returned
70
    ok(
82
    ok(
Lines 80-87 subtest 'list_rules() tests' => sub { Link Here
80
        'Given I added an issuing rule branchcode => undef,' . ' categorycode => undef, itemtype => undef,'
92
        'Given I added an issuing rule branchcode => undef,' . ' categorycode => undef, itemtype => undef,'
81
    );
93
    );
82
94
95
    note("One default rule defined");
83
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)
96
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)
84
        ->json_is( '/0' => { 'fine' => 2 }, "Our single rule is returned" );
97
        ->json_is( '/0/fine'     => 2,     "Default fine rule is returned as expected" )
98
        ->json_is( '/0/finedays' => undef, "Rule finedays is undefined as expected" );
85
99
86
    # Two circulation_rules created, they should both be returned
100
    # Two circulation_rules created, they should both be returned
87
    ok(
101
    ok(
Lines 97-109 subtest 'list_rules() tests' => sub { Link Here
97
        'Given I added another issuing rule branchcode => undef,' . ' categorycode => undef, itemtype => undef,'
111
        'Given I added another issuing rule branchcode => undef,' . ' categorycode => undef, itemtype => undef,'
98
    );
112
    );
99
113
100
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is(
114
    note("Two default rules defined");
101
        '/0' => {
115
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)
102
            fine     => 2,
116
        ->json_is( '/0/fine'     => 2, "Default fine rule is returned as expected" )
103
            finedays => 5,
117
        ->json_is( '/0/finedays' => 5, "Default finedays rule is returned as expected" );
104
        },
105
        "Two default rules are returned"
106
    );
107
118
108
    # Specificity works, three circulation_rules stored, one branchcode specific
119
    # Specificity works, three circulation_rules stored, one branchcode specific
109
    ok(
120
    ok(
Lines 119-139 subtest 'list_rules() tests' => sub { Link Here
119
        "Given I added an issuing rule branchcode => $branchcode," . ' categorycode => undef, itemtype => undef,'
130
        "Given I added an issuing rule branchcode => $branchcode," . ' categorycode => undef, itemtype => undef,'
120
    );
131
    );
121
132
122
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library_id=$branchcode")->status_is(200)->json_is(
133
    note("Two default rules and one branch rule defined");
123
        '/0' => {
134
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library_id=$branchcode")->status_is(200)
124
            fine     => 4,
135
        ->json_is( '/0/fine' => 4, "Branch specific fine rule is returned when library is added to request query" )
125
            finedays => 5,
136
        ->json_is(
126
        },
137
        '/0/finedays' => 5,
127
        "Branch specific rule is returned when library is added to request query"
138
        "Default finedays rule is returned when library is added to request query but no branch specific rule is defined"
128
    );
139
        );
129
140
130
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is(
141
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)
131
        '/0' => {
142
        ->json_is( '/0/fine'     => 2, "Defaul fine rule returned when no library is added to request query" )
132
            fine     => 2,
143
        ->json_is( '/0/finedays' => 5, "Default finedays rule returned when no library is added to request query" );
133
            finedays => 5,
134
        },
135
        "Default rules are returned when no library is added to request query"
136
    );
137
144
138
    # Warn on unsupported query parameter
145
    # Warn on unsupported query parameter
139
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules?rules_blah=blah")->status_is(400)
146
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules?rules_blah=blah")->status_is(400)
Lines 202-206 subtest 'list_rules() tests' => sub { Link Here
202
    # Unauthorized access
209
    # Unauthorized access
203
    $t->get_ok("//$unauth_userid:$password@/api/v1/circulation_rules")->status_is(403);
210
    $t->get_ok("//$unauth_userid:$password@/api/v1/circulation_rules")->status_is(403);
204
211
212
    subtest 'effective=false tests' => sub {
213
214
        my $count = scalar( @{$expected_rules} );
215
216
        plan tests => ( $count * 2 ) + $count + 10;
217
218
        # All rules
219
        $t->get_ok("//$userid:$password@/api/v1/circulation_rules?effective=0")->status_is(200);
220
221
        # Extract and decode the JSON response
222
        my $json = $t->tx->res->json;
223
224
        # Check if the response is an array
225
        is( ref $json,          'ARRAY', 'Response is an array' );
226
        is( scalar( @{$json} ), 2,       'Response contains 2 rule sets' );
227
228
        # Iterate over each hash in the array
229
        my $index = 0;
230
        foreach my $hash ( @{$json} ) {
231
            my $pointer = Mojo::JSON::Pointer->new($hash);
232
233
            # First rule set should march default, default, default
234
            if ( $index == 0 ) {
235
                ok(        $pointer->get('/branchcode') eq "*"
236
                        && $pointer->get('/itemtype') eq '*'
237
                        && $pointer->get('/categorycode') eq '*', "Default rules returned first" );
238
            }
239
240
            # Iterate over the list of expected keys for each hash
241
            foreach my $key ( @{$expected_rules} ) {
242
                ok( $pointer->contains( '/' . $key ), "Hash contains key '$key'" );
243
            }
244
245
            $index++;
246
        }
247
248
        # Filter on library
249
        $t->get_ok("//$userid:$password@/api/v1/circulation_rules?effective=0&library_id=$branchcode")->status_is(200);
250
251
        # Extract and decode the JSON response
252
        $json = $t->tx->res->json;
253
254
        # Check if the response is an array
255
        is( ref $json,          'ARRAY', 'Response is an array' );
256
        is( scalar( @{$json} ), 1,       'Filtered response contains 1 rule set' );
257
258
        $index = 0;
259
        foreach my $hash ( @{$json} ) {
260
            my $pointer = Mojo::JSON::Pointer->new($hash);
261
262
            # First (and only) rule set should match branchcode, default, default.
263
            if ( $index == 0 ) {
264
                ok(        $pointer->get('/branchcode') eq $branchcode
265
                        && $pointer->get('/itemtype') eq '*'
266
                        && $pointer->get('/categorycode') eq '*', "Branchcode rule set returned when filtered" );
267
            }
268
269
            # Iterate over the list of expected keys for each hash
270
            foreach my $key ( @{$expected_rules} ) {
271
                ok( $pointer->contains( '/' . $key ), "Hash contains key '$key'" );
272
            }
273
274
            $index++;
275
        }
276
277
    };
205
    $schema->storage->txn_rollback;
278
    $schema->storage->txn_rollback;
206
};
279
};
207
- 

Return to bug 36641