test_course_optout.py 4.93 KB
Newer Older
1
# -*- coding: utf-8 -*-
2 3 4 5
"""
Unit tests for student optouts from course email
"""
import json
6
from mock import patch, Mock
7
from nose.plugins.attrib import attr
8 9

from django.core import mail
10
from django.core.management import call_command
11
from django.core.urlresolvers import reverse
12 13 14
from django.conf import settings

from student.tests.factories import UserFactory, AdminFactory, CourseEnrollmentFactory
15
from student.models import CourseEnrollment
16 17
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory
18
from bulk_email.models import BulkEmailFlag
19

20

21
@attr('shard_1')
22
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True))
23
class TestOptoutCourseEmails(ModuleStoreTestCase):
24 25 26 27
    """
    Test that optouts are referenced in sending course email.
    """

28
    def setUp(self):
29
        super(TestOptoutCourseEmails, self).setUp()
30
        course_title = u"ẗëṡẗ title イ乇丂イ ᄊ乇丂丂ムg乇 キo尺 ムレレ тэѕт мэѕѕаБэ"
31
        self.course = CourseFactory.create(display_name=course_title)
32 33 34 35
        self.instructor = AdminFactory.create()
        self.student = UserFactory.create()
        CourseEnrollmentFactory.create(user=self.student, course_id=self.course.id)

36 37 38
        # load initial content (since we don't run migrations as part of tests):
        call_command("loaddata", "course_email_template.json")

39 40
        self.client.login(username=self.student.username, password="test")

41
        self.send_mail_url = reverse('send_email', kwargs={'course_id': self.course.id.to_deprecated_string()})
42
        self.success_content = {
43
            'course_id': self.course.id.to_deprecated_string(),
44 45
            'success': True,
        }
46 47 48 49 50
        BulkEmailFlag.objects.create(enabled=True, require_course_email_auth=False)

    def tearDown(self):
        super(TestOptoutCourseEmails, self).tearDown()
        BulkEmailFlag.objects.all().delete()
51

52 53 54
    def navigate_to_email_view(self):
        """Navigate to the instructor dash's email view"""
        # Pull up email view on instructor dashboard
55
        url = reverse('instructor_dashboard', kwargs={'course_id': self.course.id.to_deprecated_string()})
56
        response = self.client.get(url)
57
        email_section = '<div class="vert-left send-email" id="section-send-email">'
58
        # If this fails, it is likely because BulkEmailFlag.is_enabled() is set to False
59
        self.assertTrue(email_section in response.content)
60

61 62 63 64 65
    def test_optout_course(self):
        """
        Make sure student does not receive course email after opting out.
        """
        url = reverse('change_email_settings')
66 67
        # 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'
68
        response = self.client.post(url, {'course_id': self.course.id.to_deprecated_string()})
69 70 71
        self.assertEquals(json.loads(response.content), {'success': True})

        self.client.logout()
72

73
        self.client.login(username=self.instructor.username, password="test")
74
        self.navigate_to_email_view()
75

76 77
        test_email = {
            'action': 'Send email',
78
            'send_to': '["myself", "staff", "learners"]',
79 80 81
            'subject': 'test subject for all',
            'message': 'test message for all'
        }
82 83
        response = self.client.post(self.send_mail_url, test_email)
        self.assertEquals(json.loads(response.content), self.success_content)
84

85 86 87
        # Assert that self.student.email not in mail.to, outbox should only contain "myself" target
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to[0], self.instructor.email)
88 89 90 91 92 93

    def test_optin_course(self):
        """
        Make sure student receives course email after opting in.
        """
        url = reverse('change_email_settings')
94
        response = self.client.post(url, {'course_id': self.course.id.to_deprecated_string(), 'receive_emails': 'on'})
95 96 97
        self.assertEquals(json.loads(response.content), {'success': True})

        self.client.logout()
98

99 100
        self.assertTrue(CourseEnrollment.is_enrolled(self.student, self.course.id))

101
        self.client.login(username=self.instructor.username, password="test")
102
        self.navigate_to_email_view()
103

104 105
        test_email = {
            'action': 'Send email',
106
            'send_to': '["myself", "staff", "learners"]',
107 108 109
            'subject': 'test subject for all',
            'message': 'test message for all'
        }
110 111
        response = self.client.post(self.send_mail_url, test_email)
        self.assertEquals(json.loads(response.content), self.success_content)
112

113 114 115 116 117
        # Assert that self.student.email in mail.to, along with "myself" target
        self.assertEqual(len(mail.outbox), 2)
        sent_addresses = [message.to[0] for message in mail.outbox]
        self.assertIn(self.student.email, sent_addresses)
        self.assertIn(self.instructor.email, sent_addresses)