diff --git a/guidance_and_support/tests/test_zendesk.py b/guidance_and_support/tests/test_zendesk.py index d8030da6a..07295f5b1 100644 --- a/guidance_and_support/tests/test_zendesk.py +++ b/guidance_and_support/tests/test_zendesk.py @@ -1,4 +1,5 @@ """A module of unit tests for guidance and support.""" +import copy import pytest from django.http import HttpRequest from django import forms @@ -9,7 +10,8 @@ LEGITIMATE_USER['request'] = HttpRequest() LEGITIMATE_USER['request'].META['SERVER_NAME'] = "iatistandard.org" LEGITIMATE_USER['request'].META['SERVER_PORT'] = 80 -LEGITIMATE_USER['request'].path = "/en/a-test-path" +LEGITIMATE_USER['request'].META['HTTP_REFERER'] = "/en/a-test-path" +LEGITIMATE_USER['request'].path = "/en/guidance/get-support/" LEGITIMATE_USER['form'] = forms.Form() LEGITIMATE_USER['form'].cleaned_data = { 'phone': '', @@ -25,7 +27,10 @@ 'email': 'test@user.com' }, 'comment': { - 'body': 'A request was sent from /en/a-test-path.\nA very serious matter.' + 'body': ( + 'A request was sent from /en/guidance/get-support/. The sender was previously on /en/a-test-path.' + '\nA very serious matter.' + ) }, 'subject': 'Automated request from A legitimate user' } @@ -52,3 +57,21 @@ def test_generate_ticket(user): """Test a ticket from a valid user and a spam bot.""" ticket = generate_ticket(user['request'], user['form'], score=user['score']) assert ticket == user['expected_output'] + + +@pytest.mark.django_db +def test_generate_ticket_with_referer_path_false(): + """ + Test a ticket request sent from get-support page without referer page. + """ + user = copy.deepcopy(LEGITIMATE_USER) + user['request'].META['HTTP_REFERER'] = None + user['expected_output']['request']['comment']['body'] = ( + 'A request was sent from /en/guidance/get-support/.\nA very serious matter.' + ) + ticket = generate_ticket( + user['request'], + user['form'], + score=user['score'] + ) + assert ticket == user['expected_output'] diff --git a/guidance_and_support/zendeskhelper.py b/guidance_and_support/zendeskhelper.py index af5025007..e2f003a94 100644 --- a/guidance_and_support/zendeskhelper.py +++ b/guidance_and_support/zendeskhelper.py @@ -17,10 +17,14 @@ def generate_ticket(request, form, score=None, suspicious=False): spam_settings = SpamSettings.for_request(request) if score <= spam_settings.spam_threshold: return False - path = request.path email = form.cleaned_data.get('email') query = form.cleaned_data.get('query') name = form.cleaned_data.get('name', 'Anonymous requester') + path = request.path + referer_path = request.META.get('HTTP_REFERER', None) + if referer_path is not None: + path = "{}. The sender was previously on {}".format(path, referer_path) + if email and query: request_obj = { "request": {