|
17 | 17 |
|
18 | 18 | from packageurl import PackageURL |
19 | 19 |
|
20 | | -import vulnerabilities.importers.ubuntu_usn as ubuntu_usn |
21 | 20 | from vulnerabilities.importer import AdvisoryData |
22 | 21 | from vulnerabilities.importer import Reference |
| 22 | +from vulnerabilities.importers.ubuntu_usn import UbuntuUSNImporter |
| 23 | +from vulnerabilities.tests import util_tests |
23 | 24 |
|
24 | 25 | BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
25 | | -TEST_DATA = os.path.join(BASE_DIR, "test_data/", "ubuntu_usn_db", "database-all.json.bz2") |
26 | | - |
27 | | - |
28 | | -class TestUbuntuUSNImporter(TestCase): |
29 | | - @classmethod |
30 | | - def setUpClass(cls): |
31 | | - data_src_cfg = {"etags": {}, "db_url": "http://exampledb.com"} |
32 | | - cls.data_src = ubuntu_usn.UbuntuUSNImporter(batch_size=1, config=data_src_cfg) |
33 | | - with open(TEST_DATA, "rb") as f: |
34 | | - cls.raw_data = f.read() |
35 | | - cls.db = json.loads(bz2.decompress(cls.raw_data)) |
36 | | - |
37 | | - def test_get_usn_references(self): |
38 | | - |
39 | | - eg_usn = "435-1" |
40 | | - expected_references = Reference( |
41 | | - reference_id="USN-435-1", url="https://usn.ubuntu.com/435-1/" |
42 | | - ) |
43 | | - |
44 | | - found_references = ubuntu_usn.get_usn_references(eg_usn) |
45 | | - assert found_references == expected_references |
46 | | - |
47 | | - def test_fetch(self): |
48 | | - |
49 | | - mock_response = MagicMock() |
50 | | - mock_response.content = self.raw_data |
51 | | - with patch("vulnerabilities.importers.ubuntu_usn.requests.get", return_value=mock_response): |
52 | | - assert ubuntu_usn.fetch("www.db.com") == self.db |
53 | | - |
54 | | - def test_to_advisories(self): |
55 | | - |
56 | | - expected_advisories = [ |
57 | | - Advisory( |
58 | | - summary="", |
59 | | - references=[ |
60 | | - Reference(url="https://usn.ubuntu.com/763-1/", reference_id="USN-763-1") |
61 | | - ], |
62 | | - vulnerability_id="CVE-2009-0698", |
63 | | - ), |
64 | | - Advisory( |
65 | | - summary="", |
66 | | - references=[ |
67 | | - Reference(url="https://usn.ubuntu.com/763-1/", reference_id="USN-763-1") |
68 | | - ], |
69 | | - vulnerability_id="CVE-2009-1274", |
70 | | - ), |
71 | | - ] |
72 | | - found_advisories = self.data_src.to_advisories(self.db) |
73 | | - |
74 | | - found_advisories = list(map(Advisory.normalized, found_advisories)) |
75 | | - expected_advisories = list(map(Advisory.normalized, expected_advisories)) |
76 | | - assert sorted(found_advisories) == sorted(expected_advisories) |
77 | | - |
78 | | - def test_create_etag(self): |
79 | | - assert self.data_src.config.etags == {} |
80 | | - |
81 | | - mock_response = MagicMock() |
82 | | - mock_response.headers = {"etag": "2131151243&2191"} |
83 | | - |
84 | | - with patch("vulnerabilities.importers.ubuntu.requests.head", return_value=mock_response): |
85 | | - assert self.data_src.create_etag("https://example.org") |
86 | | - assert self.data_src.config.etags == {"https://example.org": "2131151243&2191"} |
87 | | - assert not self.data_src.create_etag("https://example.org") |
| 26 | +TEST_DIR = os.path.join(BASE_DIR, "test_data", "ubuntu_usn_db") |
| 27 | + |
| 28 | + |
| 29 | +def test_ubuntu_usn(): |
| 30 | + database = os.path.join(TEST_DIR, "database-all.json.bz2") |
| 31 | + with open(database, "rb") as f: |
| 32 | + raw_data = f.read() |
| 33 | + db = json.loads(bz2.decompress(raw_data)) |
| 34 | + advisories = UbuntuUSNImporter().to_advisories(db) |
| 35 | + expected_file = os.path.join(TEST_DIR, f"ubuntu-usn-expected.json") |
| 36 | + result = [data.to_dict() for data in list(advisories)] |
| 37 | + util_tests.check_results_against_json(result, expected_file) |
0 commit comments