Moves ENABLE_INSTRUCTOR_EMAIL and REQUIRE_COURSE_EMAIL_AUTH from settings files
to admin-accessible configuration models. This allows for the bulk email settings
to be modified without a new AMI deploy. See TNL-4504.
Also updates tests:
-python tests mock out the new configurations in place of the old settings
-lettuce test has been moved to bokchoy
(note that there was some loss of coverage here - the lettuce tests had
been doing some voodoo to allow for cross-process inspection of emails
messages being "sent" by the server, from the client! In discussion with
testeng, this seems outside the realm of a visual acceptance test. So,
the bokchoy test simply confirm the successful queueing of the message,
and leaves the validation of sending messages to the relevant unit tests.)
-bok choy fixture has been added, to replace the settings in acceptance.py
-lettuce and bok choy databases have been updated to reflect the backend changes
The new default is to have bulk_email disabled, we'll need to call this out in the
next OpenEdx release to ensure administrators enable this feature if needed.
116 lines
4.7 KiB
Python
116 lines
4.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Unit tests for student optouts from course email
|
|
"""
|
|
import json
|
|
from mock import patch, Mock
|
|
from nose.plugins.attrib import attr
|
|
|
|
from django.core import mail
|
|
from django.core.management import call_command
|
|
from django.core.urlresolvers import reverse
|
|
from django.conf import settings
|
|
|
|
from student.tests.factories import UserFactory, AdminFactory, CourseEnrollmentFactory
|
|
from student.models import CourseEnrollment
|
|
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
|
|
from xmodule.modulestore.tests.factories import CourseFactory
|
|
from bulk_email.models import BulkEmailFlag
|
|
|
|
|
|
@attr('shard_1')
|
|
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True))
|
|
class TestOptoutCourseEmails(ModuleStoreTestCase):
|
|
"""
|
|
Test that optouts are referenced in sending course email.
|
|
"""
|
|
|
|
def setUp(self):
|
|
super(TestOptoutCourseEmails, self).setUp()
|
|
course_title = u"ẗëṡẗ title イ乇丂イ ᄊ乇丂丂ムg乇 キo尺 ムレレ тэѕт мэѕѕаБэ"
|
|
self.course = CourseFactory.create(display_name=course_title)
|
|
self.instructor = AdminFactory.create()
|
|
self.student = UserFactory.create()
|
|
CourseEnrollmentFactory.create(user=self.student, course_id=self.course.id)
|
|
|
|
# load initial content (since we don't run migrations as part of tests):
|
|
call_command("loaddata", "course_email_template.json")
|
|
|
|
self.client.login(username=self.student.username, password="test")
|
|
|
|
self.send_mail_url = reverse('send_email', kwargs={'course_id': self.course.id.to_deprecated_string()})
|
|
self.success_content = {
|
|
'course_id': self.course.id.to_deprecated_string(),
|
|
'success': True,
|
|
}
|
|
BulkEmailFlag.objects.create(enabled=True, require_course_email_auth=False)
|
|
|
|
def tearDown(self):
|
|
super(TestOptoutCourseEmails, self).tearDown()
|
|
BulkEmailFlag.objects.all().delete()
|
|
|
|
def navigate_to_email_view(self):
|
|
"""Navigate to the instructor dash's email view"""
|
|
# Pull up email view on instructor dashboard
|
|
url = reverse('instructor_dashboard', kwargs={'course_id': self.course.id.to_deprecated_string()})
|
|
response = self.client.get(url)
|
|
email_section = '<div class="vert-left send-email" id="section-send-email">'
|
|
# If this fails, it is likely because BulkEmailFlag.is_enabled() is set to False
|
|
self.assertTrue(email_section in response.content)
|
|
|
|
def test_optout_course(self):
|
|
"""
|
|
Make sure student does not receive course email after opting out.
|
|
"""
|
|
url = reverse('change_email_settings')
|
|
# This is a checkbox, so on the post of opting out (that is, an Un-check of the box),
|
|
# the Post that is sent will not contain 'receive_emails'
|
|
response = self.client.post(url, {'course_id': self.course.id.to_deprecated_string()})
|
|
self.assertEquals(json.loads(response.content), {'success': True})
|
|
|
|
self.client.logout()
|
|
|
|
self.client.login(username=self.instructor.username, password="test")
|
|
self.navigate_to_email_view()
|
|
|
|
test_email = {
|
|
'action': 'Send email',
|
|
'send_to': 'all',
|
|
'subject': 'test subject for all',
|
|
'message': 'test message for all'
|
|
}
|
|
response = self.client.post(self.send_mail_url, test_email)
|
|
self.assertEquals(json.loads(response.content), self.success_content)
|
|
|
|
# Assert that self.student.email not in mail.to, outbox should be empty
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
def test_optin_course(self):
|
|
"""
|
|
Make sure student receives course email after opting in.
|
|
"""
|
|
url = reverse('change_email_settings')
|
|
response = self.client.post(url, {'course_id': self.course.id.to_deprecated_string(), 'receive_emails': 'on'})
|
|
self.assertEquals(json.loads(response.content), {'success': True})
|
|
|
|
self.client.logout()
|
|
|
|
self.assertTrue(CourseEnrollment.is_enrolled(self.student, self.course.id))
|
|
|
|
self.client.login(username=self.instructor.username, password="test")
|
|
self.navigate_to_email_view()
|
|
|
|
test_email = {
|
|
'action': 'Send email',
|
|
'send_to': 'all',
|
|
'subject': 'test subject for all',
|
|
'message': 'test message for all'
|
|
}
|
|
response = self.client.post(self.send_mail_url, test_email)
|
|
self.assertEquals(json.loads(response.content), self.success_content)
|
|
|
|
# Assert that self.student.email in mail.to
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
self.assertEqual(len(mail.outbox[0].to), 1)
|
|
self.assertEquals(mail.outbox[0].to[0], self.student.email)
|