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

(-)a/Koha/REST/V1/Authorities.pm (-1 / +67 lines)
Lines 21-27 use Mojo::Base 'Mojolicious::Controller'; Link Here
21
21
22
use Koha::Authorities;
22
use Koha::Authorities;
23
use Koha::Authority;
23
use Koha::Authority;
24
use C4::AuthoritiesMarc qw( DelAuthority AddAuthority FindDuplicateAuthority);
24
use C4::AuthoritiesMarc qw( DelAuthority AddAuthority FindDuplicateAuthority ModAuthority);
25
25
26
use List::MoreUtils qw( any );
26
use List::MoreUtils qw( any );
27
use MARC::Record::MiJ;
27
use MARC::Record::MiJ;
Lines 205-208 sub add { Link Here
205
    };
205
    };
206
}
206
}
207
207
208
209
=head3 update
210
211
Controller function that handles modifying an authority object
212
213
=cut
214
215
sub update {
216
    my $c = shift->openapi->valid_input or return;
217
218
    my $authid = $c->validation->param('authority_id');
219
    my $authority = Koha::Authorities->find( { authid => $authid } );
220
221
    if ( not defined $authority ) {
222
        return $c->render(
223
            status  => 404,
224
            openapi => { error => "Object not found" }
225
        );
226
    }
227
228
    try {
229
        my $body = $c->validation->param('Body');
230
231
        my $flavour =
232
            C4::Context->preference('marcflavour') eq 'UNIMARC'
233
            ? 'UNIMARCAUTH'
234
            : 'MARC21';
235
236
        my $record;
237
        my $authtypecode;
238
239
        if ( $c->req->headers->content_type =~ m/application\/json/ ) {
240
            $record = MARC::Record->new_from_xml( $body->{marcxml}, 'UTF-8', $flavour );
241
            $authtypecode = $body->{authtypecode} || $authority->authtypecode;
242
        } else {
243
            $authtypecode = $c->validation->param('x-authority-type') || $authority->authtypecode;
244
            if ( $c->req->headers->content_type =~ m/application\/marcxml\+xml/ ) {
245
                $record = MARC::Record->new_from_xml( $body, 'UTF-8', $flavour );
246
            } elsif ( $c->req->headers->content_type =~ m/application\/marc-in-json/ ) {
247
                $record = MARC::Record->new_from_mij_structure( $body );
248
            } elsif ( $c->req->headers->content_type =~ m/application\/marc/ ) {
249
                $record = MARC::Record->new_from_usmarc( $body );
250
            } else {
251
                return $c->render(
252
                    status  => 406,
253
                    openapi => [
254
                        "application/json",
255
                        "application/marcxml+xml",
256
                        "application/marc-in-json",
257
                        "application/marc"
258
                    ]
259
                );
260
            }
261
        }
262
263
        my $authid = ModAuthority( $authid, $record, $authtypecode );
264
265
        $c->render(
266
            status  => 200,
267
            openapi => { id => $authid }
268
        );
269
    }
270
    catch {
271
        $c->unhandled_exception($_);
272
    };
273
}
208
1;
274
1;
(-)a/api/v1/swagger/paths/authorities.yaml (-2 / +62 lines)
Lines 130-140 Link Here
130
        schema:
130
        schema:
131
          $ref: "../swagger.yaml#/definitions/error"
131
          $ref: "../swagger.yaml#/definitions/error"
132
      "404":
132
      "404":
133
        description: Biblio not found
133
        description: Authority not found
134
        schema:
134
        schema:
135
          $ref: "../swagger.yaml#/definitions/error"
135
          $ref: "../swagger.yaml#/definitions/error"
136
      "409":
136
      "409":
137
        description: Unable to perform action on biblio
137
        description: Unable to perform action on Authority
138
        schema:
138
        schema:
139
          $ref: "../swagger.yaml#/definitions/error"
139
          $ref: "../swagger.yaml#/definitions/error"
140
      "500":
140
      "500":
Lines 148-150 Link Here
148
    x-koha-authorization:
148
    x-koha-authorization:
149
      permissions:
149
      permissions:
150
        editcatalogue: edit_catalogue
150
        editcatalogue: edit_catalogue
151
  put:
152
    x-mojo-to: Authorities#update
153
    operationId: updateAuthority
154
    tags:
155
      - authorities
156
    summary: Update authority
157
    parameters:
158
      - $ref: "../swagger.yaml#/parameters/authority_id_pp"
159
      - name: Body
160
        in: body
161
        description: A JSON object or the Marc string describing an authority
162
        required: true
163
        schema:
164
          type:
165
            - string
166
            - object
167
      - $ref: "../swagger.yaml#/parameters/authority_type_header"
168
      - $ref: "../swagger.yaml#/parameters/confirm_not_duplicate_header"
169
    produces:
170
      - application/json
171
    responses:
172
      "200":
173
        description: An authority
174
      "400":
175
        description: Bad request
176
        schema:
177
          $ref: "../swagger.yaml#/definitions/error"
178
      "401":
179
        description: Authentication required
180
        schema:
181
          $ref: "../swagger.yaml#/definitions/error"
182
      "403":
183
        description: Access forbidden
184
        schema:
185
          $ref: "../swagger.yaml#/definitions/error"
186
      "404":
187
        description: Authority not found
188
        schema:
189
          $ref: "../swagger.yaml#/definitions/error"
190
      "406":
191
        description: Not acceptable
192
        schema:
193
          type: array
194
          description: Accepted content-types
195
          items:
196
            type: string
197
      "500":
198
        description: |
199
          Internal server error. Possible `error_code` attribute values:
200
201
          * `internal_server_error`
202
        schema:
203
          $ref: "../swagger.yaml#/definitions/error"
204
      "503":
205
        description: Under maintenance
206
        schema:
207
          $ref: "../swagger.yaml#/definitions/error"
208
    x-koha-authorization:
209
      permissions:
210
        editcatalogue: edit_catalogue
(-)a/t/db_dependent/api/v1/authorities.t (-5 / +120 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 => 3;
23
use Test::More tests => 4;
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 216-232 subtest 'post() tests' => sub { Link Here
216
      ->status_is(200)
216
      ->status_is(200)
217
      ->json_has('/id');
217
      ->json_has('/id');
218
218
219
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marcxml+xml', authority_type => 'CORPO_NAME'} => $marcxml)
219
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marcxml+xml', 'x-authority-type' => 'CORPO_NAME'} => $marcxml)
220
      ->status_is(200)
220
      ->status_is(200)
221
      ->json_has('/id');
221
      ->json_has('/id');
222
222
223
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc-in-json', authority_type => 'CORPO_NAME'} => $mij)
223
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc-in-json', 'x-authority-type' => 'CORPO_NAME'} => $mij)
224
      ->status_is(200)
224
      ->status_is(200)
225
      ->json_has('/id');
225
      ->json_has('/id');
226
226
227
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc', authority_type => 'CORPO_NAME'} => $marc)
227
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc', 'x-authority-type' => 'CORPO_NAME'} => $marc)
228
      ->status_is(200)
228
      ->status_is(200)
229
      ->json_has('/id');
229
      ->json_has('/id');
230
230
231
    $schema->storage->txn_rollback;
232
};
233
234
subtest 'put() tests' => sub {
235
236
    plan tests => 18;
237
238
    $schema->storage->txn_begin;
239
240
    Koha::Authorities->delete;
241
242
    my $record;
243
    my $subfield_a;
244
245
    my $patron = $builder->build_object(
246
        {
247
            class => 'Koha::Patrons',
248
            value => { flags => 0 } # no permissions
249
        }
250
    );
251
    my $password = 'thePassword123';
252
    $patron->set_password( { password => $password, skip_validation => 1 } );
253
    my $userid = $patron->userid;
254
255
    my $authority = $builder->build_object({ 'class' => 'Koha::Authorities', value => {
256
      marcxml => q|<?xml version="1.0" encoding="UTF-8"?>
257
<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">
258
    <controlfield tag="001">1001</controlfield>
259
    <datafield tag="110" ind1=" " ind2=" ">
260
        <subfield code="9">102</subfield>
261
        <subfield code="a">My Corporation</subfield>
262
    </datafield>
263
</record>|
264
    } });
265
266
    my $authid       = $authority->authid;
267
    my $authtypecode = $authority->authtypecode;
268
269
    my $marcxml = q|<?xml version="1.0" encoding="UTF-8"?>
270
<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">
271
    <controlfield tag="001">1001</controlfield>
272
    <datafield tag="110" ind1=" " ind2=" ">
273
        <subfield code="9">102</subfield>
274
        <subfield code="a">MARCXML</subfield>
275
    </datafield>
276
</record>|;
277
278
    my $mij = '{"fields":[{"001":"1001"},{"110":{"subfields":[{"9":"102"},{"a":"MIJ"}],"ind1":" ","ind2":" "}}],"leader":"                        "}';
279
    my $marc = '00079     2200049   45000010005000001100024000051001  9102aUSMARCFormated';
280
    my $json = {
281
      authtypecode => $authtypecode,
282
      marcxml      => q|<?xml version="1.0" encoding="UTF-8"?>
283
<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">
284
    <controlfield tag="001">1001</controlfield>
285
    <datafield tag="110" ind1=" " ind2=" ">
286
        <subfield code="9">102</subfield>
287
        <subfield code="a">JSON</subfield>
288
    </datafield>
289
</record>|
290
    };
291
292
    $t->put_ok("//$userid:$password@/api/v1/authorities/$authid")
293
      ->status_is(403, 'Not enough permissions makes it return the right code');
294
295
    # Add permissions
296
    $builder->build(
297
        {
298
            source => 'UserPermission',
299
            value  => {
300
                borrowernumber => $patron->borrowernumber,
301
                module_bit     => 9,
302
                code           => 'edit_catalogue'
303
            }
304
        }
305
    );
306
307
    $t->put_ok("//$userid:$password@/api/v1/authorities/$authid" => json => $json)
308
      ->status_is(200)
309
      ->json_has('/id');
310
311
    $authority = Koha::Authorities->find($authid);
312
    $record = $authority->record;
313
    $subfield_a = $record->subfield('110', 'a');
314
315
    is($subfield_a, 'JSON');
316
317
    $t->put_ok("//$userid:$password@/api/v1/authorities/$authid" => {'Content-Type' => 'application/marcxml+xml', 'x-authority-type' => $authtypecode} => $marcxml)
318
      ->status_is(200)
319
      ->json_has('/id');
320
321
    $authority = Koha::Authorities->find($authid);
322
    $record = $authority->record;
323
    $subfield_a = $record->subfield('110', 'a');
324
325
    is($subfield_a, 'MARCXML');
326
327
    $t->put_ok("//$userid:$password@/api/v1/authorities/$authid" => {'Content-Type' => 'application/marc-in-json', 'x-authority-type' => $authtypecode} => $mij)
328
      ->status_is(200)
329
      ->json_has('/id');
330
331
    $authority = Koha::Authorities->find($authid);
332
    $record = $authority->record;
333
    $subfield_a = $record->subfield('110', 'a');
334
335
    is($subfield_a, 'MIJ');
336
337
    $t->put_ok("//$userid:$password@/api/v1/authorities/$authid" => {'Content-Type' => 'application/marc', 'x-authority-type' => $authtypecode} => $marc)
338
      ->status_is(200)
339
      ->json_has('/id');
340
341
    $authority = Koha::Authorities->find($authid);
342
    $record = $authority->record;
343
    $subfield_a = $record->subfield('110', 'a');
344
345
    is($subfield_a, 'USMARCFormated');
346
231
    $schema->storage->txn_rollback;
347
    $schema->storage->txn_rollback;
232
};
348
};
233
- 

Return to bug 31796