Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion kitsune/questions/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@ def clean_content(self):
raise forms.ValidationError(_("Question content cannot be empty."))
return content

def clean_troubleshooting(self):
troubleshooting = self.cleaned_data.get("troubleshooting", "")
if not troubleshooting:
return troubleshooting

try:
parsed = json.loads(troubleshooting)
except ValueError:
parsed = None

if not isinstance(parsed, dict):
# L10n: An error shown when troubleshooting information isn't a JSON object.
raise forms.ValidationError(
_("Troubleshooting information must be a valid JSON object.")
)

return troubleshooting

@property
def metadata_field_keys(self):
"""Returns the keys of the metadata fields for the current
Expand Down Expand Up @@ -214,7 +232,7 @@ def cleaned_metadata(self):
except ValueError:
parsed = None

if parsed:
if isinstance(parsed, dict):
# Clean out unwanted garbage preferences.
if "modifiedPreferences" in parsed and isinstance(
parsed["modifiedPreferences"], dict
Expand Down
23 changes: 23 additions & 0 deletions kitsune/questions/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,29 @@ def test_cleaned_metadata(self):
actual = form.cleaned_metadata
self.assertDictEqual(actual, expected)

def test_invalid_troubleshooting_json(self):
"""Troubleshooting information must be a valid JSON object."""
topic = TopicFactory(slug="cookies", products=[self.product], in_aaq=True)
base_data = {
"title": "Test question",
"content": "Test question content",
"email": "t@t.com",
"category": topic.id,
}

for troubleshooting in ("[1,2,3]", "123", "true", '"hello"', "not json"):
with self.subTest(troubleshooting=troubleshooting):
form = NewQuestionForm(
product=self.product,
data={**base_data, "troubleshooting": troubleshooting},
)

self.assertFalse(form.is_valid())
self.assertEqual(
"Troubleshooting information must be a valid JSON object.",
form.errors["troubleshooting"][0],
)

def test_clean_content_with_html_entities(self):
"""Test that content with only HTML entities is rejected."""
topic = TopicFactory(slug="cookies", products=[self.product], in_aaq=True)
Expand Down