diff --git a/tests/unit/test_form.py b/tests/unit/test_form.py index 6f5de573c..68223f7a5 100644 --- a/tests/unit/test_form.py +++ b/tests/unit/test_form.py @@ -6,6 +6,7 @@ import json import unittest +import pytest from werkzeug.datastructures import MultiDict import webcompat @@ -321,5 +322,18 @@ def test_report_issue_anonymous_fails_with_wrong_credentials(self): self.assertEqual(rv.status_code, 400) +@pytest.mark.parametrize( + 'test_input,expected', + [({}, 'web'), + ({'src': ''}, 'unknown'), + ({'src': 'punkCat'}, 'unknown'), + ({'src': 'mobile-reporter'}, 'mobile-reporter'), + ] +) +def test_report_source(test_input, expected): + """Extract the reporting source when valid and invalid.""" + assert form.extract_report_source(test_input) == expected + + if __name__ == '__main__': unittest.main() diff --git a/webcompat/form.py b/webcompat/form.py index 06e84a4a2..17d0f04c6 100644 --- a/webcompat/form.py +++ b/webcompat/form.py @@ -294,7 +294,7 @@ def get_form(form_data, form=IssueForm): 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') + bug_form.reported_with.data = extract_report_source(form_data) bug_form.ua_header.data = ua_header bug_form.url.data = form_data.get('url', None) return bug_form @@ -443,6 +443,28 @@ def add_metadata(form, metadata_dict): return form +def extract_report_source(form_data): + """Extract the source of report. + + This also controls if the src argument is part of a known list. + """ + reported_with = 'unknown' + try: + reported_with = form_data['src'] + if not is_valid_source(reported_with): + reported_with = 'unknown' + except KeyError: + reported_with = 'web' + return reported_with + + +def is_valid_source(reported_with): + """Only a known subset of known values are accepted.""" + if reported_with in app.config['REPORTED_WITH']: + return True + return False + + def build_formdata(form_object): """Convert HTML form data to GitHub API data.