Bug 31794: Add REST endpoint to get an authority
[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 => 1;
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 };