Skip to content

Commit

Permalink
Merge pull request #3776 from webcompat/issue/3775/1
Browse files Browse the repository at this point in the history
Issue #3775 - Add UA string to details (if it's missing in the additional data from the reporter)
  • Loading branch information
ksy36 authored Jul 12, 2023
2 parents c5b4ef8 + 2e449dc commit 0014e39
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
58 changes: 51 additions & 7 deletions tests/unit/test_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)']
})
Expand All @@ -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'
Expand Down
10 changes: 8 additions & 2 deletions webcompat/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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


Expand All @@ -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
Expand Down

0 comments on commit 0014e39

Please sign in to comment.