Skip to content

Commit d523f7a

Browse files
committed
Add a test for write_vul_data
Signed-off-by: ziadhany <ziadhany2016@gmail.com>
1 parent 821808e commit d523f7a

1 file changed

Lines changed: 79 additions & 4 deletions

File tree

vulnerabilities/tests/test_export.py

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import os
22
from io import StringIO
3+
from pathlib import Path
34
from unittest import TestCase
45

56
import pytest
7+
import saneyaml
68
from django.core.management import call_command
79
from django.core.management.base import CommandError
810

911
from vulnerabilities.management.commands.export import create_sub_paths
12+
from vulnerabilities.models import Alias
1013
from vulnerabilities.models import Package
1114
from vulnerabilities.models import PackageRelatedVulnerability
1215
from vulnerabilities.models import Vulnerability
16+
from vulnerabilities.models import VulnerabilityReference
17+
from vulnerabilities.models import VulnerabilityRelatedReference
18+
from vulnerabilities.models import VulnerabilitySeverity
19+
from vulnerabilities.models import Weakness
1320

1421

1522
@pytest.mark.parametrize(
@@ -41,11 +48,38 @@ def package(db):
4148

4249

4350
@pytest.fixture
44-
def vulnerability(db):
45-
return Vulnerability.objects.create(
51+
def vulnerability_reference():
52+
return VulnerabilityReference.objects.create(
53+
reference_id="fake",
54+
url=f"https://..",
55+
)
56+
57+
58+
@pytest.fixture
59+
def vulnerability_severity(vulnerability_reference):
60+
return VulnerabilitySeverity.objects.create(
61+
scoring_system="cvssv3_vector",
62+
value="CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H",
63+
reference_id=vulnerability_reference.id,
64+
)
65+
66+
67+
@pytest.fixture
68+
def vulnerability(db, vulnerability_reference, vulnerability_severity):
69+
vulnerability = Vulnerability.objects.create(
4670
vulnerability_id="VCID-pst6-b358-aaap",
4771
summary="test-vuln",
4872
)
73+
Alias.objects.create(alias=f"CVE-xxx-xxx-xx", vulnerability=vulnerability)
74+
75+
VulnerabilityRelatedReference.objects.create(
76+
reference=vulnerability_reference, vulnerability=vulnerability
77+
)
78+
79+
weakness = Weakness.objects.create(cwe_id=15)
80+
vulnerability.weaknesses.add(weakness)
81+
82+
return vulnerability
4983

5084

5185
@pytest.fixture
@@ -67,9 +101,50 @@ def test_missing_path(self):
67101
assert "Error: the following arguments are required: path" in err
68102

69103
def test_bad_path_fail_error(self):
70-
buf = StringIO()
71104
with pytest.raises(CommandError) as cm:
72-
call_command("export", "/bad path", stdout=buf)
105+
call_command("export", "/bad path", stdout=StringIO())
73106

74107
err = str(cm)
75108
assert "Please enter a valid path" in err
109+
110+
111+
@pytest.mark.django_db
112+
def test_write_vul_data(
113+
tmp_path, package_related_vulnerability, vulnerability_reference, vulnerability_severity
114+
):
115+
expected_vul = {
116+
"vulnerability_id": "VCID-pst6-b358-aaap",
117+
"aliases": ["CVE-xxx-xxx-xx"],
118+
"summary": "test-vuln",
119+
"severities": [
120+
{
121+
"id": vulnerability_severity.id,
122+
"reference_id": vulnerability_reference.id,
123+
"scoring_system": "cvssv3_vector",
124+
"value": "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H",
125+
"scoring_elements": "",
126+
}
127+
],
128+
"references": [
129+
{"id": vulnerability_reference.id, "url": "https://..", "reference_id": "fake"}
130+
],
131+
"weaknesses": ["CWE-15"],
132+
}
133+
expected_pkg = {
134+
"package": "pkg:generic/nginx/test",
135+
"versions": [
136+
{
137+
"purl": "pkg:generic/nginx/test@2",
138+
"affected_by_vulnerabilities": ["VCID-pst6-b358-aaap"],
139+
"fixing_vulnerabilities": [],
140+
},
141+
],
142+
}
143+
144+
call_command("export", tmp_path, stdout=StringIO())
145+
# path: type/namespace/name
146+
vul_filepath = os.path.join(tmp_path, "generic/nginx/test/VCID-pst6-b358-aaap.yml")
147+
pck_filepath = os.path.join(tmp_path, "generic/nginx/test/generic-nginx-test.yml")
148+
149+
assert Path(vul_filepath).read_text() == saneyaml.dump(expected_vul)
150+
assert Path(pck_filepath).read_text() == saneyaml.dump(expected_pkg)

0 commit comments

Comments
 (0)