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