diff --git a/tests/unit/test_form.py b/tests/unit/test_form.py index d4fc46441..3a93137e1 100644 --- a/tests/unit/test_form.py +++ b/tests/unit/test_form.py @@ -196,24 +196,46 @@ def test_build_formdata(self): self.assertEqual(actual, expected) def test_get_details(self): - """Assert we handle valid JSON and other values.""" + """Assert we handle valid dict and other values.""" actual_string_arg = form.get_details('cool') expected_string_arg = 'cool' self.assertEqual(actual_string_arg, expected_string_arg) - actual_json_arg = form.get_details(json.dumps({'a': 'b', 'c': False})) - expected_json_arg = '
Console Messages:
\n\n[u\'console.log(hi)\']\n\n
Console Messages:
\n\nsup\n' + self.assertEqual(actual_stringy_arg, expected_stringy_arg) + def test_is_valid_issue_form(self): """Assert that we get the form parameters we want.""" incomplete_form = MultiDict([('problem_category', u'unknown_bug')]) diff --git a/webcompat/form.py b/webcompat/form.py index 2c5967db1..88ce64de6 100644 --- a/webcompat/form.py +++ b/webcompat/form.py @@ -103,6 +103,12 @@ def get_form(form_data): ua_header = form_data['user_agent'] # Populate the form bug_form.browser.data = get_browser(ua_header) + # Note: The details JSON that was POSTed to the new issue endpoint is at + # this point a Python dict. We need to re-serialize to JSON when we store + # its value in the hidden details form element, otherwise when we attempt + # to decode it as JSON on form submission, it will throw (because Python + # dicts are not valid JSON) + bug_form.details.data = json.dumps(form_data.get('details'), indent=2) bug_form.extra_labels = form_data.get('extra_labels', None) bug_form.os.data = get_os(ua_header) bug_form.reported_with.data = form_data.get('src', 'web') @@ -111,32 +117,61 @@ def get_form(form_data): return bug_form -def get_details(details_string): +def get_details(details): """Return details content. - * If JSON as a formatted string + * If a dict, as a formatted string * Otherwise as a string as-is. """ - content = details_string + content = details rv = '' - try: - details = json.loads(content) rv = ''.join(['
Console Messages:
++{console_logs} +""".format(console_logs=console_logs) + + def build_details(details): - """Populate and return the Browser Configuration section template.""" + """Populate and return the Browser Configuration section template. + + If we get JSON, we try to pull out the console logs before building the + rest of the details. + """ + + console_logs = None + content = details + try: + content = json.loads(details) + console_logs = content.pop('consoleLog', None) + except ValueError: + # if we got a ValueError, details was a string, so just pass it + # into get_details below + pass + return """