From 5893991f6b334ff1d15a819868460dc29522f257 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 14 Nov 2023 09:49:47 +0000 Subject: [PATCH] Bug 30070: Add unit tests Signed-off-by: Ray Delahunty Signed-off-by: Kyle M Hall --- t/db_dependent/Koha/Edifact/File.t | 72 +++++++++++++++++ t/db_dependent/api/v1/edifact_files.t | 110 ++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100755 t/db_dependent/Koha/Edifact/File.t create mode 100755 t/db_dependent/api/v1/edifact_files.t diff --git a/t/db_dependent/Koha/Edifact/File.t b/t/db_dependent/Koha/Edifact/File.t new file mode 100755 index 00000000000..0792994d129 --- /dev/null +++ b/t/db_dependent/Koha/Edifact/File.t @@ -0,0 +1,72 @@ +#!/usr/bin/perl + +# Copyright 2023 Koha Development team +# +# 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 => 2; + +use Koha::Edifact::Files; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'vendor() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $file = $builder->build_object( { class => 'Koha::Edifact::Files' } ); + + my $vendor = $file->vendor; + is( + ref($vendor), 'Koha::Acquisition::Bookseller', + 'Koha::Edifact::File->vendor should return a Koha::Acquisition::Bookseller' + ); + + $file->vendor_id(undef)->store; + is( $file->vendor, undef, 'Koha::Edifact::File->vendor should return undef if no vendor linked' ); + + $schema->storage->txn_rollback; +}; + +subtest 'basket() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $file = $builder->build_object( { class => 'Koha::Edifact::Files' } ); + + my $basket = $file->basket; + is( + ref($basket), 'Koha::Acquisition::Basket', + 'Koha::Edifact::File->basket should return a Koha::Acquisition::Basket' + ); + + $file->basketno(undef)->store; + is( $file->basket, undef, 'Koha::Edifact::File->basket should return undef if no basket linked' ); + + $schema->storage->txn_rollback; +}; + +1; diff --git a/t/db_dependent/api/v1/edifact_files.t b/t/db_dependent/api/v1/edifact_files.t new file mode 100755 index 00000000000..5f85920ce84 --- /dev/null +++ b/t/db_dependent/api/v1/edifact_files.t @@ -0,0 +1,110 @@ +#!/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 => 1; +use Test::Mojo; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use JSON qw(encode_json); + +use Koha::Edifact::Files; +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 => 20; + + $schema->storage->txn_begin; + + Koha::Edifact::Files->search->delete; + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**11 } #acquisition + } + ); + my $password = 'thePassword123'; + $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; + + ## Authorized user tests + # No acquisitions\/edifiles, so empty array should be returned + $t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles")->status_is(200)->json_is( [] ); + + my $file = $builder->build_object( { class => 'Koha::Edifact::Files', value => { message_type => 'ORDER' } } ); + + # One file created, should get returned + $t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles")->status_is(200)->json_is( [ $file->to_api ] ); + + my $another_file_ORDER = + $builder->build_object( { class => 'Koha::Edifact::Files', value => { message_type => 'ORDER' } } ); + my $another_file_QUOTE = + $builder->build_object( { class => 'Koha::Edifact::Files', value => { message_type => 'QUOTE' } } ); + + # Two files created, they should both be returned + $t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles")->status_is(200)->json_is( + [ + $file->to_api, + $another_file_ORDER->to_api, + $another_file_QUOTE->to_api + ] + ); + + # Filtering works, two files sharing type + my $api_filter = encode_json( { 'me.type' => ['ORDER'] } ); + $t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles?q=$api_filter")->status_is(200)->json_is( + [ + $file->to_api, + $another_file_ORDER->to_api + ] + ); + + $api_filter = encode_json( { 'me.filename' => $file->filename } ); + $t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles?q=$api_filter")->status_is(200) + ->json_is( [ $file->to_api ] ); + + # Warn on unsupported query parameter + $api_filter = encode_json( { 'me.file_blah' => 'blah' } ); + $t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles?file_blah=blah")->status_is(400) + ->json_is( [ { path => '/query/file_blah', message => 'Malformed query string' } ] ); + + # Unauthorized access + $t->get_ok("//$unauth_userid:$password@/api/v1/acquisitions/edifiles")->status_is(403); + + $schema->storage->txn_rollback; +}; -- 2.41.0