From 038d68d53cc2403d10cef6bf1749c5f74045de26 Mon Sep 17 00:00:00 2001 From: Thomas Tracy Date: Thu, 2 Dec 2021 13:11:55 -0500 Subject: [PATCH] feat: [MICROBA-1531] bulk email deny list logic (#29465) * feat: [MICROBA-1531] bulk_email deny list logic --- lms/djangoapps/bulk_email/api.py | 3 ++- lms/djangoapps/instructor/tests/test_email.py | 18 +++++++++++++++++- .../instructor/views/instructor_dashboard.py | 7 ++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lms/djangoapps/bulk_email/api.py b/lms/djangoapps/bulk_email/api.py index 35ca38eb6c..5c2e40ee64 100644 --- a/lms/djangoapps/bulk_email/api.py +++ b/lms/djangoapps/bulk_email/api.py @@ -10,6 +10,7 @@ from django.conf import settings from django.urls import reverse from lms.djangoapps.bulk_email.models_api import ( + is_bulk_email_disabled_for_course, is_bulk_email_enabled_for_course, is_bulk_email_feature_enabled, is_user_opted_out_for_course @@ -30,7 +31,7 @@ def get_emails_enabled(user, course_id): (bool): True if emails are enabled for the course associated with course_id for the user; False otherwise """ - if is_bulk_email_feature_enabled(course_id=course_id): + if is_bulk_email_feature_enabled(course_id=course_id) and not is_bulk_email_disabled_for_course(course_id): return not is_user_opted_out_for_course(user=user, course_id=course_id) return None diff --git a/lms/djangoapps/instructor/tests/test_email.py b/lms/djangoapps/instructor/tests/test_email.py index 1b2fc5e980..65f98cd6be 100644 --- a/lms/djangoapps/instructor/tests/test_email.py +++ b/lms/djangoapps/instructor/tests/test_email.py @@ -11,10 +11,12 @@ from opaque_keys.edx.keys import CourseKey from common.djangoapps.student.tests.factories import AdminFactory from lms.djangoapps.bulk_email.api import is_bulk_email_enabled_for_course, is_bulk_email_feature_enabled -from lms.djangoapps.bulk_email.models import BulkEmailFlag, CourseAuthorization +from lms.djangoapps.bulk_email.models import BulkEmailFlag, CourseAuthorization, DisabledCourse from xmodule.modulestore.tests.django_utils import TEST_DATA_MIXED_MODULESTORE, SharedModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory +from lms.djangoapps.bulk_email.models_api import is_bulk_email_disabled_for_course + class TestNewInstructorDashboardEmailViewMongoBacked(SharedModuleStoreTestCase): """ @@ -94,6 +96,20 @@ class TestNewInstructorDashboardEmailViewMongoBacked(SharedModuleStoreTestCase): response = self.client.get(self.url) self.assertContains(response, self.email_link) + def test_course_authorized_and_on_deny_list(self): + BulkEmailFlag.objects.create(enabled=True, require_course_email_auth=True) + # Authorize the course to use email + cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True) + cauth.save() + # Disabled the course to use email + disabled_course = DisabledCourse(course_id=self.course.id) + disabled_course.save() + # Assert that instructor email is disabled for this course + assert is_bulk_email_disabled_for_course(self.course.id) + # Assert that the URL for the email view is not in the response + response = self.client.get(self.url) + self.assertNotContains(response, self.email_link) + # Flag is disabled, but course is authorized def test_course_authorized_feature_off(self): BulkEmailFlag.objects.create(enabled=False, require_course_email_auth=True) diff --git a/lms/djangoapps/instructor/views/instructor_dashboard.py b/lms/djangoapps/instructor/views/instructor_dashboard.py index 73f922f3be..be88a9ca0b 100644 --- a/lms/djangoapps/instructor/views/instructor_dashboard.py +++ b/lms/djangoapps/instructor/views/instructor_dashboard.py @@ -38,6 +38,7 @@ from common.djangoapps.student.roles import ( ) from common.djangoapps.util.json_request import JsonResponse from lms.djangoapps.bulk_email.api import is_bulk_email_feature_enabled +from lms.djangoapps.bulk_email.models_api import is_bulk_email_disabled_for_course from lms.djangoapps.certificates import api as certs_api from lms.djangoapps.certificates.data import CertificateStatuses from lms.djangoapps.certificates.models import ( @@ -177,7 +178,11 @@ def instructor_dashboard_2(request, course_id): # lint-amnesty, pylint: disable sections.insert(3, _section_extensions(course)) # Gate access to course email by feature flag & by course-specific authorization - if is_bulk_email_feature_enabled(course_key) and (access['staff'] or access['instructor']): + if ( + is_bulk_email_feature_enabled(course_key) and not + is_bulk_email_disabled_for_course(course_key) and + (access['staff'] or access['instructor']) + ): sections.append(_section_send_email(course, access)) # Gate access to Special Exam tab depending if either timed exams or proctored exams