3 # This file is part of Koha.
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.
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.
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>.
23 use Test::More tests => 3;
29 use t::lib::TestBuilder;
33 use Koha::Authorities;
35 my $schema = Koha::Database->new->schema;
36 my $builder = t::lib::TestBuilder->new;
38 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
40 my $t = Test::Mojo->new('Koha::REST::V1');
42 subtest 'get() tests' => sub {
46 $schema->storage->txn_begin;
48 my $patron = $builder->build_object(
50 class => 'Koha::Patrons',
51 value => { flags => 0 }
54 my $password = 'thePassword123';
55 $patron->set_password( { password => $password, skip_validation => 1 } );
56 $patron->discard_changes;
57 my $userid = $patron->userid;
59 my $authority = $builder->build_object({ 'class' => 'Koha::Authorities', value => {
60 marcxml => q|<?xml version="1.0" encoding="UTF-8"?>
61 <record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
62 <controlfield tag="001">1001</controlfield>
63 <datafield tag="110" ind1=" " ind2=" ">
64 <subfield code="9">102</subfield>
65 <subfield code="a">My Corporation</subfield>
70 $t->get_ok("//$userid:$password@/api/v1/authorities/" . $authority->authid)
73 $patron->flags(4)->store;
75 $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
76 => { Accept => 'application/weird+format' } )
79 $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
80 => { Accept => 'application/json' } )
82 ->json_is( '/authid', $authority->authid )
83 ->json_is( '/authtypecode', $authority->authtypecode );
85 $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
86 => { Accept => 'application/marcxml+xml' } )
89 $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
90 => { Accept => 'application/marc-in-json' } )
93 $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
94 => { Accept => 'application/marc' } )
97 $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
98 => { Accept => 'text/plain' } )
100 ->content_is(q|LDR 00079 2200049 4500
106 $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
107 => { Accept => 'application/marc' } )
109 ->json_is( '/error', 'Object not found.' );
111 $schema->storage->txn_rollback;
114 subtest 'delete() tests' => sub {
118 $schema->storage->txn_begin;
120 my $patron = $builder->build_object(
122 class => 'Koha::Patrons',
123 value => { flags => 0 } # no permissions
126 my $password = 'thePassword123';
127 $patron->set_password( { password => $password, skip_validation => 1 } );
128 my $userid = $patron->userid;
130 my $authority = $builder->build_object({ 'class' => 'Koha::Authorities', value => {
131 marcxml => q|<?xml version="1.0" encoding="UTF-8"?>
132 <record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
133 <controlfield tag="001">1001</controlfield>
134 <datafield tag="110" ind1=" " ind2=" ">
135 <subfield code="9">102</subfield>
136 <subfield code="a">My Corporation</subfield>
141 $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid)
142 ->status_is(403, 'Not enough permissions makes it return the right code');
144 $patron->flags( 2 ** 14 )->store; # 14 => editauthorities userflag
146 $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid)
147 ->status_is(204, 'SWAGGER3.2.4')
148 ->content_is('', 'SWAGGER3.3.4');
150 $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid)
153 $schema->storage->txn_rollback;
156 subtest 'post() tests' => sub {
160 $schema->storage->txn_begin;
162 my $patron = $builder->build_object(
164 class => 'Koha::Patrons',
165 value => { flags => 0 } # no permissions
168 my $password = 'thePassword123';
169 $patron->set_password( { password => $password, skip_validation => 1 } );
170 my $userid = $patron->userid;
172 my $marcxml = q|<?xml version="1.0" encoding="UTF-8"?>
173 <record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
174 <controlfield tag="001">1001</controlfield>
175 <datafield tag="110" ind1=" " ind2=" ">
176 <subfield code="9">102</subfield>
177 <subfield code="a">My Corporation</subfield>
181 my $mij = '{"fields":[{"001":"1001"},{"110":{"subfields":[{"9":"102"},{"a":"My Corporation"}],"ind1":" ","ind2":" "}}],"leader":" "}';
182 my $marc = '00079 2200049 4500001000500000110002400005
\1e1001
\1e \1f9102
\1faMy Corporation
\1e\1d';
184 authtypecode => "CORPO_NAME",
188 $t->post_ok("//$userid:$password@/api/v1/authorities")
189 ->status_is(403, 'Not enough permissions makes it return the right code');
192 $patron->flags( 2 ** 14 )->store; # 14 => editauthorities userflag
194 # x-koha-override passed to make sure it goes through
195 $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marcxml+xml', 'x-authority-type' => 'CORPO_NAME', 'x-koha-override' => 'any' } => $marcxml)
199 Location => qr|^\/api\/v1\/authorities/\d*|,
203 # x-koha-override not passed to force block because duplicate
204 $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc-in-json', 'x-authority-type' => 'CORPO_NAME' } => $mij)
206 ->header_exists_not( 'Location', 'Location header is only set when the new resource is created' )
207 ->json_like( '/error' => qr/Duplicate record (\d*)/ )
208 ->json_is( '/error_code' => q{duplicate} );
210 $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc-in-json', 'x-authority-type' => 'CORPO_NAME', 'x-koha-override' => 'duplicate' } => $mij)
214 Location => qr|^\/api\/v1\/authorities/\d*|,
218 $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc', 'x-authority-type' => 'CORPO_NAME', 'x-koha-override' => 'duplicate' } => $marc)
222 Location => qr|^\/api\/v1\/authorities/\d*|,
226 $schema->storage->txn_rollback;