From 1ad643acc55a79cf8e8a4b5bce1f75f73f9fb385 Mon Sep 17 00:00:00 2001 From: Adam Palay Date: Fri, 19 May 2017 12:10:14 -0400 Subject: [PATCH] allow sending emails to course modes even if they've expired (EDUCATOR-364) --- lms/djangoapps/bulk_email/models.py | 2 +- .../bulk_email/tests/test_models.py | 46 +++++++++++++------ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lms/djangoapps/bulk_email/models.py b/lms/djangoapps/bulk_email/models.py index 4e13cad377..0f879735c5 100644 --- a/lms/djangoapps/bulk_email/models.py +++ b/lms/djangoapps/bulk_email/models.py @@ -217,7 +217,7 @@ class CourseModeTarget(Target): if mode_slug is None: raise ValueError("Cannot create a CourseModeTarget without specifying a mode_slug.") try: - validate_course_mode(unicode(course_id), mode_slug) + validate_course_mode(unicode(course_id), mode_slug, include_expired=True) except CourseModeNotFoundError: raise ValueError( "Track {track} does not exist in course {course_id}".format( diff --git a/lms/djangoapps/bulk_email/tests/test_models.py b/lms/djangoapps/bulk_email/tests/test_models.py index 9fce4ebf4d..2886f58e86 100644 --- a/lms/djangoapps/bulk_email/tests/test_models.py +++ b/lms/djangoapps/bulk_email/tests/test_models.py @@ -1,14 +1,9 @@ """ Unit tests for bulk-email-related models. """ -from django.test import TestCase -from django.core.management import call_command - -from student.tests.factories import UserFactory - -from mock import patch, Mock -from nose.plugins.attrib import attr +import datetime +import ddt from bulk_email.models import ( CourseEmail, SEND_TO_COHORT, @@ -19,13 +14,22 @@ from bulk_email.models import ( BulkEmailFlag ) from course_modes.models import CourseMode -from openedx.core.djangoapps.course_groups.models import CourseCohort +from django.core.management import call_command +from django.test import TestCase +from mock import patch, Mock +from nose.plugins.attrib import attr from opaque_keys.edx.keys import CourseKey +from student.tests.factories import UserFactory +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from xmodule.modulestore.tests.factories import CourseFactory + +from openedx.core.djangoapps.course_groups.models import CourseCohort +@ddt.ddt @attr(shard=1) @patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True)) -class CourseEmailTest(TestCase): +class CourseEmailTest(ModuleStoreTestCase): """Test the CourseEmail model.""" def test_creation(self): @@ -69,16 +73,28 @@ class CourseEmailTest(TestCase): with self.assertRaises(ValueError): CourseEmail.create(course_id, sender, to_option, subject, html_message) - def test_track_target(self): - course_id = CourseKey.from_string('abc/123/doremi') + @ddt.data( + datetime.datetime(1999, 1, 1), + datetime.datetime(datetime.MAXYEAR, 1, 1), + ) + def test_track_target(self, expiration_datetime): + """ + Tests that emails can be sent to a specific track. Also checks that + emails can be sent to an expired track (EDUCATOR-364) + """ + course = CourseFactory.create() + course_id = course.id sender = UserFactory.create() to_option = 'track:test' subject = "dummy subject" html_message = "dummy message" - CourseMode.objects.create(mode_slug='test', mode_display_name='Test', course_id=course_id) - with patch('bulk_email.models.validate_course_mode'): - # we don't have a real course, so validation will fail. Mock it out! - email = CourseEmail.create(course_id, sender, [to_option], subject, html_message) + CourseMode.objects.create( + mode_slug='test', + mode_display_name='Test', + course_id=course_id, + expiration_datetime=expiration_datetime, + ) + email = CourseEmail.create(course_id, sender, [to_option], subject, html_message) self.assertEqual(len(email.targets.all()), 1) target = email.targets.all()[0] self.assertEqual(target.target_type, SEND_TO_TRACK)