-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from instriq/develop
Improve tests and fix linter warnings
- Loading branch information
Showing
16 changed files
with
954 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
package SecurityGate::Utils::Helper { | ||
use strict; | ||
use warnings; | ||
use warnings; | ||
|
||
sub new { | ||
return " | ||
\rSecurity Gate v0.1.0 | ||
\rCore Commands | ||
\r==================== | ||
\r\tCommand Description | ||
\r\t------- ----------- | ||
\r\t-t, --token GitHub token | ||
\r\t-r, --repo GitHub repository, organization/repository-name | ||
\r\t-c, --critical Critical severity limit | ||
\r\t-h, --high High severity limit | ||
\r\t-m, --medium Medium severity limit | ||
\r\t-l, --low Low severity limit | ||
\r\t--dependency-alerts Check dependency alerts | ||
\r\t--secret-alerts Check secret scanning alerts | ||
\r\t--code-alerts Check code scanning alerts\n\n"; | ||
} | ||
sub new { | ||
return <<"EOT"; | ||
Security Gate v0.0.3 | ||
Core Commands | ||
============== | ||
Command Description | ||
------- ----------- | ||
-t, --token GitHub token | ||
-r, --repo GitHub repository | ||
-c, --critical Critical severity limit | ||
-h, --high High severity limit | ||
-m, --medium Medium severity limit | ||
-l, --low Low severity limit | ||
--dependency-alerts Check for dependency alerts | ||
--secret-scanning-alerts Check for secret scanning alerts | ||
--code-scanning-alerts Check for code scanning alerts | ||
EOT | ||
} | ||
} | ||
|
||
1; | ||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use Test::Exception; | ||
use Test::MockObject; | ||
use Test::Output; | ||
|
||
{ | ||
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; | ||
} | ||
} | ||
|
||
use lib '../lib'; | ||
use SecurityGate::Engine::Dependencies; | ||
|
||
subtest 'API error handling' => sub { | ||
plan tests => 1; | ||
|
||
my $mock_response = Test::MockObject -> new; | ||
Mojo::UserAgent -> set_mock_response($mock_response); | ||
$mock_response -> set_always('code', 401); | ||
|
||
my %severity_limits = ( | ||
critical => 0, | ||
high => 0, | ||
medium => 0, | ||
low => 0 | ||
); | ||
|
||
is( | ||
SecurityGate::Engine::Dependencies -> new('invalid_token', 'test_repo', \%severity_limits), | ||
1, | ||
'Returns 1 when API request fails' | ||
); | ||
}; | ||
|
||
done_testing(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use Test::Exception; | ||
use Test::MockObject; | ||
use Test::Output; | ||
|
||
{ | ||
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; | ||
} | ||
} | ||
|
||
use lib '../lib'; | ||
use SecurityGate::Engine::Dependencies; | ||
|
||
subtest 'Severity counting' => sub { | ||
plan tests => 1; | ||
|
||
my $mock_response = Test::MockObject -> new; | ||
Mojo::UserAgent -> set_mock_response($mock_response); | ||
$mock_response -> set_always('code', 200); | ||
$mock_response -> set_always('json', [ | ||
{ state => 'open', security_vulnerability => { severity => 'high' } }, | ||
{ state => 'open', security_vulnerability => { severity => 'critical' } }, | ||
{ state => 'open', security_vulnerability => { severity => 'medium' } }, | ||
{ state => 'closed', security_vulnerability => { severity => 'low' } }, | ||
]); | ||
|
||
my %severity_limits = ( | ||
critical => 0, | ||
high => 0, | ||
medium => 0, | ||
low => 0 | ||
); | ||
|
||
stdout_like( | ||
sub { SecurityGate::Engine::Dependencies -> new('test_token', 'test_repo', \%severity_limits) }, | ||
qr/critical:\ 1.*high:\ 1.*medium:\ 1.*low:\ 0/xs, | ||
'Severity counts are correct' | ||
); | ||
}; | ||
|
||
done_testing(); |
Oops, something went wrong.