Bug 31795: (QA follow-up) Use x-koha-override header
[koha.git] / t / db_dependent / api / v1 / authorities.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 utf8;
21 use Encode;
22
23 use Test::More tests => 3;
24 use Test::MockModule;
25 use Test::Mojo;
26 use Test::Warn;
27
28 use t::lib::Mocks;
29 use t::lib::TestBuilder;
30
31 use C4::Auth;
32
33 use Koha::Authorities;
34
35 my $schema  = Koha::Database->new->schema;
36 my $builder = t::lib::TestBuilder->new;
37
38 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
39
40 my $t = Test::Mojo->new('Koha::REST::V1');
41
42 subtest 'get() tests' => sub {
43
44     plan tests => 20;
45
46     $schema->storage->txn_begin;
47
48     my $patron = $builder->build_object(
49         {
50             class => 'Koha::Patrons',
51             value => { flags => 0 }
52         }
53     );
54     my $password = 'thePassword123';
55     $patron->set_password( { password => $password, skip_validation => 1 } );
56     $patron->discard_changes;
57     my $userid = $patron->userid;
58
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>
66     </datafield>
67 </record>|
68     } });
69
70     $t->get_ok("//$userid:$password@/api/v1/authorities/" . $authority->authid)
71       ->status_is(403);
72
73     $patron->flags(4)->store;
74
75     $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
76                 => { Accept => 'application/weird+format' } )
77       ->status_is(400);
78
79     $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
80                  => { Accept => 'application/json' } )
81       ->status_is(200)
82       ->json_is( '/authid', $authority->authid )
83       ->json_is( '/authtypecode', $authority->authtypecode );
84
85     $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
86                  => { Accept => 'application/marcxml+xml' } )
87       ->status_is(200);
88
89     $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
90                  => { Accept => 'application/marc-in-json' } )
91       ->status_is(200);
92
93     $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
94                  => { Accept => 'application/marc' } )
95       ->status_is(200);
96
97     $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
98                  => { Accept => 'text/plain' } )
99       ->status_is(200)
100       ->content_is(q|LDR 00079     2200049   4500
101 001     1001
102 110    _9102
103        _aMy Corporation|);
104
105     $authority->delete;
106     $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
107                  => { Accept => 'application/marc' } )
108       ->status_is(404)
109       ->json_is( '/error', 'Object not found.' );
110
111     $schema->storage->txn_rollback;
112 };
113
114 subtest 'delete() tests' => sub {
115
116     plan tests => 7;
117
118     $schema->storage->txn_begin;
119
120     my $patron = $builder->build_object(
121         {
122             class => 'Koha::Patrons',
123             value => { flags => 0 } # no permissions
124         }
125     );
126     my $password = 'thePassword123';
127     $patron->set_password( { password => $password, skip_validation => 1 } );
128     my $userid = $patron->userid;
129
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>
137     </datafield>
138 </record>|
139     } });
140
141     $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid)
142       ->status_is(403, 'Not enough permissions makes it return the right code');
143
144     $patron->flags( 2 ** 14 )->store; # 14 => editauthorities userflag
145
146     $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid)
147       ->status_is(204, 'SWAGGER3.2.4')
148       ->content_is('', 'SWAGGER3.3.4');
149
150     $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid)
151       ->status_is(404);
152
153     $schema->storage->txn_rollback;
154 };
155
156 subtest 'post() tests' => sub {
157
158     plan tests => 19;
159
160     $schema->storage->txn_begin;
161
162     my $patron = $builder->build_object(
163         {
164             class => 'Koha::Patrons',
165             value => { flags => 0 } # no permissions
166         }
167     );
168     my $password = 'thePassword123';
169     $patron->set_password( { password => $password, skip_validation => 1 } );
170     my $userid = $patron->userid;
171
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>
178     </datafield>
179 </record>|;
180
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';
183     my $json = {
184       authtypecode => "CORPO_NAME",
185       marcxml      => $marcxml
186     };
187
188     $t->post_ok("//$userid:$password@/api/v1/authorities")
189       ->status_is(403, 'Not enough permissions makes it return the right code');
190
191     # Add permissions
192     $builder->build(
193         {
194             source => 'UserPermission',
195             value  => {
196                 borrowernumber => $patron->borrowernumber,
197                 module_bit     => 9,
198                 code           => 'edit_catalogue'
199             }
200         }
201     );
202
203     # x-koha-override passed to make sure it goes through
204     $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marcxml+xml', 'x-authority-type' => 'CORPO_NAME', 'x-koha-override' => 'any' } => $marcxml)
205       ->status_is(201)
206       ->json_is(q{})
207       ->header_like(
208           Location => qr|^\/api\/v1\/authorities/\d*|,
209           'SWAGGER3.4.1'
210       );
211
212     # x-koha-override not passed to force block because duplicate
213     $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc-in-json', 'x-authority-type' => 'CORPO_NAME' } => $mij)
214       ->status_is(409)
215       ->header_exists_not( 'Location', 'Location header is only set when the new resource is created' )
216       ->json_like( '/error' => qr/Duplicate record (\d*)/ )
217       ->json_is( '/error_code' => q{duplicate} );
218
219     $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc-in-json', 'x-authority-type' => 'CORPO_NAME', 'x-koha-override' => 'duplicate' } => $mij)
220       ->status_is(201)
221       ->json_is(q{})
222       ->header_like(
223           Location => qr|^\/api\/v1\/authorities/\d*|,
224           'SWAGGER3.4.1'
225       );
226
227     $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc', 'x-authority-type' => 'CORPO_NAME', 'x-koha-override' => 'duplicate' } => $marc)
228       ->status_is(201)
229       ->json_is(q{})
230       ->header_like(
231           Location => qr|^\/api\/v1\/authorities/\d*|,
232           'SWAGGER3.4.1'
233       );
234
235     $schema->storage->txn_rollback;
236 };