1111
1212from django .test import TestCase
1313from packageurl import PackageURL
14+ from univers .version_range import VersionRange
1415
1516from vulnerabilities .importer import AdvisoryDataV2
1617from vulnerabilities .importer import AffectedPackageV2
1718from vulnerabilities .importer import ReferenceV2
18- from vulnerabilities .models import AdvisoryAlias
1919from vulnerabilities .models import AdvisoryToDoV2
2020from vulnerabilities .models import AdvisoryV2
2121from vulnerabilities .models import ImpactedPackage
2222from vulnerabilities .pipelines .v2_improvers .compute_advisory_todo import ComputeToDo
23+ from vulnerabilities .pipes .advisory import insert_advisory_v2
24+ from vulnerabilities .tests .pipelines import TestLogger
2325
2426
2527class TestComputeToDo (TestCase ):
2628 def setUp (self ):
29+ self .log = TestLogger ()
2730 self .advisory_data1 = AdvisoryDataV2 (
2831 advisory_id = "test_id" ,
32+ aliases = ["CVE-000-000" ],
2933 summary = "Test summary" ,
3034 affected_packages = [
3135 AffectedPackageV2 (
3236 package = PackageURL (type = "npm" , name = "package1" ),
33- affected_version_range = "vers:npm/>=1.0.0|<2.0.0" ,
34- fixed_version_range = "vers:npm/2.0.0" ,
37+ affected_version_range = VersionRange . from_string ( "vers:npm/>=1.0.0|<2.0.0" ) ,
38+ fixed_version_range = VersionRange . from_string ( "vers:npm/2.0.0" ) ,
3539 )
3640 ],
3741 references = [ReferenceV2 (url = "https://example.com/vuln1" )],
@@ -44,7 +48,7 @@ def setUp(self):
4448 affected_packages = [
4549 AffectedPackageV2 (
4650 package = PackageURL (type = "npm" , name = "package1" ),
47- affected_version_range = "vers:npm/>=1.0.0|<2.0.0" ,
51+ affected_version_range = VersionRange . from_string ( "vers:npm/>=1.0.0|<2.0.0" ) ,
4852 )
4953 ],
5054 references = [ReferenceV2 (url = "https://example.com/vuln1" )],
@@ -57,7 +61,7 @@ def setUp(self):
5761 affected_packages = [
5862 AffectedPackageV2 (
5963 package = PackageURL (type = "npm" , name = "package1" ),
60- fixed_version_range = "vers:npm/2.0.0" ,
64+ fixed_version_range = VersionRange . from_string ( "vers:npm/2.0.0" ) ,
6165 )
6266 ],
6367 references = [ReferenceV2 (url = "https://example.com/vuln1" )],
@@ -66,36 +70,28 @@ def setUp(self):
6670
6771 self .advisory_data4 = AdvisoryDataV2 (
6872 advisory_id = "test_id_3" ,
73+ aliases = ["CVE-000-000" ],
6974 summary = "Test summary" ,
7075 affected_packages = [
7176 AffectedPackageV2 (
7277 package = PackageURL (type = "npm" , name = "package1" ),
73- affected_version_range = "vers:npm/>=1.0.0|<=2.0.0" ,
74- fixed_version_range = "vers:npm/2.0.1" ,
78+ affected_version_range = VersionRange . from_string ( "vers:npm/>=1.0.0|<=2.0.0" ) ,
79+ fixed_version_range = VersionRange . from_string ( "vers:npm/2.0.1" ) ,
7580 )
7681 ],
7782 references = [ReferenceV2 (url = "https://example.com/vuln1" )],
7883 url = "https://test.url/" ,
7984 )
8085
8186 def test_advisory_todo_missing_summary (self ):
82- date = datetime .now ()
83- adv = AdvisoryV2 .objects .create (
84- unique_content_id = "test_id" ,
85- url = self .advisory_data1 .url ,
86- summary = "" ,
87- date_collected = date ,
88- advisory_id = "test_id" ,
89- avid = "test_pipeline/test_id" ,
90- datasource_id = "test_pipeline" ,
87+ insert_advisory_v2 (
88+ advisory = self .advisory_data1 ,
89+ pipeline_id = "test_pipeline1" ,
90+ logger = self .log .write ,
9191 )
92- for pkg in self .advisory_data1 .affected_packages :
93- ImpactedPackage .objects .create (
94- advisory = adv ,
95- base_purl = pkg .package ,
96- affecting_vers = pkg .affected_version_range ,
97- fixed_vers = pkg .fixed_version_range ,
98- )
92+ adv = AdvisoryV2 .objects .first ()
93+ adv .summary = ""
94+ adv .save ()
9995 pipeline = ComputeToDo ()
10096 pipeline .execute ()
10197
@@ -105,23 +101,11 @@ def test_advisory_todo_missing_summary(self):
105101 self .assertEqual (1 , todo .advisories .count ())
106102
107103 def test_advisory_todo_missing_fixed (self ):
108- date = datetime .now ()
109- adv = AdvisoryV2 .objects .create (
110- unique_content_id = "test_id" ,
111- url = self .advisory_data2 .url ,
112- summary = self .advisory_data2 .summary ,
113- date_collected = date ,
114- advisory_id = "test_id" ,
115- avid = "test_pipeline/test_id" ,
116- datasource_id = "test_pipeline" ,
104+ insert_advisory_v2 (
105+ advisory = self .advisory_data2 ,
106+ pipeline_id = "test_pipeline1" ,
107+ logger = self .log .write ,
117108 )
118- for pkg in self .advisory_data2 .affected_packages :
119- ImpactedPackage .objects .create (
120- advisory = adv ,
121- base_purl = pkg .package ,
122- affecting_vers = pkg .affected_version_range ,
123- fixed_vers = pkg .fixed_version_range or "" ,
124- )
125109 pipeline = ComputeToDo ()
126110 pipeline .execute ()
127111
@@ -131,23 +115,11 @@ def test_advisory_todo_missing_fixed(self):
131115 self .assertEqual (1 , todo .advisories .count ())
132116
133117 def test_advisory_todo_missing_affected (self ):
134- date = datetime .now ()
135- adv = AdvisoryV2 .objects .create (
136- unique_content_id = "test_id" ,
137- url = self .advisory_data3 .url ,
138- summary = self .advisory_data3 .summary ,
139- date_collected = date ,
140- advisory_id = "test_id" ,
141- avid = "test_pipeline/test_id" ,
142- datasource_id = "test_pipeline" ,
118+ insert_advisory_v2 (
119+ advisory = self .advisory_data3 ,
120+ pipeline_id = "test_pipeline1" ,
121+ logger = self .log .write ,
143122 )
144- for pkg in self .advisory_data3 .affected_packages :
145- ImpactedPackage .objects .create (
146- advisory = adv ,
147- base_purl = pkg .package ,
148- affecting_vers = pkg .affected_version_range or "" ,
149- fixed_vers = pkg .fixed_version_range ,
150- )
151123 pipeline = ComputeToDo ()
152124 pipeline .execute ()
153125
@@ -157,52 +129,31 @@ def test_advisory_todo_missing_affected(self):
157129 self .assertEqual (1 , todo .advisories .count ())
158130
159131 def test_advisory_todo_conflicting_fixed_affected (self ):
160- alias = AdvisoryAlias .objects .create (alias = "CVE-0000-0000" )
161- date = datetime .now ()
162- adv1 = AdvisoryV2 .objects .create (
163- unique_content_id = "test_id1" ,
164- url = self .advisory_data1 .url ,
165- summary = self .advisory_data1 .summary ,
166- date_collected = date ,
167- advisory_id = "test_id" ,
168- avid = "test_pipeline/test_id_2" ,
169- datasource_id = "test_pipeline" ,
132+ insert_advisory_v2 (
133+ advisory = self .advisory_data1 ,
134+ pipeline_id = "test_pipeline1" ,
135+ logger = self .log .write ,
170136 )
171- for pkg in self .advisory_data1 .affected_packages :
172- ImpactedPackage .objects .create (
173- advisory = adv1 ,
174- base_purl = pkg .package ,
175- affecting_vers = pkg .affected_version_range or "" ,
176- fixed_vers = pkg .fixed_version_range or "" ,
177- )
178- adv1 .aliases .add (alias )
179- adv2 = AdvisoryV2 .objects .create (
180- unique_content_id = "test_id2" ,
181- url = self .advisory_data4 .url ,
182- summary = self .advisory_data4 .summary ,
183- date_collected = date ,
184- advisory_id = "test_id" ,
185- avid = "test_pipeline/test_id_2" ,
186- datasource_id = "test_pipeline" ,
137+ insert_advisory_v2 (
138+ advisory = self .advisory_data4 ,
139+ pipeline_id = "test_pipeline2" ,
140+ logger = self .log .write ,
187141 )
188- for pkg in self .advisory_data4 .affected_packages :
189- ImpactedPackage .objects .create (
190- advisory = adv2 ,
191- base_purl = pkg .package ,
192- affecting_vers = pkg .affected_version_range or "" ,
193- fixed_vers = pkg .fixed_version_range or "" ,
194- )
195- adv2 .aliases .add (alias )
142+ for imp in ImpactedPackage .objects .all ():
143+ imp .last_successful_range_unfurl_at = datetime .now ()
144+ imp .save ()
196145
197146 self .assertEqual (0 , AdvisoryToDoV2 .objects .count ())
198147 pipeline = ComputeToDo ()
199148 pipeline .execute ()
200149
201150 todo = AdvisoryToDoV2 .objects .first ()
151+ adv = AdvisoryV2 .objects .first ()
202152 self .assertEqual (1 , AdvisoryToDoV2 .objects .count ())
203153 self .assertEqual ("CONFLICTING_AFFECTED_AND_FIXED_BY_PACKAGES" , todo .issue_type )
204154 self .assertIn (
205- "CVE-0000-0000: pkg:npm/package1 with conflicting fixed version" , todo .issue_detail
155+ '"conflict_checksum": "57f32de5f41f137f0e3808535c2d974d54eeeda426c4279e7fb90475d26f0313",' ,
156+ todo .issue_detail ,
206157 )
207158 self .assertEqual (2 , todo .advisories .count ())
208- self .assertEqual (todo , adv2 .advisory_todos .first ())
159+ self .assertEqual (todo , adv .advisory_todos .first ())
0 commit comments