|
1 | | -import datetime |
2 | | -import os |
| 1 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 2 | +# http://nexb.com and https://github.com/nexB/vulnerablecode/ |
| 3 | +# The VulnerableCode software is licensed under the Apache License version 2.0. |
| 4 | +# Data generated with VulnerableCode require an acknowledgment. |
| 5 | +# |
| 6 | +# You may not use this software except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 9 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 10 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +# specific language governing permissions and limitations under the License. |
| 12 | +# |
| 13 | +# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode |
| 14 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 15 | +# |
| 16 | +# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 17 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 18 | +# VulnerableCode should be considered or used as legal advice. Consult an Attorney |
| 19 | +# for any legal advice. |
| 20 | +# VulnerableCode is a free software tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 22 | + |
| 23 | +from pathlib import Path |
3 | 24 | from unittest.mock import patch |
4 | 25 |
|
5 | | -from django.test import TestCase |
6 | | -from packageurl import PackageURL |
7 | | -from univers.version_constraint import VersionConstraint |
8 | | -from univers.version_range import NginxVersionRange |
9 | | -from univers.versions import SemverVersion |
| 26 | +import pytest |
| 27 | +from commoncode import testcase |
10 | 28 |
|
11 | 29 | from vulnerabilities import models |
12 | 30 | from vulnerabilities.import_runner import ImportRunner |
13 | | -from vulnerabilities.importer import AdvisoryData |
14 | | -from vulnerabilities.importer import AffectedPackage |
15 | | -from vulnerabilities.importer import Reference |
16 | | -from vulnerabilities.importer import ScoringSystem |
17 | | -from vulnerabilities.importer import VulnerabilitySeverity |
18 | 31 | from vulnerabilities.improve_runner import ImproveRunner |
19 | 32 | from vulnerabilities.improvers.default import DefaultImprover |
| 33 | +from vulnerabilities.tests import util_tests |
20 | 34 | from vulnerabilities.tests.example_importer_improver import ExampleAliasImprover |
21 | 35 | from vulnerabilities.tests.example_importer_improver import ExampleImporter |
22 | 36 | from vulnerabilities.tests.example_importer_improver import parse_advisory_data |
@@ -50,74 +64,42 @@ def mock_fetch_additional_aliases(alias): |
50 | 64 | "vulnerabilities.tests.example_importer_improver.fetch_additional_aliases", |
51 | 65 | mock_fetch_additional_aliases, |
52 | 66 | ) |
53 | | -class TestExampleImporter(TestCase): |
| 67 | +class TestExampleImporter(testcase.FileBasedTesting): |
| 68 | + |
| 69 | + test_data_dir = str(Path(__file__).resolve().parent / "test_data" / "example") |
| 70 | + |
54 | 71 | def test_parse_advisory_data(self): |
55 | | - raw_data = mock_fetch_advisory_data()[0] |
56 | | - expected = AdvisoryData( |
57 | | - aliases=["CVE-2021-12341337"], |
58 | | - summary="Dummy advisory", |
59 | | - affected_packages=[ |
60 | | - AffectedPackage( |
61 | | - package=PackageURL( |
62 | | - type="example", |
63 | | - namespace=None, |
64 | | - name="dummy_package", |
65 | | - version=None, |
66 | | - qualifiers={}, |
67 | | - subpath=None, |
68 | | - ), |
69 | | - affected_version_range=NginxVersionRange( |
70 | | - constraints=( |
71 | | - VersionConstraint( |
72 | | - comparator=">=", version=SemverVersion(string="0.6.18") |
73 | | - ), |
74 | | - VersionConstraint( |
75 | | - comparator="<=", version=SemverVersion(string="1.20.0") |
76 | | - ), |
77 | | - ) |
78 | | - ), |
79 | | - fixed_version=SemverVersion(string="1.20.1"), |
80 | | - ) |
81 | | - ], |
82 | | - references=[ |
83 | | - Reference( |
84 | | - reference_id="", |
85 | | - url="http://example.com/cve-2021-1234", |
86 | | - severities=[ |
87 | | - VulnerabilitySeverity( |
88 | | - system=ScoringSystem( |
89 | | - identifier="generic_textual", |
90 | | - name="Generic textual severity rating", |
91 | | - url="", |
92 | | - notes="Severity for unknown scoring systems. Contains generic textual values like High, Low etc", |
93 | | - ), |
94 | | - value="high", |
95 | | - ) |
96 | | - ], |
97 | | - ) |
98 | | - ], |
99 | | - date_published=datetime.datetime(2021, 10, 6, 0, 0, tzinfo=datetime.timezone.utc), |
100 | | - ) |
101 | | - actual = parse_advisory_data(raw_data) |
102 | | - assert actual == expected |
| 72 | + raw_data = { |
| 73 | + "id": "CVE-2021-12341337", |
| 74 | + "summary": "Dummy advisory", |
| 75 | + "advisory_severity": "high", |
| 76 | + "vulnerable": "0.6.18-1.20.0", |
| 77 | + "fixed": "1.20.1", |
| 78 | + "reference": "http://example.com/cve-2021-1234", |
| 79 | + "published_on": "06-10-2021 UTC", |
| 80 | + } |
| 81 | + expected_file = self.get_test_loc("parse_advisory_data-expected.json", must_exist=False) |
| 82 | + result = parse_advisory_data(raw_data).to_dict() |
| 83 | + util_tests.check_results_against_json(result, expected_file) |
103 | 84 |
|
| 85 | + @pytest.mark.django_db(transaction=True) |
104 | 86 | def test_import_framework_using_example_importer(self): |
105 | | - raw_datas = mock_fetch_advisory_data() |
106 | 87 | ImportRunner(ExampleImporter).run() |
107 | 88 |
|
108 | | - for raw_data in raw_datas: |
109 | | - assert models.Advisory.objects.get(aliases__contains=raw_data["id"]) |
| 89 | + for expected in mock_fetch_advisory_data(): |
| 90 | + assert models.Advisory.objects.get(aliases__contains=expected["id"]) |
110 | 91 |
|
| 92 | + @pytest.mark.django_db(transaction=True) |
111 | 93 | def test_improve_framework_using_example_improver(self): |
112 | 94 | ImportRunner(ExampleImporter).run() |
113 | 95 | ImproveRunner(DefaultImprover).run() |
114 | 96 | ImproveRunner(ExampleAliasImprover).run() |
115 | | - raw_datas = mock_fetch_advisory_data() |
116 | 97 |
|
117 | 98 | assert models.Package.objects.count() == 3 |
118 | 99 | assert models.PackageRelatedVulnerability.objects.filter(fix=True).count() == 1 |
119 | 100 | assert models.PackageRelatedVulnerability.objects.filter(fix=False).count() == 2 |
120 | 101 | assert models.VulnerabilitySeverity.objects.count() == 1 |
121 | 102 | assert models.VulnerabilityReference.objects.count() == 1 |
122 | | - for raw_data in raw_datas: |
123 | | - assert models.Vulnerability.objects.get(summary=raw_data["summary"]) |
| 103 | + |
| 104 | + for expected in mock_fetch_advisory_data(): |
| 105 | + assert models.Vulnerability.objects.get(summary=expected["summary"]) |
0 commit comments