From 622c909f271afc79f6aa029a8d0d84371316c3ec Mon Sep 17 00:00:00 2001 From: Dylan Pulver <35541198+dylanpulver@users.noreply.github.com> Date: Thu, 7 Nov 2024 13:54:34 -0500 Subject: [PATCH] fix/fix-test-validate-func (#631) --- tests/test_cli.py | 48 +++++++++++------------------------------------ 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index cf4aa02..1d8425f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -180,7 +180,7 @@ def test_validate_with_wrong_path(self): def test_validate_with_basic_policy_file(self): dirname = os.path.dirname(__file__) - + # Test with policy version 2.0 path = os.path.join(dirname, "test_policy_file", "default_policy_file.yml") result = self.runner.invoke(cli.cli, ['validate', 'policy_file', '2.0', '--path', path]) @@ -213,7 +213,7 @@ def test_validate_with_basic_policy_file(self): result = self.runner.invoke(cli.cli, ['validate', 'policy_file', '3.0', '--path', path]) cleaned_stdout = click.unstyle(result.stdout) msg = 'The Safety policy (3.0) file (Used for scan and system-scan commands) was successfully parsed with the following values:\n' - + parsed = { "version": "3.0", "scan": { @@ -261,42 +261,15 @@ def test_validate_with_basic_policy_file(self): ] } }, - "installation": { - "allow": { - "packages": [], - "vulnerabilities": {} - }, - "audit_logging": { - "enabled": True - }, - "default_action": "deny", - "deny": { - "packages": { - "block_on_any_of": { - "age_below": None, - "packages": [] - }, - "warning_on_any_of": { - "age_below": None, - "packages": [] - } - }, - "vulnerabilities": { - "block_on_any_of": { - "cvss_severity": [] - }, - "warning_on_any_of": { - "cvss_severity": [] - } - } - } - } } msg_stdout, parsed_policy = cleaned_stdout.split('\n', 1) msg_stdout += '\n' parsed_policy = json.loads(parsed_policy.replace('\n', '')) + # Remove the 'installation' key if it exists + parsed_policy.pop("installation", None) + # Sorting and comparing specific fields fail_scan = parsed_policy.get("fail_scan", None) self.assertIsNotNone(fail_scan) @@ -306,15 +279,16 @@ def test_validate_with_basic_policy_file(self): # Assert that the message is the same self.assertEqual(msg, msg_stdout) - + # Assert that the parsed policy matches the expected policy self.assertEqual(parsed, parsed_policy) - + # Check the exit code self.assertEqual(result.exit_code, 0) + def test_validate_with_policy_file_using_invalid_keyword(self): dirname = os.path.dirname(__file__) filename = 'default_policy_file_using_invalid_keyword.yml' @@ -559,11 +533,11 @@ def test_license_with_file(self, fetch_database_url): def test_debug_flag(self, mock_get_auth_info, mock_is_valid, mock_get_auth_type, mock_fetch_database): """ Test the behavior of the CLI when invoked with the '--debug' flag. - + This test invokes the CLI with the 'scan' command and the '--debug' flag enabled, verifies that the command exits successfully, and checks that the expected output snippet is present in the CLI output. - + Args: mock_get_auth_info: Mock for retrieving authentication info. mock_is_valid: Mock for checking validity of inputs or authentication. @@ -574,7 +548,7 @@ def test_debug_flag(self, mock_get_auth_info, mock_is_valid, mock_get_auth_type, assert result.exit_code == 0, ( f"CLI exited with code {result.exit_code} and output: {result.output} and error: {result.stderr}" ) - expected_output_snippet = f"{get_safety_version()} scanning" + expected_output_snippet = f"{get_safety_version()} scanning" assert expected_output_snippet in result.output, ( f"Expected output to contain: {expected_output_snippet}, but got: {result.output}" )