From 29c1a17da548a1690a80ddfd2e2fe6ffd458b8b2 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Mon, 8 Sep 2025 15:44:53 +0000 Subject: [PATCH] Bug 37762: Add ISO18626 tests prove t/db_dependent/api/v1/iso18626/request.t prove t/db_dependent/api/v1/iso18626/requesting_agencies.t Signed-off-by: Jason Robb Signed-off-by: Lisette Scheer Signed-off-by: Lucas Gass --- t/db_dependent/api/v1/iso18626/request.t | 251 ++++++++++++++++++ .../api/v1/iso18626/requesting_agencies.t | 85 ++++++ 2 files changed, 336 insertions(+) create mode 100755 t/db_dependent/api/v1/iso18626/request.t create mode 100755 t/db_dependent/api/v1/iso18626/requesting_agencies.t diff --git a/t/db_dependent/api/v1/iso18626/request.t b/t/db_dependent/api/v1/iso18626/request.t new file mode 100755 index 0000000000..597f2db7b7 --- /dev/null +++ b/t/db_dependent/api/v1/iso18626/request.t @@ -0,0 +1,251 @@ +#!/usr/bin/env perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::NoWarnings; +use Test::More tests => 2; + +use Test::MockModule; +use Test::MockObject; +use Test::Mojo; + +use MIME::Base64 qw(encode_base64); +use JSON qw(encode_json); + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::AuthorisedValueCategories; +use Koha::ILL::ISO18626::Requests; +use Koha::DateUtils qw( format_sqldatetime ); + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); +t::lib::Mocks::mock_preference( 'ILLModule', 1 ); + +my $t = Test::Mojo->new('Koha::REST::V1'); + +subtest 'list() tests' => sub { + + plan tests => 29; + + $schema->storage->txn_begin; + + # create an authorized user + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**22 } # 22 => ill + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + my $requestXml = <<'XML'; + +
+ XYZ + 2023-03-15 14:30:00 + + ISIL + req_agency_value + +
+ + This is an optional title + +
+XML + + #FIXME: This error message should be something like "Expected content-type application/xml" + $t->post_ok( + "//$userid:$password@/api/v1/public/ill/iso18626", + $requestXml + ) + ->status_is(400) + ->json_is( '/errors' => [ { path => '/message', message => 'Expected object - got string.' } ] ); + + $t->post_ok( + "//$userid:$password@/api/v1/public/ill/iso18626", + { 'Content-Type' => 'application/xml' }, + $requestXml + )->status_is(400)->content_like( + qr{/request/serviceInfo: object, required}, + 'serviceInfo missing' + )->content_like( + qr{ERROR}, + 'messageStatus is ERROR' + ); + + my $bad_auth_requestXml = <<'XML'; + +
+ XYZ + 2023-03-15 14:30:00 + + ISIL + req_agency_value + +
+ + This is an optional title + + + Copy + +
+XML + + $t->post_ok( + "//$userid:$password@/api/v1/public/ill/iso18626", + { 'Content-Type' => 'application/xml' }, + $bad_auth_requestXml + )->status_is(400)->content_like( + qr{[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z}, + 'timestamp is present' + )->content_like( + qr{AuthenticationFailed}, + 'error value is authentication failed' + ); + my $requesting_agency = $builder->build_object( + { + class => 'Koha::ILL::ISO18626::RequestingAgencies', + value => { account_id => 'asd', securityCode => 'asds' } + } + ); + + my $authenticated_requestXml = <<'XML'; + +
+ + asd + asds + + XYZ + 2023-03-15 14:30:00 + + ISIL + req_agency_value + +
+ + This is an optional title + + + Copy + +
+XML + + $t->post_ok( + "//$userid:$password@/api/v1/public/ill/iso18626", + { 'Content-Type' => 'application/xml' }, + $authenticated_requestXml + )->status_is(201)->content_like( + qr{[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z}, + 'timestamp is present' + )->content_like( + qr{OK}, + 'messageStatus is OK' + ); + + my $last_request = Koha::ILL::ISO18626::Requests->search( + {}, + { order_by => { -desc => 'iso18626_request_id' }, rows => 1 } + )->single; + + $t->patch_ok( "//$userid:$password@/api/v1/ill/iso18626_requests/" + . $last_request->iso18626_request_id => json => { status => 'Loaned' } )->status_is(200); + + my $requestingAgencyMessagexml = ' + +
+ + asd + asds + + 1 + %s + 2023-03-15 14:30:00 + + ISIL + req_agency_value + + + ISIL + sup_agency_value + +
+ %s +
'; + + my $invalid_action_requestingAgencyMessagexml = + sprintf( $requestingAgencyMessagexml, $last_request->iso18626_request_id, 'InvalidAction' ); + + $t->post_ok( + "//$userid:$password@/api/v1/public/ill/iso18626", + { 'Content-Type' => 'application/xml' }, + $invalid_action_requestingAgencyMessagexml + )->status_is(400)->content_like( + qr{BadlyFormedMessage}, + 'invalid action error as expected' + ); + + my $unsupported_action_requestingAgencyMessagexml = + sprintf( $requestingAgencyMessagexml, $last_request->iso18626_request_id, 'ShippedForward' ); + $t->post_ok( + "//$userid:$password@/api/v1/public/ill/iso18626", + { 'Content-Type' => 'application/xml' }, + $unsupported_action_requestingAgencyMessagexml + )->status_is(400)->content_like( + qr{UnsupportedActionType}, + 'unsupported action error as expected' + ); + + my $good_requestingAgencyMessagexml = + sprintf( $requestingAgencyMessagexml, $last_request->iso18626_request_id, 'StatusRequest' ); + + $t->post_ok( + "//$userid:$password@/api/v1/public/ill/iso18626", + { 'Content-Type' => 'application/xml' }, + $good_requestingAgencyMessagexml + )->status_is(201)->content_like( + qr{OK}, + 'message is okay' + ); + + my $nonexistent_supplyingAgency_requestingAgencyMessagexml = + sprintf( $requestingAgencyMessagexml, $last_request->iso18626_request_id, 'StatusRequest' ); + + $last_request->delete; + + $t->post_ok( + "//$userid:$password@/api/v1/public/ill/iso18626", + { 'Content-Type' => 'application/xml' }, + $nonexistent_supplyingAgency_requestingAgencyMessagexml + )->status_is(400)->content_like( + qr{UnrecognizedDataValue}, + 'Cant find this supplyingAgencyRequestId' + ); + + $schema->storage->txn_rollback; +}; + diff --git a/t/db_dependent/api/v1/iso18626/requesting_agencies.t b/t/db_dependent/api/v1/iso18626/requesting_agencies.t new file mode 100755 index 0000000000..2651cfb3a0 --- /dev/null +++ b/t/db_dependent/api/v1/iso18626/requesting_agencies.t @@ -0,0 +1,85 @@ +#!/usr/bin/env perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::NoWarnings; +use Test::More tests => 2; + +use Test::MockModule; +use Test::MockObject; +use Test::Mojo; + +use MIME::Base64 qw(encode_base64); +use JSON qw(encode_json); + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::ILL::ISO18626::RequestingAgencies; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + +my $t = Test::Mojo->new('Koha::REST::V1'); + +subtest 'list() tests' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + # create an authorized user + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**22 } # 22 => ill + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + my $requesting_agency_1 = $builder->build_object( + { + class => 'Koha::ILL::ISO18626::RequestingAgencies', + } + ); + + my $requesting_agency_2 = $builder->build_object( + { + class => 'Koha::ILL::ISO18626::RequestingAgencies', + } + ); + + # Two requesting agencies created, they should both be returned + $t->get_ok("//$userid:$password@/api/v1/ill/iso18626_requesting_agencies")->status_is(200)->json_is( + [ + $requesting_agency_1->to_api, + $requesting_agency_2->to_api, + ] + ); + + $t->get_ok( "//$userid:$password@/api/v1/ill/iso18626_requesting_agencies/" + . $requesting_agency_1->iso18626_requesting_agency_id )->status_is(200)->json_is( + $requesting_agency_1->to_api, + ); + + $schema->storage->txn_rollback; +};