From f4e40f2c9c5ce51d893de1c283b8244f5d7f39da Mon Sep 17 00:00:00 2001 From: priv <140729444+scriptprivate@users.noreply.github.com> Date: Mon, 12 Aug 2024 03:45:59 -0300 Subject: [PATCH] test: enhance test coverage and add new test cases - add test for invalid token or repository - add test for empty response from GitHub API - add test for multiple severity thresholds --- tests/security-gate.t | 69 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/security-gate.t b/tests/security-gate.t index 2c97624..c8d2e3c 100644 --- a/tests/security-gate.t +++ b/tests/security-gate.t @@ -97,4 +97,73 @@ subtest 'Output formatting' => sub { ); }; +subtest 'Invalid token or repository' => sub { + my $mock_response = Test::MockObject -> new(); + $mock_response -> set_always('code', 401); + + my $mock_tx = Test::MockObject -> new(); + $mock_tx -> set_always('result', $mock_response); + + $mock_ua -> set_always('get', $mock_tx); + + local @ARGV = ('-t', 'invalid_token', '-r', 'invalid_repo'); + is( + scalar(main()), + 1, + 'Script exits with non-zero code when token or repository is invalid' + ); +}; + +subtest 'Empty response from GitHub API' => sub { + my $mock_response = Test::MockObject -> new(); + $mock_response -> set_always('code', 200); + $mock_response -> set_always('json', []); + + my $mock_tx = Test::MockObject -> new(); + $mock_tx -> set_always('result', $mock_response); + + $mock_ua -> set_always('get', $mock_tx); + + local @ARGV = ('-t', 'test_token', '-r', 'test_repo'); + is( + scalar(main()), + 0, + 'Script exits with zero code when no alerts are found' + ); +}; + +subtest 'Multiple severity thresholds' => sub { + my $mock_response = Test::MockObject -> new(); + $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' } }, + ]); + + my $mock_tx = Test::MockObject -> new(); + $mock_tx -> set_always('result', $mock_response); + + $mock_ua -> set_always('get', $mock_tx); + + local @ARGV = ('-t', 'test_token', '-r', 'test_repo', '-c', '0', '-h', '0', '-m', '0', '-l', '0'); + + my $stdout; + { + local *STDOUT; + open STDOUT, '>', \$stdout; + + my $result = main(); + + diag("STDOUT: $stdout"); + diag("Result: $result"); + + is( + $result, + 1, + 'Script exits with non-zero code when multiple thresholds are exceeded' + ); + } +}; + done_testing();