Skip to content

Commit bdfdffa

Browse files
committed
Add tests for Xen importer
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent e06acee commit bdfdffa

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
from unittest.mock import patch
11+
12+
import pytest
13+
from dateutil.parser import parse as date_parse
14+
15+
from vulnerabilities.importer import AdvisoryData
16+
from vulnerabilities.importer import ReferenceV2
17+
from vulnerabilities.pipelines.v2_importers.xen_importer import XenImporterPipeline
18+
19+
SAMPLE_XSA_JSON = [
20+
{
21+
"xsas": [
22+
{
23+
"xsa": 123,
24+
"title": "Sample Xen Advisory",
25+
"public_time": "2022-09-15T00:00:00Z",
26+
"cve": ["CVE-2022-12345"],
27+
},
28+
{
29+
"xsa": 456,
30+
"title": "Another Advisory",
31+
"public_time": "2023-01-01T00:00:00Z",
32+
"cve": [],
33+
},
34+
]
35+
}
36+
]
37+
38+
39+
@pytest.fixture
40+
def pipeline():
41+
return XenImporterPipeline()
42+
43+
44+
@patch("vulnerabilities.pipelines.v2_importers.xen_importer.fetch_response")
45+
def test_get_xsa_data(mock_fetch, pipeline):
46+
mock_fetch.return_value.json.return_value = SAMPLE_XSA_JSON
47+
data = pipeline.get_xsa_data()
48+
assert isinstance(data, list)
49+
assert "xsas" in data[0]
50+
51+
52+
@patch("vulnerabilities.pipelines.v2_importers.xen_importer.fetch_response")
53+
def test_advisories_count(mock_fetch, pipeline):
54+
mock_fetch.return_value.json.return_value = SAMPLE_XSA_JSON
55+
count = pipeline.advisories_count()
56+
assert count == 2
57+
58+
59+
@patch("vulnerabilities.pipelines.v2_importers.xen_importer.fetch_response")
60+
def test_collect_advisories(mock_fetch, pipeline):
61+
mock_fetch.return_value.json.return_value = SAMPLE_XSA_JSON
62+
advisories = list(pipeline.collect_advisories())
63+
64+
assert len(advisories) == 2
65+
66+
first = advisories[0]
67+
assert isinstance(first, AdvisoryData)
68+
assert first.advisory_id == "XSA-123"
69+
assert first.aliases == ["CVE-2022-12345"]
70+
assert first.summary == "Sample Xen Advisory"
71+
assert isinstance(first.references_v2[0], ReferenceV2)
72+
assert first.date_published == date_parse("2022-09-15T00:00:00Z")
73+
74+
75+
def test_to_advisories_single(pipeline):
76+
xsa_sample = {
77+
"xsa": 999,
78+
"title": "Test Advisory",
79+
"public_time": "2021-07-01T00:00:00Z",
80+
"cve": ["CVE-2021-9999"],
81+
}
82+
83+
results = list(pipeline.to_advisories(xsa_sample))
84+
assert len(results) == 1
85+
86+
advisory = results[0]
87+
assert advisory.advisory_id == "XSA-999"
88+
assert advisory.aliases == ["CVE-2021-9999"]
89+
assert advisory.summary == "Test Advisory"
90+
assert advisory.date_published == date_parse("2021-07-01T00:00:00Z")
91+
assert advisory.original_advisory_text.startswith('{\n "xsa"')
92+
93+
94+
def test_to_advisories_missing_fields(pipeline):
95+
xsa_sample = {"xsa": None, "title": None, "public_time": "2020-01-01T00:00:00Z", "cve": []}
96+
97+
results = list(pipeline.to_advisories(xsa_sample))
98+
advisory = results[0]
99+
100+
assert advisory.advisory_id == "XSA-None"
101+
assert advisory.aliases == []
102+
assert advisory.summary == None

0 commit comments

Comments
 (0)