Koha/t/db_dependent/api/v1/ill_batches.t
Pedro Amorim ac157b7a75
Bug 35581: Koha::Illrequest -> Koha::ILL::Request
Koha/Illrequests.pm -> Koha/ILL/Requests.pm

Merged:
t/db_dependent/Koha/Illrequests.t
t/db_dependent/Illrequests.t

Into:
t/db_dependent/Koha/ILL/Requests.t

ILL classes file structure is, for the most part, around 7 years old and doesn't follow a strict logic. It's so confusing that some test files exist redundantly.

This housekeeping should help future work in regards to ISO18626 to add Koha as a supplying agency instead of just requesting agency, as is now.
It should also help future housekeeping of moving backend related logic out of the Illrequest.pm into Illbackend.pm (now ILL/Request.pm and ILL/Backend.pm as of this patchset).
It should also help in structuring the addition of a master generic form (see bug 35570)

This patchset will require existing backends to be updated to match the new class names and structure, if they invoke them.

Test plan, k-t-d, run tests:
prove t/db_dependent/api/v1/ill_*
prove t/db_dependent/Koha/ILL/*

Test plan, k-t-d, manual:
1) Install FreeForm, enable ILL module, run:
bash <(curl -s https://raw.githubusercontent.com/ammopt/koha-ill-dev/master/start-ill-dev.sh)
2) You'll have to switch the FreeForm repo to the one compatible with this work, like:
cd /kohadevbox/koha/Koha/Illbackends/FreeForm
git checkout reorganize_ILL
3) Do some generic ILL testing:
3.1) Create a request
3.2) Add a comment to a request
3.3) Edit a request
3.4) Edit a request's item metadata
3.5) Confirm a request
3.6) List requests
3.7) Filter requests list using left side filters
4) Install a metadata enrichment plugin:
https://github.com/PTFS-Europe/koha-plugin-api-pubmed
4.1) Create an ILL batch and insert a pubmedid like 123
4.2) Add the request and finish batch
5) Verify all of the above works as expected

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Pedro Amorim <pedro.amorim@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-04-22 08:57:45 +02:00

391 lines
13 KiB
Perl
Executable file

#!/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 <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 5;
use Test::Mojo;
use t::lib::TestBuilder;
use t::lib::Mocks;
use JSON qw(encode_json);
use Koha::ILL::Batch;
use Koha::ILL::Batches;
use Koha::ILL::Requests;
use Koha::ILL::Batch::Statuses;
use Koha::Database;
my $schema = Koha::Database->new->schema;
my $builder = t::lib::TestBuilder->new;
my $t = Test::Mojo->new('Koha::REST::V1');
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
subtest 'list() tests' => sub {
plan tests => 21;
$schema->storage->txn_begin;
my $librarian = $builder->build_object(
{
class => 'Koha::Patrons',
value => {
flags => 2**22 # 22 => ill
}
}
);
my $library = $builder->build_object( { class => 'Koha::Libraries' } );
my $password = 'sheev_is_da_boss!';
$librarian->set_password( { password => $password, skip_validation => 1 } );
my $userid = $librarian->userid;
my $batch_to_delete = $builder->build_object( { class => 'Koha::ILL::Batches' } );
my $deleted_batch_id = $batch_to_delete->id;
$batch_to_delete->delete;
my $query = { ill_batch_id => [$deleted_batch_id] };
## Authorized user tests
# No batches, so empty array should be returned
$t->get_ok( "//$userid:$password@/api/v1/ill/batches?q=" . encode_json($query) )->status_is(200)->json_is( [] );
my $batch_1 = $builder->build_object(
{
class => 'Koha::ILL::Batches',
value => {
backend => "Mock",
patron_id => $librarian->id,
library_id => $library->id,
}
}
);
my $illrq = $builder->build_object(
{
class => 'Koha::ILL::Requests',
value => {
batch_id => $batch_1->id,
borrowernumber => $librarian->id,
}
}
);
$query = { ill_batch_id => [ $batch_1->id ] };
# One batch created, should get returned
$t->get_ok( "//$userid:$password@/api/v1/ill/batches?q="
. encode_json($query) => { 'x-koha-embed' => '+strings,requests+count,patron,library' } )->status_is(200)
->json_has( '/0/ill_batch_id', 'Batch ID' )->json_has( '/0/name', 'Batch name' )
->json_has( '/0/backend', 'Backend name' )->json_has( '/0/patron_id', 'Borrowernumber' )
->json_has( '/0/library_id', 'Branchcode' )->json_has( '/0/patron', 'patron embedded' )
->json_has( '/0/library', 'branch embedded' )->json_has( '/0/requests_count', 'request count' );
# Create a second batch with a different name
my $batch_2 = $builder->build_object( { class => 'Koha::ILL::Batches' } );
$query = { ill_batch_id => [ $batch_1->id, $batch_2->id ] };
# Two batches created, they should both be returned
$t->get_ok( "//$userid:$password@/api/v1/ill/batches?q=" . encode_json($query) )->status_is(200)
->json_has( '/0', 'has first batch' )->json_is( '/0/ill_batch_id', $batch_1->id )
->json_has( '/1', 'has second batch' )->json_is( '/1/ill_batch_id', $batch_2->id );
my $patron = $builder->build_object(
{
class => 'Koha::Patrons',
value => {
cardnumber => 999,
flags => 0
}
}
);
$patron->set_password( { password => $password, skip_validation => 1 } );
my $unauth_userid = $patron->userid;
# Unauthorized access
$t->get_ok("//$unauth_userid:$password@/api/v1/ill/batches")->status_is(403);
$schema->storage->txn_rollback;
};
subtest 'get() tests' => sub {
plan tests => 15;
$schema->storage->txn_begin;
my $librarian = $builder->build_object(
{
class => 'Koha::Patrons',
value => { flags => 2**22 } # 22 => ill
}
);
my $password = 'Rebelz4DaWin';
$librarian->set_password( { password => $password, skip_validation => 1 } );
my $userid = $librarian->userid;
my $patron = $builder->build_object(
{
class => 'Koha::Patrons',
value => { flags => 0 }
}
);
my $library = $builder->build_object( { class => 'Koha::Libraries' } );
my $batch = $builder->build_object(
{
class => 'Koha::ILL::Batches',
value => {
backend => "Mock",
patron_id => $librarian->id,
library_id => $library->id,
}
}
);
$patron->set_password( { password => $password, skip_validation => 1 } );
my $unauth_userid = $patron->userid;
$t->get_ok( "//$userid:$password@/api/v1/ill/batches/"
. $batch->id => { 'x-koha-embed' => '+strings,requests+count,patron,library' } )->status_is(200)
->json_has( '/ill_batch_id', 'Batch ID' )->json_has( '/name', 'Batch name' )
->json_has( '/backend', 'Backend name' )->json_has( '/patron_id', 'Borrowernumber' )
->json_has( '/library_id', 'Branchcode' )->json_has( '/patron', 'patron embedded' )
->json_has( '/library', 'library embedded' )->json_has( '/requests_count', 'request count' );
$t->get_ok( "//$unauth_userid:$password@/api/v1/ill/batches/" . $batch->id )->status_is(403);
my $batch_to_delete = $builder->build_object( { class => 'Koha::ILL::Batches' } );
my $non_existent_id = $batch_to_delete->id;
$batch_to_delete->delete;
$t->get_ok("//$userid:$password@/api/v1/ill/batches/$non_existent_id")->status_is(404)
->json_is( '/error' => 'ILL batch not found' );
$schema->storage->txn_rollback;
};
subtest 'add() tests' => sub {
plan tests => 20;
$schema->storage->txn_begin;
my $librarian = $builder->build_object(
{
class => 'Koha::Patrons',
value => { flags => 2**22 } # 22 => ill
}
);
my $password = 'v4d3rRox';
$librarian->set_password( { password => $password, skip_validation => 1 } );
my $userid = $librarian->userid;
my $patron = $builder->build_object(
{
class => 'Koha::Patrons',
value => { flags => 0 }
}
);
$patron->set_password( { password => $password, skip_validation => 1 } );
my $unauth_userid = $patron->userid;
my $library = $builder->build_object( { class => 'Koha::Libraries' } );
my $batch_status = $builder->build_object( { class => 'Koha::ILL::Batch::Statuses' } );
my $batch_metadata = {
name => "Anakin's requests",
backend => "Mock",
cardnumber => $librarian->cardnumber,
library_id => $library->branchcode,
status_code => $batch_status->code
};
# Unauthorized attempt to write
$t->post_ok( "//$unauth_userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(403);
# Authorized attempt to write invalid data
my $batch_with_invalid_field = {
%{$batch_metadata},
doh => 1
};
$t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_with_invalid_field )->status_is(400)
->json_is(
"/errors" => [
{
message => "Properties not allowed: doh.",
path => "/body"
}
]
);
# Authorized attempt to write
my $batch_id =
$t->post_ok(
"//$userid:$password@/api/v1/ill/batches" => { 'x-koha-embed' => '+strings,requests+count,patron,library' } =>
json => $batch_metadata )->status_is(201)->json_is( '/name' => $batch_metadata->{name} )
->json_is( '/backend' => $batch_metadata->{backend} )->json_is( '/patron_id' => $librarian->borrowernumber )
->json_is( '/library_id' => $batch_metadata->{library_id} )->json_is( '/status_code' => $batch_status->code )
->json_has('/patron')->json_has('/_strings/status_code')->json_has('/_strings/library_id')
->json_has('/requests_count')->json_has('/library');
# Authorized attempt to create with null id
$batch_metadata->{id} = undef;
$t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(400)
->json_has('/errors');
$schema->storage->txn_rollback;
};
subtest 'update() tests' => sub {
plan tests => 15;
$schema->storage->txn_begin;
my $librarian = $builder->build_object(
{
class => 'Koha::Patrons',
value => { flags => 2**22 } # 22 => ill
}
);
my $password = 'aw3s0m3y0d41z';
$librarian->set_password( { password => $password, skip_validation => 1 } );
my $userid = $librarian->userid;
my $patron = $builder->build_object(
{
class => 'Koha::Patrons',
value => { flags => 0 }
}
);
$patron->set_password( { password => $password, skip_validation => 1 } );
my $unauth_userid = $patron->userid;
my $library = $builder->build_object( { class => 'Koha::Libraries' } );
my $batch_id = $builder->build_object( { class => 'Koha::ILL::Batches' } )->id;
# Unauthorized attempt to update
$t->put_ok( "//$unauth_userid:$password@/api/v1/ill/batches/$batch_id" => json =>
{ name => 'These are not the droids you are looking for' } )->status_is(403);
my $batch_status = $builder->build_object( { class => 'Koha::ILL::Batch::Statuses' } );
# Attempt partial update on a PUT
my $batch_with_missing_field = {
backend => "Mock",
patron_id => $librarian->borrowernumber,
library_id => $library->branchcode,
status_code => $batch_status->code
};
$t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_missing_field )
->status_is(400)->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] );
# Full object update on PUT
my $batch_with_updated_field = {
name => "Master Ploo Koon",
backend => "Mock",
patron_id => $librarian->borrowernumber,
library_id => $library->branchcode,
status_code => $batch_status->code
};
$t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field )
->status_is(200)->json_is( '/name' => 'Master Ploo Koon' );
# Authorized attempt to write invalid data
my $batch_with_invalid_field = {
doh => 1,
name => "Master Mace Windu",
backend => "Mock"
};
$t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_invalid_field )
->status_is(400)->json_is(
"/errors" => [
{
message => "Properties not allowed: doh.",
path => "/body"
}
]
);
my $batch_to_delete = $builder->build_object( { class => 'Koha::ILL::Batches' } );
my $non_existent_id = $batch_to_delete->id;
$batch_to_delete->delete;
$t->put_ok( "//$userid:$password@/api/v1/ill/batches/$non_existent_id" => json => $batch_with_updated_field )
->status_is(404);
# Wrong method (POST)
$batch_with_updated_field->{id} = 2;
$t->post_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field )
->status_is(404);
$schema->storage->txn_rollback;
};
subtest 'delete() tests' => sub {
plan tests => 6;
$schema->storage->txn_begin;
my $librarian = $builder->build_object(
{
class => 'Koha::Patrons',
value => { flags => 2**22 } # 22 => ill
}
);
my $password = 's1th43v3r!';
$librarian->set_password( { password => $password, skip_validation => 1 } );
my $userid = $librarian->userid;
my $patron = $builder->build_object(
{
class => 'Koha::Patrons',
value => { flags => 0 }
}
);
$patron->set_password( { password => $password, skip_validation => 1 } );
my $unauth_userid = $patron->userid;
my $batch_id = $builder->build_object( { class => 'Koha::ILL::Batches' } )->id;
# Unauthorized attempt to delete
$t->delete_ok("//$unauth_userid:$password@/api/v1/ill/batches/$batch_id")->status_is(403);
$t->delete_ok("//$userid:$password@/api/v1/ill/batches/$batch_id")->status_is(204);
$t->delete_ok("//$userid:$password@/api/v1/ill/batches/$batch_id")->status_is(404);
$schema->storage->txn_rollback;
};