Skip to content

Commit 741e239

Browse files
Tags: Add support for comma separation for multipart forms (import/reimport) (#12434)
* Tags: Add support for comma separation for multipart forms (import/reimport) * Correcting ruff * Revert multipart specific tests * Ruff stuff * Update unittests/test_tags.py Co-authored-by: valentijnscholten <valentijnscholten@gmail.com> --------- Co-authored-by: valentijnscholten <valentijnscholten@gmail.com>
1 parent bc62293 commit 741e239

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

dojo/api_v2/serializers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ def to_internal_value(self, data):
225225
self.fail("not_a_str")
226226
# Run the children validation
227227
self.child.run_validation(s)
228-
# Validate the tag to ensure it doesn't contain invalid characters
229-
tag_validator(s, exception_class=RestFrameworkValidationError)
228+
# Split the tags up in any way we need to
230229
substrings = re.findall(r'(?:"[^"]*"|[^",]+)', s)
230+
# Validate the tag to ensure it doesn't contain invalid characters
231+
for sub in substrings:
232+
tag_validator(sub, exception_class=RestFrameworkValidationError)
231233
data_safe.extend(substrings)
232234

233235
return tagulous.utils.render_tags(data_safe)

unittests/test_tags.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,6 @@ def test_finding_patch_remove_tags_all(self):
163163
def test_finding_patch_remove_tags_non_existent(self):
164164
return self.test_finding_put_remove_tags_non_existent()
165165

166-
def test_finding_create_tags_with_commas(self):
167-
tags = ["one,two"]
168-
self.create_finding_with_tags(tags, expected_status_code=400)
169-
170166
def test_finding_create_tags_with_spaces(self):
171167
tags = ["one two"]
172168
self.create_finding_with_tags(tags, expected_status_code=400)
@@ -212,6 +208,25 @@ def test_import_and_reimport_with_tags(self):
212208
for tag in tags:
213209
self.assertIn(tag, response["tags"])
214210

211+
def test_import_multipart_tags(self):
212+
with (self.zap_sample5_filename).open(encoding="utf-8") as testfile:
213+
data = {
214+
"engagement": [1],
215+
"file": [testfile],
216+
"scan_type": ["ZAP Scan"],
217+
"tags": ["bug,security", "urgent"], # Attempting to mimic the two "tag" fields (-F 'tags=tag1' -F 'tags=tag2')
218+
}
219+
response = self.import_scan(data, 201)
220+
# Make sure the serializer returns the correct tags
221+
success_tags = ["bug", "security", "urgent"]
222+
self.assertEqual(response["tags"], success_tags)
223+
# Check that the test has the same issue
224+
test_id = response["test"]
225+
response = self.get_test_api(test_id)
226+
self.assertEqual(len(success_tags), len(response.get("tags")))
227+
for tag in success_tags:
228+
self.assertIn(tag, response["tags"])
229+
215230

216231
class InheritedTagsTests(DojoAPITestCase):
217232
fixtures = ["dojo_testdata.json"]

0 commit comments

Comments
 (0)