Example #1
0
    def setup(cls):
        cls.course1 = conftest.create_course(1)
        cls.course2 = conftest.create_course(2)
        cls.course_no_submissions = conftest.create_course(3)
        cls.course_no_exercises = conftest.create_course(4)

        cls.ex1_1 = conftest.create_exercise(cls.course1, 1, 1)
        cls.ex1_2 = conftest.create_exercise(cls.course1, 2, 2)
        cls.ex2_1 = conftest.create_exercise(cls.course2, 1, 3)
        cls.ex3_1 = conftest.create_exercise(cls.course_no_submissions, 1, 4)

        cls.user1 = conftest.create_user(index=1)
        cls.user2 = conftest.create_user(index=2)

        conftest.create_usercourse(cls.user1, cls.course1)
        conftest.create_usercourse(cls.user1, cls.course2)
        conftest.create_usercourse(cls.user2, cls.course1)

        solution = conftest.create_solution

        cls.s1 = solution(cls.ex1_1, cls.user1)
        cls.s2 = solution(cls.ex1_1, cls.user1, code='Other submission')
        cls.s3 = solution(cls.ex1_1, cls.user2)
        cls.s4 = solution(cls.ex1_2, cls.user1)

        cls.s5 = solution(cls.ex2_1, cls.user1)
Example #2
0
    def test_zip_bomb(self, student_user: User):
        conftest.create_exercise()

        client = conftest.get_logged_user(username=student_user.username)

        # Trying to upload a zipbomb file
        upload_response = client.post('/upload', data={
            'file': self.zipbomb_bytes_io,
        })
        assert upload_response.status_code == 413
Example #3
0
    def test_zip(self, course: Course, student_user: User):
        conftest.create_exercise(course, 1)
        conftest.create_exercise(course, 2)
        conftest.create_exercise(course, 3)
        conftest.create_exercise(course, 4, is_archived=True)
        conftest.create_usercourse(student_user, course)

        client = conftest.get_logged_user(username=student_user.username)

        # Uploading a multiple zip solutions file
        upload_response = client.post(f'/upload/{course.id}',
                                      data={
                                          'file':
                                          self.zipfiles_extractors_bytes_io[1],
                                      })
        json_response_upload = json.loads(
            upload_response.get_data(as_text=True), )
        assert len(json_response_upload['exercise_misses']) == 1
        assert len(json_response_upload['exercise_matches']) == 2
        assert upload_response.status_code == 200

        # Uploading a zip file with a same solution exists in the previous zip
        second_upload_response = client.post(
            f'/upload/{course.id}',
            data={
                'file': self.zipfiles_extractors_bytes_io[0],
            })
        assert second_upload_response.status_code == 400
Example #4
0
    def test_upload_correct_course(
        self,
        course: Course,
        student_user: User,
    ):
        conftest.create_exercise(course, 1)
        conftest.create_usercourse(student_user, course)

        client = conftest.get_logged_user(username=student_user.username)
        success_upload_response = client.post(f'/upload/{course.id}',
                                              data={
                                                  'file':
                                                  self.pyfile_different_course,
                                              })
        assert success_upload_response.status_code == 200
Example #5
0
    def test_solutions_of_user(
        staff_user: User,
        student_user: User,
        course: Course,
        solution: Solution,
        _assessments,
    ):
        conftest.create_usercourse(student_user, course)
        client = conftest.get_logged_user(staff_user.username)
        client.post(
            f'/assessment/{solution.id}',
            data=json.dumps({'assessment': 2}),
            content_type='application/json',
        )
        solution = Solution.get_by_id(solution.id)
        assert solution.assessment.name == 'Nice'

        client.post(
            f'/assessment/{solution.id}',
            data=json.dumps({'assessment': None}),
            content_type='application/json',
        )

        exercise2 = conftest.create_exercise(course, 2)
        solution2 = conftest.create_solution(exercise2, student_user)
        client.post(
            f'/assessment/{solution2.id}',
            data=json.dumps({'assessment': 3}),
            content_type='application/json',
        )
        solution2 = Solution.get_by_id(solution2.id)

        exercises = solution.of_user(student_user.id, from_all_courses=True)
        assert exercises[0].get('assessment') is None
        assert exercises[1].get('assessment') == 'Try again'
Example #6
0
    def test_upload_another_course(
        self,
        course: Course,
        student_user: User,
    ):
        course2 = conftest.create_course(index=1)
        conftest.create_exercise(course2, 1)
        conftest.create_usercourse(student_user, course)

        client = conftest.get_logged_user(username=student_user.username)
        fail_upload_response = client.post(f'/upload/{course2.id}',
                                           data={
                                               'file':
                                               self.pyfile_different_course,
                                           })
        assert fail_upload_response.status_code == 400
Example #7
0
    def test_courses_exercises(
        course: Course,
        student_user: User,
        captured_templates,
    ):
        course2 = conftest.create_course(index=1)
        conftest.create_usercourse(student_user, course)
        conftest.create_exercise(course2, 1)
        conftest.create_exercise(course2, 2)
        assert len(
            list(Exercise.get_objects(student_user.id,
                                      from_all_courses=True), )) == 0

        client = conftest.get_logged_user(username=student_user.username)
        client.get(f'course/{course.id}')
        template, _ = captured_templates[-1]
        assert template.name == 'exercises.html'
        conftest.create_exercise(course, 1, index=1)
        assert len(list(Exercise.get_objects(student_user.id))) == 1

        unregistered_response = client.get(f'course/{course2.id}')
        assert unregistered_response.status_code == 403

        fail_response = client.get('course/123456')
        assert fail_response.status_code == 404

        conftest.create_usercourse(student_user, course2)
        client.get(f'course/{course2.id}')
        template, _ = captured_templates[-1]
        assert template.name == 'exercises.html'
        assert len(list(Exercise.get_objects(student_user.id))) == 2
Example #8
0
    def test_start_checking(exercise: Exercise, student_user: User):
        student_user2 = conftest.create_student_user(index=1)
        exercise2 = conftest.create_exercise(1)
        solution1 = conftest.create_solution(exercise, student_user)
        solution2 = conftest.create_solution(exercise2, student_user)
        solution3 = conftest.create_solution(exercise, student_user2)

        is_checking = solutions.start_checking(solution=None)
        assert not is_checking

        with no_celery:
            for solution in (solution1, solution2, solution3):
                assert solution.state == Solution.STATES.CREATED.name
                is_checking = solutions.start_checking(solution=solution)
                assert is_checking
                the_solution = Solution.get_by_id(solution.id)
                assert the_solution.state == Solution.STATES.IN_CHECKING.name
Example #9
0
    def test_send_page(
        student_user: User,
        course: Course,
        exercise: Exercise,
    ):
        course2 = conftest.create_course(2)
        exercise2 = conftest.create_exercise(course2, 1)
        conftest.create_usercourse(student_user, course)
        client = conftest.get_logged_user(student_user.username)
        success_send_response = client.get(f'send/{course.id}')
        assert success_send_response.status_code == 200

        success_send_response2 = client.get(f'send/{course.id}/{exercise.id}')
        assert success_send_response2.status_code == 200

        fail_send_response = client.get(f'send/{course2.id}')
        assert fail_send_response.status_code == 403

        fail_send_response = client.get(f'send/{course2.id}/{exercise2.id}')
        assert fail_send_response.status_code == 403
Example #10
0
    def test_get_next_unchecked(
        course: Course,
        student_user: User,
        exercise: Exercise,
        staff_user: User,
        _assessments,
    ):
        student_user2 = conftest.create_student_user(index=1)
        exercise2 = conftest.create_exercise(course, 2, index=3)
        solution1 = conftest.create_solution(exercise, student_user)
        solution2 = conftest.create_solution(exercise2, student_user)
        solution3 = conftest.create_solution(exercise, student_user2)

        assert len(list(Solution.select())) == 3

        unchecked = solutions.get_next_unchecked(exercise.id)
        assert unchecked is not None
        assert unchecked.exercise.id == solution1.exercise.id
        assert unchecked == solution1

        solutions.mark_as_checked(solution1.id, staff_user)
        unchecked = solutions.get_next_unchecked(exercise.id)
        assert unchecked is not None
        assert unchecked.exercise.id == solution3.exercise.id
        assert unchecked == solution3

        solutions.mark_as_checked(solution3.id, staff_user)
        unchecked = solutions.get_next_unchecked(exercise.id)
        assert unchecked is None

        unchecked = solutions.get_next_unchecked()
        assert unchecked is not None
        assert unchecked == solution2

        solutions.mark_as_checked(solution2.id, staff_user)
        unchecked = solutions.get_next_unchecked()
        assert unchecked is None

        unchecked = solutions.get_next_unchecked(solution2.id)
        assert unchecked is None