Bug 35218: Add unit tests for the counter registry API
[koha.git] / t / db_dependent / api / v1 / erm_counter_registries.t
1 #!/usr/bin/env perl
2
3 # Copyright 2023 PTFS Europe
4
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 1;
23 use Test::Mojo;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use JSON qw(encode_json);
29
30 use Koha::ERM::EUsage::CounterFiles;
31 use Koha::Database;
32
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 'get() tests' => sub {
39     plan tests => 5;
40
41     my $expected_response = [
42         {
43             "abbrev" => "EBSCO",
44             "address" => "EBSCO Information Services\n10 Estes Street\nIpswich, MA 01938",
45             "address_country" => {
46                 "code" => "US",
47                 "name" => "United States of America"
48             },
49             "contact" => {
50                 "email" => 'chadmovalli@ebsco.com',
51                 "form_url" => "",
52                 "person" => "Chad Movalli",
53                 "phone" => ""
54             },
55             "content_provider_name" => "EBSCO",
56             "host_types" => [
57                 {
58                     "name" => "Aggregated_Full_Content"
59                 }
60             ],
61             "id" => "b2b2736c-2cb9-48ec-91f4-870336acfb1c",
62             "name" => "EBSCO Information Services",
63             "reports" => [
64                 {
65                     "counter_release" => "5",
66                     "report_id" => "TR_J4",
67                     "report_name" => "Title Report - Journal Report 4"
68                 },
69                 {
70                     "counter_release" => "5",
71                     "report_id" => "DR_D2",
72                     "report_name" => "Database Report - Report 2"
73                 },
74                 {
75                     "counter_release" => "5",
76                     "report_id" => "TR_J3",
77                     "report_name" => "Title Report - Journal Report 3"
78                 },
79                 {
80                     "counter_release" => "5",
81                     "report_id" => "DR_D1",
82                     "report_name" => "Database Report - Report 1"
83                 },
84                 {
85                     "counter_release" => "5",
86                     "report_id" => "TR_J2",
87                     "report_name" => "Title Report - Journal Report 2"
88                 },
89                 {
90                     "counter_release" => "5",
91                     "report_id" => "PR",
92                     "report_name" => "Platform Master Report"
93                 },
94                 {
95                     "counter_release" => "5",
96                     "report_id" => "TR_B2",
97                     "report_name" => "Title Report - Book Report 2"
98                 },
99                 {
100                     "counter_release" => "5",
101                     "report_id" => "TR_B3",
102                     "report_name" => "Title Report - Book Report 3"
103                 },
104                 {
105                     "counter_release" => "5",
106                     "report_id" => "TR",
107                     "report_name" => "Title Master Report"
108                 },
109                 {
110                     "counter_release" => "5",
111                     "report_id" => "TR_B1",
112                     "report_name" => "Title Report - Book Report 1"
113                 },
114                 {
115                     "counter_release" => "5",
116                     "report_id" => "PR_P1",
117                     "report_name" => "Platform Report - Report 1"
118                 },
119                 {
120                     "counter_release" => "5",
121                     "report_id" => "TR_J1",
122                     "report_name" => "Title Report - Journal Report 1"
123                 },
124                 {
125                     "counter_release" => "5",
126                     "report_id" => "DR",
127                     "report_name" => "Database Master Report"
128                 }
129             ],
130             "sushi_services" => [
131                 {
132                     "counter_release" => "5",
133                     "url" => "https:\/\/registry.projectcounter.org\/api\/v1\/sushi-service\/b94bc981-fa16-4bf6-ba5f-6c113f7ffa0b\/"
134                 }
135             ],
136             "website" => "https:\/\/www.ebsco.com\/"
137         }
138     ];
139
140     my $librarian = $builder->build_object(
141         {
142             class => 'Koha::Patrons',
143             value => { flags => 2**28 }
144         }
145     );
146     my $password = 'thePassword123';
147     $librarian->set_password( { password => $password, skip_validation => 1 } );
148     my $userid = $librarian->userid;
149
150     my $patron = $builder->build_object(
151         {
152             class => 'Koha::Patrons',
153             value => { flags => 0 }
154         }
155     );
156
157     $patron->set_password( { password => $password, skip_validation => 1 } );
158     my $unauth_userid = $patron->userid;
159
160     # Unauthorized access
161     $t->get_ok("//$unauth_userid:$password@/api/v1/erm/counter_registry")->status_is(403);
162
163     # Authorised access test
164     my $q                = encode_json( { "name" => "EBSCO Information Services" } );
165     my $counter_registry = $t->get_ok("//$userid:$password@/api/v1/erm/counter_registry?q=$q")->status_is(200)->tx->res->json;
166     is_deeply( $counter_registry, $expected_response );
167 };