test_course_optout.py 4.89 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

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

19

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

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

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

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

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

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

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

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

        self.client.logout()
71

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

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

84 85 86
        # 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)
87 88 89 90 91 92

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

        self.client.logout()
97

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

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

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

112 113 114 115 116
        # 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)