View | Details | Raw Unified | Return to bug 37963
Collapse All | Expand All

(-)a/Koha/ERM/EUsage/UsageDataProvider.pm (-4 / +4 lines)
Lines 180-186 sub harvest_sushi { Link Here
180
    $ua->agent( 'Koha/' . Koha::version() );
180
    $ua->agent( 'Koha/' . Koha::version() );
181
    my $response = $ua->simple_request($request);
181
    my $response = $ua->simple_request($request);
182
182
183
    my $result   = decode_json( $response->decoded_content );
183
    my $result = decode_json( $response->decoded_content );
184
184
185
    if ( $response->code >= 400 ) {
185
    if ( $response->code >= 400 ) {
186
186
Lines 459-468 sub _sushi_errors { Link Here
459
    my ( $self, $decoded_response ) = @_;
459
    my ( $self, $decoded_response ) = @_;
460
460
461
    my $severity = $decoded_response->{Severity} // $decoded_response->{severity};
461
    my $severity = $decoded_response->{Severity} // $decoded_response->{severity};
462
    my $message = $decoded_response->{Message} // $decoded_response->{message};
462
    my $message  = $decoded_response->{Message}  // $decoded_response->{message};
463
    my $code = $decoded_response->{Code} // $decoded_response->{code};
463
    my $code     = $decoded_response->{Code}     // $decoded_response->{code};
464
464
465
    if ( $severity ) {
465
    if ($severity) {
466
        $self->{job_callbacks}->{add_message_callback}->(
466
        $self->{job_callbacks}->{add_message_callback}->(
467
            {
467
            {
468
                type    => 'error',
468
                type    => 'error',
(-)a/t/db_dependent/Koha/BackgroundJob/ErmSushiHarvester.t (-45 / +84 lines)
Lines 24-30 use Koha::BackgroundJobs; Link Here
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 JSON           qw( decode_json );
28
use File::Slurp;
28
use File::Slurp;
29
29
30
use t::lib::Mocks;
30
use t::lib::Mocks;
Lines 75-102 subtest 'invalid_date_arguments() tests' => sub { Link Here
75
    $schema->storage->txn_begin;
75
    $schema->storage->txn_begin;
76
76
77
    my $ua = Test::MockModule->new('LWP::UserAgent');
77
    my $ua = Test::MockModule->new('LWP::UserAgent');
78
    $ua->mock('simple_request', sub {
78
    $ua->mock(
79
            return mock_sushi_response({'error'=>'invalid_date_arguments'});
79
        'simple_request',
80
    });
80
        sub {
81
            return mock_sushi_response( { 'error' => 'invalid_date_arguments' } );
82
        }
83
    );
81
84
82
    my $usage_data_provider = $builder->build_object(
85
    my $usage_data_provider = $builder->build_object(
83
        { class => 'Koha::ERM::EUsage::UsageDataProviders', value => { name => 'TestProvider' } } );
86
        { class => 'Koha::ERM::EUsage::UsageDataProviders', value => { name => 'TestProvider' } } );
84
87
85
    my $job_args = {
88
    my $job_args = {
86
            ud_provider_id   => $usage_data_provider->erm_usage_data_provider_id,
89
        ud_provider_id   => $usage_data_provider->erm_usage_data_provider_id,
87
            report_type      => 'TR_J1',
90
        report_type      => 'TR_J1',
88
            begin_date       => '2023-08-01',
91
        begin_date       => '2023-08-01',
89
            end_date         => '2023-09-30',
92
        end_date         => '2023-09-30',
90
            ud_provider_name => $usage_data_provider->name,
93
        ud_provider_name => $usage_data_provider->name,
91
        };
94
    };
92
95
93
    my $job_id = Koha::BackgroundJob::ErmSushiHarvester->new->enqueue($job_args);
96
    my $job_id = Koha::BackgroundJob::ErmSushiHarvester->new->enqueue($job_args);
94
    my $job = Koha::BackgroundJobs->find($job_id)->_derived_class;
97
    my $job    = Koha::BackgroundJobs->find($job_id)->_derived_class;
95
    $job->process( $job_args );
98
    $job->process($job_args);
96
99
97
    is( $job->{messages}[0]->{message}, decode_json($sushi_response_errors->{invalid_date_arguments})->{severity} . ' - ' . decode_json($sushi_response_errors->{invalid_date_arguments})->{message},'SUSHI error invalid_date_arguments is stored on job messages correctly' );
100
    is(
98
    is( $job->{messages}[0]->{type},'error','SUSHI error invalid_date_arguments is stored on job messages correctly' );
101
        $job->{messages}[0]->{message},
99
    is( $job->{messages}[0]->{code},decode_json($sushi_response_errors->{invalid_date_arguments})->{code},'SUSHI error invalid_date_arguments is stored on job messages correctly' );
102
        decode_json( $sushi_response_errors->{invalid_date_arguments} )->{severity} . ' - '
103
            . decode_json( $sushi_response_errors->{invalid_date_arguments} )->{message},
104
        'SUSHI error invalid_date_arguments is stored on job messages correctly'
105
    );
106
    is(
107
        $job->{messages}[0]->{type}, 'error',
108
        'SUSHI error invalid_date_arguments is stored on job messages correctly'
109
    );
110
    is(
111
        $job->{messages}[0]->{code}, decode_json( $sushi_response_errors->{invalid_date_arguments} )->{code},
112
        'SUSHI error invalid_date_arguments is stored on job messages correctly'
113
    );
100
114
101
    $schema->storage->txn_rollback;
115
    $schema->storage->txn_rollback;
102
};
116
};
Lines 108-135 subtest 'invalid_api_key() tests' => sub { Link Here
108
    $schema->storage->txn_begin;
122
    $schema->storage->txn_begin;
109
123
110
    my $ua = Test::MockModule->new('LWP::UserAgent');
124
    my $ua = Test::MockModule->new('LWP::UserAgent');
111
    $ua->mock('simple_request', sub {
125
    $ua->mock(
112
            return mock_sushi_response({'error'=>'invalid_api_key', 'code'=>401});
126
        'simple_request',
113
    });
127
        sub {
128
            return mock_sushi_response( { 'error' => 'invalid_api_key', 'code' => 401 } );
129
        }
130
    );
114
131
115
    my $usage_data_provider = $builder->build_object(
132
    my $usage_data_provider = $builder->build_object(
116
        { class => 'Koha::ERM::EUsage::UsageDataProviders', value => { name => 'TestProvider' } } );
133
        { class => 'Koha::ERM::EUsage::UsageDataProviders', value => { name => 'TestProvider' } } );
117
134
118
    my $job_args = {
135
    my $job_args = {
119
            ud_provider_id   => $usage_data_provider->erm_usage_data_provider_id,
136
        ud_provider_id   => $usage_data_provider->erm_usage_data_provider_id,
120
            report_type      => 'TR_J1',
137
        report_type      => 'TR_J1',
121
            begin_date       => '2023-08-01',
138
        begin_date       => '2023-08-01',
122
            end_date         => '2023-09-30',
139
        end_date         => '2023-09-30',
123
            ud_provider_name => $usage_data_provider->name,
140
        ud_provider_name => $usage_data_provider->name,
124
        };
141
    };
125
142
126
    my $job_id = Koha::BackgroundJob::ErmSushiHarvester->new->enqueue($job_args);
143
    my $job_id = Koha::BackgroundJob::ErmSushiHarvester->new->enqueue($job_args);
127
    my $job = Koha::BackgroundJobs->find($job_id)->_derived_class;
144
    my $job    = Koha::BackgroundJobs->find($job_id)->_derived_class;
128
    $job->process( $job_args );
145
    $job->process($job_args);
129
146
130
    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' );
147
    is(
131
    is( $job->{messages}[0]->{type},'error','SUSHI error invalid_date_arguments is stored on job messages correctly' );
148
        $job->{messages}[0]->{message},
132
    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' );
149
        decode_json( $sushi_response_errors->{invalid_api_key} )->{Severity} . ' - '
150
            . decode_json( $sushi_response_errors->{invalid_api_key} )->{Message},
151
        'SUSHI error invalid_date_arguments is stored on job messages correctly'
152
    );
153
    is(
154
        $job->{messages}[0]->{type}, 'error',
155
        'SUSHI error invalid_date_arguments is stored on job messages correctly'
156
    );
157
    is(
158
        $job->{messages}[0]->{code}, decode_json( $sushi_response_errors->{invalid_api_key} )->{Code},
159
        'SUSHI error invalid_date_arguments is stored on job messages correctly'
160
    );
133
161
134
    $schema->storage->txn_rollback;
162
    $schema->storage->txn_rollback;
135
};
163
};
Lines 138-153 sub mock_sushi_response { Link Here
138
    my ($args) = @_;
166
    my ($args) = @_;
139
    my $response = Test::MockObject->new();
167
    my $response = Test::MockObject->new();
140
168
141
    $response->mock('code', sub {
169
    $response->mock(
142
        return $args->{code} || 200;
170
        'code',
143
    });
171
        sub {
144
    $response->mock('is_error', sub {
172
            return $args->{code} || 200;
145
        return 0;
173
        }
146
    });
174
    );
147
    $response->mock('is_redirect', sub {
175
    $response->mock(
148
        return 0;
176
        'is_error',
149
    });
177
        sub {
150
    $response->mock('decoded_content', sub {
178
            return 0;
151
        return $sushi_response_errors->{$args->{error}};
179
        }
152
    });
180
    );
153
}
181
    $response->mock(
182
        'is_redirect',
183
        sub {
184
            return 0;
185
        }
186
    );
187
    $response->mock(
188
        'decoded_content',
189
        sub {
190
            return $sushi_response_errors->{ $args->{error} };
191
        }
192
    );
193
}
154
- 

Return to bug 37963