Skip to content

Commit ee1736e

Browse files
committed
Add unit tests related to the vulnerabilities_risk_threshold #97
Signed-off-by: tdruez <[email protected]>
1 parent 6984d5d commit ee1736e

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

dje/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,19 @@ def get_configuration(self, field_name=None):
391391
return getattr(configuration, field_name, None)
392392
return configuration
393393

394+
def set_configuration(self, field_name, value):
395+
"""
396+
Set the `value` for `field_name` on the DataspaceConfiguration linked
397+
with this Dataspace instance.
398+
"""
399+
try:
400+
configuration = self.configuration
401+
except ObjectDoesNotExist:
402+
configuration = DataspaceConfiguration(dataspace=self)
403+
404+
setattr(configuration, field_name, value)
405+
configuration.save()
406+
394407
@property
395408
def has_configuration(self):
396409
"""Return True if an associated DataspaceConfiguration instance exists."""

dje/tests/test_models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ def test_dataspace_get_configuration(self):
212212

213213
self.assertIsNone(self.dataspace.get_configuration("non_available_field"))
214214

215+
def test_dataspace_set_configuration(self):
216+
self.dataspace.set_configuration("vulnerabilities_risk_threshold", 5.0)
217+
self.dataspace.refresh_from_db()
218+
self.assertEqual(5.0, self.dataspace.get_configuration("vulnerabilities_risk_threshold"))
219+
215220
def test_dataspace_has_configuration(self):
216221
self.assertFalse(self.dataspace.has_configuration)
217222
DataspaceConfiguration.objects.create(dataspace=self.dataspace)

product_portfolio/tests/test_models.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,12 @@ def test_product_model_improve_packages_from_purldb(self, mock_update_from_purld
508508
def test_product_model_get_vulnerability_qs(self):
509509
package1 = make_package(self.dataspace)
510510
package2 = make_package(self.dataspace)
511-
vulnerability1 = make_vulnerability(self.dataspace, affecting=[package1, package2])
512-
vulnerability2 = make_vulnerability(self.dataspace, affecting=[package1, package2])
511+
vulnerability1 = make_vulnerability(
512+
self.dataspace, affecting=[package1, package2], risk_score=10.0
513+
)
514+
vulnerability2 = make_vulnerability(
515+
self.dataspace, affecting=[package1, package2], risk_score=1.0
516+
)
513517
make_product_package(self.product1, package=package1)
514518
make_product_package(self.product1, package=package2)
515519

@@ -519,6 +523,12 @@ def test_product_model_get_vulnerability_qs(self):
519523
self.assertIn(vulnerability1, queryset)
520524
self.assertIn(vulnerability2, queryset)
521525

526+
queryset = self.product1.get_vulnerability_qs(risk_threshold=5.0)
527+
# Makeing sure the distinct() is properly applied
528+
self.assertEqual(1, len(queryset))
529+
self.assertIn(vulnerability1, queryset)
530+
self.assertNotIn(vulnerability2, queryset)
531+
522532
def test_product_model_vulnerability_count_property(self):
523533
self.assertEqual(0, self.product1.vulnerability_count)
524534

@@ -534,6 +544,15 @@ def test_product_model_vulnerability_count_property(self):
534544
self.product1 = Product.unsecured_objects.get(pk=self.product1.pk)
535545
self.assertEqual(2, self.product1.vulnerability_count)
536546

547+
def test_product_model_get_vulnerabilities_risk_threshold(self):
548+
self.assertIsNone(self.product1.get_vulnerabilities_risk_threshold())
549+
550+
self.product1.dataspace.set_configuration("vulnerabilities_risk_threshold", 5.0)
551+
self.assertEqual(5.0, self.product1.get_vulnerabilities_risk_threshold())
552+
553+
self.product1.update(vulnerabilities_risk_threshold=10.0)
554+
self.assertEqual(10.0, self.product1.get_vulnerabilities_risk_threshold())
555+
537556
def test_productcomponent_model_license_expression_handle_assigned_licenses(self):
538557
p1 = ProductComponent.objects.create(
539558
product=self.product1, name="p1", dataspace=self.dataspace

product_portfolio/tests/test_views.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,29 @@ def test_product_portfolio_tab_vulnerability_view_queries(self):
356356
with self.assertNumQueries(10):
357357
self.client.get(url)
358358

359+
def test_product_portfolio_tab_vulnerability_risk_threshold(self):
360+
self.client.login(username="nexb_user", password="secret")
361+
362+
p1 = make_package(self.dataspace)
363+
vulnerability1 = make_vulnerability(self.dataspace, affecting=[p1], risk_score=1.0)
364+
vulnerability2 = make_vulnerability(self.dataspace, affecting=[p1], risk_score=5.0)
365+
product1 = make_product(self.dataspace)
366+
make_product_package(product1, package=p1)
367+
url = product1.get_url("tab_vulnerabilities")
368+
369+
response = self.client.get(url)
370+
self.assertContains(response, vulnerability1.vcid)
371+
self.assertContains(response, vulnerability2.vcid)
372+
self.assertContains(response, "2 results")
373+
self.assertNotContains(response, "A risk threshold filter at")
374+
375+
product1.update(vulnerabilities_risk_threshold=3.0)
376+
response = self.client.get(url)
377+
self.assertNotContains(response, vulnerability1.vcid)
378+
self.assertContains(response, vulnerability2.vcid)
379+
self.assertContains(response, "1 results")
380+
self.assertContains(response, 'A risk threshold filter at "3.0" is currently applied.')
381+
359382
def test_product_portfolio_tab_vulnerability_view_analysis_rendering(self):
360383
self.client.login(username="nexb_user", password="secret")
361384
# Each have a unique vulnerability, and p1 p2 are sharing a common one.

0 commit comments

Comments
 (0)