From 8ee682d44bfe7ca47cd142090fe5b71547f6c5b4 Mon Sep 17 00:00:00 2001 From: Usman Khalid <2200617@gmail.com> Date: Fri, 2 May 2014 19:53:26 +0500 Subject: [PATCH] Fixed and added more tests for bulk email. LMS-2565 --- lms/djangoapps/bulk_email/tests/test_email.py | 15 ++++++++ .../bulk_email/tests/test_err_handling.py | 1 + lms/djangoapps/instructor/tests/test_api.py | 3 ++ .../instructor/tests/test_legacy_email.py | 35 +++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/lms/djangoapps/bulk_email/tests/test_email.py b/lms/djangoapps/bulk_email/tests/test_email.py index 4531c7a374..d1b8e380e9 100644 --- a/lms/djangoapps/bulk_email/tests/test_email.py +++ b/lms/djangoapps/bulk_email/tests/test_email.py @@ -41,6 +41,7 @@ class MockCourseEmailResult(object): @override_settings(MODULESTORE=TEST_DATA_MONGO_MODULESTORE) +@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False}) class TestEmailSendFromDashboard(ModuleStoreTestCase): """ Test that emails send correctly. @@ -88,6 +89,20 @@ class TestEmailSendFromDashboard(ModuleStoreTestCase): """ patch.stopall() + @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': True}) + def test_email_disabled(self): + """ + Test response when email is disabled for course. + """ + test_email = { + 'action': 'Send email', + 'to_option': 'myself', + 'subject': 'test subject for myself', + 'message': 'test message for myself' + } + response = self.client.post(self.url, test_email) + self.assertContains(response, "Email is not enabled for this course.") + def test_send_to_self(self): """ Make sure email send to myself goes to myself. diff --git a/lms/djangoapps/bulk_email/tests/test_err_handling.py b/lms/djangoapps/bulk_email/tests/test_err_handling.py index f9365c86ef..f7b8b32d1f 100644 --- a/lms/djangoapps/bulk_email/tests/test_err_handling.py +++ b/lms/djangoapps/bulk_email/tests/test_err_handling.py @@ -38,6 +38,7 @@ class EmailTestException(Exception): @override_settings(MODULESTORE=TEST_DATA_MONGO_MODULESTORE) +@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False}) class TestEmailErrors(ModuleStoreTestCase): """ Test that errors from sending email are handled properly. diff --git a/lms/djangoapps/instructor/tests/test_api.py b/lms/djangoapps/instructor/tests/test_api.py index daf46a3db5..e84c79cfce 100644 --- a/lms/djangoapps/instructor/tests/test_api.py +++ b/lms/djangoapps/instructor/tests/test_api.py @@ -11,6 +11,7 @@ from urllib import quote from django.test import TestCase from nose.tools import raises from mock import Mock, patch +from django.conf import settings from django.test.utils import override_settings from django.core.urlresolvers import reverse from django.http import HttpRequest, HttpResponse @@ -99,6 +100,7 @@ class TestCommonExceptions400(unittest.TestCase): @override_settings(MODULESTORE=TEST_DATA_MIXED_MODULESTORE) +@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False}) class TestInstructorAPIDenyLevels(ModuleStoreTestCase, LoginEnrollmentTestCase): """ Ensure that users cannot access endpoints they shouldn't be able to. @@ -1570,6 +1572,7 @@ class TestInstructorAPIRegradeTask(ModuleStoreTestCase, LoginEnrollmentTestCase) @override_settings(MODULESTORE=TEST_DATA_MIXED_MODULESTORE) +@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False}) class TestInstructorSendEmail(ModuleStoreTestCase, LoginEnrollmentTestCase): """ Checks that only instructors have access to email endpoints, and that diff --git a/lms/djangoapps/instructor/tests/test_legacy_email.py b/lms/djangoapps/instructor/tests/test_legacy_email.py index 0e2642c829..823d112b3f 100644 --- a/lms/djangoapps/instructor/tests/test_legacy_email.py +++ b/lms/djangoapps/instructor/tests/test_legacy_email.py @@ -108,3 +108,38 @@ class TestInstructorDashboardEmailView(ModuleStoreTestCase): # 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) + + @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True}) + def test_send_mail_unauthorized(self): + """ Test 'Send email' action returns an error if course is not authorized to send email. """ + + response = self.client.post( + self.url, { + 'action': 'Send email', + 'to_option': 'all', + 'subject': "Welcome to the course!", + 'message': "Lets start with an introduction!" + } + ) + self.assertContains(response, "Email is not enabled for this course.") + + @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True}) + def test_send_mail_authorized(self): + """ Test 'Send email' action when course is authorized to send email. """ + + course_authorization = CourseAuthorization(course_id=self.course.id, email_enabled=True) + course_authorization.save() + + session = self.client.session + session[u'idash_mode:{0}'.format(self.course.location.course_id)] = 'Email' + session.save() + + response = self.client.post( + self.url, { + 'action': 'Send email', + 'to_option': 'all', + 'subject': 'Welcome to the course!', + 'message': 'Lets start with an introduction!', + } + ) + self.assertContains(response, "Your email was successfully queued for sending.")