Skip to content

Commit fb6640b

Browse files
committed
fix unit tests
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent 8a0dbba commit fb6640b

3 files changed

Lines changed: 28 additions & 17 deletions

File tree

dejacode/static/css/dejacode_bootstrap.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ table.vulnerabilities-table .column-summary {
417417
width: 240px;
418418
}
419419
#tab_vulnerabilities .column-affected_packages {
420-
min-width: 300px;
421-
width: 300px;
420+
min-width: 310px;
421+
width: 310px;
422422
}
423423
#tab_vulnerabilities .column-triage_action {
424424
min-width: 165px;

vulnerabilities/tests/test_fetch.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,28 @@ def test_vulnerabilities_fetch_for_packages(self, mock_bulk_search_by_purl):
8585
response_json = json.loads(response_file.read_text())
8686
mock_bulk_search_by_purl.return_value = response_json
8787

88-
with self.assertNumQueries(12):
88+
with self.assertNumQueries(13):
8989
results = fetch_for_packages(
9090
queryset, self.dataspace, batch_size=1, update=True, log_func=buffer.write
9191
)
92-
self.assertEqual(results, {"created": 2, "updated": 0})
92+
self.assertEqual(results, {"created": 3, "updated": 0})
9393

9494
self.assertEqual("Progress: 1/1", buffer.getvalue())
95-
self.assertEqual(2, package1.affected_by_vulnerabilities.count())
95+
self.assertEqual(3, package1.affected_by_vulnerabilities.count())
9696
vulnerability = package1.affected_by_vulnerabilities.filter(
9797
advisory_uid="pypa/idna/PYSEC-2024-60"
9898
).get()
9999
self.assertEqual("PYSEC-2024-60", vulnerability.advisory_id)
100100
self.assertEqual(Decimal("0.5"), vulnerability.exploitability)
101101
self.assertEqual(Decimal("6.8"), vulnerability.weighted_severity)
102102
self.assertEqual(Decimal("3.4"), vulnerability.risk_score)
103+
self.assertEqual(["pkg:pypi/idna@3.7"], vulnerability.fixed_by_packages)
103104
package1.refresh_from_db()
104105
pp1.refresh_from_db()
105106
self.assertEqual(Decimal("3.4"), package1.risk_score)
106107
self.assertEqual(Decimal("3.4"), pp1.weighted_risk_score)
108+
self.assertEqual("3.15", package1.next_non_vulnerable_version)
109+
self.assertEqual("3.15", package1.latest_non_vulnerable_version)
107110

108111
purpose1 = make_product_item_purpose(self.dataspace, exposure_factor=0.5)
109112
pp1.raw_update(purpose=purpose1)
@@ -151,9 +154,9 @@ def test_vulnerabilities_fetch_for_packages_cross_batch_no_spurious_update(
151154
mock_bulk_search_by_purl.side_effect = [response_36, response_37]
152155

153156
results = fetch_for_packages(queryset, self.dataspace, batch_size=1, update=True)
154-
# 2 vulnerabilities created from response_36; the shared one is NOT re-updated
157+
# 3 vulnerabilities created from response_36; the shared one is NOT re-updated
155158
# when encountered in response_37's batch, because created_advisory_uids guards it.
156-
self.assertEqual(results, {"created": 2, "updated": 0})
159+
self.assertEqual(results, {"created": 3, "updated": 0})
157160

158161
@mock.patch("vulnerabilities.fetch.fire_webhooks")
159162
def test_vulnerabilities_fetch_notify_vulnerability_data_update(self, mock_fire_hook):

vulnerabilities/tests/test_models.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ def test_vulnerability_mixin_get_entry_for_package(self, mock_bulk_search):
4949
mock_bulk_search.return_value = json.loads(response_file.read_text())
5050

5151
affected_by_vulnerabilities = package1.get_entry_for_package(vulnerablecode)
52-
self.assertEqual(2, len(affected_by_vulnerabilities))
52+
self.assertEqual(3, len(affected_by_vulnerabilities))
5353
self.assertEqual("pypa/idna/PYSEC-2024-60", affected_by_vulnerabilities[0]["advisory_uid"])
54+
self.assertEqual(["pkg:pypi/idna@3.7"], affected_by_vulnerabilities[0]["fixed_by_packages"])
5455

5556
@mock.patch("vulnerabilities.models.AffectedByVulnerabilityMixin.get_entry_for_package")
5657
@mock.patch("dejacode_toolkit.vulnerablecode.VulnerableCode.is_configured")
@@ -79,12 +80,19 @@ def test_vulnerability_mixin_fetch_vulnerabilities(self, mock_is_configured, moc
7980
package1 = make_package(self.dataspace, package_url="pkg:pypi/idna@3.6")
8081
package1.fetch_vulnerabilities()
8182

82-
self.assertEqual(2, Vulnerability.objects.scope(self.dataspace).count())
83-
self.assertEqual(2, package1.affected_by_vulnerabilities.count())
83+
self.assertEqual(3, Vulnerability.objects.scope(self.dataspace).count())
84+
self.assertEqual(3, package1.affected_by_vulnerabilities.count())
8485
vulnerability = package1.affected_by_vulnerabilities.filter(
8586
advisory_uid="pypa/idna/PYSEC-2024-60"
8687
).get()
8788
self.assertEqual("PYSEC-2024-60", vulnerability.advisory_id)
89+
self.assertEqual(["pkg:pypi/idna@3.7"], vulnerability.fixed_by_packages)
90+
91+
# This code path (single-package fetch) does not go through
92+
# vulnerabilities.fetch.process_vc_entry, so the purl-level fields are not set.
93+
package1.refresh_from_db()
94+
self.assertEqual("", package1.next_non_vulnerable_version)
95+
self.assertEqual("", package1.latest_non_vulnerable_version)
8896

8997
def test_vulnerability_mixin_create_vulnerabilities(self):
9098
response_file = self.data / "vulnerabilities" / "idna_3.6_response.json"
@@ -96,7 +104,7 @@ def test_vulnerability_mixin_create_vulnerabilities(self):
96104
product1 = make_product(self.dataspace, inventory=[package1])
97105
package1.create_vulnerabilities(vulnerabilities_data)
98106

99-
self.assertEqual(3, Vulnerability.objects.scope(self.dataspace).count())
107+
self.assertEqual(4, Vulnerability.objects.scope(self.dataspace).count())
100108
self.assertEqual("5.0", str(package1.risk_score))
101109
self.assertEqual("5.0", str(product1.productpackages.get().weighted_risk_score))
102110

@@ -199,17 +207,17 @@ def test_vulnerability_model_add_affected(self):
199207
self.assertQuerySetEqual(vulnerability2.affected_packages.all(), [package1])
200208
self.assertQuerySetEqual(vulnerability2.affected_components.all(), [component1])
201209

202-
def test_vulnerability_model_fixed_packages_count_generated_field(self):
210+
def test_vulnerability_model_fixed_by_packages_count_generated_field(self):
203211
vulnerability1 = make_vulnerability(dataspace=self.dataspace)
204-
self.assertEqual(0, vulnerability1.fixed_packages_count)
212+
self.assertEqual(0, vulnerability1.fixed_by_packages_count)
205213

206-
vulnerability1.fixed_packages = [
207-
{"purl": "pkg:pypi/gitpython@3.1.41", "is_vulnerable": True},
208-
{"purl": "pkg:pypi/gitpython@3.2", "is_vulnerable": False},
214+
vulnerability1.fixed_by_packages = [
215+
"pkg:pypi/gitpython@3.1.41",
216+
"pkg:pypi/gitpython@3.2",
209217
]
210218
vulnerability1.save()
211219
vulnerability1.refresh_from_db()
212-
self.assertEqual(2, vulnerability1.fixed_packages_count)
220+
self.assertEqual(2, vulnerability1.fixed_by_packages_count)
213221

214222
def test_vulnerability_model_create_from_data(self):
215223
package1 = make_package(self.dataspace)

0 commit comments

Comments
 (0)