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

19

20
@attr('shard_1')
21
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message'))
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 30
        course_title = u"ẗëṡẗ title イ乇丂イ ᄊ乇丂丂ムg乇 キo尺 ムレレ тэѕт мэѕѕаБэ"
        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 45
            'success': True,
        }

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

55
    @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
56 57 58 59 60
    def test_optout_course(self):
        """
        Make sure student does not receive course email after opting out.
        """
        url = reverse('change_email_settings')
61 62
        # 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'
63
        response = self.client.post(url, {'course_id': self.course.id.to_deprecated_string()})
64 65 66
        self.assertEquals(json.loads(response.content), {'success': True})

        self.client.logout()
67

68
        self.client.login(username=self.instructor.username, password="test")
69
        self.navigate_to_email_view()
70

71 72
        test_email = {
            'action': 'Send email',
73
            'send_to': 'all',
74 75 76
            'subject': 'test subject for all',
            'message': 'test message for all'
        }
77 78
        response = self.client.post(self.send_mail_url, test_email)
        self.assertEquals(json.loads(response.content), self.success_content)
79

80
        # Assert that self.student.email not in mail.to, outbox should be empty
81 82
        self.assertEqual(len(mail.outbox), 0)

83
    @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
84 85 86 87 88
    def test_optin_course(self):
        """
        Make sure student receives course email after opting in.
        """
        url = reverse('change_email_settings')
89
        response = self.client.post(url, {'course_id': self.course.id.to_deprecated_string(), 'receive_emails': 'on'})
90 91 92
        self.assertEquals(json.loads(response.content), {'success': True})

        self.client.logout()
93

94 95
        self.assertTrue(CourseEnrollment.is_enrolled(self.student, self.course.id))

96
        self.client.login(username=self.instructor.username, password="test")
97
        self.navigate_to_email_view()
98

99 100
        test_email = {
            'action': 'Send email',
101
            'send_to': 'all',
102 103 104
            'subject': 'test subject for all',
            'message': 'test message for all'
        }
105 106
        response = self.client.post(self.send_mail_url, test_email)
        self.assertEquals(json.loads(response.content), self.success_content)
107

108
        # Assert that self.student.email in mail.to
109 110 111
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(len(mail.outbox[0].to), 1)
        self.assertEquals(mail.outbox[0].to[0], self.student.email)