def test_enrollment_by_email(self): user = User.objects.create(username="******", email="*****@*****.**") course_id = CourseLocator("edX", "Test101", "2013") CourseEnrollment.enroll_by_email("*****@*****.**", course_id) assert CourseEnrollment.is_enrolled(user, course_id) self.assert_enrollment_event_was_emitted(user, course_id) # This won't throw an exception, even though the user is not found assert CourseEnrollment.enroll_by_email('*****@*****.**', course_id) is None self.assert_no_events_were_emitted() self.assertRaises(User.DoesNotExist, CourseEnrollment.enroll_by_email, "*****@*****.**", course_id, ignore_errors=False) self.assert_no_events_were_emitted() # Now unenroll them by email CourseEnrollment.unenroll_by_email("*****@*****.**", course_id) assert not CourseEnrollment.is_enrolled(user, course_id) self.assert_unenrollment_event_was_emitted(user, course_id) # Harmless second unenroll CourseEnrollment.unenroll_by_email("*****@*****.**", course_id) assert not CourseEnrollment.is_enrolled(user, course_id) self.assert_no_events_were_emitted() # Unenroll on non-existent user shouldn't throw an error CourseEnrollment.unenroll_by_email("*****@*****.**", course_id) self.assert_no_events_were_emitted()
def enroll_email(course_id, student_email, auto_enroll=False, email_students=False, email_params=None, language=None): """ Enroll a student by email. `student_email` is student's emails e.g. "*****@*****.**" `auto_enroll` determines what is put in CourseEnrollmentAllowed.auto_enroll if auto_enroll is set, then when the email registers, they will be enrolled in the course automatically. `email_students` determines if student should be notified of action by email. `email_params` parameters used while parsing email templates (a `dict`). `language` is the language used to render the email. returns two EmailEnrollmentState's representing state before and after the action. """ previous_state = EmailEnrollmentState(course_id, student_email) enrollment_obj = None if previous_state.user and User.objects.get(email=student_email).is_active: # if the student is currently unenrolled, don't enroll them in their # previous mode # for now, White Labels use the # "honor" course_mode. Given the change to use "audit" as the default # course_mode in Open edX, we need to be backwards compatible with # how White Labels approach enrollment modes. if CourseMode.is_white_label(course_id): course_mode = CourseMode.HONOR else: course_mode = None if previous_state.enrollment: course_mode = previous_state.mode enrollment_obj = CourseEnrollment.enroll_by_email(student_email, course_id, course_mode) if email_students: email_params['message_type'] = 'enrolled_enroll' email_params['email_address'] = student_email email_params['full_name'] = previous_state.full_name if is_testing_environment(): send_mail_to_student(student_email, email_params, language=language) else: compose_and_send_adg_course_enrollment_invitation_email(student_email, email_params) elif not is_email_retired(student_email): cea, _ = CourseEnrollmentAllowed.objects.get_or_create(course_id=course_id, email=student_email) cea.auto_enroll = auto_enroll cea.save() if email_students: email_params['message_type'] = 'allowed_enroll' email_params['email_address'] = student_email if is_testing_environment(): send_mail_to_student(student_email, email_params, language=language) else: compose_and_send_adg_course_enrollment_invitation_email(student_email, email_params) after_state = EmailEnrollmentState(course_id, student_email) return previous_state, after_state, enrollment_obj