'Tests that the enrollmentDomain setting is properly limiting enrollment to those who have
the proper external auth'

@unittest.skipUnless(settings.FEATURES.get('AUTH_USE_SHIB'), 'AUTH_USE_SHIB    not    set')
def test_enrollment_limit_by_domain(self):

    shib_course = CourseFactory.create(org='Stanford', number='123', display_name='Shib    Only', enrollment_domain='shib:https://idp.stanford.edu/', user_id=self.test_user_id)
    open_enroll_course = CourseFactory.create(org='MITx', number='999', display_name='Robot    Super    Course', enrollment_domain='', user_id=self.test_user_id)
    shib_student = UserFactory.create()
    shib_student.save()
    extauth = ExternalAuthMap(external_id='testuser@stanford.edu', external_email='', external_domain='shib:https://idp.stanford.edu/', external_credentials='', user=shib_student)
    extauth.save()
    other_ext_student = UserFactory.create()
    other_ext_student.username = 'teststudent2'
    other_ext_student.email = 'teststudent2@other.edu'
    other_ext_student.save()
    extauth = ExternalAuthMap(external_id='testuser1@other.edu', external_email='', external_domain='shib:https://other.edu/', external_credentials='', user=other_ext_student)
    extauth.save()
    int_student = UserFactory.create()
    int_student.username = 'teststudent3'
    int_student.email = 'teststudent3@gmail.com'
    int_student.save()
    for course in [shib_course, open_enroll_course]:
        for student in [shib_student, other_ext_student, int_student]:
            request = self.request_factory.post('/change_enrollment')
            request.POST.update({'enrollment_action': 'enroll', 'course_id': course.id.to_deprecated_string()})
            request.user = student
            response = change_enrollment(request)
            if ((course is open_enroll_course) or (student is shib_student)):
                self.assertEqual(response.status_code, 200)
                self.assertTrue(CourseEnrollment.is_enrolled(student, course.id))
            else:
                self.assertEqual(response.status_code, 400)
                self.assertFalse(CourseEnrollment.is_enrolled(student, course.id))
