Skip to content

Commit f7be093

Browse files
committed
Added test for checking codefix_id
Signed-off-by: Shrish0098 <shrish409@gmail.com>
1 parent e8e4c5e commit f7be093

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import re
2+
import json
3+
import uuid
4+
5+
def is_valid_uuid(value):
6+
try:
7+
uuid.UUID(value, version=4)
8+
return True
9+
except ValueError:
10+
return False
11+
12+
def test_api_invalid_codefix_ids(test_client, mocker):
13+
# Mock an invalid response
14+
mock_data = {
15+
"vulnerabilities": [
16+
{"codefix_id": "invalid-uuid-format", "name": "Fake Vuln"},
17+
]
18+
}
19+
# Use a simple mock response instead of the real API call
20+
mocker.patch.object(test_client, "get", return_value=mocker.Mock(json=lambda: mock_data, status_code=200))
21+
22+
response = test_client.get("/api/vulnerabilities/")
23+
data = response.json()
24+
25+
# Verify that the test fails if an invalid UUID is present
26+
for vulnerability in data.get("vulnerabilities", []):
27+
if "codefix_id" in vulnerability:
28+
codefix_id = vulnerability["codefix_id"]
29+
assert not is_valid_uuid(codefix_id), (
30+
f"'{codefix_id}' was unexpectedly treated as a valid UUID."
31+
)
32+
33+
34+
for vulnerability in data.get("vulnerabilities", []):
35+
if "codefix_id" in vulnerability:
36+
codefix_id = vulnerability["codefix_id"]
37+
# Check that it is a valid UUID
38+
assert is_valid_uuid(codefix_id), (
39+
f"codefix_id '{codefix_id}' is not a valid UUID."
40+
)
41+
42+
43+
response_str = json.dumps(data)
44+
45+
leaked_pattern = re.compile(r"id\d+")
46+
assert not leaked_pattern.search(response_str), "Old codefix ID format still present in the response."

0 commit comments

Comments
 (0)