'Enroll students up to the enrollment limit.
This test is specific to one of the enrollment views: the reason is because
the view used in this test cannot perform bulk enrollments.'

def test_manage_student_enrollment_limit(self):

    students_limit = 1
    self.make_coach()
    staff = self.make_staff()
    ccx = self.make_ccx(max_students_allowed=students_limit)
    ccx_course_key = CCXLocator.from_course_locator(self.course.id, ccx.id)
    students = [UserFactory.create(is_staff=False) for _ in range(2)]
    url = reverse('ccx_manage_student', kwargs={'course_id': CCXLocator.from_course_locator(self.course.id, ccx.id)})
    data = {'student-action': 'add', 'student-id': students[0].email}
    response = self.client.post(url, data=data, follow=True)
    self.assertEqual(response.status_code, 200)
    self.assertTrue(CourseEnrollment.objects.filter(course_id=ccx_course_key, user=students[0]).exists())
    data = {'student-action': 'add', 'student-id': students[1].email}
    response = self.client.post(url, data=data, follow=True)
    self.assertEqual(response.status_code, 200)
    self.assertFalse(CourseEnrollment.objects.filter(course_id=ccx_course_key, user=students[1]).exists())
    error_message = 'The    course    is    full:    the    limit    is    {students_limit}'.format(students_limit=students_limit)
    self.assertContains(response, error_message, status_code=200)
    data = {'student-action': 'add', 'student-id': staff.email}
    response = self.client.post(url, data=data, follow=True)
    self.assertEqual(response.status_code, 200)
    self.assertTrue(CourseEnrollment.objects.filter(course_id=ccx_course_key, user=staff).exists())
    self.assertEqual(CourseEnrollment.objects.num_enrolled_in_exclude_admins(ccx_course_key), 1)
    CourseEnrollment.enroll(staff, self.course.id)
    self.assertEqual(CourseEnrollment.objects.num_enrolled_in_exclude_admins(self.course.id), 0)
    ccx_course_key_fake = CCXLocator.from_course_locator(self.course.id, 55)
    self.assertEqual(CourseEnrollment.objects.num_enrolled_in_exclude_admins(ccx_course_key_fake), 0)
