Bug 37963: Case 1 - Add tests for invalid api key use case

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
(cherry picked from commit 05381ba6b1)
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Pedro Amorim 2024-09-19 13:45:59 +00:00 committed by Lucas Gass
parent 7d17200a38
commit 407661f5d3
1 changed files with 58 additions and 1 deletions

View File

@ -17,22 +17,28 @@
use Modern::Perl;
use Test::More tests => 1;
use Test::More tests => 2;
use Koha::Database;
use Koha::BackgroundJobs;
use Koha::BackgroundJob::ErmSushiHarvester;
use File::Basename qw( dirname );
use JSON qw( decode_json );
use File::Slurp;
use t::lib::Mocks;
use t::lib::TestBuilder;
use Test::MockModule;
use Test::MockObject;
my $schema = Koha::Database->new->schema;
my $builder = t::lib::TestBuilder->new;
my $sushi_response_errors = {
'invalid_api_key' => '{"Code": 2020, "Severity": "Error", "Message": "API Key Invalid"}',
};
subtest 'enqueue() tests' => sub {
plan tests => 3;
@ -60,3 +66,54 @@ subtest 'enqueue() tests' => sub {
$schema->storage->txn_rollback;
};
subtest 'invalid_api_key() tests' => sub {
plan tests => 3;
$schema->storage->txn_begin;
my $ua = Test::MockModule->new('LWP::UserAgent');
$ua->mock('simple_request', sub {
return mock_sushi_response({'error'=>'invalid_api_key', 'code'=>401});
});
my $usage_data_provider = $builder->build_object(
{ class => 'Koha::ERM::EUsage::UsageDataProviders', value => { name => 'TestProvider' } } );
my $job_args = {
ud_provider_id => $usage_data_provider->erm_usage_data_provider_id,
report_type => 'TR_J1',
begin_date => '2023-08-01',
end_date => '2023-09-30',
ud_provider_name => $usage_data_provider->name,
};
my $job_id = Koha::BackgroundJob::ErmSushiHarvester->new->enqueue($job_args);
my $job = Koha::BackgroundJobs->find($job_id)->_derived_class;
$job->process( $job_args );
is( $job->{messages}[0]->{message}, decode_json($sushi_response_errors->{invalid_api_key})->{Severity} . ' - ' . decode_json($sushi_response_errors->{invalid_api_key})->{Message},'SUSHI error invalid_date_arguments is stored on job messages correctly' );
is( $job->{messages}[0]->{type},'error','SUSHI error invalid_date_arguments is stored on job messages correctly' );
is( $job->{messages}[0]->{code},decode_json($sushi_response_errors->{invalid_api_key})->{Code},'SUSHI error invalid_date_arguments is stored on job messages correctly' );
$schema->storage->txn_rollback;
};
sub mock_sushi_response {
my ($args) = @_;
my $response = Test::MockObject->new();
$response->mock('code', sub {
return $args->{code} || 200;
});
$response->mock('is_error', sub {
return 0;
});
$response->mock('is_redirect', sub {
return 0;
});
$response->mock('decoded_content', sub {
return $sushi_response_errors->{$args->{error}};
});
}