Bug 26297: (QA follow-up): Update tests
[koha.git] / t / db_dependent / api / v1 / patron_categories.t
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 use t::lib::Dates;
26
27 use C4::Auth;
28 use Koha::Database;
29
30 use JSON qw(encode_json);
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 my $t = Test::Mojo->new('Koha::REST::V1');
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37
38 subtest 'list() tests' => sub {
39
40     plan tests => 15;
41
42     $schema->storage->txn_begin;
43
44     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
45
46     my $category = $builder->build_object(
47         {
48             class => 'Koha::Patron::Categories',
49             value => { categorycode => 'TEST', description => 'Test' }
50         }
51     );
52
53     $category->add_library_limit( $library->branchcode );
54
55     my $librarian = $builder->build_object(
56         {
57             class => 'Koha::Patrons',
58             value => { flags => 2**3, categorycode => 'TEST', branchcode => $library->branchcode } # parameters flag = 3
59         }
60     );
61
62     my $password = 'thePassword123';
63     $librarian->set_password( { password => $password, skip_validation => 1 } );
64     my $userid = $librarian->userid;
65
66     $t->get_ok("//$userid:$password@/api/v1/patron_categories")->status_is(200);
67
68     $t->get_ok("//$userid:$password@/api/v1/patron_categories?q={\"me.categorycode\":\"TEST\"}")->status_is(200)
69         ->json_has('/0/name')->json_is( '/0/name' => 'Test' )->json_hasnt('/1');
70
71     # Off limits search
72
73     my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
74
75     my $off_limits_category = $builder->build_object(
76         {
77             class => 'Koha::Patron::Categories',
78             value => { categorycode => 'CANT', description => 'Cant' }
79         }
80     );
81
82     my $off_limits_librarian = $builder->build_object(
83         {
84             class => 'Koha::Patrons',
85             value =>
86                 { flags => 2**3, categorycode => 'CANT', branchcode => $library_2->branchcode }    # parameters flag = 3
87         }
88     );
89     my $off_limits_password = 'thePassword123';
90     $off_limits_librarian->set_password( { password => $password, skip_validation => 1 } );
91     my $off_limits_userid = $off_limits_librarian->userid;
92
93     $t->get_ok("//$off_limits_userid:$off_limits_password@/api/v1/patron_categories?q={\"me.categorycode\":\"TEST\"}")
94         ->status_is(200)->json_hasnt('/0');
95
96     # Off limits librarian category has changed to one within limits
97
98     $off_limits_librarian->branchcode( $library->branchcode )->store;
99
100     $t->get_ok("//$off_limits_userid:$off_limits_password@/api/v1/patron_categories?q={\"me.categorycode\":\"TEST\"}")
101         ->status_is(200)->json_has('/0/name')->json_is( '/0/name' => 'Test' )->json_hasnt('/1');
102
103     $schema->storage->txn_rollback;
104
105 };