From 0349849833ded860b150c4e508c73cc27e384d86 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 24 Jun 2024 16:29:44 +0100 Subject: [PATCH] Bug 36641: WIP - Add unit tests for /circulation_rules --- t/db_dependent/api/v1/circulation_rules.t | 108 ++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 t/db_dependent/api/v1/circulation_rules.t diff --git a/t/db_dependent/api/v1/circulation_rules.t b/t/db_dependent/api/v1/circulation_rules.t new file mode 100644 index 00000000000..a06b65846ef --- /dev/null +++ b/t/db_dependent/api/v1/circulation_rules.t @@ -0,0 +1,108 @@ +#!/usr/bin/env perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; +use Test::Mojo; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::CirculationRules; +use Koha::Database; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +my $t = Test::Mojo->new('Koha::REST::V1'); +t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + +subtest 'list_effective_rules() tests' => sub { + + plan tests => 20; + + $schema->storage->txn_begin; + + my $categorycode = $builder->build( { source => 'Category' } )->{'categorycode'}; + my $itemtype = $builder->build( { source => 'Itemtype' } )->{'itemtype'}; + my $branchcode = $builder->build( { source => 'Branch' } )->{'branchcode'}; + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**2 } # catalogue flag = 2 + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $unauth_userid = $patron->userid; + + ## Authorized user tests + # No circulation_rules, so empty array should be returned + $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( [] ); + + my $rules = $builder->build_object( { class => 'Koha::CirculationRules' } ); + + # One rules created, should get returned + $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( [ $rules->to_api ] ); + + my $another_rules = $builder->build_object( + { class => 'Koha::CirculationRules', value => { rules_country => $rules->rules_country } } ); + my $rules_with_another_country = $builder->build_object( { class => 'Koha::CirculationRules' } ); + + # Two circulation_rules created, they should both be returned + $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( + [ + $rules->to_api, + $another_rules->to_api, + $rules_with_another_country->to_api + ] + ); + + # Filtering works, two circulation_rules sharing rules_country + $t->get_ok( "//$userid:$password@/api/v1/circulation_rules?country=" . $rules->rules_country )->status_is(200) + ->json_is( + [ + $rules->to_api, + $another_rules->to_api + ] + ); + + $t->get_ok( "//$userid:$password@/api/v1/circulation_rules?name=" . $rules->rules_name )->status_is(200) + ->json_is( [ $rules->to_api ] ); + + # Warn on unsupported query parameter + $t->get_ok("//$userid:$password@/api/v1/circulation_rules?rules_blah=blah")->status_is(400) + ->json_is( [ { path => '/query/rules_blah', message => 'Malformed query string' } ] ); + + # Unauthorized access + $t->get_ok("//$unauth_userid:$password@/api/v1/circulation_rules")->status_is(403); + + $schema->storage->txn_rollback; +}; + -- 2.45.2