diff --git a/tests/unit/test_form.py b/tests/unit/test_form.py index f45f3228e..eac3a6c68 100644 --- a/tests/unit/test_form.py +++ b/tests/unit/test_form.py @@ -300,26 +300,68 @@ def test_build_bq_details(self): 'updateChannel': 'default' }, 'consoleLog': ['console.log(hi)'] - })) - expected = {'applicationName': 'Firefox', 'updateChannel': 'default', 'consoleLog': ['console.log(hi)']} # noqa + }), + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0' # noqa + ) + expected = { + 'applicationName': 'Firefox', + 'updateChannel': 'default', + 'consoleLog': ['console.log(hi)'], + 'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0' # noqa + } # noqa self.assertEqual(actual, expected) actual_log_only = form.build_bq_details(json.dumps( - {'a': 'b', 'c': False, 'consoleLog': ['console.log(hi)']})) - expected_log_only = {'consoleLog': ['console.log(hi)']} # noqa + { + 'a': 'b', + 'c': False, + 'consoleLog': ['console.log(hi)'], + }), + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0' # noqa + ) + expected_log_only = { + 'consoleLog': ['console.log(hi)'], + 'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0' # noqa + } # noqa self.assertEqual(actual_log_only, expected_log_only) def test_build_bq_details_empty(self): """Empty dict is returned if no data is passed.""" actual_no_data = form.build_bq_details(json.dumps( - {'additionalData': ''})) - expected = {} + {'additionalData': ''}), + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0' # noqa + ) + expected = { + 'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0' # noqa + } self.assertEqual(actual_no_data, expected) - actual_no_data = form.build_bq_details({}) + actual_no_data = form.build_bq_details({}, '') expected = {} self.assertEqual(actual_no_data, expected) + def test_build_bq_details_ua_original(self): + """Test that the original UA is not overwritten.""" + actual = form.build_bq_details(json.dumps( + {'a': 'b', 'c': False, + 'additionalData': { + 'applicationName': 'Firefox', + 'updateChannel': 'default', + 'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0' # noqa + }, + 'consoleLog': ['console.log(hi)'] + }), + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/114.0' # noqa + ) + + expected = { + 'applicationName': 'Firefox', + 'updateChannel': 'default', + 'consoleLog': ['console.log(hi)'], + 'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0' # noqa + } # noqa + self.assertEqual(actual, expected) + def test_build_formdata_bq(self): testdt = datetime(2023, 6, 1, 0, 0, 0, 0) @@ -335,6 +377,7 @@ def utcnow(cls): 'details': json.dumps({ 'additionalData': { 'applicationName': 'Firefox', + 'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0' # noqa }, 'consoleLog': ['console.log(hi)'] }) @@ -349,6 +392,7 @@ def utcnow(cls): 'reported_at': [testdt.isoformat()], 'details': json.dumps({ 'applicationName': 'Firefox', + 'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0', # noqa 'consoleLog': ['console.log(hi)'], 'githubUrl': 'https://github.com/webcompat/web-bugs/issues/111' diff --git a/webcompat/form.py b/webcompat/form.py index 13faafc1f..3f367981a 100644 --- a/webcompat/form.py +++ b/webcompat/form.py @@ -547,7 +547,7 @@ def build_formdata(form_object): return rv -def build_bq_details(details): +def build_bq_details(details, ua_header_string): """Extract additional details from form.""" bq_details = {} @@ -566,6 +566,9 @@ def build_bq_details(details): except ValueError: pass + if 'userAgent' not in bq_details and ua_header_string: + bq_details['userAgent'] = ua_header_string + return bq_details @@ -577,7 +580,10 @@ def build_formdata_bq(form_object, github_issue_url=None): 'reported_at': [datetime.datetime.utcnow().isoformat()] } - details = build_bq_details(form_object.get('details')) + details = build_bq_details( + form_object.get('details'), + form_object.get("ua_header") + ) if github_issue_url: details['githubUrl'] = github_issue_url