Commit 940787a1 by Adam Committed by GitHub

Merge pull request #13906 from edx/unicode-errors-refund-messages

handle refund notification message formatting when translated with un…
parents f21dbc79 9068c7e2
""" """
Signal handling functions for use with external commerce service. Signal handling functions for use with external commerce service.
""" """
from __future__ import unicode_literals
import json import json
import logging import logging
from urlparse import urljoin from urlparse import urljoin
import requests
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import AnonymousUser from django.contrib.auth.models import AnonymousUser
from django.dispatch import receiver from django.dispatch import receiver
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from edx_rest_api_client.exceptions import HttpClientError from edx_rest_api_client.exceptions import HttpClientError
import requests
from request_cache.middleware import RequestCache from request_cache.middleware import RequestCache
from student.models import UNENROLL_DONE from student.models import UNENROLL_DONE
from openedx.core.djangoapps.commerce.utils import ecommerce_api_client, is_commerce_service_configured from openedx.core.djangoapps.commerce.utils import ecommerce_api_client, is_commerce_service_configured
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
from openedx.core.djangoapps.theming import helpers as theming_helpers from openedx.core.djangoapps.theming import helpers as theming_helpers
...@@ -182,7 +184,7 @@ def create_zendesk_ticket(requester_name, requester_email, subject, body, tags=N ...@@ -182,7 +184,7 @@ def create_zendesk_ticket(requester_name, requester_email, subject, body, tags=N
# Check for HTTP codes other than 201 (Created) # Check for HTTP codes other than 201 (Created)
if response.status_code != 201: if response.status_code != 201:
log.error(u'Failed to create ticket. Status: [%d], Body: [%s]', response.status_code, response.content) log.error('Failed to create ticket. Status: [%d], Body: [%s]', response.status_code, response.content)
else: else:
log.debug('Successfully created ticket.') log.debug('Successfully created ticket.')
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
......
# coding=UTF-8
""" """
Tests for signal handling in commerce djangoapp. Tests for signal handling in commerce djangoapp.
""" """
from __future__ import unicode_literals
import base64 import base64
import json import json
from urlparse import urljoin from urlparse import urljoin
...@@ -41,7 +44,10 @@ class TestRefundSignal(TestCase): ...@@ -41,7 +44,10 @@ class TestRefundSignal(TestCase):
def setUp(self): def setUp(self):
super(TestRefundSignal, self).setUp() super(TestRefundSignal, self).setUp()
self.requester = UserFactory(username="test-requester") self.requester = UserFactory(username="test-requester")
self.student = UserFactory(username="test-student", email="test-student@example.com") self.student = UserFactory(
username="test-student",
email="test-student@example.com",
)
self.course_enrollment = CourseEnrollmentFactory( self.course_enrollment = CourseEnrollmentFactory(
user=self.student, user=self.student,
course_id=CourseKey.from_string('course-v1:org+course+run'), course_id=CourseKey.from_string('course-v1:org+course+run'),
...@@ -207,25 +213,35 @@ class TestRefundSignal(TestCase): ...@@ -207,25 +213,35 @@ class TestRefundSignal(TestCase):
with self.assertRaises(NotImplementedError): with self.assertRaises(NotImplementedError):
send_refund_notification(self.course_enrollment, [1, 2, 3]) send_refund_notification(self.course_enrollment, [1, 2, 3])
def test_send_refund_notification(self): @ddt.data('email@example.com', 'üñîcode.email@example.com')
@mock.patch('lms.djangoapps.commerce.signals.create_zendesk_ticket')
def test_send_refund_notification(self, student_email, mock_zendesk):
""" Verify the support team is notified of the refund request. """ """ Verify the support team is notified of the refund request. """
refund_ids = [1, 2, 3]
with mock.patch('commerce.signals.create_zendesk_ticket') as mock_zendesk:
refund_ids = [1, 2, 3] # pass a student with unicode and ascii email to ensure that
send_refund_notification(self.course_enrollment, refund_ids) # generate_refund_notification_body can handle formatting a unicode
body = generate_refund_notification_body(self.student, refund_ids) # message
mock_zendesk.assert_called_with(self.student.profile.name, self.student.email, self.student.email = student_email
"[Refund] User-Requested Refund", body, ['auto_refund']) send_refund_notification(self.course_enrollment, refund_ids)
body = generate_refund_notification_body(self.student, refund_ids)
mock_zendesk.assert_called_with(
self.student.profile.name,
self.student.email,
"[Refund] User-Requested Refund",
body,
['auto_refund']
)
def _mock_zendesk_api(self, status=201): def _mock_zendesk_api(self, status=201):
""" Mock Zendesk's ticket creation API. """ """ Mock Zendesk's ticket creation API. """
httpretty.register_uri(httpretty.POST, urljoin(ZENDESK_URL, '/api/v2/tickets.json'), status=status, httpretty.register_uri(httpretty.POST, urljoin(ZENDESK_URL, '/api/v2/tickets.json'), status=status,
body='{}', content_type=JSON) body='{}', content_type=JSON)
def call_create_zendesk_ticket(self, name=u'Test user', email=u'user@example.com', subject=u'Test Ticket', def call_create_zendesk_ticket(self, name='Test user', email='user@example.com', subject='Test Ticket',
body=u'I want a refund!', tags=None): body='I want a refund!', tags=None):
""" Call the create_zendesk_ticket function. """ """ Call the create_zendesk_ticket function. """
tags = tags or [u'auto_refund'] tags = tags or ['auto_refund']
create_zendesk_ticket(name, email, subject, body, tags) create_zendesk_ticket(name, email, subject, body, tags)
@override_settings(ZENDESK_URL=ZENDESK_URL, ZENDESK_USER=None, ZENDESK_API_KEY=None) @override_settings(ZENDESK_URL=ZENDESK_URL, ZENDESK_USER=None, ZENDESK_API_KEY=None)
...@@ -250,11 +266,11 @@ class TestRefundSignal(TestCase): ...@@ -250,11 +266,11 @@ class TestRefundSignal(TestCase):
""" Verify the Zendesk API is called. """ """ Verify the Zendesk API is called. """
self._mock_zendesk_api() self._mock_zendesk_api()
name = u'Test user' name = 'Test user'
email = u'user@example.com' email = 'user@example.com'
subject = u'Test Ticket' subject = 'Test Ticket'
body = u'I want a refund!' body = 'I want a refund!'
tags = [u'auto_refund'] tags = ['auto_refund']
self.call_create_zendesk_ticket(name, email, subject, body, tags) self.call_create_zendesk_ticket(name, email, subject, body, tags)
last_request = httpretty.last_request() last_request = httpretty.last_request()
...@@ -268,14 +284,14 @@ class TestRefundSignal(TestCase): ...@@ -268,14 +284,14 @@ class TestRefundSignal(TestCase):
# Verify the content # Verify the content
expected = { expected = {
u'ticket': { 'ticket': {
u'requester': { 'requester': {
u'name': name, 'name': name,
u'email': email 'email': email
}, },
u'subject': subject, 'subject': subject,
u'comment': {u'body': body}, 'comment': {'body': body},
u'tags': [u'LMS'] + tags 'tags': ['LMS'] + tags
} }
} }
self.assertDictEqual(json.loads(last_request.body), expected) self.assertDictEqual(json.loads(last_request.body), expected)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment