Commit 66228224 by Christine Lytwynec

Merge pull request #6403 from edx/clytwynec/remove_lynx_from_bulk_email

Mock html_to_text.py for unit tests
parents 1b02ed5d 9dc85bf1
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
Unit tests for student optouts from course email Unit tests for student optouts from course email
""" """
import json import json
from mock import patch from mock import patch, Mock
from django.core import mail from django.core import mail
from django.core.management import call_command from django.core.management import call_command
...@@ -18,6 +18,7 @@ from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase ...@@ -18,6 +18,7 @@ from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.tests.factories import CourseFactory
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message'))
@override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE) @override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE)
class TestOptoutCourseEmails(ModuleStoreTestCase): class TestOptoutCourseEmails(ModuleStoreTestCase):
......
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
Unit tests for sending course email Unit tests for sending course email
""" """
import json import json
from mock import patch from mock import patch, Mock
import os
from unittest import skipIf
from django.conf import settings from django.conf import settings
from django.core import mail from django.core import mail
...@@ -42,9 +44,7 @@ class MockCourseEmailResult(object): ...@@ -42,9 +44,7 @@ class MockCourseEmailResult(object):
return mock_update_subtask_status return mock_update_subtask_status
@override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE) class EmailSendFromDashboardTestCase(ModuleStoreTestCase):
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
class TestEmailSendFromDashboard(ModuleStoreTestCase):
""" """
Test that emails send correctly. Test that emails send correctly.
""" """
...@@ -90,6 +90,14 @@ class TestEmailSendFromDashboard(ModuleStoreTestCase): ...@@ -90,6 +90,14 @@ class TestEmailSendFromDashboard(ModuleStoreTestCase):
""" """
patch.stopall() patch.stopall()
@override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE)
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message'))
class TestEmailSendFromDashboardMockedHtmlToText(EmailSendFromDashboardTestCase):
"""
Tests email sending with mocked html_to_text.
"""
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': True}) @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': True})
def test_email_disabled(self): def test_email_disabled(self):
""" """
...@@ -105,6 +113,7 @@ class TestEmailSendFromDashboard(ModuleStoreTestCase): ...@@ -105,6 +113,7 @@ class TestEmailSendFromDashboard(ModuleStoreTestCase):
# We should get back a HttpResponseForbidden (status code 403) # We should get back a HttpResponseForbidden (status code 403)
self.assertContains(response, "Email is not enabled for this course.", status_code=403) self.assertContains(response, "Email is not enabled for this course.", status_code=403)
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message'))
def test_send_to_self(self): def test_send_to_self(self):
""" """
Make sure email send to myself goes to myself. Make sure email send to myself goes to myself.
...@@ -220,33 +229,6 @@ class TestEmailSendFromDashboard(ModuleStoreTestCase): ...@@ -220,33 +229,6 @@ class TestEmailSendFromDashboard(ModuleStoreTestCase):
'[' + self.course.display_name + '] ' + uni_subject '[' + self.course.display_name + '] ' + uni_subject
) )
def test_unicode_message_send_to_all(self):
"""
Make sure email (with Unicode characters) send to all goes there.
"""
# Now we know we have pulled up the instructor dash's email view
# (in the setUp method), we can test sending an email.
uni_message = u'ẗëṡẗ ṁëṡṡäġë ḟöṛ äḷḷ イ乇丂イ ᄊ乇丂丂ムg乇 キo尺 ムレレ тэѕт мэѕѕаБэ fоѓ аll'
test_email = {
'action': 'Send email',
'send_to': 'all',
'subject': 'test subject for all',
'message': uni_message
}
# Post the email to the instructor dashboard API
response = self.client.post(self.send_mail_url, test_email)
self.assertEquals(json.loads(response.content), self.success_content)
self.assertEquals(len(mail.outbox), 1 + len(self.staff) + len(self.students))
self.assertItemsEqual(
[e.to[0] for e in mail.outbox],
[self.instructor.email] + [s.email for s in self.staff] + [s.email for s in self.students]
)
message_body = mail.outbox[0].body
self.assertIn(uni_message, message_body)
def test_unicode_students_send_to_all(self): def test_unicode_students_send_to_all(self):
""" """
Make sure email (with Unicode characters) send to all goes there. Make sure email (with Unicode characters) send to all goes there.
...@@ -315,3 +297,42 @@ class TestEmailSendFromDashboard(ModuleStoreTestCase): ...@@ -315,3 +297,42 @@ class TestEmailSendFromDashboard(ModuleStoreTestCase):
[s.email for s in self.students] + [s.email for s in self.students] +
[s.email for s in added_users if s not in optouts]) [s.email for s in added_users if s not in optouts])
self.assertItemsEqual(outbox_contents, should_send_contents) self.assertItemsEqual(outbox_contents, should_send_contents)
@override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE)
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
@skipIf(os.environ.get("TRAVIS") == 'true', "Skip this test in Travis CI.")
class TestEmailSendFromDashboard(EmailSendFromDashboardTestCase):
"""
Tests email sending without mocked html_to_text.
Note that these tests are skipped on Travis because we can't use the
function `html_to_text` as it is currently implemented on Travis.
"""
def test_unicode_message_send_to_all(self):
"""
Make sure email (with Unicode characters) send to all goes there.
"""
# Now we know we have pulled up the instructor dash's email view
# (in the setUp method), we can test sending an email.
uni_message = u'ẗëṡẗ ṁëṡṡäġë ḟöṛ äḷḷ イ乇丂イ ᄊ乇丂丂ムg乇 キo尺 ムレレ тэѕт мэѕѕаБэ fоѓ аll'
test_email = {
'action': 'Send email',
'send_to': 'all',
'subject': 'test subject for all',
'message': uni_message
}
# Post the email to the instructor dashboard API
response = self.client.post(self.send_mail_url, test_email)
self.assertEquals(json.loads(response.content), self.success_content)
self.assertEquals(len(mail.outbox), 1 + len(self.staff) + len(self.students))
self.assertItemsEqual(
[e.to[0] for e in mail.outbox],
[self.instructor.email] + [s.email for s in self.staff] + [s.email for s in self.students]
)
message_body = mail.outbox[0].body
self.assertIn(uni_message, message_body)
...@@ -11,7 +11,7 @@ from django.core.management import call_command ...@@ -11,7 +11,7 @@ from django.core.management import call_command
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.db import DatabaseError from django.db import DatabaseError
import json import json
from mock import patch from mock import patch, Mock
from smtplib import SMTPDataError, SMTPServerDisconnected, SMTPConnectError from smtplib import SMTPDataError, SMTPServerDisconnected, SMTPConnectError
from bulk_email.models import CourseEmail, SEND_TO_ALL from bulk_email.models import CourseEmail, SEND_TO_ALL
...@@ -37,6 +37,7 @@ class EmailTestException(Exception): ...@@ -37,6 +37,7 @@ class EmailTestException(Exception):
pass pass
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message'))
@override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE) @override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE)
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False}) @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
class TestEmailErrors(ModuleStoreTestCase): class TestEmailErrors(ModuleStoreTestCase):
......
...@@ -7,12 +7,13 @@ from django.conf import settings ...@@ -7,12 +7,13 @@ from django.conf import settings
from student.tests.factories import UserFactory from student.tests.factories import UserFactory
from mock import patch from mock import patch, Mock
from bulk_email.models import CourseEmail, SEND_TO_STAFF, CourseEmailTemplate, CourseAuthorization from bulk_email.models import CourseEmail, SEND_TO_STAFF, CourseEmailTemplate, CourseAuthorization
from opaque_keys.edx.locations import SlashSeparatedCourseKey from opaque_keys.edx.locations import SlashSeparatedCourseKey
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message'))
class CourseEmailTest(TestCase): class CourseEmailTest(TestCase):
"""Test the CourseEmail model.""" """Test the CourseEmail model."""
......
...@@ -71,6 +71,7 @@ def my_update_subtask_status(entry_id, current_task_id, new_subtask_status): ...@@ -71,6 +71,7 @@ def my_update_subtask_status(entry_id, current_task_id, new_subtask_status):
update_subtask_status(entry_id, current_task_id, new_subtask_status) update_subtask_status(entry_id, current_task_id, new_subtask_status)
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message'))
class TestBulkEmailInstructorTask(InstructorTaskCourseTestCase): class TestBulkEmailInstructorTask(InstructorTaskCourseTestCase):
"""Tests instructor task that send bulk email.""" """Tests instructor task that send bulk email."""
......
...@@ -139,6 +139,7 @@ class TestCommonExceptions400(TestCase): ...@@ -139,6 +139,7 @@ class TestCommonExceptions400(TestCase):
@override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE) @override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE)
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message'))
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False}) @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
class TestInstructorAPIDenyLevels(ModuleStoreTestCase, LoginEnrollmentTestCase): class TestInstructorAPIDenyLevels(ModuleStoreTestCase, LoginEnrollmentTestCase):
""" """
...@@ -2228,6 +2229,7 @@ class TestInstructorAPIRegradeTask(ModuleStoreTestCase, LoginEnrollmentTestCase) ...@@ -2228,6 +2229,7 @@ class TestInstructorAPIRegradeTask(ModuleStoreTestCase, LoginEnrollmentTestCase)
@override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE) @override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE)
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message'))
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False}) @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
class TestInstructorSendEmail(ModuleStoreTestCase, LoginEnrollmentTestCase): class TestInstructorSendEmail(ModuleStoreTestCase, LoginEnrollmentTestCase):
""" """
......
""" """
Test for LMS instructor background task queue management Test for LMS instructor background task queue management
""" """
from mock import patch, Mock
from bulk_email.models import CourseEmail, SEND_TO_ALL from bulk_email.models import CourseEmail, SEND_TO_ALL
from courseware.tests.factories import UserFactory from courseware.tests.factories import UserFactory
from xmodule.modulestore.exceptions import ItemNotFoundError from xmodule.modulestore.exceptions import ItemNotFoundError
...@@ -158,6 +159,7 @@ class InstructorTaskModuleSubmitTest(InstructorTaskModuleTestCase): ...@@ -158,6 +159,7 @@ class InstructorTaskModuleSubmitTest(InstructorTaskModuleTestCase):
self._test_submit_task(submit_delete_problem_state_for_all_students) self._test_submit_task(submit_delete_problem_state_for_all_students)
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message'))
class InstructorTaskCourseSubmitTest(TestReportMixin, InstructorTaskCourseTestCase): class InstructorTaskCourseSubmitTest(TestReportMixin, InstructorTaskCourseTestCase):
"""Tests API methods that involve the submission of course-based background tasks.""" """Tests API methods that involve the submission of course-based background tasks."""
......
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