Skip to content

Commit 069909e

Browse files
committed
Adjust text example
* Use file-based test expectation for sanity. Otherwise updating test expectations to match code changes becomes a painful chore. Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 792c7d3 commit 069909e

File tree

3 files changed

+81
-75
lines changed

3 files changed

+81
-75
lines changed

vulnerabilities/tests/example_importer_improver.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def fetch_advisory_data():
5454

5555

5656
def parse_advisory_data(raw_data) -> AdvisoryData:
57+
"""
58+
Return AdvisoryData build from a mapping of ``raw_data`` example advisory.
59+
"""
5760
purl = PackageURL(type="example", name="dummy_package")
5861
affected_version_range = NginxVersionRange.from_native(raw_data["vulnerable"])
5962
fixed_version = SemverVersion(raw_data["fixed"])
@@ -64,6 +67,7 @@ def parse_advisory_data(raw_data) -> AdvisoryData:
6467
system=SCORING_SYSTEMS["generic_textual"], value=raw_data["advisory_severity"]
6568
)
6669
references = [Reference(url=raw_data["reference"], severities=[severity])]
70+
# The original format is "06-10-2021 UTC" and we convert this a
6771
date_published = datetime.strptime(raw_data["published_on"], "%d-%m-%Y %Z").replace(
6872
tzinfo=timezone.utc
6973
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"aliases": [
3+
"CVE-2021-12341337"
4+
],
5+
"summary": "Dummy advisory",
6+
"affected_packages": [
7+
{
8+
"package": {
9+
"type": "example",
10+
"namespace": null,
11+
"name": "dummy_package",
12+
"version": null,
13+
"qualifiers": null,
14+
"subpath": null
15+
},
16+
"affected_version_range": "vers:nginx/>=0.6.18|<=1.20.0",
17+
"fixed_version": "1.20.1"
18+
}
19+
],
20+
"references": [
21+
{
22+
"reference_id": "",
23+
"url": "http://example.com/cve-2021-1234",
24+
"severities": [
25+
{
26+
"system": "generic_textual",
27+
"value": "high"
28+
}
29+
]
30+
}
31+
],
32+
"date_published": "2021-10-06T00:00:00+00:00"
33+
}

vulnerabilities/tests/test_example.py

Lines changed: 44 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,49 @@
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
324
from unittest.mock import patch
425

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+
from commoncode import testcase
1027

1128
from vulnerabilities import models
1229
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-
from vulnerabilities.improve_runner import ImproveRunner
1930
from vulnerabilities.improvers.default import DefaultImprover
31+
from vulnerabilities.tests import util_tests
2032
from vulnerabilities.tests.example_importer_improver import ExampleAliasImprover
2133
from vulnerabilities.tests.example_importer_improver import ExampleImporter
2234
from vulnerabilities.tests.example_importer_improver import parse_advisory_data
2335

2436

2537
def mock_fetch_advisory_data():
26-
return [
27-
{
28-
"id": "CVE-2021-12341337",
29-
"summary": "Dummy advisory",
30-
"advisory_severity": "high",
31-
"vulnerable": "0.6.18-1.20.0",
32-
"fixed": "1.20.1",
33-
"reference": "http://example.com/cve-2021-1234",
34-
"published_on": "06-10-2021 UTC",
35-
}
36-
]
38+
return {
39+
"id": "CVE-2021-12341337",
40+
"summary": "Dummy advisory",
41+
"advisory_severity": "high",
42+
"vulnerable": "0.6.18-1.20.0",
43+
"fixed": "1.20.1",
44+
"reference": "http://example.com/cve-2021-1234",
45+
"published_on": "06-10-2021 UTC",
46+
}
3747

3848

3949
def mock_fetch_additional_aliases(alias):
@@ -50,59 +60,18 @@ def mock_fetch_additional_aliases(alias):
5060
"vulnerabilities.tests.example_importer_improver.fetch_additional_aliases",
5161
mock_fetch_additional_aliases,
5262
)
53-
class TestExampleImporter(TestCase):
63+
class TestExampleImporter(testcase.FileBasedTesting):
64+
65+
test_data_dir = str(Path(__file__).resolve().parent / "test_data" / "example")
66+
5467
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
68+
raw_data = mock_fetch_advisory_data()
69+
expected_file = self.get_test_loc("parse_advisory_data-expected.json", must_exist=False)
70+
result = parse_advisory_data(raw_data).to_dict()
71+
util_tests.check_results_against_json(result, expected_file)
10372

10473
def test_import_framework_using_example_importer(self):
105-
raw_datas = mock_fetch_advisory_data()
74+
raw_datas = [mock_fetch_advisory_data()]
10675
ImportRunner(ExampleImporter).run()
10776

10877
for raw_data in raw_datas:
@@ -112,7 +81,7 @@ def test_improve_framework_using_example_improver(self):
11281
ImportRunner(ExampleImporter).run()
11382
ImproveRunner(DefaultImprover).run()
11483
ImproveRunner(ExampleAliasImprover).run()
115-
raw_datas = mock_fetch_advisory_data()
84+
raw_datas = [mock_fetch_advisory_data()]
11685

11786
assert models.Package.objects.count() == 3
11887
assert models.PackageRelatedVulnerability.objects.filter(fix=True).count() == 1

0 commit comments

Comments
 (0)