def test_cea_enrolls_only_one_user(self): """ Tests that a CourseEnrollmentAllowed can be used by just one user. If the user changes e-mail and then a second user tries to enroll with the same accepted e-mail, the second enrollment should fail. However, the original user can reuse the CEA many times. """ cea = CourseEnrollmentAllowedFactory( email='*****@*****.**', course_id=self.course.id, auto_enroll=False, ) # Still unlinked assert cea.user is None user1 = UserFactory.create(username="******", email="*****@*****.**", password="******") user2 = UserFactory.create(username="******", email="*****@*****.**", password="******") assert not CourseEnrollment.objects.filter(course_id=self.course.id, user=user1).exists() user1.email = '*****@*****.**' user1.save() CourseEnrollment.enroll(user1, self.course.id, check_access=True) assert CourseEnrollment.objects.filter(course_id=self.course.id, user=user1).exists() # The CEA is now linked cea.refresh_from_db() assert cea.user == user1 # user2 wants to enroll too, (ab)using the same allowed e-mail, but cannot user1.email = '*****@*****.**' user1.save() user2.email = '*****@*****.**' user2.save() with pytest.raises(EnrollmentClosedError): CourseEnrollment.enroll(user2, self.course.id, check_access=True) # CEA still linked to user1. Also after unenrolling cea.refresh_from_db() assert cea.user == user1 CourseEnrollment.unenroll(user1, self.course.id) cea.refresh_from_db() assert cea.user == user1 # Enroll user1 again. Because it's the original owner of the CEA, the enrollment is allowed CourseEnrollment.enroll(user1, self.course.id, check_access=True) # Still same cea.refresh_from_db() assert cea.user == user1
def test_enrolled_after_email_change(self): """ Test that when a user's email changes, the user is enrolled in pending courses. """ pending_enrollment = CourseEnrollmentAllowedFactory(auto_enroll=True) # lint-amnesty, pylint: disable=unused-variable # the e-mail will change to [email protected] (from something else) self.assertNotEqual(self.user.email, '*****@*****.**') # there's a CEA for the new e-mail self.assertEqual(CourseEnrollmentAllowed.objects.count(), 1) self.assertEqual( CourseEnrollmentAllowed.objects.filter( email='*****@*****.**').count(), 1) # Changing the e-mail to the enrollment-allowed e-mail should enroll self.user.email = '*****@*****.**' self.user.save() self.assert_user_enrollment_occurred('edX/toy/2012_Fall') # CEAs shouldn't have been affected self.assertEqual(CourseEnrollmentAllowed.objects.count(), 1) self.assertEqual( CourseEnrollmentAllowed.objects.filter( email='*****@*****.**').count(), 1)
def test_invitation_only_but_allowed(self): """ Test for user logged in and allowed to enroll in invitation only course. """ # Course is invitation only, student is allowed to enroll and logged in user = UserFactory.create(username='******', password='******', email='*****@*****.**') CourseEnrollmentAllowedFactory(email=user.email, course_id=self.course.id) self.client.login(username=user.username, password='******') url = reverse('about_course', args=[str(self.course.id)]) resp = self.client.get(url) self.assertContains(resp, "Enroll Now") # Check that registration button is present self.assertContains(resp, REG_STR)
def test__has_access_course_can_enroll(self): yesterday = datetime.datetime.now( pytz.utc) - datetime.timedelta(days=1) tomorrow = datetime.datetime.now(pytz.utc) + datetime.timedelta(days=1) # Non-staff can enroll if authenticated and specifically allowed for that course # even outside the open enrollment period user = UserFactory.create() course = Mock(enrollment_start=tomorrow, enrollment_end=tomorrow, id=CourseLocator('edX', 'test', '2012_Fall'), enrollment_domain='') CourseEnrollmentAllowedFactory(email=user.email, course_id=course.id) assert access._has_access_course(user, 'enroll', course) # Staff can always enroll even outside the open enrollment period user = StaffFactory.create(course_key=course.id) assert access._has_access_course(user, 'enroll', course) # Non-staff cannot enroll if it is between the start and end dates and invitation only # and not specifically allowed course = Mock(enrollment_start=yesterday, enrollment_end=tomorrow, id=CourseLocator('edX', 'test', '2012_Fall'), enrollment_domain='', invitation_only=True) user = UserFactory.create() assert not access._has_access_course(user, 'enroll', course) # Non-staff can enroll if it is between the start and end dates and not invitation only course = Mock(enrollment_start=yesterday, enrollment_end=tomorrow, id=CourseLocator('edX', 'test', '2012_Fall'), enrollment_domain='', invitation_only=False) assert access._has_access_course(user, 'enroll', course) # Non-staff cannot enroll outside the open enrollment period if not specifically allowed course = Mock(enrollment_start=tomorrow, enrollment_end=tomorrow, id=CourseLocator('edX', 'test', '2012_Fall'), enrollment_domain='', invitation_only=False) assert not access._has_access_course(user, 'enroll', course)