-fixed tests
This commit is contained in:
@@ -38,6 +38,7 @@ from xblock.fields import Scope, String
|
||||
|
||||
import courseware.views.views as views
|
||||
import shoppingcart
|
||||
|
||||
from capa.tests.response_xml_factory import MultipleChoiceResponseXMLFactory
|
||||
from course_modes.models import CourseMode
|
||||
from course_modes.tests.factories import CourseModeFactory
|
||||
@@ -846,8 +847,8 @@ class ViewsTestCase(ModuleStoreTestCase):
|
||||
url = reverse('submit_financial_assistance_request')
|
||||
return self.client.post(url, json.dumps(data), content_type='application/json')
|
||||
|
||||
@patch.object(views, '_record_feedback_in_zendesk')
|
||||
def test_submit_financial_assistance_request(self, mock_record_feedback):
|
||||
@patch.object(views, 'create_zendesk_ticket', return_value=200)
|
||||
def test_submit_financial_assistance_request(self, mock_create_zendesk_ticket):
|
||||
username = self.user.username
|
||||
course = six.text_type(self.course_key)
|
||||
legal_name = 'Jesse Pinkman'
|
||||
@@ -871,10 +872,12 @@ class ViewsTestCase(ModuleStoreTestCase):
|
||||
response = self._submit_financial_assistance_form(data)
|
||||
self.assertEqual(response.status_code, 204)
|
||||
|
||||
__, __, ticket_subject, __, tags, additional_info = mock_record_feedback.call_args[0]
|
||||
mocked_kwargs = mock_record_feedback.call_args[1]
|
||||
group_name = mocked_kwargs['group_name']
|
||||
require_update = mocked_kwargs['require_update']
|
||||
__, __, ticket_subject, __ = mock_create_zendesk_ticket.call_args[0]
|
||||
mocked_kwargs = mock_create_zendesk_ticket.call_args[1]
|
||||
group_name = mocked_kwargs['group']
|
||||
tags = mocked_kwargs['tags']
|
||||
additional_info = mocked_kwargs['additional_info']
|
||||
|
||||
private_comment = '\n'.join(list(additional_info.values()))
|
||||
for info in (country, income, reason_for_applying, goals, effort, username, legal_name, course):
|
||||
self.assertIn(info, private_comment)
|
||||
@@ -891,10 +894,9 @@ class ViewsTestCase(ModuleStoreTestCase):
|
||||
self.assertDictContainsSubset({'course_id': course}, tags)
|
||||
self.assertIn('Client IP', additional_info)
|
||||
self.assertEqual(group_name, 'Financial Assistance')
|
||||
self.assertTrue(require_update)
|
||||
|
||||
@patch.object(views, '_record_feedback_in_zendesk', return_value=False)
|
||||
def test_zendesk_submission_failed(self, _mock_record_feedback):
|
||||
@patch.object(views, 'create_zendesk_ticket', return_value=500)
|
||||
def test_zendesk_submission_failed(self, _mock_create_zendesk_ticket):
|
||||
response = self._submit_financial_assistance_form({
|
||||
'username': self.user.username,
|
||||
'course': six.text_type(self.course.id),
|
||||
|
||||
@@ -1653,8 +1653,7 @@ def financial_assistance_request(request):
|
||||
)),
|
||||
group='Financial Assistance',
|
||||
)
|
||||
|
||||
if not zendesk_submitted:
|
||||
if not (zendesk_submitted >= 200 and zendesk_submitted < 300):
|
||||
# The call to Zendesk failed. The frontend will display a
|
||||
# message to the user.
|
||||
return HttpResponse(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
@@ -3,11 +3,15 @@ Tests of Zendesk interaction utility functions
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
import ddt
|
||||
from django.test.utils import override_settings
|
||||
from mock import MagicMock, patch
|
||||
|
||||
from openedx.core.djangoapps.zendesk_proxy.utils import create_zendesk_ticket
|
||||
import json
|
||||
from collections import OrderedDict
|
||||
|
||||
from django.test.utils import override_settings
|
||||
|
||||
import ddt
|
||||
from mock import MagicMock, patch
|
||||
from openedx.core.djangoapps.zendesk_proxy.utils import create_zendesk_ticket, get_zendesk_group_by_name
|
||||
from openedx.core.lib.api.test_utils import ApiTestCase
|
||||
|
||||
|
||||
@@ -61,3 +65,51 @@ class TestUtils(ApiTestCase):
|
||||
body=self.request_data['body'],
|
||||
)
|
||||
self.assertEqual(status_code, 500)
|
||||
|
||||
def test_financial_assistant_ticket(self):
|
||||
""" Test Financial Assistent request ticket. """
|
||||
ticket_creation_response_data = {
|
||||
"ticket": {
|
||||
"id": 35436,
|
||||
"subject": "My printer is on fire!",
|
||||
}
|
||||
}
|
||||
response_text = json.dumps(ticket_creation_response_data)
|
||||
with patch('requests.post', return_value=MagicMock(status_code=200, text=response_text)):
|
||||
with patch('requests.put', return_value=MagicMock(status_code=200)):
|
||||
with patch('openedx.core.djangoapps.zendesk_proxy.utils.get_zendesk_group_by_name', return_value=2):
|
||||
status_code = create_zendesk_ticket(
|
||||
requester_name=self.request_data['name'],
|
||||
requester_email=self.request_data['email'],
|
||||
subject=self.request_data['subject'],
|
||||
body=self.request_data['body'],
|
||||
group='Financial Assistant',
|
||||
additional_info=OrderedDict(
|
||||
(
|
||||
('Username', 'test'),
|
||||
('Full Name', 'Legal Name'),
|
||||
('Course ID', 'course_key'),
|
||||
('Annual Household Income', 'Income'),
|
||||
('Country', 'Country'),
|
||||
)
|
||||
),
|
||||
)
|
||||
self.assertEqual(status_code, 200)
|
||||
|
||||
def test_get_zendesk_group_by_name(self):
|
||||
""" Tests the functionality of the get zendesk group. """
|
||||
response_data = {
|
||||
"groups": [
|
||||
{
|
||||
"name": "DJs",
|
||||
"created_at": "2009-05-13T00:07:08Z",
|
||||
"updated_at": "2011-07-22T00:11:12Z",
|
||||
"id": 211
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
response_text = json.dumps(response_data)
|
||||
with patch('requests.get', return_value=MagicMock(status_code=200, text=response_text)):
|
||||
group_id = get_zendesk_group_by_name('DJs')
|
||||
self.assertEqual(group_id, 211)
|
||||
|
||||
@@ -3,14 +3,14 @@ Utility functions for zendesk interaction.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
import json
|
||||
import logging
|
||||
from six.moves.urllib.parse import urljoin # pylint: disable=import-error
|
||||
|
||||
from django.conf import settings
|
||||
import requests
|
||||
from django.conf import settings
|
||||
from rest_framework import status
|
||||
|
||||
from six.moves.urllib.parse import urljoin # pylint: disable=import-error
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -26,7 +26,18 @@ def _get_request_headers():
|
||||
'Authorization': u"Bearer {}".format(settings.ZENDESK_OAUTH_ACCESS_TOKEN),
|
||||
}
|
||||
|
||||
def create_zendesk_ticket(requester_name, requester_email, subject, body, group=None, custom_fields=None, uploads=None, tags=None, additional_info=None):
|
||||
|
||||
def create_zendesk_ticket(
|
||||
requester_name,
|
||||
requester_email,
|
||||
subject,
|
||||
body,
|
||||
group=None,
|
||||
custom_fields=None,
|
||||
uploads=None,
|
||||
tags=None,
|
||||
additional_info=None
|
||||
):
|
||||
"""
|
||||
Create a Zendesk ticket via API.
|
||||
"""
|
||||
@@ -96,15 +107,15 @@ def get_zendesk_group_by_name(name):
|
||||
url = urljoin(settings.ZENDESK_URL, '/api/v2/groups.json')
|
||||
|
||||
try:
|
||||
response = requests.post(url, headers=_get_request_headers())
|
||||
response = requests.get(url, headers=_get_request_headers())
|
||||
|
||||
groups = json.loads(response.text)['groups']
|
||||
for group in groups:
|
||||
if group['name'] == name:
|
||||
return group['id']
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
except Exception: # pylint: disable=broad-except
|
||||
log.exception(_std_error_message('Internal server error', 'None'))
|
||||
|
||||
|
||||
return status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
log.exception(_std_error_message('Tried to get zendesk group which does not exist', name))
|
||||
raise Exception
|
||||
@@ -113,7 +124,7 @@ def get_zendesk_group_by_name(name):
|
||||
def post_additional_info_as_comment(ticket_id, additional_info):
|
||||
"""
|
||||
Post the Additional Provided as a comment, So that it is only visible
|
||||
to management and not students.
|
||||
to management and not students.
|
||||
"""
|
||||
additional_info_string = (
|
||||
u"Additional information:\n\n" +
|
||||
@@ -133,7 +144,7 @@ def post_additional_info_as_comment(ticket_id, additional_info):
|
||||
|
||||
try:
|
||||
response = requests.put(url, data=json.dumps(data), headers=_get_request_headers())
|
||||
if response.status_code == 200:
|
||||
if response.status_code >= 200 and response.status_code < 300:
|
||||
log.debug(u'Successfully created comment for ticket {}'.format(ticket_id))
|
||||
else:
|
||||
log.error(
|
||||
|
||||
@@ -279,11 +279,7 @@ requests-oauthlib==1.1.0
|
||||
requests==2.22.0
|
||||
rest-condition==1.0.3
|
||||
rfc6266-parser==0.0.5.post2
|
||||
<<<<<<< HEAD
|
||||
ruamel.ordereddict==0.4.14 ; python_version == "2.7"
|
||||
=======
|
||||
ruamel.ordereddict==0.4.13 ; python_version == "2.7"
|
||||
>>>>>>> e976dd37a3... BOM-70
|
||||
ruamel.yaml.clib==0.1.0
|
||||
ruamel.yaml==0.16.0
|
||||
rules==2.0.1
|
||||
|
||||
Reference in New Issue
Block a user