test_email.py 6.61 KB
Newer Older
1
"""
2 3
Unit tests for email feature flag in new instructor dashboard.
Additionally tests that bulk email is always disabled for
4 5
non-Mongo backed courses, regardless of email feature flag, and
that the view is conditionally available when Course Auth is turned on.
6 7 8
"""
from django.conf import settings
from django.core.urlresolvers import reverse
9 10 11
from django.test.utils import override_settings
from mock import patch
from opaque_keys.edx.locations import SlashSeparatedCourseKey
12

13
from bulk_email.models import CourseAuthorization
14
from xmodule.modulestore.tests.django_utils import TEST_DATA_MIXED_TOY_MODULESTORE, ModuleStoreTestCase
15
from student.tests.factories import AdminFactory
16
from xmodule.modulestore.tests.factories import CourseFactory
17

18

19
class TestNewInstructorDashboardEmailViewMongoBacked(ModuleStoreTestCase):
20
    """
21 22
    Check for email view on the new instructor dashboard
    for Mongo-backed courses
23 24
    """
    def setUp(self):
25
        super(TestNewInstructorDashboardEmailViewMongoBacked, self).setUp()
26 27 28 29 30 31
        self.course = CourseFactory.create()

        # Create instructor account
        instructor = AdminFactory.create()
        self.client.login(username=instructor.username, password="test")

32
        # URL for instructor dash
Calen Pennington committed
33
        self.url = reverse('instructor_dashboard', kwargs={'course_id': self.course.id.to_deprecated_string()})
34
        # URL for email view
35
        self.email_link = '<a href="" data-section="send_email">Email</a>'
36

37 38 39 40 41 42
    def tearDown(self):
        """
        Undo all patches.
        """
        patch.stopall()

43 44 45
    # In order for bulk email to work, we must have both the ENABLE_INSTRUCTOR_EMAIL_FLAG
    # set to True and for the course to be Mongo-backed.
    # The flag is enabled and the course is Mongo-backed (should work)
46
    @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
47
    def test_email_flag_true_mongo_true(self):
48 49 50
        # Assert that instructor email is enabled for this course - since REQUIRE_COURSE_EMAIL_AUTH is False,
        # all courses should be authorized to use email.
        self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id))
51
        # Assert that the URL for the email view is in the response
52
        response = self.client.get(self.url)
53
        self.assertIn(self.email_link, response.content)
54 55 56

        send_to_label = '<label for="id_to">Send to:</label>'
        self.assertTrue(send_to_label in response.content)
57
        self.assertEqual(response.status_code, 200)
58

59
    # The course is Mongo-backed but the flag is disabled (should not work)
60
    @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': False})
61
    def test_email_flag_false_mongo_true(self):
62
        # Assert that the URL for the email view is not in the response
63 64
        response = self.client.get(self.url)
        self.assertFalse(self.email_link in response.content)
65

66
    # Flag is enabled, but we require course auth and haven't turned it on for this course
67
    @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': True})
68 69 70 71 72 73 74 75
    def test_course_not_authorized(self):
        # Assert that instructor email is not enabled for this course
        self.assertFalse(CourseAuthorization.instructor_email_enabled(self.course.id))
        # Assert that the URL for the email view is not in the response
        response = self.client.get(self.url)
        self.assertFalse(self.email_link in response.content)

    # Flag is enabled, we require course auth and turn it on for this course
76
    @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': True})
77 78 79 80 81 82 83 84 85 86 87 88 89
    def test_course_authorized(self):
        # Assert that instructor email is not enabled for this course
        self.assertFalse(CourseAuthorization.instructor_email_enabled(self.course.id))
        # Assert that the URL for the email view is not in the response
        response = self.client.get(self.url)
        self.assertFalse(self.email_link in response.content)

        # Authorize the course to use email
        cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True)
        cauth.save()

        # Assert that instructor email is enabled for this course
        self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id))
90
        # Assert that the URL for the email view is in the response
91 92 93 94
        response = self.client.get(self.url)
        self.assertTrue(self.email_link in response.content)

    # Flag is disabled, but course is authorized
95
    @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': False, 'REQUIRE_COURSE_EMAIL_AUTH': True})
96 97 98 99 100 101 102 103 104 105 106
    def test_course_authorized_feature_off(self):
        # Authorize the course to use email
        cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True)
        cauth.save()

        # Assert that instructor email IS enabled for this course
        self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id))
        # Assert that the URL for the email view IS NOT in the response
        response = self.client.get(self.url)
        self.assertFalse(self.email_link in response.content)

107

108 109 110 111
class TestNewInstructorDashboardEmailViewXMLBacked(ModuleStoreTestCase):
    """
    Check for email view on the new instructor dashboard
    """
112 113 114

    MODULESTORE = TEST_DATA_MIXED_TOY_MODULESTORE

115
    def setUp(self):
116
        super(TestNewInstructorDashboardEmailViewXMLBacked, self).setUp()
117
        self.course_key = SlashSeparatedCourseKey('edX', 'toy', '2012_Fall')
118

119 120 121
        # Create instructor account
        instructor = AdminFactory.create()
        self.client.login(username=instructor.username, password="test")
122

123
        # URL for instructor dash
Calen Pennington committed
124
        self.url = reverse('instructor_dashboard', kwargs={'course_id': self.course_key.to_deprecated_string()})
125 126
        # URL for email view
        self.email_link = '<a href="" data-section="send_email">Email</a>'
127

128 129
    # The flag is enabled, and since REQUIRE_COURSE_EMAIL_AUTH is False, all courses should
    # be authorized to use email. But the course is not Mongo-backed (should not work)
130
    @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
131 132 133
    def test_email_flag_true_mongo_false(self):
        response = self.client.get(self.url)
        self.assertFalse(self.email_link in response.content)
134

135
    # The flag is disabled and the course is not Mongo-backed (should not work)
136
    @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': False, 'REQUIRE_COURSE_EMAIL_AUTH': False})
137 138 139
    def test_email_flag_false_mongo_false(self):
        response = self.client.get(self.url)
        self.assertFalse(self.email_link in response.content)