View | Details | Raw Unified | Return to bug 31795
Collapse All | Expand All

(-)a/Koha/REST/V1/Authorities.pm (-1 / +70 lines)
Lines 20-26 use Modern::Perl; Link Here
20
use Mojo::Base 'Mojolicious::Controller';
20
use Mojo::Base 'Mojolicious::Controller';
21
21
22
use Koha::Authorities;
22
use Koha::Authorities;
23
use C4::AuthoritiesMarc qw( DelAuthority );
23
use Koha::Authority;
24
use C4::AuthoritiesMarc qw( DelAuthority AddAuthority FindDuplicateAuthority);
24
25
25
use List::MoreUtils qw( any );
26
use List::MoreUtils qw( any );
26
use MARC::Record::MiJ;
27
use MARC::Record::MiJ;
Lines 136-139 sub delete { Link Here
136
    };
137
    };
137
}
138
}
138
139
140
=head3 add
141
142
Controller function that handles creating an authority object
143
144
=cut
145
146
sub add {
147
    my $c = shift->openapi->valid_input or return;
148
149
    try {
150
        my $body = $c->validation->param('Body');
151
152
        my $flavour =
153
            C4::Context->preference('marcflavour') eq 'UNIMARC'
154
            ? 'UNIMARCAUTH'
155
            : 'MARC21';
156
157
        my $record;
158
        my $authtypecode;
159
160
        if ( $c->req->headers->content_type =~ m/application\/json/ ) {
161
            $record = MARC::Record->new_from_xml( $body->{marcxml}, 'UTF-8', $flavour );
162
            $authtypecode = $body->{authtypecode};
163
        } else {
164
            $authtypecode = $c->validation->param('authority_type');
165
            if ( $c->req->headers->content_type =~ m/application\/marcxml\+xml/ ) {
166
                $record = MARC::Record->new_from_xml( $body, 'UTF-8', $flavour );
167
            } elsif ( $c->req->headers->content_type =~ m/application\/marc-in-json/ ) {
168
                $record = MARC::Record->new_from_mij_structure( $body );
169
            } elsif ( $c->req->headers->content_type =~ m/application\/marc/ ) {
170
                $record = MARC::Record->new_from_usmarc( $body );
171
            } else {
172
                return $c->render(
173
                    status  => 406,
174
                    openapi => [
175
                        "application/json",
176
                        "application/marcxml+xml",
177
                        "application/marc-in-json",
178
                        "application/marc"
179
                    ]
180
                );
181
            }
182
        }
183
184
        my ($duplicateauthid,$duplicateauthvalue);
185
            ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode);
186
187
        my $confirm_not_duplicate = $c->validation->param('confirm_not_duplicate');
188
189
        return $c->render(
190
            status  => 400,
191
            openapi => {
192
                error => "Duplicate authority $duplicateauthid"
193
            }
194
        ) unless !$duplicateauthid || $confirm_not_duplicate;
195
196
        my $authid = AddAuthority( $record, undef, $authtypecode );
197
198
        $c->render(
199
            status  => 200,
200
            openapi => { id => $authid }
201
        );
202
    }
203
    catch {
204
        $c->unhandled_exception($_);
205
    };
206
}
207
139
1;
208
1;
(-)a/api/v1/swagger/paths/authorities.yaml (+56 lines)
Lines 1-4 Link Here
1
---
1
---
2
"/authorities":
3
  post:
4
    x-mojo-to: Authorities#add
5
    operationId: addAuthority
6
    tags:
7
      - authorities
8
    summary: Add authority
9
    parameters:
10
      - name: Body
11
        in: body
12
        description: A JSON object or the Marc string describing an authority
13
        required: true
14
        schema:
15
          type:
16
            - string
17
            - object
18
      - $ref: "../swagger.yaml#/parameters/authority_type_header"
19
      - $ref: "../swagger.yaml#/parameters/confirm_not_duplicate_header"
20
    produces:
21
      - application/json
22
    responses:
23
      "200":
24
        description: An authority
25
      "400":
26
        description: Bad request
27
        schema:
28
          $ref: "../swagger.yaml#/definitions/error"
29
      "401":
30
        description: Authentication required
31
        schema:
32
          $ref: "../swagger.yaml#/definitions/error"
33
      "403":
34
        description: Access forbidden
35
        schema:
36
          $ref: "../swagger.yaml#/definitions/error"
37
      "406":
38
        description: Not acceptable
39
        schema:
40
          type: array
41
          description: Accepted content-types
42
          items:
43
            type: string
44
      "500":
45
        description: |
46
          Internal server error. Possible `error_code` attribute values:
47
48
          * `internal_server_error`
49
        schema:
50
          $ref: "../swagger.yaml#/definitions/error"
51
      "503":
52
        description: Under maintenance
53
        schema:
54
          $ref: "../swagger.yaml#/definitions/error"
55
    x-koha-authorization:
56
      permissions:
57
        editcatalogue: edit_catalogue
2
"/authorities/{authority_id}":
58
"/authorities/{authority_id}":
3
  get:
59
  get:
4
    x-mojo-to: Authorities#get
60
    x-mojo-to: Authorities#get
(-)a/api/v1/swagger/swagger.yaml (+14 lines)
Lines 137-142 paths: Link Here
137
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains
137
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains
138
  "/auth/identity_providers/{identity_provider_id}/domains/{identity_provider_domain_id}":
138
  "/auth/identity_providers/{identity_provider_id}/domains/{identity_provider_domain_id}":
139
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains~1{identity_provider_domain_id}
139
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains~1{identity_provider_domain_id}
140
  "/authorities":
141
    $ref: paths/authorities.yaml#/~1authorities
140
  "/authorities/{authority_id}":
142
  "/authorities/{authority_id}":
141
    $ref: paths/authorities.yaml#/~1authorities~1{authority_id}
143
    $ref: paths/authorities.yaml#/~1authorities~1{authority_id}
142
  "/biblios/{biblio_id}":
144
  "/biblios/{biblio_id}":
Lines 344-349 parameters: Link Here
344
    name: authority_id
346
    name: authority_id
345
    required: true
347
    required: true
346
    type: integer
348
    type: integer
349
  authority_type_header:
350
    description: Authority type code. Use when content type is not application/json
351
    name: authority_type
352
    in: header
353
    required: false
354
    type: string
355
  confirm_not_duplicate_header:
356
    description: Confirm the posted element is not a duplicate
357
    name: confirm_not_duplicate
358
    in: header
359
    required: false
360
    type: integer
347
  identity_provider_id_pp:
361
  identity_provider_id_pp:
348
    description: Authentication provider internal identifier
362
    description: Authentication provider internal identifier
349
    in: path
363
    in: path
(-)a/t/db_dependent/api/v1/authorities.t (-2 / +69 lines)
Lines 20-26 use Modern::Perl; Link Here
20
use utf8;
20
use utf8;
21
use Encode;
21
use Encode;
22
22
23
use Test::More tests => 2;
23
use Test::More tests => 3;
24
use Test::MockModule;
24
use Test::MockModule;
25
use Test::Mojo;
25
use Test::Mojo;
26
use Test::Warn;
26
use Test::Warn;
Lines 160-164 subtest 'delete() tests' => sub { Link Here
160
    $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid)
160
    $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid)
161
      ->status_is(404);
161
      ->status_is(404);
162
162
163
    $schema->storage->txn_rollback;
164
};
165
166
subtest 'post() tests' => sub {
167
168
    plan tests => 14;
169
170
    $schema->storage->txn_begin;
171
172
    Koha::Authorities->delete;
173
174
    my $patron = $builder->build_object(
175
        {
176
            class => 'Koha::Patrons',
177
            value => { flags => 0 } # no permissions
178
        }
179
    );
180
    my $password = 'thePassword123';
181
    $patron->set_password( { password => $password, skip_validation => 1 } );
182
    my $userid = $patron->userid;
183
184
    my $marcxml = q|<?xml version="1.0" encoding="UTF-8"?>
185
<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">
186
    <controlfield tag="001">1001</controlfield>
187
    <datafield tag="110" ind1=" " ind2=" ">
188
        <subfield code="9">102</subfield>
189
        <subfield code="a">My Corporation</subfield>
190
    </datafield>
191
</record>|;
192
193
    my $mij = '{"fields":[{"001":"1001"},{"110":{"subfields":[{"9":"102"},{"a":"My Corporation"}],"ind1":" ","ind2":" "}}],"leader":"                        "}';
194
    my $marc = '00079     2200049   45000010005000001100024000051001  9102aMy Corporation';
195
    my $json = {
196
      authtypecode => "CORPO_NAME",
197
      marcxml      => $marcxml
198
    };
199
200
    $t->post_ok("//$userid:$password@/api/v1/authorities")
201
      ->status_is(403, 'Not enough permissions makes it return the right code');
202
203
    # Add permissions
204
    $builder->build(
205
        {
206
            source => 'UserPermission',
207
            value  => {
208
                borrowernumber => $patron->borrowernumber,
209
                module_bit     => 9,
210
                code           => 'edit_catalogue'
211
            }
212
        }
213
    );
214
215
    $t->post_ok("//$userid:$password@/api/v1/authorities" => json => $json)
216
      ->status_is(200)
217
      ->json_has('/id');
218
219
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marcxml+xml', authority_type => 'CORPO_NAME'} => $marcxml)
220
      ->status_is(200)
221
      ->json_has('/id');
222
223
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc-in-json', authority_type => 'CORPO_NAME'} => $mij)
224
      ->status_is(200)
225
      ->json_has('/id');
226
227
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc', authority_type => 'CORPO_NAME'} => $marc)
228
      ->status_is(200)
229
      ->json_has('/id');
230
163
    $schema->storage->txn_rollback;
231
    $schema->storage->txn_rollback;
164
};
232
};
165
- 

Return to bug 31795