Skip to content

Commit

Permalink
[Bug 844933] Only accept troubleshooting dicts.
Browse files Browse the repository at this point in the history
There is apparently some questions in SUMO that have this data as a list
that contained the string 'modifedPreferenes', which caused errors.
Being specific about the type will prevent this kind of error.
  • Loading branch information
mythmon committed Feb 25, 2013
1 parent ebee714 commit 260c892
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 10 additions & 0 deletions apps/questions/tests/test_templates.py
Expand Up @@ -637,6 +637,16 @@ def test_empty_troubleshooting_info(self):
response = get(self.client, 'questions.answers', args=[q.id])
eq_(200, response.status_code)

def test_weird_list_troubleshooting_info(self):
"""Test the corner case in which 'modifiedPReferences' is in a
list in troubleshooting data. This is weird, but caused a bug."""
q = question(save=True)
q.add_metadata(troubleshooting='["modifiedPreferences"]')

# This case should not raise an error.
response = get(self.client, 'questions.answers', args=[q.id])
eq_(200, response.status_code)


class TaggedQuestionsTestCase(TaggingTestCaseBase):
"""Questions/answers template tests that require tagged questions."""
Expand Down
10 changes: 8 additions & 2 deletions apps/questions/views.py
Expand Up @@ -215,17 +215,23 @@ def answers(request, template, question_id, form=None, watch_form=None,
# Try to parse troubleshooting data as JSON.
try:
parsed = json.loads(question.metadata['troubleshooting'])
if not isinstance(parsed, dict):
# If something not a dict comes out of JSON, it is probably
# a list, and should not be treated like parsed data.
raise TypeError
# Remove all the printing preferences. These probably aren't relavent,
# and are really noisy.
if 'modifiedPreferences' in parsed:
parsed['modifiedPreferences'] = dict(
(k, v) for k, v in parsed['modifiedPreferences'].items()
if not k.startswith('print'))
question.metadata['troubleshooting_parsed'] = parsed
except (ValueError, KeyError):
except (ValueError, KeyError, TypeError):
# If the field was not filled in, KeyError will be raised.
# If the field was filled manually and is not valid JSON,
# ValueError will be raised. The template will display the raw
# ValueError will be raised. If the field was filled in with
# JSON data that is a list and not a dict, it will raise
# TypeError. In any case, the template will display the raw
# data.
question.metadata['troubleshooting_parsed'] = None

Expand Down

0 comments on commit 260c892

Please sign in to comment.