From f8657165f183466e5f383ccfb0eb3692eb43be52 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 19 Sep 2024 14:28:47 +0000 Subject: [PATCH] Bug 37526: Add tests Signed-off-by: David Nind Signed-off-by: Katrin Fischer --- .../Koha/BackgroundJob/ErmSushiHarvester.t | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/BackgroundJob/ErmSushiHarvester.t b/t/db_dependent/Koha/BackgroundJob/ErmSushiHarvester.t index 874a5e99d8..214cbc4186 100755 --- a/t/db_dependent/Koha/BackgroundJob/ErmSushiHarvester.t +++ b/t/db_dependent/Koha/BackgroundJob/ErmSushiHarvester.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 5; use Koha::Database; use Koha::BackgroundJobs; @@ -225,6 +225,42 @@ subtest 'multiple_exceptions() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'is_redirect() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $ua = Test::MockModule->new('LWP::UserAgent'); + $ua->mock('simple_request', sub { + return mock_HTTP_redirect(); + }); + $ua->mock('get', sub { + return mock_sushi_response({'error'=>'invalid_api_key'}); + }); + + 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(); @@ -256,3 +292,23 @@ sub mock_sushi_response { } ); } + +sub mock_HTTP_redirect { + my $response = Test::MockObject->new(); + + $response->mock('code', sub { + return 301; + }); + $response->mock('is_error', sub { + return 0; + }); + $response->mock('is_redirect', sub { + return 1; + }); + $response->mock('header', sub { + return 'www.whatever.com'; + }); + $response->mock('decoded_content', sub { + return 'Moved permanently'; + }); +} -- 2.39.5