Skip to content

Commit 28ac0cc

Browse files
add unit tests for tags on products
1 parent f9237e8 commit 28ac0cc

2 files changed

Lines changed: 72 additions & 26 deletions

File tree

unittests/dojo_test_case.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,29 @@ def log_finding_summary_json_api(self, findings_content_json=None):
784784
for eps in Endpoint_Status.objects.all():
785785
logger.debug(str(eps.id) + ": " + str(eps.endpoint) + ": " + str(eps.endpoint.id) + ": " + str(eps.mitigated))
786786

787+
def get_product_api(self, product_id):
788+
response = self.client.get(reverse("product-list") + f"{product_id}/", format="json")
789+
self.assertEqual(200, response.status_code, response.content[:1000])
790+
return response.data
791+
792+
def post_new_product_api(self, product_details: dict, expected_status_code: int = 201):
793+
payload = copy.deepcopy(product_details)
794+
response = self.client.post(reverse("product-list"), payload, format="json")
795+
self.assertEqual(expected_status_code, response.status_code, response.content[:1000])
796+
return response.data
797+
798+
def put_product_api(self, product_id, product_details: dict, expected_status_code: int = 201):
799+
payload = copy.deepcopy(product_details)
800+
response = self.client.put(reverse("product-list") + f"{product_id}/", payload, format="json")
801+
self.assertEqual(expected_status_code, response.status_code, response.content[:1000])
802+
return response.data
803+
804+
def patch_product_api(self, product_id, product_details: dict, expected_status_code: int = 201):
805+
payload = copy.deepcopy(product_details)
806+
response = self.client.patch(reverse("product-list") + f"{product_id}/", payload, format="json")
807+
self.assertEqual(expected_status_code, response.status_code, response.content[:1000])
808+
return response.data
809+
787810

788811
class DojoVCRTestCase(DojoTestCase, VCRTestCase):
789812
def __init__(self, *args, **kwargs):

unittests/test_tags.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import random
33

4-
from dojo.models import Finding, Test
4+
from dojo.models import Finding, Product, Test
55
from dojo.product.helpers import propagate_tags_on_product_sync
66

77
from .dojo_test_case import DojoAPITestCase, get_unit_tests_scans_path
@@ -18,6 +18,53 @@ def setUp(self, *args, **kwargs):
1818
self.scans_path = get_unit_tests_scans_path("zap")
1919
self.zap_sample5_filename = self.scans_path / "5_zap_sample_one.xml"
2020

21+
def test_create_product_with_tags(self, expected_status_code: int = 201):
22+
product_id = Product.objects.all().first().id
23+
product_details = self.get_product_api(product_id)
24+
25+
del product_details["id"]
26+
27+
product_details["name"] = "tags test " + str(random.randint(1, 9999)) # noqa: S311
28+
product_details["tags"] = ["tag1", "tag2"]
29+
response = self.post_new_product_api(product_details, expected_status_code=expected_status_code)
30+
31+
self.assertEqual(response["tags"], product_details["tags"])
32+
33+
def test_put_product_with_tags(self):
34+
product_id = Product.objects.all().first().id
35+
product_details = self.get_product_api(product_id)
36+
37+
del product_details["id"]
38+
39+
product_details["name"] = "tags test " + str(random.randint(1, 9999)) # noqa: S311
40+
product_details["tags"] = ["tag4", "tag5"]
41+
response = self.put_product_api(product_id, product_details, expected_status_code=200)
42+
43+
self.assertEqual(response["tags"], product_details["tags"])
44+
45+
def test_patch_product_with_tags(self):
46+
product_id = Product.objects.all().first().id
47+
product_details = self.get_product_api(product_id)
48+
49+
del product_details["id"]
50+
51+
product_details["tags"] = ["tag9", "tag10"]
52+
response = self.patch_product_api(product_id, product_details, expected_status_code=200)
53+
54+
self.assertEqual(response["tags"], product_details["tags"])
55+
56+
def test_patch_product_with_invalid_tags(self):
57+
product_id = Product.objects.all().first().id
58+
59+
product_details = {"tags": ["'tag9"]}
60+
self.patch_product_api(product_id, product_details, expected_status_code=400)
61+
product_details["tags"] = ["tag 10"]
62+
self.patch_product_api(product_id, product_details, expected_status_code=400)
63+
product_details["tags"] = ["tagA,tagB"]
64+
# since https://github.com/DefectDojo/django-DefectDojo/pull/12434 tags are split again by commas
65+
response = self.patch_product_api(product_id, product_details, expected_status_code=200)
66+
self.assertEqual(response["tags"], ["tagA", "tagB"])
67+
2168
def create_finding_with_tags(self, tags: list[str], expected_status_code: int = 201):
2269
finding_id = Finding.objects.all().first().id
2370
finding_details = self.get_finding_api(finding_id)
@@ -79,31 +126,6 @@ def test_finding_post_tags(self):
79126
# logger.debug('looking for tag %s in tag list %s', tag, response['tags'])
80127
self.assertIn(tag, response["tags"])
81128

82-
def test_finding_post_tags_extra(self):
83-
# create finding
84-
tags = ["tag1", "tag2"]
85-
finding_id = self.create_finding_with_tags(tags)
86-
87-
response = self.get_finding_api(finding_id)
88-
89-
self.assertEqual(["tag1", "tag2"], response.get("tags", None))
90-
91-
tags_new = ["tag3", "tag4"]
92-
response = self.patch_finding_api(finding_id, {"tags": tags_new})
93-
self.assertEqual(["tag3", "tag4"], response.get("tags", None))
94-
95-
response = self.post_finding_tags_api(finding_id, tags)
96-
self.assertEqual(["tag3", "tag4", "tag1", "tag2"], response.get("tags", None))
97-
98-
# # post tags. POST will ADD tags to existing tags (which is possibly not REST compliant?)
99-
# tags_new = ["tag3", "tag4"]
100-
# response = self.post_finding_tags_api(finding_id, tags_new)
101-
# tags_merged = list(set(tags) | set(tags_new))
102-
# self.assertEqual(len(tags_merged), len(response.get("tags")))
103-
# for tag in tags_merged:
104-
# # logger.debug('looking for tag %s in tag list %s', tag, response['tags'])
105-
# self.assertIn(tag, response["tags"])
106-
107129
def test_finding_post_tags_overlap(self):
108130
# create finding
109131
tags = ["tag1", "tag2"]
@@ -193,6 +215,7 @@ def test_finding_create_tags_with_commas(self):
193215
finding_id = self.create_finding_with_tags(tags)
194216
response = self.get_finding_tags_api(finding_id)
195217

218+
# since https://github.com/DefectDojo/django-DefectDojo/pull/12434 tags are split again by commas
196219
self.assertEqual(["one", "two"], response.get("tags"))
197220
self.assertEqual(2, len(response.get("tags")))
198221
self.assertIn("one", str(response["tags"]))

0 commit comments

Comments
 (0)