import json
import pytest
from PrismaCloudComputeParseVulnerabilityAlert import parse_vulnerability
def test_parse_vulnerability():
valid_raw_json = json.dumps(
{
"_id": "testID",
"time": "1970-01-01T00:00:00.000Z",
"kind": "vulnerability",
"imageName": "testImage",
"distroName": "testDestribution",
"labels": {},
"vulnerabilities": [
{
"cve": "testCVE",
"severity": "testSeverity",
"link": "testLink",
"status": "testStatus",
"packages": "testPackage",
"packageVersion": "1.0.0",
"sourcePackage": "testSourcePackage",
},
],
}
)
no_kind_raw_json = json.dumps({"_id": "testID", "time": "1970-01-01T00:00:00.000Z"})
wrong_kind_raw_json = json.dumps({"_id": "testID", "time": "1970-01-01T00:00:00.000Z", "kind": "wrongKind"})
tests = [
{
"input": valid_raw_json,
"expectedException": False,
"expectedResult": {
"readable": r"""### Vulnerability Information
|distroName|imageName|labels|time|
|---|---|---|---|
| testDestribution | testImage | | 1970-01-01T00:00:00.000Z |
### Vulnerabilities
|cve|link|packageVersion|packages|severity|sourcePackage|status|
|---|---|---|---|---|---|---|
| testCVE | testLink | 1.0.0 | testPackage | testSeverity | testSourcePackage | testStatus |
""",
"output": {"PrismaCloudCompute.VulnerabilityAlert": json.loads(valid_raw_json)},
"raw": valid_raw_json,
},
},
{
"input": no_kind_raw_json,
"expectedException": True,
},
{
"input": wrong_kind_raw_json,
"expectedException": True,
},
]
for test in tests:
if test["expectedException"]:
with pytest.raises(Exception) as ex:
parse_vulnerability(test["input"])
assert str(ex.value) == f"Input should be a raw JSON vulnerability alert, received: {test['input']}"
else:
assert parse_vulnerability(test["input"]) == (
test["expectedResult"]["readable"],
test["expectedResult"]["output"],
test["expectedResult"]["raw"],
)