Skip to content

Commit

Permalink
test(code): add test for API request error
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptprivate authored Aug 28, 2024
1 parent a7e2b38 commit 975f42c
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/code-api-request-error.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;
use Test::MockObject;
use Test::Output;
use Capture::Tiny qw(capture_stdout);

{
package Mojo::UserAgent;
use Test::MockObject;

my $mock_response;

sub new {
my $class = shift;
return Test::MockObject -> new -> mock('get', sub {
my ($self, $url, $headers) = @_;
return Test::MockObject -> new -> mock('result', sub {
return $mock_response;
});
});
}

sub set_mock_response {
my ($class, $response) = @_;
$mock_response = $response;
return $mock_response;
}
}

use lib '../lib';
use SecurityGate::Engine::Code;

subtest 'API request error' => sub {
plan tests => 2;

my $mock_response = Mojo::UserAgent -> set_mock_response(Test::MockObject -> new);
$mock_response -> set_always('code', 401);

my %severity_limits = (
critical => 0,
high => 0,
medium => 0,
low => 0
);

my $result;
my $error_message = qr/Error: \s Unable \s to \s fetch \s code \s scanning \s alerts\./x;
my $status_code = qr/\s HTTP \s status \s code: \s 401/x;
my $full_error_pattern = qr/$error_message$status_code/x;

stdout_like(
sub { $result = SecurityGate::Engine::Code -> new('test_token', 'test_repo', \%severity_limits) },
$full_error_pattern,
'Correct error message for API request failure'
);

is($result, 1, 'Returns 1 when API request fails');
};

done_testing();

0 comments on commit 975f42c

Please sign in to comment.