def test_bad_to_option(self): course_id = CourseKey.from_string('abc/123/doremi') sender = UserFactory.create() to_option = "fake" subject = "dummy subject" html_message = "<html>dummy message</html>" with pytest.raises(ValueError): CourseEmail.create(course_id, sender, to_option, subject, html_message)
def test_nonexistent_course(self): """ Tests exception when the course in the email doesn't exist """ course_id = CourseLocator("I", "DONT", "EXIST") email = CourseEmail(course_id=course_id) email.save() entry = InstructorTask.create(course_id, "task_type", "task_key", "task_input", self.instructor) task_input = {"email_id": email.id} with pytest.raises(CourseRunNotFound): perform_delegate_email_batches(entry.id, course_id, task_input, "action_name")
def test_nonexistent_course(self): """ Tests exception when the course in the email doesn't exist """ course_id = CourseLocator("I", "DONT", "EXIST") email = CourseEmail(course_id=course_id) email.save() entry = InstructorTask.create(course_id, "task_type", "task_key", "task_input", self.instructor) task_input = {"email_id": email.id} # (?i) is a regex for ignore case with self.assertRaisesRegex(ValueError, r"(?i)course not found"): perform_delegate_email_batches(entry.id, course_id, task_input, "action_name")
def _define_course_email(self): """Create CourseEmail object for testing.""" course_email = CourseEmail.create( self.course.id, self.instructor, [SEND_TO_MYSELF, SEND_TO_STAFF, SEND_TO_LEARNERS], "Test Subject", "<p>This is a test message</p>") return course_email.id
def test_track_target_with_free_mode(self, free_mode): """ Tests that when emails are sent to a free track the track display should not contain currency. """ course = CourseFactory.create() mode_display_name = free_mode.capitalize course_id = course.id sender = UserFactory.create() to_option = f'track:{free_mode}' subject = "dummy subject" html_message = "<html>dummy message</html>" CourseMode.objects.create( mode_slug=free_mode, mode_display_name=mode_display_name, course_id=course_id, ) email = CourseEmail.create(course_id, sender, [to_option], subject, html_message) assert len(email.targets.all()) == 1 target = email.targets.all()[0] assert target.target_type == SEND_TO_TRACK assert target.short_display() == f'track-{free_mode}' assert target.long_display() == f'Course mode: {mode_display_name}'
def create_course_email(course_id, sender, targets, subject, html_message, text_message=None, template_name=None, from_addr=None): """ Python API for creating a new CourseEmail instance. Args: course_id (CourseKey): The CourseKey of the course. sender (String): Email author. targets (Target): Recipient groups the message should be sent to (e.g. SEND_TO_MYSELF) subject (String)): Email subject. html_message (String): Email body. Includes HTML markup. text_message (String, optional): Plaintext version of email body. Defaults to None. template_name (String, optional): Name of custom email template to use. Defaults to None. from_addr (String, optional): Custom sending address, if desired. Defaults to None. Returns: CourseEmail: Returns the created CourseEmail instance. """ try: course_email = CourseEmail.create( course_id, sender, targets, subject, html_message, text_message=text_message, template_name=template_name, from_addr=from_addr ) return course_email except ValueError as err: log.exception(f"Cannot create course email for {course_id} requested by user {sender} for targets {targets}") raise ValueError from err
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 = "<html>dummy message</html>" 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) self.assertEqual(target.short_display(), 'track-test') self.assertEqual(target.long_display(), 'Course mode: Test, Currency: usd')
def test_track_target_with_free_mode(self, free_mode): """ Tests that when emails are sent to a free track the track display should not contain currency. """ course = CourseFactory.create() mode_display_name = free_mode.capitalize course_id = course.id sender = UserFactory.create() to_option = 'track:{}'.format(free_mode) subject = "dummy subject" html_message = "<html>dummy message</html>" CourseMode.objects.create( mode_slug=free_mode, mode_display_name=mode_display_name, course_id=course_id, ) 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) self.assertEqual(target.short_display(), 'track-{}'.format(free_mode)) self.assertEqual(target.long_display(), u'Course mode: {}'.format(mode_display_name))
def test_nonexistent_grouping(self, target_type): """ Tests exception when the cohort or course mode doesn't exist """ with self.assertRaisesRegex(ValueError, '.* IDONTEXIST does not exist .*'): email = CourseEmail.create( # pylint: disable=unused-variable self.course.id, self.instructor, [f"{target_type}:IDONTEXIST"], "re: subject", "dummy body goes here")
def test_nonexistent_to_option(self): """ Tests exception when the to_option in the email doesn't exist """ with self.assertRaisesRegex( ValueError, 'Course email being sent to unrecognized target: "IDONTEXIST" *' ): email = CourseEmail.create( # pylint: disable=unused-variable self.course.id, self.instructor, ["IDONTEXIST"], "re: subject", "dummy body goes here")
def test_creation(self): course_id = CourseKey.from_string('abc/123/doremi') sender = UserFactory.create() to_option = SEND_TO_STAFF subject = "dummy subject" html_message = "<html>dummy message</html>" email = CourseEmail.create(course_id, sender, [to_option], subject, html_message) assert email.course_id == course_id assert SEND_TO_STAFF in [target.target_type for target in email.targets.all()] assert email.subject == subject assert email.html_message == html_message assert email.sender == sender
def _define_course_email(self): """Create CourseEmail object for testing.""" # TODO: convert to use bulk_email app's `create_course_email` API function and remove direct import and use of # bulk_email model course_email = CourseEmail.create( self.course.id, self.instructor, [SEND_TO_MYSELF, SEND_TO_STAFF, SEND_TO_LEARNERS], "Test Subject", "<p>This is a test message</p>" ) return course_email.id
def test_wrong_course_id_in_email(self): """ Tests exception when the course_id in CourseEmail is not the same as one explicitly passed in. """ email = CourseEmail.create(CourseLocator("bogus", "course", "id"), self.instructor, [SEND_TO_MYSELF], "re: subject", "dummy body goes here") entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor) task_input = {"email_id": email.id} with self.assertRaisesRegex(ValueError, 'does not match email value'): perform_delegate_email_batches(entry.id, self.course.id, task_input, "action_name")
def test_cohort_target(self): course_id = CourseKey.from_string('abc/123/doremi') sender = UserFactory.create() to_option = 'cohort:test cohort' subject = "dummy subject" html_message = "<html>dummy message</html>" CourseCohort.create(cohort_name='test cohort', course_id=course_id) email = CourseEmail.create(course_id, sender, [to_option], subject, html_message) assert len(email.targets.all()) == 1 target = email.targets.all()[0] assert target.target_type == SEND_TO_COHORT assert target.short_display() == 'cohort-test cohort' assert target.long_display() == 'Cohort: test cohort'
def test_creation(self): course_id = CourseKey.from_string('abc/123/doremi') sender = UserFactory.create() to_option = SEND_TO_STAFF subject = "dummy subject" html_message = "<html>dummy message</html>" email = CourseEmail.create(course_id, sender, [to_option], subject, html_message) self.assertEqual(email.course_id, course_id) self.assertIn(SEND_TO_STAFF, [target.target_type for target in email.targets.all()]) self.assertEqual(email.subject, subject) self.assertEqual(email.html_message, html_message) self.assertEqual(email.sender, sender)
def test_creation_with_optional_attributes(self): course_id = CourseKey.from_string('abc/123/doremi') sender = UserFactory.create() to_option = SEND_TO_STAFF subject = "dummy subject" html_message = "<html>dummy message</html>" template_name = "branded_template" from_addr = "*****@*****.**" email = CourseEmail.create( course_id, sender, [to_option], subject, html_message, template_name=template_name, from_addr=from_addr ) assert email.course_id == course_id assert email.targets.all()[0].target_type == SEND_TO_STAFF assert email.subject == subject assert email.html_message == html_message assert email.sender == sender assert email.template_name == template_name assert email.from_addr == from_addr