11import logging
22import random
33
4- from dojo .models import Finding , Test
4+ from dojo .models import Finding , Product , Test
55from dojo .product .helpers import propagate_tags_on_product_sync
66
77from .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