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

(-)a/Koha/REST/V1/Authorities.pm (-1 / +69 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 C4::AuthoritiesMarc qw( DelAuthority AddAuthority FindDuplicateAuthority);
24
24
25
use List::MoreUtils qw( any );
25
use List::MoreUtils qw( any );
26
use MARC::Record::MiJ;
26
use MARC::Record::MiJ;
Lines 128-131 sub delete { Link Here
128
    };
128
    };
129
}
129
}
130
130
131
=head3 add
132
133
Controller function that handles creating an authority object
134
135
=cut
136
137
sub add {
138
    my $c = shift->openapi->valid_input or return;
139
140
    try {
141
        my $body = $c->validation->param('Body');
142
143
        my $flavour =
144
            C4::Context->preference('marcflavour') eq 'UNIMARC'
145
            ? 'UNIMARCAUTH'
146
            : 'MARC21';
147
148
        my $record;
149
        my $authtypecode;
150
151
        if ( $c->req->headers->content_type =~ m/application\/json/ ) {
152
            $record = MARC::Record->new_from_xml( $body->{marcxml}, 'UTF-8', $flavour );
153
            $authtypecode = $body->{authtypecode};
154
        } else {
155
            $authtypecode = $c->validation->param('x-authority-type');
156
            if ( $c->req->headers->content_type =~ m/application\/marcxml\+xml/ ) {
157
                $record = MARC::Record->new_from_xml( $body, 'UTF-8', $flavour );
158
            } elsif ( $c->req->headers->content_type =~ m/application\/marc-in-json/ ) {
159
                $record = MARC::Record->new_from_mij_structure( $body );
160
            } elsif ( $c->req->headers->content_type =~ m/application\/marc/ ) {
161
                $record = MARC::Record->new_from_usmarc( $body );
162
            } else {
163
                return $c->render(
164
                    status  => 406,
165
                    openapi => [
166
                        "application/json",
167
                        "application/marcxml+xml",
168
                        "application/marc-in-json",
169
                        "application/marc"
170
                    ]
171
                );
172
            }
173
        }
174
175
        my ($duplicateauthid,$duplicateauthvalue);
176
            ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode);
177
178
        my $confirm_not_duplicate = $c->validation->param('x-confirm-not-duplicate');
179
180
        return $c->render(
181
            status  => 400,
182
            openapi => {
183
                error => "Duplicate authority $duplicateauthid"
184
            }
185
        ) unless !$duplicateauthid || $confirm_not_duplicate;
186
187
        my $authid = AddAuthority( $record, undef, $authtypecode );
188
189
        $c->render(
190
            status  => 200,
191
            openapi => { id => $authid }
192
        );
193
    }
194
    catch {
195
        $c->unhandled_exception($_);
196
    };
197
}
198
131
1;
199
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 (+8 lines)
Lines 151-156 paths: Link Here
151
    $ref: paths/authorised_value_categories.yaml#/~1authorised_value_categories
151
    $ref: paths/authorised_value_categories.yaml#/~1authorised_value_categories
152
  "/authorised_value_categories/{authorised_value_category_name}/authorised_values":
152
  "/authorised_value_categories/{authorised_value_category_name}/authorised_values":
153
    $ref: "./paths/authorised_values.yaml#/~1authorised_value_categories~1{authorised_value_category_name}~1authorised_values"
153
    $ref: "./paths/authorised_values.yaml#/~1authorised_value_categories~1{authorised_value_category_name}~1authorised_values"
154
  "/authorities":
155
    $ref: paths/authorities.yaml#/~1authorities
154
  "/authorities/{authority_id}":
156
  "/authorities/{authority_id}":
155
    $ref: paths/authorities.yaml#/~1authorities~1{authority_id}
157
    $ref: paths/authorities.yaml#/~1authorities~1{authority_id}
156
  "/biblios":
158
  "/biblios":
Lines 376-381 parameters: Link Here
376
    name: authority_id
378
    name: authority_id
377
    required: true
379
    required: true
378
    type: integer
380
    type: integer
381
  authority_type_header:
382
    description: Authority type code. Use when content type is not application/json
383
    name: x-authority-type
384
    in: header
385
    required: false
386
    type: string
379
  framework_id_header:
387
  framework_id_header:
380
    description: Framework id. Use when content type is not application/json
388
    description: Framework id. Use when content type is not application/json
381
    name: x-framework-id
389
    name: x-framework-id
(-)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 152-154 subtest 'delete() tests' => sub { Link Here
152
152
153
    $schema->storage->txn_rollback;
153
    $schema->storage->txn_rollback;
154
};
154
};
155
- 
155
156
subtest 'post() tests' => sub {
157
158
    plan tests => 14;
159
160
    $schema->storage->txn_begin;
161
162
    Koha::Authorities->delete;
163
164
    my $patron = $builder->build_object(
165
        {
166
            class => 'Koha::Patrons',
167
            value => { flags => 0 } # no permissions
168
        }
169
    );
170
    my $password = 'thePassword123';
171
    $patron->set_password( { password => $password, skip_validation => 1 } );
172
    my $userid = $patron->userid;
173
174
    my $marcxml = q|<?xml version="1.0" encoding="UTF-8"?>
175
<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">
176
    <controlfield tag="001">1001</controlfield>
177
    <datafield tag="110" ind1=" " ind2=" ">
178
        <subfield code="9">102</subfield>
179
        <subfield code="a">My Corporation</subfield>
180
    </datafield>
181
</record>|;
182
183
    my $mij = '{"fields":[{"001":"1001"},{"110":{"subfields":[{"9":"102"},{"a":"My Corporation"}],"ind1":" ","ind2":" "}}],"leader":"                        "}';
184
    my $marc = '00079     2200049   45000010005000001100024000051001  9102aMy Corporation';
185
    my $json = {
186
      authtypecode => "CORPO_NAME",
187
      marcxml      => $marcxml
188
    };
189
190
    $t->post_ok("//$userid:$password@/api/v1/authorities")
191
      ->status_is(403, 'Not enough permissions makes it return the right code');
192
193
    # Add permissions
194
    $builder->build(
195
        {
196
            source => 'UserPermission',
197
            value  => {
198
                borrowernumber => $patron->borrowernumber,
199
                module_bit     => 9,
200
                code           => 'edit_catalogue'
201
            }
202
        }
203
    );
204
205
    $t->post_ok("//$userid:$password@/api/v1/authorities" => json => $json)
206
      ->status_is(200)
207
      ->json_has('/id');
208
209
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marcxml+xml', 'x-authority-type' => 'CORPO_NAME'} => $marcxml)
210
      ->status_is(200)
211
      ->json_has('/id');
212
213
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc-in-json', 'x-authority-type' => 'CORPO_NAME'} => $mij)
214
      ->status_is(200)
215
      ->json_has('/id');
216
217
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc', 'x-authority-type' => 'CORPO_NAME'} => $marc)
218
      ->status_is(200)
219
      ->json_has('/id');
220
221
    $schema->storage->txn_rollback;
222
};

Return to bug 31795