From 11978e0f172b8a76d69d3fdf15548684e6d410be Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 13 Jun 2023 10:13:40 -0300 Subject: [PATCH] Bug 33996: Add missing authority objects mapping This patch adds a minimal mapping (i.e. some things get excluded from the API objects for now). It makes some attributes consistent with the current API names, and the rest can be added later when they become needed and we discuss a proper name. But right now is a mess :-/ To test: 1. Apply this patch 2. Run: $ ktd --shell k$ prove t/db_dependent/api/v1/authorities.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Sam Lau Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 3b0fc1fc41ce540a3f1373ec3d0e8aa84a31c046) Signed-off-by: Martin Renvoize --- Koha/Authority.pm | 27 +++++++++-- t/db_dependent/api/v1/authorities.t | 71 +++++++++++++---------------- 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/Koha/Authority.pm b/Koha/Authority.pm index ee175c2244..d05192007a 100644 --- a/Koha/Authority.pm +++ b/Koha/Authority.pm @@ -30,7 +30,7 @@ Koha::Authority - Koha Authority Object class =head1 API -=head2 Instance Methods +=head2 Class methods =head3 get_usage_count @@ -168,9 +168,30 @@ sub record_schema { : 'MARC21'; } -=head2 Class Methods +=head3 to_api_mapping -=head3 type +This method returns the mapping for representing a Koha::Authority object +on the API. + +=cut + +sub to_api_mapping { + return { + authid => 'authority_id', + authtrees => undef, + authtypecode => 'framework_id', + datecreated => 'creation_date', + linkid => undef, + marc => undef, + marcxml => undef, + modification_time => 'updated_on', + origincode => undef, + }; +} + +=head2 Internal methods + +=head3 _type =cut diff --git a/t/db_dependent/api/v1/authorities.t b/t/db_dependent/api/v1/authorities.t index ba6eaaf186..85da554eb1 100755 --- a/t/db_dependent/api/v1/authorities.t +++ b/t/db_dependent/api/v1/authorities.t @@ -28,6 +28,8 @@ use Test::Warn; use t::lib::Mocks; use t::lib::TestBuilder; +use Mojo::JSON qw(encode_json); + use C4::Auth; use Koha::Authorities; @@ -67,34 +69,34 @@ subtest 'get() tests' => sub { | } }); - $t->get_ok("//$userid:$password@/api/v1/authorities/" . $authority->authid) + $t->get_ok("//$userid:$password@/api/v1/authorities/" . $authority->id) ->status_is(403); $patron->flags(4)->store; - $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid + $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->id => { Accept => 'application/weird+format' } ) ->status_is(400); - $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid + $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->id => { Accept => 'application/json' } ) ->status_is(200) - ->json_is( '/authid', $authority->authid ) - ->json_is( '/authtypecode', $authority->authtypecode ); + ->json_is( '/authority_id', $authority->id ) + ->json_is( '/framework_id', $authority->authtypecode ); - $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid + $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->id => { Accept => 'application/marcxml+xml' } ) ->status_is(200); - $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid + $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->id => { Accept => 'application/marc-in-json' } ) ->status_is(200); - $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid + $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->id => { Accept => 'application/marc' } ) ->status_is(200); - $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid + $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->id => { Accept => 'text/plain' } ) ->status_is(200) ->content_is(q|LDR 00079 2200049 4500 @@ -103,7 +105,7 @@ subtest 'get() tests' => sub { _aMy Corporation|); $authority->delete; - $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid + $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->id => { Accept => 'application/marc' } ) ->status_is(404) ->json_is( '/error', 'Object not found.' ); @@ -138,16 +140,16 @@ subtest 'delete() tests' => sub { | } }); - $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid) + $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->id) ->status_is(403, 'Not enough permissions makes it return the right code'); $patron->flags( 2 ** 14 )->store; # 14 => editauthorities userflag - $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid) + $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->id) ->status_is(204, 'SWAGGER3.2.4') ->content_is('', 'SWAGGER3.3.4'); - $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid) + $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->id) ->status_is(404); $schema->storage->txn_rollback; @@ -261,7 +263,7 @@ subtest 'put() tests' => sub { | } }); - my $authid = $authority->authid; + my $authid = $authority->id; my $authtypecode = $authority->authtypecode; my $marcxml = q| @@ -333,7 +335,7 @@ subtest 'list() tests' => sub { $patron->discard_changes; my $userid = $patron->userid; - my $authid1 = $builder->build_object({ 'class' => 'Koha::Authorities', value => { + my $auth_id_1 = $builder->build_object({ 'class' => 'Koha::Authorities', value => { marcxml => q| 1001 @@ -344,7 +346,7 @@ subtest 'list() tests' => sub { | } })->authid; - my $authid2 = $builder->build_object({ 'class' => 'Koha::Authorities', value => { + my $auth_id_2 = $builder->build_object({ 'class' => 'Koha::Authorities', value => { marcxml => q| 1001 @@ -355,37 +357,28 @@ subtest 'list() tests' => sub { | } })->authid; - my $search = -"[{\"authid\": \"$authid1\"}, {\"authid\": \"$authid2\"}]"; - $t->get_ok( - "//$userid:$password@/api/v1/authorities/" => { 'x-koha-query' => $search } - )->status_is(403); + my $query = encode_json( [ { authority_id => $auth_id_1 }, { authority_id => $auth_id_2 } ] ); + + $t->get_ok("//$userid:$password@/api/v1/authorities?q=$query")->status_is(403); $patron->flags(4)->store; - $t->get_ok( "//$userid:$password@/api/v1/authorities/" => - { Accept => 'application/weird+format', 'x-koha-query' => $search } ) - ->status_is(400); + $t->get_ok( "//$userid:$password@/api/v1/authorities?q=$query" => { Accept => 'application/weird+format' } ) + ->status_is(400); - $t->get_ok( "//$userid:$password@/api/v1/authorities/" => - { Accept => 'application/json', 'x-koha-query' => $search } ) - ->status_is(200); + $t->get_ok( "//$userid:$password@/api/v1/authorities?q=$query" => { Accept => 'application/json' } ) + ->status_is(200); - $t->get_ok( "//$userid:$password@/api/v1/authorities/" => - { Accept => 'application/marcxml+xml', 'x-koha-query' => $search } ) - ->status_is(200); + $t->get_ok( "//$userid:$password@/api/v1/authorities?q=$query" => { Accept => 'application/marcxml+xml' } ) + ->status_is(200); - $t->get_ok( "//$userid:$password@/api/v1/authorities/" => - { Accept => 'application/marc-in-json', 'x-koha-query' => $search } ) - ->status_is(200); + $t->get_ok( "//$userid:$password@/api/v1/authorities?q=$query" => { Accept => 'application/marc-in-json' } ) + ->status_is(200); - $t->get_ok( "//$userid:$password@/api/v1/authorities/" => - { Accept => 'application/marc', 'x-koha-query' => $search } ) - ->status_is(200); + $t->get_ok( "//$userid:$password@/api/v1/authorities?q=$query" => { Accept => 'application/marc' } ) + ->status_is(200); - $t->get_ok( "//$userid:$password@/api/v1/authorities/" => - { Accept => 'text/plain', 'x-koha-query' => $search } ) - ->status_is(200); + $t->get_ok( "//$userid:$password@/api/v1/authorities?q=$query" => { Accept => 'text/plain' } )->status_is(200); $schema->storage->txn_rollback; }; -- 2.39.5