Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/fix-test-validate-func #631

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 11 additions & 37 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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)
Expand All @@ -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'
Expand Down Expand Up @@ -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.
Expand All @@ -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}"
)
Expand Down
Loading