From ff068a390e928cfb10799e251f141c92518749c2 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Fri, 12 May 2023 17:10:00 +0000 Subject: [PATCH] Bug 30719: Tests Co-authored-by: Andrew Isherwood Signed-off-by: Edith Speller --- t/db_dependent/IllbatchStatuses.t | 183 ++++++++++ t/db_dependent/Illbatches.t | 96 +++++ t/db_dependent/api/v1/ill_requests.t | 86 ++++- t/db_dependent/api/v1/illbatches.t | 436 +++++++++++++++++++++++ t/db_dependent/api/v1/illbatchstatuses.t | 352 ++++++++++++++++++ 5 files changed, 1152 insertions(+), 1 deletion(-) create mode 100755 t/db_dependent/IllbatchStatuses.t create mode 100755 t/db_dependent/Illbatches.t create mode 100755 t/db_dependent/api/v1/illbatches.t create mode 100755 t/db_dependent/api/v1/illbatchstatuses.t diff --git a/t/db_dependent/IllbatchStatuses.t b/t/db_dependent/IllbatchStatuses.t new file mode 100755 index 0000000000..487b20d7d4 --- /dev/null +++ b/t/db_dependent/IllbatchStatuses.t @@ -0,0 +1,183 @@ +#s!/usr/bin/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 File::Basename qw/basename/; +use Koha::Database; +use Koha::IllbatchStatus; +use Koha::IllbatchStatuses; +use Koha::Patrons; +use Koha::Libraries; +use t::lib::Mocks; +use t::lib::TestBuilder; +use Test::MockObject; +use Test::MockModule; + +use Test::More tests => 13; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; +use_ok('Koha::IllbatchStatus'); +use_ok('Koha::IllbatchStatuses'); + +$schema->storage->txn_begin; + +Koha::IllbatchStatuses->search->delete; + +# Keep track of whether our CRUD logging side-effects are happening +my $effects = { + batch_status_create => 0, + batch_status_update => 0, + batch_status_delete => 0 +}; + +# Mock a logger so we can check it is called +my $logger = Test::MockModule->new('Koha::Illrequest::Logger'); +$logger->mock('log_something', sub { + my ($self, $to_log ) = @_; + $effects->{$to_log->{actionname}} ++; +}); + +# Create a batch status +my $status = $builder->build({ + source => 'IllbatchStatus', + value => { + name => "Feeling the call to the Dark Side", + code => "OH_NO", + is_system => 1 + } +}); + +my $status_obj = Koha::IllbatchStatuses->find({ code => $status->{code} }); +isa_ok( $status_obj, 'Koha::IllbatchStatus' ); + +# Try to delete the status, it's a system status, so this should fail +$status_obj->delete_and_log; +my $status_obj_del = Koha::IllbatchStatuses->find({ code => $status->{code} }); +isa_ok( $status_obj_del, 'Koha::IllbatchStatus' ); + +## Status create + +# Try creating a duplicate status +my $status2 = Koha::IllbatchStatus->new({ + name => "Obi-wan", + code => $status->{code}, + is_system => 0 +}); +is_deeply( + $status2->create_and_log, + { error => "Duplicate status found" }, + "Creation of statuses with duplicate codes prevented" +); + +# Create a non-duplicate status and ensure that the logger is called +my $status3 = Koha::IllbatchStatus->new({ + name => "Kylo", + code => "DARK_SIDE", + is_system => 0 +}); +$status3->create_and_log; +is( + $effects->{'batch_status_create'}, + 1, + "Creation of status calls log_something" +); + +# Try creating a system status and ensure it's not created +my $cannot_create_system = Koha::IllbatchStatus->new({ + name => "Jar Jar Binks", + code => "GUNGAN", + is_system => 1 +}); +$cannot_create_system->create_and_log; +my $created_but_not_system = Koha::IllbatchStatuses->find({ code => "GUNGAN" }); +is($created_but_not_system->{is_system}, undef, "is_system statuses cannot be created"); + +## Status update + +# Ensure only name can be updated +$status3->update_and_log({ + name => "Rey", + code => "LIGHT_SIDE", + is_system => 1 +}); +# Get our updated status, if we can get it by it's code, we know that hasn't changed +my $not_updated = Koha::IllbatchStatuses->find({ code => "DARK_SIDE" })->unblessed; +is($not_updated->{is_system}, 0, "is_system cannot be changed"); +is($not_updated->{name}, "Rey", "name can be changed"); +# Ensure the logger is called +is( + $effects->{'batch_status_update'}, + 1, + "Update of status calls log_something" +); + +## Status delete +my $cannot_delete = Koha::IllbatchStatus->new({ + name => "Palapatine", + code => "SITH", + is_system => 1 +})->store; +my $can_delete = Koha::IllbatchStatus->new({ + name => "Windu", + code => "JEDI", + is_system => 0 +}); +$cannot_delete->delete_and_log; +my $not_deleted = Koha::IllbatchStatuses->find({ code => "SITH" }); +isa_ok( $not_deleted, 'Koha::IllbatchStatus', "is_system statuses cannot be deleted" ); +$can_delete->create_and_log; +$can_delete->delete_and_log; +# Ensure the logger is called following a successful delete +is( + $effects->{'batch_status_delete'}, + 1, + "Delete of status calls log_something" +); + +# Create a system "UNKNOWN" status +my $status_unknown = Koha::IllbatchStatus->new({ + name => "Unknown", + code => "UNKNOWN", + is_system => 1 +}); +$status_unknown->create_and_log; +# Create a batch and assign it a status +my $patron = $builder->build_object({ class => 'Koha::Patrons' }); +my $library = $builder->build_object({ class => 'Koha::Libraries' }); +my $status5 = Koha::IllbatchStatus->new({ + name => "Plagueis", + code => "DEAD_SITH", + is_system => 0 +}); +$status5->create_and_log; +my $batch = Koha::Illbatch->new({ + name => "My test batch", + borrowernumber => $patron->borrowernumber, + branchcode => $library->branchcode, + backend => "TEST", + statuscode => $status5->code +}); +$batch->create_and_log; +# Delete the batch status and ensure the batch's status has been changed +# to UNKNOWN +$status5->delete_and_log; +my $updated_code = Koha::Illbatches->find({ statuscode => "UNKNOWN" }); +is($updated_code->statuscode, "UNKNOWN", "batches attached to deleted status have status changed to UNKNOWN"); + +$schema->storage->txn_rollback; \ No newline at end of file diff --git a/t/db_dependent/Illbatches.t b/t/db_dependent/Illbatches.t new file mode 100755 index 0000000000..0d28a8f44b --- /dev/null +++ b/t/db_dependent/Illbatches.t @@ -0,0 +1,96 @@ +#s!/usr/bin/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 File::Basename qw/basename/; +use Koha::Database; +use Koha::Illbatch; +use Koha::Illbatches; +use Koha::Illrequests; +use Koha::Patrons; +use t::lib::Mocks; +use t::lib::TestBuilder; +use Test::MockObject; +use Test::MockModule; + +use Test::More tests => 8; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; +use_ok('Koha::Illbatch'); +use_ok('Koha::Illbatches'); + +$schema->storage->txn_begin; + +Koha::Illrequests->search->delete; + +# Create a patron +my $patron = $builder->build({ source => 'Borrower' }); + +# Create a librarian +my $librarian = $builder->build({ + source => 'Borrower', + value => { + firstname => "Grogu" + } +}); + +# Create a branch +my $branch = $builder->build({ + source => 'Branch' +}); + +# Create a batch +my $illbatch = $builder->build({ + source => 'Illbatch', + value => { + name => "My test batch", + backend => "Mock", + borrowernumber => $librarian->{borrowernumber}, + branchcode => $branch->{branchcode} + } +}); +my $batch_obj = Koha::Illbatches->find($illbatch->{id}); +isa_ok( $batch_obj, 'Koha::Illbatch' ); + +# Create an ILL request in the batch +my $illrq = $builder->build({ + source => 'Illrequest', + value => { + borrowernumber => $patron->{borrowernumber}, + batch_id => $illbatch->{id} + } +}); +my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id}); + +# Check requests_count +my $requests_count = $batch_obj->requests_count; +is( $requests_count, 1, 'requests_count returns correctly' ); + +# Check patron +my $batch_patron = $batch_obj->patron; +isa_ok( $batch_patron, 'Koha::Patron' ); +is( $batch_patron->firstname, "Grogu", "patron returns correctly" ); + +# Check branch +my $batch_branch = $batch_obj->branch; +isa_ok( $batch_branch, 'Koha::Library' ); +is( $batch_branch->branchcode, $branch->{branchcode}, "branch returns correctly" ); + +$illrq_obj->delete; +$schema->storage->txn_rollback; diff --git a/t/db_dependent/api/v1/ill_requests.t b/t/db_dependent/api/v1/ill_requests.t index dc3974e5d3..959a58189f 100755 --- a/t/db_dependent/api/v1/ill_requests.t +++ b/t/db_dependent/api/v1/ill_requests.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; use Test::MockModule; use Test::MockObject; @@ -235,3 +235,87 @@ subtest 'list() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'add() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + # create an authorized user + my $patron = $builder->build_object({ + class => 'Koha::Patrons', + value => { flags => 2 ** 22 } # 22 => ill + }); + my $password = 'thePassword123'; + $patron->set_password({ password => $password, skip_validation => 1 }); + my $userid = $patron->userid; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + + # Create an ILL request + my $illrequest = $builder->build_object( + { + class => 'Koha::Illrequests', + value => { + backend => 'Mock', + branchcode => $library->branchcode, + borrowernumber => $patron->borrowernumber, + status => 'STATUS1', + } + } + ); + + # Mock ILLBackend (as object) + my $backend = Test::MockObject->new; + $backend->set_isa('Koha::Illbackends::Mock'); + $backend->set_always('name', 'Mock'); + $backend->set_always('capabilities', sub { + return $illrequest; + } ); + $backend->mock( + 'metadata', + sub { + my ( $self, $rq ) = @_; + return { + ID => $rq->illrequest_id, + Title => $rq->patron->borrowernumber + } + } + ); + $backend->mock( + 'status_graph', sub {}, + ); + + # Mock Koha::Illrequest::load_backend (to load Mocked Backend) + my $illreqmodule = Test::MockModule->new('Koha::Illrequest'); + $illreqmodule->mock( 'load_backend', + sub { my $self = shift; $self->{_my_backend} = $backend; return $self } + ); + + $schema->storage->txn_begin; + + Koha::Illrequests->search->delete; + + my $body = { + backend => 'Mock', + borrowernumber => $patron->borrowernumber, + branchcode => $library->branchcode, + metadata => { + article_author => "Jessop, E. G.", + article_title => "Sleep", + issn => "0957-4832", + issue => "2", + pages => "89-90", + publisher => "OXFORD UNIVERSITY PRESS", + title => "Journal of public health medicine.", + year => "2001" + } + }; + + ## Authorized user test + $t->post_ok( "//$userid:$password@/api/v1/illrequests" => json => $body) + ->status_is(201); + + $schema->storage->txn_rollback; +}; diff --git a/t/db_dependent/api/v1/illbatches.t b/t/db_dependent/api/v1/illbatches.t new file mode 100755 index 0000000000..088f7d296f --- /dev/null +++ b/t/db_dependent/api/v1/illbatches.t @@ -0,0 +1,436 @@ +#!/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::More tests => 5; +use Test::Mojo; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::Illbatch; +use Koha::Illbatches; +use Koha::Illrequests; +use Koha::IllbatchStatuses; +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 => 19; + + $schema->storage->txn_begin; + + Koha::Illbatches->search->delete; + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { + flags => 2 ** 22 # 22 => ill + } + } + ); + + my $branch = $builder->build_object( + { + class => 'Koha::Libraries' + } + ); + + my $password = 'sheev_is_da_boss!'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + ## Authorized user tests + # No batches, so empty array should be returned + $t->get_ok("//$userid:$password@/api/v1/illbatches") + ->status_is(200) + ->json_is( [] ); + + my $batch = $builder->build_object({ + class => 'Koha::Illbatches', + value => { + name => "PapaPalpatine", + backend => "Mock", + borrowernumber => $librarian->borrowernumber, + branchcode => $branch->branchcode + } + }); + + my $illrq = $builder->build({ + source => 'Illrequest', + value => { + borrowernumber => $librarian->borrowernumber, + batch_id => $batch->id + } + }); + + # One batch created, should get returned + $t->get_ok("//$userid:$password@/api/v1/illbatches") + ->status_is(200) + ->json_has( '/0/id', 'Batch ID' ) + ->json_has( '/0/name', 'Batch name' ) + ->json_has( '/0/backend', 'Backend name' ) + ->json_has( '/0/borrowernumber', 'Borrowernumber' ) + ->json_has( '/0/branchcode', 'Branchcode' ) + ->json_has( '/0/patron', 'patron embedded' ) + ->json_has( '/0/branch', 'branch embedded' ) + ->json_has( '/0/requests_count', 'request count' ); + + # Try to create a second batch with the same name, this should fail + my $another_batch = $builder->build_object({ class => 'Koha::Illbatches', value => { + name => $batch->name + } }); + # Create a second batch with a different name + my $batch_with_another_name = $builder->build_object({ class => 'Koha::Illbatches' }); + + # Two batches created, they should both be returned + $t->get_ok("//$userid:$password@/api/v1/illbatches") + ->status_is(200) + ->json_has('/0', 'has first batch') + ->json_has('/1', 'has second batch'); + + 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/illbatches") + ->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 $branch = $builder->build_object( + { + class => 'Koha::Libraries' + } + ); + + my $batch = $builder->build_object({ + class => 'Koha::Illbatches', + value => { + name => "LeiaOrgana", + backend => "Mock", + borrowernumber => $librarian->borrowernumber, + branchcode => $branch->branchcode + } + }); + + + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $unauth_userid = $patron->userid; + + $t->get_ok( "//$userid:$password@/api/v1/illbatches/" . $batch->id ) + ->status_is(200) + ->json_has( '/id', 'Batch ID' ) + ->json_has( '/name', 'Batch name' ) + ->json_has( '/backend', 'Backend name' ) + ->json_has( '/borrowernumber', 'Borrowernumber' ) + ->json_has( '/branchcode', 'Branchcode' ) + ->json_has( '/patron', 'patron embedded' ) + ->json_has( '/branch', 'branch embedded' ) + ->json_has( '/requests_count', 'request count' ); + + $t->get_ok( "//$unauth_userid:$password@/api/v1/illbatches/" . $batch->id ) + ->status_is(403); + + my $batch_to_delete = $builder->build_object({ class => 'Koha::Illbatches' }); + my $non_existent_id = $batch_to_delete->id; + $batch_to_delete->delete; + + $t->get_ok( "//$userid:$password@/api/v1/illbatches/$non_existent_id" ) + ->status_is(404) + ->json_is( '/error' => 'ILL batch not found' ); + + $schema->storage->txn_rollback; +}; + +subtest 'add() tests' => sub { + + plan tests =>19; + + $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 $branch = $builder->build_object( + { + class => 'Koha::Libraries' + } + ); + + my $batch_status = $builder->build_object( + { + class => 'Koha::IllbatchStatuses' + } + ); + + my $batch_metadata = { + name => "Anakin's requests", + backend => "Mock", + cardnumber => $librarian->cardnumber, + branchcode => $branch->branchcode, + statuscode => $batch_status->code + }; + + # Unauthorized attempt to write + $t->post_ok("//$unauth_userid:$password@/api/v1/illbatches" => 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/illbatches" => 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/illbatches" => json => $batch_metadata ) + ->status_is( 201 ) + ->json_is( '/name' => $batch_metadata->{name} ) + ->json_is( '/backend' => $batch_metadata->{backend} ) + ->json_is( '/borrowernumber' => $librarian->borrowernumber ) + ->json_is( '/branchcode' => $batch_metadata->{branchcode} ) + ->json_is( '/statuscode' => $batch_status->code ) + ->json_has( '/patron' ) + ->json_has( '/status' ) + ->json_has( '/requests_count' ) + ->json_has( '/branch' ); + + # Authorized attempt to create with null id + $batch_metadata->{id} = undef; + $t->post_ok( "//$userid:$password@/api/v1/illbatches" => 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 $branch = $builder->build_object( + { + class => 'Koha::Libraries' + } + ); + + my $batch_id = $builder->build_object({ class => 'Koha::Illbatches' } )->id; + + # Unauthorized attempt to update + $t->put_ok( "//$unauth_userid:$password@/api/v1/illbatches/$batch_id" => json => { name => 'These are not the droids you are looking for' } ) + ->status_is(403); + + my $batch_status = $builder->build_object( + { + class => 'Koha::IllbatchStatuses' + } + ); + + # Attempt partial update on a PUT + my $batch_with_missing_field = { + backend => "Mock", + borrowernumber => $librarian->borrowernumber, + branchcode => $branch->branchcode, + statuscode => $batch_status->code + }; + + $t->put_ok( "//$userid:$password@/api/v1/illbatches/$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", + borrowernumber => $librarian->borrowernumber, + branchcode => $branch->branchcode, + statuscode => $batch_status->code + }; + + $t->put_ok( "//$userid:$password@/api/v1/illbatches/$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/illbatches/$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::Cities' }); + my $non_existent_id = $batch_to_delete->id; + $batch_to_delete->delete; + + $t->put_ok( "//$userid:$password@/api/v1/illbatches/$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/illbatches/$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::Illbatches' })->id; + + # Unauthorized attempt to delete + $t->delete_ok( "//$unauth_userid:$password@/api/v1/illbatches/$batch_id" ) + ->status_is(403); + + $t->delete_ok("//$userid:$password@/api/v1/illbatches/$batch_id") + ->status_is(204); + + $t->delete_ok("//$userid:$password@/api/v1/illbatches/$batch_id") + ->status_is(404); + + $schema->storage->txn_rollback; +}; diff --git a/t/db_dependent/api/v1/illbatchstatuses.t b/t/db_dependent/api/v1/illbatchstatuses.t new file mode 100755 index 0000000000..335b4365a3 --- /dev/null +++ b/t/db_dependent/api/v1/illbatchstatuses.t @@ -0,0 +1,352 @@ +#!/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::More tests => 5; +use Test::Mojo; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::IllbatchStatus; +use Koha::IllbatchStatuses; +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 => 9; + + $schema->storage->txn_begin; + + Koha::IllbatchStatuses->search->delete; + + # Create an admin user + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { + flags => 2 ** 22 # 22 => ill + } + } + ); + my $password = 'yoda4ever!'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + ## Authorized user tests + # No statuses, so empty array should be returned + $t->get_ok("//$userid:$password@/api/v1/illbatchstatuses") + ->status_is(200) + ->json_is( [] ); + + my $status = $builder->build_object({ + class => 'Koha::IllbatchStatuses', + value => { + name => "Han Solo", + code => "SOLO", + is_system => 0 + } + }); + + # One batch created, should get returned + $t->get_ok("//$userid:$password@/api/v1/illbatchstatuses") + ->status_is(200) + ->json_has( '/0/id', 'ID' ) + ->json_has( '/0/name', 'Name' ) + ->json_has( '/0/code', 'Code' ) + ->json_has( '/0/is_system', 'is_system' ); + + $schema->storage->txn_rollback; +}; + +subtest 'get() tests' => sub { + + plan tests => 11; + + $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 $status = $builder->build_object({ + class => 'Koha::IllbatchStatuses', + value => { + name => "Han Solo", + code => "SOLO", + is_system => 0 + } + }); + + # Unauthorised user + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $unauth_userid = $patron->userid; + + $t->get_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $status->code ) + ->status_is(200) + ->json_has( '/id', 'ID' ) + ->json_has( '/name', 'Name' ) + ->json_has( '/code', 'Code' ) + ->json_has( '/is_system', 'is_system' ); + + $t->get_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $status->id ) + ->status_is(403); + + my $status_to_delete = $builder->build_object({ class => 'Koha::IllbatchStatuses' }); + my $non_existent_code = $status_to_delete->code; + $status_to_delete->delete; + + $t->get_ok( "//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code" ) + ->status_is(404) + ->json_is( '/error' => 'ILL batch status not found' ); + + $schema->storage->txn_rollback; +}; + +subtest 'add() tests' => sub { + + plan tests =>14; + + $schema->storage->txn_begin; + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**22 } # 22 => ill + } + ); + my $password = '3poRox'; + $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 $status_metadata = { + name => "In a bacta tank", + code => "BACTA", + is_system => 0 + }; + + # Unauthorized attempt to write + $t->post_ok("//$unauth_userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata) + ->status_is(403); + + # Authorized attempt to write invalid data + my $status_with_invalid_field = { + %{$status_metadata}, + doh => 1 + }; + + $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_with_invalid_field ) + ->status_is(400) + ->json_is( + "/errors" => [ + { + message => "Properties not allowed: doh.", + path => "/body" + } + ] + ); + + # Authorized attempt to write + my $status_id = + $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata ) + ->status_is( 201 ) + ->json_has( '/id', 'ID' ) + ->json_has( '/name', 'Name' ) + ->json_has( '/code', 'Code' ) + ->json_has( '/is_system', 'is_system' ); + + # Authorized attempt to create with null id + $status_metadata->{id} = undef; + $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata ) + ->status_is(400) + ->json_has('/errors'); + + $schema->storage->txn_rollback; +}; + +subtest 'update() tests' => sub { + + plan tests => 13; + + $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 $status_code = $builder->build_object({ class => 'Koha::IllbatchStatuses' } )->code; + + # Unauthorized attempt to update + $t->put_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/$status_code" => json => { name => 'These are not the droids you are looking for' } ) + ->status_is(403); + + # Attempt partial update on a PUT + my $status_with_missing_field = { + code => $status_code, + is_system => 0 + }; + + $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_missing_field ) + ->status_is(400) + ->json_is( "/errors" => + [ { message => "Missing property.", path => "/body/name" } ] + ); + + # Full object update on PUT + my $status_with_updated_field = { + name => "Master Ploo Koon", + code => $status_code, + is_system => 0 + }; + + $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_updated_field ) + ->status_is(200) + ->json_is( '/name' => 'Master Ploo Koon' ); + + # Authorized attempt to write invalid data + my $status_with_invalid_field = { + doh => 1, + name => "Master Mace Windu", + code => $status_code + }; + + $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_invalid_field ) + ->status_is(400) + ->json_is( + "/errors" => [ + { + message => "Properties not allowed: doh.", + path => "/body" + } + ] + ); + + my $status_to_delete = $builder->build_object({ class => 'Koha::IllbatchStatuses' }); + my $non_existent_code = $status_to_delete->code; + $status_to_delete->delete; + + $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code" => json => $status_with_updated_field ) + ->status_is(404); + + $schema->storage->txn_rollback; +}; + +subtest 'delete() tests' => sub { + + plan tests => 9; + + $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 $non_system_status = $builder->build_object({ + class => 'Koha::IllbatchStatuses', + value => { + is_system => 0 + } + }); + + my $system_status = $builder->build_object({ + class => 'Koha::IllbatchStatuses', + value => { + is_system => 1 + } + }); + + # Unauthorized attempt to delete + $t->delete_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code ) + ->status_is(403); + + $t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code ) + ->status_is(204); + + $t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code ) + ->status_is(404); + + $t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $system_status->code ) + ->status_is(400) + ->json_is( + "/errors" => [ + { + message => "ILL batch status cannot be deleted" + } + ] + ); + + $schema->storage->txn_rollback; +}; -- 2.30.2