Skip to content

Commit da8f604

Browse files
Switch to DojoTestCase for better test compatibility
Use DojoTestCase instead of plain TestCase to align with DefectDojo testing conventions and ensure proper test setup/teardown.
1 parent 1efdfd9 commit da8f604

2 files changed

Lines changed: 40 additions & 22 deletions

File tree

dojo/filters.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,14 +2024,15 @@ def get_finding_group_queryset_for_context(pid=None, eid=None, tid=None):
20242024
"""
20252025
Helper function to build finding group queryset based on context hierarchy.
20262026
Context priority: test > engagement > product > global
2027-
2027+
20282028
Args:
20292029
pid: Product ID (least specific)
20302030
eid: Engagement ID
20312031
tid: Test ID (most specific)
2032-
2032+
20332033
Returns:
20342034
QuerySet of Finding_Group filtered by context
2035+
20352036
"""
20362037
if tid is not None:
20372038
# Most specific: filter by test
@@ -2238,37 +2239,50 @@ def set_related_object_fields(self, *args: list, **kwargs: dict):
22382239
eid=self.eid,
22392240
tid=self.tid,
22402241
)
2241-
2242+
22422243
# Filter by most specific context: test > engagement > product
22432244
if self.tid is not None:
22442245
# Test context: filter finding groups by test
2245-
del self.form.fields["test__engagement__product"]
2246-
del self.form.fields["test__engagement__product__prod_type"]
2247-
del self.form.fields["test__engagement"]
2248-
del self.form.fields["test"]
2246+
if "test__engagement__product" in self.form.fields:
2247+
del self.form.fields["test__engagement__product"]
2248+
if "test__engagement__product__prod_type" in self.form.fields:
2249+
del self.form.fields["test__engagement__product__prod_type"]
2250+
if "test__engagement" in self.form.fields:
2251+
del self.form.fields["test__engagement"]
2252+
if "test" in self.form.fields:
2253+
del self.form.fields["test"]
22492254
elif self.eid is not None:
22502255
# Engagement context: filter finding groups by engagement
2251-
del self.form.fields["test__engagement__product"]
2252-
del self.form.fields["test__engagement__product__prod_type"]
2253-
del self.form.fields["test__engagement"]
2256+
if "test__engagement__product" in self.form.fields:
2257+
del self.form.fields["test__engagement__product"]
2258+
if "test__engagement__product__prod_type" in self.form.fields:
2259+
del self.form.fields["test__engagement__product__prod_type"]
2260+
if "test__engagement" in self.form.fields:
2261+
del self.form.fields["test__engagement"]
22542262
# Filter tests by engagement - get_authorized_tests doesn't support engagement param
2255-
engagement = Engagement.objects.get(id=self.eid)
2256-
self.form.fields["test"].queryset = get_authorized_tests(Permissions.Test_View, product=engagement.product).filter(engagement_id=self.eid).prefetch_related("test_type")
2263+
engagement = Engagement.objects.filter(id=self.eid).select_related("product").first()
2264+
if engagement:
2265+
self.form.fields["test"].queryset = get_authorized_tests(Permissions.Test_View, product=engagement.product).filter(engagement_id=self.eid).prefetch_related("test_type")
22572266
elif self.pid is not None:
22582267
# Product context: filter finding groups by product
2259-
del self.form.fields["test__engagement__product"]
2260-
del self.form.fields["test__engagement__product__prod_type"]
2268+
if "test__engagement__product" in self.form.fields:
2269+
del self.form.fields["test__engagement__product"]
2270+
if "test__engagement__product__prod_type" in self.form.fields:
2271+
del self.form.fields["test__engagement__product__prod_type"]
22612272
# TODO: add authorized check to be sure
2262-
self.form.fields["test__engagement"].queryset = Engagement.objects.filter(
2263-
product_id=self.pid,
2264-
).all()
2265-
self.form.fields["test"].queryset = get_authorized_tests(Permissions.Test_View, product=self.pid).prefetch_related("test_type")
2273+
if "test__engagement" in self.form.fields:
2274+
self.form.fields["test__engagement"].queryset = Engagement.objects.filter(
2275+
product_id=self.pid,
2276+
).all()
2277+
if "test" in self.form.fields:
2278+
self.form.fields["test"].queryset = get_authorized_tests(Permissions.Test_View, product=self.pid).prefetch_related("test_type")
22662279
else:
22672280
# Global context: show all authorized finding groups
22682281
self.form.fields[
22692282
"test__engagement__product__prod_type"].queryset = get_authorized_product_types(Permissions.Product_Type_View)
22702283
self.form.fields["test__engagement"].queryset = get_authorized_engagements(Permissions.Engagement_View)
2271-
del self.form.fields["test"]
2284+
if "test" in self.form.fields:
2285+
del self.form.fields["test"]
22722286

22732287
if self.form.fields.get("test__engagement__product"):
22742288
self.form.fields["test__engagement__product"].queryset = get_authorized_products(Permissions.Product_View)

unittests/test_finding_group_filter_context.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from django.test import TestCase
21
from django.utils.timezone import now
32

43
from dojo.filters import FindingFilter, FindingFilterWithoutObjectLookups
@@ -13,16 +12,19 @@
1312
Test_Type,
1413
)
1514

15+
from .dojo_test_case import DojoTestCase
16+
17+
18+
class TestFindingGroupFilterContext(DojoTestCase):
1619

17-
class TestFindingGroupFilterContext(TestCase):
1820
"""Test that Finding Group filter respects Test/Engagement/Product context."""
1921

2022
@classmethod
2123
def setUpTestData(cls):
2224
"""Create test data hierarchy."""
2325
# Create test type
2426
cls.test_type = Test_Type.objects.create(name="Test Type")
25-
27+
2628
# Create user
2729
cls.user = Dojo_User.objects.create(
2830
username="testuser",
@@ -37,10 +39,12 @@ def setUpTestData(cls):
3739
cls.product1 = Product.objects.create(
3840
name="Product 1",
3941
prod_type=cls.prod_type,
42+
description="Test product 1",
4043
)
4144
cls.product2 = Product.objects.create(
4245
name="Product 2",
4346
prod_type=cls.prod_type,
47+
description="Test product 2",
4448
)
4549

4650
# Create engagements for each product

0 commit comments

Comments
 (0)