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

(-)a/Koha/REST/V1/Authorities.pm (-1 / +61 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 AddAuthority FindDuplicateAuthority);
23
use C4::AuthoritiesMarc qw( DelAuthority AddAuthority FindDuplicateAuthority ModAuthority);
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 193-196 sub add { Link Here
193
    };
193
    };
194
}
194
}
195
195
196
197
=head3 update
198
199
Controller function that handles modifying an authority object
200
201
=cut
202
203
sub update {
204
    my $c = shift->openapi->valid_input or return;
205
206
    my $authid = $c->validation->param('authority_id');
207
    my $authority = Koha::Authorities->find( { authid => $authid } );
208
209
    if ( not defined $authority ) {
210
        return $c->render(
211
            status  => 404,
212
            openapi => { error => "Object not found" }
213
        );
214
    }
215
216
    try {
217
        my $headers = $c->req->headers;
218
219
        my $flavour =
220
            C4::Context->preference('marcflavour') eq 'UNIMARC'
221
            ? 'UNIMARCAUTH'
222
            : 'MARC21';
223
224
        my $record;
225
        my $authtypecode = $headers->header('x-authority-type') || $authority->authtypecode;
226
        if ( $c->req->headers->content_type =~ m/application\/marcxml\+xml/ ) {
227
            $record = MARC::Record->new_from_xml( $c->req->body, 'UTF-8', $flavour );
228
        } elsif ( $c->req->headers->content_type =~ m/application\/marc-in-json/ ) {
229
            $record = MARC::Record->new_from_mij_structure( $c->req->json );
230
        } elsif ( $c->req->headers->content_type =~ m/application\/marc/ ) {
231
            $record = MARC::Record->new_from_usmarc( $c->req->body );
232
        } else {
233
            return $c->render(
234
                status  => 406,
235
                openapi => [
236
                    "application/json",
237
                    "application/marcxml+xml",
238
                    "application/marc-in-json",
239
                    "application/marc"
240
                ]
241
            );
242
        }
243
244
        my $authid = ModAuthority( $authid, $record, $authtypecode );
245
246
        $c->render(
247
            status  => 200,
248
            openapi => { id => $authid }
249
        );
250
    }
251
    catch {
252
        $c->unhandled_exception($_);
253
    };
254
}
255
196
1;
256
1;
(-)a/api/v1/swagger/paths/authorities.yaml (+51 lines)
Lines 163-165 Link Here
163
    x-koha-authorization:
163
    x-koha-authorization:
164
      permissions:
164
      permissions:
165
        editauthorities: "1"
165
        editauthorities: "1"
166
  put:
167
    x-mojo-to: Authorities#update
168
    operationId: updateAuthority
169
    tags:
170
      - authorities
171
    summary: Update authority
172
    parameters:
173
      - $ref: "../swagger.yaml#/parameters/authority_id_pp"
174
      - $ref: "../swagger.yaml#/parameters/authority_type_header"
175
    produces:
176
      - application/json
177
    responses:
178
      "200":
179
        description: An authority id
180
      "400":
181
        description: Bad request
182
        schema:
183
          $ref: "../swagger.yaml#/definitions/error"
184
      "401":
185
        description: Authentication required
186
        schema:
187
          $ref: "../swagger.yaml#/definitions/error"
188
      "403":
189
        description: Access forbidden
190
        schema:
191
          $ref: "../swagger.yaml#/definitions/error"
192
      "404":
193
        description: Authority not found
194
        schema:
195
          $ref: "../swagger.yaml#/definitions/error"
196
      "406":
197
        description: Not acceptable
198
        schema:
199
          type: array
200
          description: Accepted content-types
201
          items:
202
            type: string
203
      "500":
204
        description: |
205
          Internal server error. Possible `error_code` attribute values:
206
207
          * `internal_server_error`
208
        schema:
209
          $ref: "../swagger.yaml#/definitions/error"
210
      "503":
211
        description: Under maintenance
212
        schema:
213
          $ref: "../swagger.yaml#/definitions/error"
214
    x-koha-authorization:
215
      permissions:
216
        editauthorities: "1"
(-)a/t/db_dependent/api/v1/authorities.t (-2 / +87 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 228-230 subtest 'post() tests' => sub { Link Here
228
228
229
    $schema->storage->txn_rollback;
229
    $schema->storage->txn_rollback;
230
};
230
};
231
- 
231
232
subtest 'put() tests' => sub {
233
234
    plan tests => 14;
235
236
    $schema->storage->txn_begin;
237
238
    Koha::Authorities->delete;
239
240
    my $record;
241
    my $subfield_a;
242
243
    my $patron = $builder->build_object(
244
        {
245
            class => 'Koha::Patrons',
246
            value => { flags => 0 } # no permissions
247
        }
248
    );
249
    my $password = 'thePassword123';
250
    $patron->set_password( { password => $password, skip_validation => 1 } );
251
    my $userid = $patron->userid;
252
253
    my $authority = $builder->build_object({ 'class' => 'Koha::Authorities', value => {
254
      marcxml => q|<?xml version="1.0" encoding="UTF-8"?>
255
<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">
256
    <controlfield tag="001">1001</controlfield>
257
    <datafield tag="110" ind1=" " ind2=" ">
258
        <subfield code="9">102</subfield>
259
        <subfield code="a">My Corporation</subfield>
260
    </datafield>
261
</record>|
262
    } });
263
264
    my $authid       = $authority->authid;
265
    my $authtypecode = $authority->authtypecode;
266
267
    my $marcxml = q|<?xml version="1.0" encoding="UTF-8"?>
268
<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">
269
    <controlfield tag="001">1001</controlfield>
270
    <datafield tag="110" ind1=" " ind2=" ">
271
        <subfield code="9">102</subfield>
272
        <subfield code="a">MARCXML</subfield>
273
    </datafield>
274
</record>|;
275
276
    my $mij = '{"fields":[{"001":"1001"},{"110":{"subfields":[{"9":"102"},{"a":"MIJ"}],"ind1":" ","ind2":" "}}],"leader":"                        "}';
277
    my $marc = '00079     2200049   45000010005000001100024000051001  9102aUSMARCFormated';
278
279
    $t->put_ok("//$userid:$password@/api/v1/authorities/$authid")
280
      ->status_is(403, 'Not enough permissions makes it return the right code');
281
282
    # Add permissions
283
    $patron->flags( 2 ** 14 )->store; # 14 => editauthorities userflag
284
285
    $t->put_ok("//$userid:$password@/api/v1/authorities/$authid" => {'Content-Type' => 'application/marcxml+xml', 'x-authority-type' => $authtypecode} => $marcxml)
286
      ->status_is(200)
287
      ->json_has('/id');
288
289
    $authority = Koha::Authorities->find($authid);
290
    $record = $authority->record;
291
    $subfield_a = $record->subfield('110', 'a');
292
293
    is($subfield_a, 'MARCXML');
294
295
    $t->put_ok("//$userid:$password@/api/v1/authorities/$authid" => {'Content-Type' => 'application/marc-in-json', 'x-authority-type' => $authtypecode} => $mij)
296
      ->status_is(200)
297
      ->json_has('/id');
298
299
    $authority = Koha::Authorities->find($authid);
300
    $record = $authority->record;
301
    $subfield_a = $record->subfield('110', 'a');
302
303
    is($subfield_a, 'MIJ');
304
305
    $t->put_ok("//$userid:$password@/api/v1/authorities/$authid" => {'Content-Type' => 'application/marc', 'x-authority-type' => $authtypecode} => $marc)
306
      ->status_is(200)
307
      ->json_has('/id');
308
309
    $authority = Koha::Authorities->find($authid);
310
    $record = $authority->record;
311
    $subfield_a = $record->subfield('110', 'a');
312
313
    is($subfield_a, 'USMARCFormated');
314
315
    $schema->storage->txn_rollback;
316
};

Return to bug 31796