Skip to content

Commit d1b1f34

Browse files
committed
Add tests
Signed-off-by: Tushar Goel <[email protected]>
1 parent fcd3003 commit d1b1f34

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

vulnerabilities/pipelines/fill_vulnerability_summary_pipeline.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def fill_missing_summaries(self):
5858

5959
if matching_advisories.exists():
6060
# Take the first matching advisory with a summary
61-
best_advisory = matching_advisories.first()
61+
# get the advisory that was collected the most recently
62+
best_advisory = matching_advisories.order_by("-date_collected").first()
6263
vulnerability.summary = best_advisory.summary
6364
vulnerability.save()
6465
self.log(

vulnerabilities/tests/pipelines/test_fill_vulnerability_summary_pipeline.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,36 @@ def test_non_nvd_advisory_ignored(self):
118118
# Check that the vulnerability still has no summary
119119
vulnerability.refresh_from_db()
120120
self.assertEqual(vulnerability.summary, "")
121+
122+
def test_multiple_matching_advisories(self):
123+
"""
124+
Test that the most recent matching advisory is used when there are multiple.
125+
"""
126+
vulnerability = Vulnerability.objects.create(
127+
vulnerability_id="VCID-1234",
128+
summary="",
129+
)
130+
alias = Alias.objects.create(alias="CVE-2024-1234", vulnerability=vulnerability)
131+
132+
# Create two NVD advisories with the same alias
133+
Advisory.objects.create(
134+
summary="First matching advisory",
135+
created_by="nvd_importer",
136+
date_collected=datetime.datetime(2024, 1, 1, tzinfo=pytz.UTC),
137+
aliases=["CVE-2024-1234"],
138+
)
139+
140+
Advisory.objects.create(
141+
summary="Second matching advisory",
142+
created_by="nvd_importer",
143+
date_collected=datetime.datetime(2024, 1, 2, tzinfo=pytz.UTC),
144+
aliases=["CVE-2024-1234"],
145+
)
146+
147+
# Run the pipeline
148+
pipeline = FillVulnerabilitySummariesPipeline()
149+
pipeline.fill_missing_summaries()
150+
151+
# Check that the vulnerability now has the most recent summary
152+
vulnerability.refresh_from_db()
153+
self.assertEqual(vulnerability.summary, "Second matching advisory")

0 commit comments

Comments
 (0)