Skip to content

Commit c4dc56d

Browse files
committed
add unit tests
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent 0dada49 commit c4dc56d

4 files changed

Lines changed: 76 additions & 1 deletion

File tree

component_catalog/tests/test_models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from component_catalog.models import ComponentType
3737
from component_catalog.models import LicenseExpressionMixin
3838
from component_catalog.models import Package
39+
from component_catalog.models import PackageAffectedByVulnerability
3940
from component_catalog.models import PackageAlreadyExistsWarning
4041
from component_catalog.models import Subcomponent
4142
from component_catalog.tests import make_package
@@ -1372,9 +1373,15 @@ def test_component_catalog_models_get_exclude_candidates_fields(self):
13721373
)
13731374

13741375
for model_class, expected in input_data:
1375-
results = [f.name for f in model_class().get_exclude_candidates_fields()]
1376+
results = [field.name for field in model_class().get_exclude_candidates_fields()]
13761377
self.assertEqual(sorted(expected), sorted(results))
13771378

1379+
def test_package_affected_by_vulnerability_excludes_auto_now_add_fields(self):
1380+
field_names = [
1381+
field.name for field in PackageAffectedByVulnerability().get_exclude_candidates_fields()
1382+
]
1383+
self.assertNotIn("detected_date", field_names)
1384+
13781385
def test_component_create_with_or_and_and_in_license_name_and_key(self):
13791386
or_license = License.objects.create(
13801387
key="orrible",

policy/tests/test_rules.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ def test_does_not_count_links_with_terminal_analysis(self):
202202
count = UnresolvedVulnerabilityRule().count_violations(self.product, 0, {})
203203
self.assertEqual(0, count)
204204

205+
def test_does_not_count_links_with_resolved_with_pedigree_analysis(self):
206+
package = make_package(self.dataspace)
207+
vulnerability = make_vulnerability(self.dataspace, affecting=package)
208+
product_package = make_product_package(self.product, package=package)
209+
make_vulnerability_analysis(product_package, vulnerability, state="resolved_with_pedigree")
210+
count = UnresolvedVulnerabilityRule().count_violations(self.product, 0, {})
211+
self.assertEqual(0, count)
212+
213+
def test_does_not_count_links_with_not_affected_analysis(self):
214+
package = make_package(self.dataspace)
215+
vulnerability = make_vulnerability(self.dataspace, affecting=package)
216+
product_package = make_product_package(self.product, package=package)
217+
make_vulnerability_analysis(product_package, vulnerability, state="not_affected")
218+
count = UnresolvedVulnerabilityRule().count_violations(self.product, 0, {})
219+
self.assertEqual(0, count)
220+
205221

206222
class StaleVulnerabilityRuleTestCase(TestCase):
207223
def setUp(self):
@@ -237,6 +253,17 @@ def test_does_not_count_links_below_min_risk_score(self):
237253
count = StaleVulnerabilityRule().count_violations(self.product, 0, {})
238254
self.assertEqual(0, count)
239255

256+
def test_custom_max_days_triggers_for_links_within_window(self):
257+
package = make_package(self.dataspace)
258+
vulnerability = make_vulnerability(self.dataspace, affecting=package, risk_score=9.0)
259+
make_product_package(self.product, package=package)
260+
recent_date = timezone.now() - timedelta(days=10)
261+
PackageAffectedByVulnerability.objects.filter(
262+
package=package, vulnerability=vulnerability
263+
).update(detected_date=recent_date)
264+
count = StaleVulnerabilityRule().count_violations(self.product, 0, {"max_days": 5})
265+
self.assertEqual(1, count)
266+
240267
def test_does_not_count_stale_links_with_terminal_analysis(self):
241268
package = make_package(self.dataspace)
242269
vulnerability = make_vulnerability(self.dataspace, affecting=package, risk_score=9.0)

product_portfolio/tests/test_filters.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@
1010
from django.test import TestCase
1111

1212
from component_catalog.models import Package
13+
from component_catalog.tests import make_component
1314
from component_catalog.tests import make_package
1415
from dje.models import Dataspace
1516
from license_library.models import License
1617
from organization.models import Owner
1718
from policy.models import UsagePolicy
19+
from product_portfolio.filters import ProductComponentFilterSet
1820
from product_portfolio.filters import ProductFilterSet
1921
from product_portfolio.filters import ProductPackageFilterSet
2022
from product_portfolio.models import Product
2123
from product_portfolio.models import ProductPackage
2224
from product_portfolio.models import ProductPolicyViolation
2325
from product_portfolio.tests import make_product
26+
from product_portfolio.tests import make_product_component
2427
from product_portfolio.tests import make_product_package
28+
from vulnerabilities.tests import make_vulnerability
2529

2630

2731
class ProductPackageFilterSetTestCase(TestCase):
@@ -164,3 +168,35 @@ def test_filter_by_unknown_rule_returns_all(self):
164168
)
165169
self.assertIn(self.pp_with_policy, filterset.qs)
166170
self.assertIn(self.pp_without_policy, filterset.qs)
171+
172+
173+
class ProductPackageFilterByRuleDistinctTestCase(TestCase):
174+
def setUp(self):
175+
self.dataspace = Dataspace.objects.create(name="nexB")
176+
self.product = make_product(self.dataspace)
177+
178+
def test_filter_by_vulnerability_rule_returns_distinct_results(self):
179+
package = make_package(self.dataspace)
180+
make_vulnerability(self.dataspace, affecting=package)
181+
make_vulnerability(self.dataspace, affecting=package)
182+
make_product_package(self.product, package=package)
183+
filterset = ProductPackageFilterSet(
184+
dataspace=self.dataspace,
185+
data={"policy_rule": "vulnerability_detected"},
186+
)
187+
self.assertEqual(1, filterset.qs.count())
188+
189+
190+
class ProductComponentFilterByRuleTestCase(TestCase):
191+
def setUp(self):
192+
self.dataspace = Dataspace.objects.create(name="nexB")
193+
self.product = make_product(self.dataspace)
194+
195+
def test_filter_by_policy_rule_returns_empty_for_components(self):
196+
component = make_component(self.dataspace)
197+
make_product_component(self.product, component=component)
198+
filterset = ProductComponentFilterSet(
199+
dataspace=self.dataspace,
200+
data={"policy_rule": "usage_policy_error"},
201+
)
202+
self.assertEqual(0, filterset.qs.count())

product_portfolio/tests/test_models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,11 @@ def test_with_policy_violation_count_counts_unresolved_violations(self):
13751375
qs = Product.unsecured_objects.filter(pk=self.product.pk).with_policy_violation_count()
13761376
self.assertEqual(2, qs.get().policy_violation_count)
13771377

1378+
def test_with_policy_violation_count_excludes_obsolete_rule_types(self):
1379+
self._make_violation("obsolete_rule")
1380+
qs = Product.unsecured_objects.filter(pk=self.product.pk).with_policy_violation_count()
1381+
self.assertEqual(0, qs.get().policy_violation_count)
1382+
13781383
def test_with_compliance_issues_includes_product_with_policy_violation(self):
13791384
self._make_violation("usage_policy_error")
13801385
qs = (

0 commit comments

Comments
 (0)