def test_enroll_students_to_exam_student_enroll_others(self): """A student cannot enroll others students""" exams_ids = set(exam.id for exam in Exam.get_query().all()) # Select two students and determines which exams they are not enrolled # to not_enr_exams = [] students = Student.get_query().limit(2).all() for student in students: student_subscriptions = \ (StudentSubscription.get_query() .filter(StudentSubscription.student_id == student.id) .all()) enr_exams = [exam.id for exam in student_subscriptions] not_enr_exams_ids = exams_ids - set(enr_exams) not_enr_exams.append(list(not_enr_exams_ids)) for i in range(len(students)): token = generate_user_token(students[i].username, STUDENT_PERMISSIONS) exam_id = not_enr_exams[(i + 1) % 2][0] student_id = students[(i + 1) % 2].id res = self.client.post(f'/api/v1/enrolls/{exam_id}', headers={ 'Content-Type': 'application/json', 'Authorization': f'bearer {token}' }, json=[student_id]) self.assertEqual(res.status_code, 403, res.get_json()) data = res.get_json() self.assertEqual(data['description'], 'Cannot enroll others students.', data)
def test_list_enrolled_students_others_exams(self): """Teacher cannot list enrolled students of another teacher's exam""" teacher1 = (Teacher.get_query().join(Exam).order_by( Teacher.id).first()) teacher2 = (Teacher.get_query().join(Exam).order_by( Teacher.id.desc()).first()) exam1 = Exam.get_query().filter(Exam.author_id == teacher1.id).first() exam2 = Exam.get_query().filter(Exam.author_id == teacher2.id).first() tests_cases = ((teacher1, exam2), (teacher2, exam1)) for teacher, exam in tests_cases: token = generate_user_token(teacher.username, TEACHER_PERMISSIONS) res = self.client.get(f'/api/v1/enrolls/{exam.id}', headers={ 'Content-Type': 'application/json', 'Authorization': f'bearer {token}' }) self.assertEqual(res.status_code, 403, res.get_json())
def test_enroll_students_to_exam_as_student(self): """Student can enroll him self to one exam""" exams_ids = [exam.id for exam in Exam.get_query().all()] # Select 5 students students = Student.get_query().limit(5).all() students_enrolls = { student.id: (subscription.exam_id for subscription in StudentSubscription.get_query().filter( StudentSubscription.student_id == student.id).all()) for student in students } # Determine which exams the students is not enrolled in students_not_enrolled = { student_id: set(exams_ids) - set(exams_ids_) for student_id, exams_ids_ in students_enrolls.items() } sub_count_before = StudentSubscription.get_query().count() try: for student in students: # Enroll the student in all the exams he is not enrolled to token = generate_user_token(student.username, STUDENT_PERMISSIONS) for exam_id in students_not_enrolled[student.id]: res = self.client.post(f'/api/v1/enrolls/{exam_id}', headers={ 'Content-Type': 'application/json', 'Authorization': f'bearer {token}' }, json=[]) self.assertEqual(res.status_code, 200, res.get_json()) data = res.get_json() self.check_fields_list(data, ['success', 'new_enrolls']) # Must be one because he is was not enrolled to this exam self.assertEqual(data['new_enrolls'], 1) # The student should be enrolled to all exams now sub_count = ((StudentSubscription.get_query().filter( StudentSubscription.student_id == student.id).count())) self.assertEqual(sub_count, len(exams_ids)) # All students should be enrolled to all exams sub_count_after = StudentSubscription.get_query().count() self.assertEqual(len(students) * len(exams_ids), sub_count_after) finally: # Restore the database to its initial state deletion_list = [] for student_id, exams_ids in students_not_enrolled.items(): for exam_id in exams_ids: deletion_list.append((StudentSubscription.get_query( ).filter( StudentSubscription.student_id == student_id).filter( StudentSubscription.exam_id == exam_id).first())) Exam.persist_changes({'delete': deletion_list}) sub_count_after = StudentSubscription.get_query().count() self.assertEqual(sub_count_after, sub_count_before)
def test_delete_exam_teacher_others_exams(self): """Teacher trying to archive others teachers exam""" teacher = Teacher.query.order_by(Teacher.id.desc()).first() token = generate_user_token(teacher.username, TEACHER_PERMISSIONS) exams_ids = [exam.id for exam in Exam.get_query().all() if exam.author_id != teacher.id][:10] for exam_id in exams_ids: res = self.client.delete(f'/api/v1/exams/{exam_id}', headers={ 'Content-Type': 'application/json', 'Authorization': f'bearer {token}'}) self.assertEqual(res.status_code, 403, res.get_json())
def test_list_enrolled_students_teacher(self): """Return the names of students enrolled to one exam""" teacher = Teacher.get_query().first() exams = Exam.get_query().filter(Exam.author_id == teacher.id).all() token = generate_user_token(teacher.username, TEACHER_PERMISSIONS) for exam in exams: res = self.client.get(f'/api/v1/enrolls/{exam.id}', headers={ 'Content-Type': 'application/json', 'Authorization': f'bearer {token}' }) self.assertEqual(res.status_code, 200, res.get_json()) data = res.get_json() self.check_fields_list(data, ['success', 'students']) self.assertTrue(data['success']) enroll_count = (StudentSubscription.get_query().filter( StudentSubscription.exam_id == exam.id).count()) self.assertEqual(len(data['students']), enroll_count)