Lines 17-38
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 1; |
20 |
use Test::More tests => 2; |
21 |
|
21 |
|
22 |
use Koha::Database; |
22 |
use Koha::Database; |
23 |
use Koha::BackgroundJobs; |
23 |
use Koha::BackgroundJobs; |
24 |
use Koha::BackgroundJob::ErmSushiHarvester; |
24 |
use Koha::BackgroundJob::ErmSushiHarvester; |
25 |
|
25 |
|
26 |
use File::Basename qw( dirname ); |
26 |
use File::Basename qw( dirname ); |
|
|
27 |
use JSON qw( decode_json ); |
27 |
use File::Slurp; |
28 |
use File::Slurp; |
28 |
|
29 |
|
29 |
use t::lib::Mocks; |
30 |
use t::lib::Mocks; |
30 |
use t::lib::TestBuilder; |
31 |
use t::lib::TestBuilder; |
31 |
use Test::MockModule; |
32 |
use Test::MockModule; |
|
|
33 |
use Test::MockObject; |
32 |
|
34 |
|
33 |
my $schema = Koha::Database->new->schema; |
35 |
my $schema = Koha::Database->new->schema; |
34 |
my $builder = t::lib::TestBuilder->new; |
36 |
my $builder = t::lib::TestBuilder->new; |
35 |
|
37 |
|
|
|
38 |
my $sushi_response_errors = { |
39 |
'invalid_api_key' => '{"Code": 2020, "Severity": "Error", "Message": "API Key Invalid"}', |
40 |
}; |
41 |
|
36 |
subtest 'enqueue() tests' => sub { |
42 |
subtest 'enqueue() tests' => sub { |
37 |
|
43 |
|
38 |
plan tests => 3; |
44 |
plan tests => 3; |
Lines 60-62
subtest 'enqueue() tests' => sub {
Link Here
|
60 |
|
66 |
|
61 |
$schema->storage->txn_rollback; |
67 |
$schema->storage->txn_rollback; |
62 |
}; |
68 |
}; |
63 |
- |
69 |
|
|
|
70 |
subtest 'invalid_api_key() tests' => sub { |
71 |
|
72 |
plan tests => 3; |
73 |
|
74 |
$schema->storage->txn_begin; |
75 |
|
76 |
my $ua = Test::MockModule->new('LWP::UserAgent'); |
77 |
$ua->mock('simple_request', sub { |
78 |
return mock_sushi_response({'error'=>'invalid_api_key', 'code'=>401}); |
79 |
}); |
80 |
|
81 |
my $usage_data_provider = $builder->build_object( |
82 |
{ class => 'Koha::ERM::EUsage::UsageDataProviders', value => { name => 'TestProvider' } } ); |
83 |
|
84 |
my $job_args = { |
85 |
ud_provider_id => $usage_data_provider->erm_usage_data_provider_id, |
86 |
report_type => 'TR_J1', |
87 |
begin_date => '2023-08-01', |
88 |
end_date => '2023-09-30', |
89 |
ud_provider_name => $usage_data_provider->name, |
90 |
}; |
91 |
|
92 |
my $job_id = Koha::BackgroundJob::ErmSushiHarvester->new->enqueue($job_args); |
93 |
my $job = Koha::BackgroundJobs->find($job_id)->_derived_class; |
94 |
$job->process( $job_args ); |
95 |
|
96 |
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' ); |
97 |
is( $job->{messages}[0]->{type},'error','SUSHI error invalid_date_arguments is stored on job messages correctly' ); |
98 |
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' ); |
99 |
|
100 |
$schema->storage->txn_rollback; |
101 |
}; |
102 |
|
103 |
sub mock_sushi_response { |
104 |
my ($args) = @_; |
105 |
my $response = Test::MockObject->new(); |
106 |
|
107 |
$response->mock('code', sub { |
108 |
return $args->{code} || 200; |
109 |
}); |
110 |
$response->mock('is_error', sub { |
111 |
return 0; |
112 |
}); |
113 |
$response->mock('is_redirect', sub { |
114 |
return 0; |
115 |
}); |
116 |
$response->mock('decoded_content', sub { |
117 |
return $sushi_response_errors->{$args->{error}}; |
118 |
}); |
119 |
} |