def test_bad_to_option(self): course_id = SlashSeparatedCourseKey('abc', '123', 'doremi') sender = UserFactory.create() to_option = "fake" subject = "dummy subject" html_message = "<html>dummy message</html>" with self.assertRaises(ValueError): CourseEmail.create(course_id, sender, to_option, subject, html_message)
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 self.assertRaises(ValueError): CourseEmail.create(course_id, sender, to_option, subject, html_message)
def test_wrong_course_id_in_task(self): """ Tests exception when the course_id in task is not the same as one explicitly passed in. """ email = CourseEmail(course_id=self.course.id, to_option=SEND_TO_ALL) email.save() entry = InstructorTask.create("bogus/task/id", "task_type", "task_key", "task_input", self.instructor) task_input = {"email_id": email.id} with self.assertRaisesRegexp(ValueError, 'does not match task value'): perform_delegate_email_batches(entry.id, self.course.id, task_input, "action_name")
def test_nonexist_course(self, mock_log): """ Tests exception when the course in the email doesn't exist """ email = CourseEmail(course_id="I/DONT/EXIST") email.save() delegate_email_batches.delay(email.id, self.instructor.id) ((log_str, _), _) = mock_log.exception.call_args self.assertTrue(mock_log.exception.called) self.assertIn('get_course_by_id failed:', log_str)
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(course_id=SlashSeparatedCourseKey("bogus", "course", "id"), to_option=SEND_TO_ALL) email.save() entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor) task_input = {"email_id": email.id} # pylint: disable=no-member with self.assertRaisesRegexp(ValueError, 'does not match email value'): perform_delegate_email_batches(entry.id, self.course.id, task_input, "action_name") # pylint: disable=no-member
def test_wrong_course_id_in_task(self): """ Tests exception when the course_id in task is not the same as one explicitly passed in. """ email = CourseEmail(course_id=self.course.id, to_option=SEND_TO_ALL) email.save() entry = InstructorTask.create("bogus/task/id", "task_type", "task_key", "task_input", self.instructor) task_input = {"email_id": email.id} # pylint: disable=E1101 with self.assertRaisesRegexp(ValueError, "does not match task value"): perform_delegate_email_batches(entry.id, self.course.id, task_input, "action_name") # pylint: disable=E1101
def test_nonexistent_to_option(self): """ Tests exception when the to_option in the email doesn't exist """ email = CourseEmail(course_id=self.course.id, to_option="IDONTEXIST") email.save() entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor) task_input = {"email_id": email.id} # pylint: disable=no-member with self.assertRaisesRegexp(Exception, 'Unexpected bulk email TO_OPTION found: IDONTEXIST'): perform_delegate_email_batches(entry.id, self.course.id, task_input, "action_name") # pylint: disable=no-member
def test_nonexistent_course(self): """ Tests exception when the course in the email doesn't exist """ course_id = "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} # pylint: disable=E1101 with self.assertRaisesRegexp(ValueError, "Course not found"): perform_delegate_email_batches(entry.id, course_id, task_input, "action_name") # pylint: disable=E1101
def test_nonexist_to_option(self, mock_log): """ Tests exception when the to_option in the email doesn't exist """ email = CourseEmail(course_id=self.course.id, to_option="IDONTEXIST") email.save() delegate_email_batches.delay(email.id, self.instructor.id) ((log_str, opt_str), _) = mock_log.error.call_args self.assertTrue(mock_log.error.called) self.assertIn('Unexpected bulk email TO_OPTION found', log_str) self.assertEqual("IDONTEXIST", opt_str)
def test_nonexistent_course(self): """ Tests exception when the course in the email doesn't exist """ course_id = SlashSeparatedCourseKey("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} # pylint: disable=no-member # (?i) is a regex for ignore case with self.assertRaisesRegexp(ValueError, r"(?i)course not found"): perform_delegate_email_batches(entry.id, course_id, task_input, "action_name") # pylint: disable=no-member
def send_email(request, course_id): """ Send an email to self, staff, or everyone involved in a course. Query Parameters: - 'send_to' specifies what group the email should be sent to Options are defined by the CourseEmail model in lms/djangoapps/bulk_email/models.py - 'subject' specifies email's subject - 'message' specifies email's content """ course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id) if not bulk_email_is_enabled_for_course(course_id): return HttpResponseForbidden("Email is not enabled for this course.") send_to = request.POST.get("send_to") subject = request.POST.get("subject") message = request.POST.get("message") # Create the CourseEmail object. This is saved immediately, so that # any transaction that has been pending up to this point will also be # committed. email = CourseEmail.create(course_id, request.user, send_to, subject, message) # Submit the task, so that the correct InstructorTask object gets created (for monitoring purposes) instructor_task.api.submit_bulk_course_email(request, course_id, email.id) # pylint: disable=E1101 response_payload = { 'course_id': course_id.to_deprecated_string(), 'success': True, } return JsonResponse(response_payload)
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_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, Currencies: usd')
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 send_email(request, course_id): """ Send an email to self, staff, or everyone involved in a course. Query Parameters: - 'send_to' specifies what group the email should be sent to Options are defined by the CourseEmail model in lms/djangoapps/bulk_email/models.py - 'subject' specifies email's subject - 'message' specifies email's content """ if not bulk_email_is_enabled_for_course(course_id): return HttpResponseForbidden("Email is not enabled for this course.") send_to = request.POST.get("send_to") subject = request.POST.get("subject") message = request.POST.get("message") # Create the CourseEmail object. This is saved immediately, so that # any transaction that has been pending up to this point will also be # committed. email = CourseEmail.create(course_id, request.user, send_to, subject, message) # Submit the task, so that the correct InstructorTask object gets created (for monitoring purposes) instructor_task.api.submit_bulk_course_email(request, course_id, email.id) # pylint: disable=E1101 response_payload = { 'course_id': course_id, 'success': True, } return JsonResponse(response_payload)
def test_nonexistent_cohort(self): """ Tests exception when the cohort doesn't exist """ with self.assertRaisesRegexp(ValueError, 'Cohort IDONTEXIST does not exist *'): email = CourseEmail.create( # pylint: disable=unused-variable self.course.id, self.instructor, ["cohort:IDONTEXIST"], "re: subject", "dummy body goes here")
def test_nonexistent_grouping(self, target_type): """ Tests exception when the cohort or course mode doesn't exist """ with self.assertRaisesRegexp(ValueError, '.* IDONTEXIST does not exist .*'): email = CourseEmail.create( # pylint: disable=unused-variable self.course.id, self.instructor, [u"{}:IDONTEXIST".format(target_type)], "re: subject", "dummy body goes here")
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_nonexistent_to_option(self): """ Tests exception when the to_option in the email doesn't exist """ with self.assertRaisesRegexp( 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_nonexistent_cohort(self): """ Tests exception when the cohort doesn't exist """ with self.assertRaisesRegexp(ValueError, 'Cohort IDONTEXIST does not exist *'): email = CourseEmail.create( # pylint: disable=unused-variable self.course.id, self.instructor, ["cohort:IDONTEXIST"], "re: subject", "dummy body goes here" )
def test_creation(self): course_id = SlashSeparatedCourseKey('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.assertEquals(email.course_id, course_id) self.assertEquals(email.to_option, SEND_TO_STAFF) self.assertEquals(email.subject, subject) self.assertEquals(email.html_message, html_message) self.assertEquals(email.sender, sender)
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_nonexistent_grouping(self, target_type): """ Tests exception when the cohort or course mode doesn't exist """ with self.assertRaisesRegexp(ValueError, '.* IDONTEXIST does not exist .*'): email = CourseEmail.create( # pylint: disable=unused-variable self.course.id, self.instructor, [u"{}:IDONTEXIST".format(target_type)], "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.assertRaisesRegexp(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_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) self.assertEqual(len(email.targets.all()), 1) target = email.targets.all()[0] self.assertEqual(target.target_type, SEND_TO_COHORT) self.assertEqual(target.short_display(), 'cohort-test cohort') self.assertEqual(target.long_display(), 'Cohort: test cohort')
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.assertRaisesRegexp(ValueError, 'does not match email value'): perform_delegate_email_batches(entry.id, self.course.id, task_input, "action_name")
def test_track_target(self): course_id = CourseKey.from_string('abc/123/doremi') 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) 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) 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, Currencies: usd')
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.assertRaisesRegexp(ValueError, 'does not match email value'): perform_delegate_email_batches(entry.id, self.course.id, task_input, "action_name")
def test_creation_with_optional_attributes(self): course_id = SlashSeparatedCourseKey('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) self.assertEquals(email.course_id, course_id) self.assertEquals(email.to_option, SEND_TO_STAFF) self.assertEquals(email.subject, subject) self.assertEquals(email.html_message, html_message) self.assertEquals(email.sender, sender) self.assertEquals(email.template_name, template_name) self.assertEquals(email.from_addr, from_addr)
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 ) self.assertEqual(email.course_id, course_id) self.assertEqual(email.targets.all()[0].target_type, SEND_TO_STAFF) self.assertEqual(email.subject, subject) self.assertEqual(email.html_message, html_message) self.assertEqual(email.sender, sender) self.assertEqual(email.template_name, template_name) self.assertEqual(email.from_addr, from_addr)
def _create_input_entry(self, course_id=None): """ Creates a InstructorTask entry for testing. Overrides the base class version in that this creates CourseEmail. """ to_option = SEND_TO_ALL course_id = course_id or self.course.id course_email = CourseEmail.create(course_id, self.instructor, to_option, "Test Subject", "<p>This is a test message</p>") task_input = {'email_id': course_email.id} # pylint: disable=no-member task_id = str(uuid4()) instructor_task = InstructorTaskFactory.create( course_id=course_id, requester=self.instructor, task_input=json.dumps(task_input), task_key='dummy value', task_id=task_id, ) return instructor_task
def _create_input_entry(self, course_id=None): """ Creates a InstructorTask entry for testing. Overrides the base class version in that this creates CourseEmail. """ to_option = SEND_TO_ALL course_id = course_id or self.course.id course_email = CourseEmail.create(course_id, self.instructor, to_option, "Test Subject", "<p>This is a test message</p>") task_input = {'email_id': course_email.id} # pylint: disable=E1101 task_id = str(uuid4()) instructor_task = InstructorTaskFactory.create( course_id=course_id, requester=self.instructor, task_input=json.dumps(task_input), task_key='dummy value', task_id=task_id, ) return instructor_task
def _create_input_entry(self, course_id=None): """ Creates a InstructorTask entry for testing. Overrides the base class version in that this creates CourseEmail. """ targets = [SEND_TO_MYSELF, SEND_TO_STAFF, SEND_TO_LEARNERS] course_id = course_id or self.course.id course_email = CourseEmail.create( course_id, self.instructor, targets, "Test Subject", "<p>This is a test message</p>" ) task_input = {'email_id': course_email.id} task_id = str(uuid4()) instructor_task = InstructorTaskFactory.create( course_id=course_id, requester=self.instructor, task_input=json.dumps(task_input), task_key='dummy value', task_id=task_id, ) return instructor_task
def _create_input_entry(self, course_id=None): """ Creates a InstructorTask entry for testing. Overrides the base class version in that this creates CourseEmail. """ targets = [SEND_TO_MYSELF, SEND_TO_STAFF, SEND_TO_LEARNERS] course_id = course_id or self.course.id course_email = CourseEmail.create(course_id, self.instructor, targets, "Test Subject", "<p>This is a test message</p>") task_input = {'email_id': course_email.id} task_id = str(uuid4()) instructor_task = InstructorTaskFactory.create( course_id=course_id, requester=self.instructor, task_input=json.dumps(task_input), task_key='dummy value', task_id=task_id, ) return instructor_task
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(), 'Course mode: {}'.format(mode_display_name))
def _define_course_email(self): """Create CourseEmail object for testing.""" course_email = CourseEmail.create(self.course.id, self.instructor, SEND_TO_ALL, "Test Subject", "<p>This is a test message</p>") return course_email.id # pylint: disable=no-member
def _define_course_email(self): """Create CourseEmail object for testing.""" course_email = CourseEmail.create(self.course.id, self.instructor, SEND_TO_ALL, "Test Subject", "<p>This is a test message</p>") return course_email.id # pylint: disable=E1101