Esempio n. 1
0
    def test_share_solution_by_another_user(
        exercise: Exercise,
        student_user: User,
    ):
        student_user2 = conftest.create_student_user(index=1)
        solution = conftest.create_solution(exercise, student_user2)

        client = conftest.get_logged_user(student_user.username)

        not_exist_share_response = client.post('/share',
                                               data=json.dumps({
                                                   'solutionId':
                                                   solution.id + 1,
                                                   'act':
                                                   'get',
                                               }),
                                               content_type='application/json')
        assert not_exist_share_response.status_code == 404

        not_user_solution_share_response = client.post(
            '/share',
            data=json.dumps({
                'solutionId': solution.id,
                'act': 'get',
            }),
            content_type='application/json',
        )
        assert not_user_solution_share_response.status_code == 403
Esempio n. 2
0
    def test_forgot_my_password(client: FlaskClient, captured_templates):
        user = conftest.create_student_user(index=1)
        conftest.reset_client_password(client, user.mail_address)
        template, _ = captured_templates[-1]
        assert template.name == "login.html"

        token = generate_user_token(user)
        conftest.recover_client_password(
            client,
            user.id,
            token,
            'new pass',
            'new pass',
        )
        template, _ = captured_templates[-1]
        assert template.name == "login.html"

        second_try_response = conftest.recover_client_password(
            client,
            user.id,
            token,
            'new pass1',
            'new pass1',
        )
        assert second_try_response.status_code == 404

        conftest.login_client_user(client, user.username, 'fake pass')
        template, _ = captured_templates[-1]
        assert template.name == 'login.html'

        conftest.login_client_user(client, user.username, 'new pass')
        template, _ = captured_templates[-1]
        assert template.name == 'exercises.html'
Esempio n. 3
0
    def test_forgot_my_password_invalid_recover(
        client: FlaskClient,
        captured_templates,
    ):
        user = conftest.create_student_user(index=1)
        conftest.reset_client_password(client, user.mail_address)
        template, _ = captured_templates[-1]
        assert template.name == "login.html"

        token = generate_user_token(user)
        unknown_id_recover_response = conftest.recover_client_password(
            client,
            user.id + 1,
            token,
            'different pass',
            'different pass',
        )
        assert unknown_id_recover_response.status_code == 404

        conftest.recover_client_password(
            client,
            user.id,
            token,
            'wrong pass',
            'different pass',
        )
        template, _ = captured_templates[-1]
        assert template.name == "recover-password.html"
Esempio n. 4
0
 def test_valid_change_password(captured_templates):
     student_user = conftest.create_student_user(index=1)
     client = conftest.get_logged_user(student_user.username)
     conftest.change_client_password(
         client,
         'fake pass',
         'some_password',
         'some_password',
     )
     template, _ = captured_templates[-1]
     assert template.name == "login.html"
     check_logout_response = client.get('/exercises')
     assert check_logout_response.status_code == 302
Esempio n. 5
0
    def test_view_page(
        exercise: Exercise,
        student_user: User,
    ):
        student_user2 = conftest.create_student_user(index=1)
        solution = conftest.create_solution(exercise, student_user)
        solution2 = conftest.create_solution(exercise, student_user2)

        client = conftest.get_logged_user(student_user.username)
        view_response = client.get(f'/view/{solution.id}')
        assert view_response.status_code == 200

        another_user_solution_response = client.get(f'/view/{solution2.id}')
        assert another_user_solution_response.status_code == 403

        not_exist_solution_response = client.get('/view/0')
        assert not_exist_solution_response.status_code == 404
Esempio n. 6
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
Esempio n. 7
0
    def test_send(self, student_user: User):
        another_user = conftest.create_student_user(index=1)
        params = self.generate_params()
        n1 = notifications.send(student_user, **params)
        n2 = notifications.send(another_user, self.kind_checked, 'yoyo2')
        n3 = notifications.send(student_user, self.kind_flake8, 'yoyo3')

        assert n1.user == student_user
        assert n1.kind == params['kind'].value
        assert n1.message == params['message']
        assert n1.related_id == params['related_id']
        assert n1.action_url == params['action_url']
        assert n2.kind != n3.kind
        assert n2.message == 'yoyo2'
        assert n2.user == another_user
        assert len(list(notifications.get(student_user))) == 2
        assert len(list(notifications.get(another_user))) == 1
Esempio n. 8
0
 def _duplicate_solution_from_comment(
     comment: models.Comment,
     first_solution_code: str,
     second_solution_code: str,
 ) -> typing.Tuple[models.Solution, models.Solution]:
     first_solution: models.Solution = comment.file.solution
     first_solution.set_state(models.Solution.STATES.DONE)
     first_solution = first_solution.refresh()
     solution_file = first_solution.solution_files.get()
     solution_file.code = first_solution_code
     solution_file.save()
     student_user: models.User = conftest.create_student_user(index=1)
     second_solution = conftest.create_solution(
         exercise=first_solution.exercise,
         student_user=student_user,
         code=second_solution_code,
     )
     return first_solution, second_solution
Esempio n. 9
0
    def test_next_exercise_with_cleanest_code(
        comment: Comment,
        staff_user: User,
    ):
        student_user: User = conftest.create_student_user(index=1)
        first_solution = comment.solution
        comment_text = comment.comment
        second_solution = conftest.create_solution(comment.solution.exercise,
                                                   student_user)

        # comment exists on first solution - second one should be the first
        next_unchecked = Solution.next_unchecked()
        assert next_unchecked is not None
        assert next_unchecked.id == second_solution.id

        # delete the comment should give us back the first solution
        Comment.delete_by_id(comment.id)
        next_unchecked = Solution.next_unchecked()
        assert next_unchecked is not None
        assert next_unchecked.id == first_solution.id

        # if second_solution has comments we should get first solution
        Comment.create_comment(
            commenter=staff_user,
            line_number=1,
            comment_text=comment_text,
            file=second_solution.solution_files.get(),
            is_auto=False,
        )
        next_unchecked = Solution.next_unchecked()
        assert next_unchecked is not None
        assert next_unchecked.id == first_solution.id

        # both have comments - the first one should be the first solution
        Comment.create_comment(
            commenter=staff_user,
            line_number=1,
            comment_text=comment_text,
            file=first_solution.solution_files.get(),
            is_auto=False,
        )
        next_unchecked = Solution.next_unchecked()
        assert next_unchecked is not None
        assert next_unchecked.id == first_solution.id
Esempio n. 10
0
    def test_view_user_page(
        student_user: User,
        staff_user: User,
    ):
        student_user2 = conftest.create_student_user(index=1)

        client = conftest.get_logged_user(student_user.username)
        user_response = client.get(f'/user/{student_user.id}')
        assert user_response.status_code == 200

        another_user_response = client.get(f'/user/{student_user2.id}')
        assert another_user_response.status_code == 403

        conftest.logout_user(client)
        client2 = conftest.get_logged_user(staff_user.username)
        not_exist_user_response = client2.get('/user/99')
        assert not_exist_user_response.status_code == 404

        another_user_response = client2.get(f'/user/{student_user2.id}')
        assert another_user_response.status_code == 200
Esempio n. 11
0
    def test_invalid_change_password(captured_templates):
        student_user = conftest.create_student_user(index=1)
        client = conftest.get_logged_user(student_user.username)
        for _ in range(MAX_INVALID_PASSWORD_TRIES):
            conftest.change_client_password(
                client,
                'wrong pass',
                'some_password',
                'some_password',
            )
            template, _ = captured_templates[-1]
            assert template.name == "change-password.html"

        conftest.change_client_password(
            client,
            'fake pass',
            'some_password',
            'some_password',
        )
        template, _ = captured_templates[-1]
        assert template.name == "change-password.html"
Esempio n. 12
0
    def test_expired_token(client: FlaskClient):
        user = conftest.create_student_user(index=1)
        conftest.reset_client_password(client, user.mail_address)
        token = generate_user_token(user)

        fake_time = time.time() + CONFIRMATION_TIME + 1
        with patch('time.time', Mock(return_value=fake_time)):
            conftest.recover_client_password(
                client,
                user.id,
                token,
                'new pass1',
                'new pass1',
            )
            conftest.login_client_user(client, user.username, 'new pass1')
            fail_login_response = client.get('/exercises')
            assert fail_login_response.status_code == 302

            conftest.login_client_user(client, user.username, 'fake pass')
            fail_login_response = client.get('/exercises')
            assert fail_login_response.status_code == 200
Esempio n. 13
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
Esempio n. 14
0
    def test_get(
        self,
        student_user: User,
        notification: Notification,
        solution: Solution,
    ):
        assert len(list(notifications.get(student_user))) == 1

        n = next(iter(notifications.get(student_user)), None)
        assert n is not None
        assert n.user == student_user
        assert n.related_id == solution.id
        assert n.message == 'Test message 0'

        student_user2 = conftest.create_student_user(index=1)
        assert len(list(notifications.get(student_user2))) == 0
        conftest.create_notification(student_user2, solution, index=2)
        assert len(list(notifications.get(student_user))) == 1
        assert len(list(notifications.get(student_user2))) == 1

        self.create_too_many_notifications(student_user, solution)
        assert (len(list(
            notifications.get(student_user))) == Notification.MAX_PER_USER)
Esempio n. 15
0
    def test_share_solution_function(
        exercise: Exercise,
        student_user: User,
    ):
        student_user2 = conftest.create_student_user(index=1)
        solution = conftest.create_solution(exercise, student_user2)

        client2 = conftest.get_logged_user(student_user2.username)
        # Sharing his own solution
        shared_response = client2.post('/share',
                                       data=json.dumps({
                                           'solutionId': solution.id,
                                           'act': 'get',
                                       }),
                                       content_type='application/json')
        assert shared_response.status_code == 200

        # Unknown act of share
        unknown_shared_response = client2.post('/share',
                                               data=json.dumps({
                                                   'solutionId':
                                                   solution.id,
                                                   'act':
                                                   'unknown',
                                               }),
                                               content_type='application/json')
        assert unknown_shared_response.status_code == 400

        # Entering another student solution
        shared_url = SharedSolution.get_or_none(
            SharedSolution.solution == solution, )
        assert len(shared_url.entries) == 0
        conftest.logout_user(client2)
        client = conftest.get_logged_user(student_user.username)
        shared_response = client.get(f'{routes.SHARED}/{shared_url}')
        assert shared_response.status_code == 200
        assert len(shared_url.entries) == 1

        # Downloading another student solution by solution.id
        solution_id_download_response = client.get(
            f'{routes.DOWNLOADS}/{solution.id}', )
        assert solution_id_download_response.status_code == 403

        # Downloading another student solution
        download_response = client.get(f'{routes.DOWNLOADS}/{shared_url}', )
        assert download_response.status_code == 200

        # Deleting another user's solution
        delete_not_user_solution_response = client.post(
            '/share',
            data=json.dumps({
                'solutionId': solution.id,
                'act': 'delete',
            }),
            content_type='application/json',
        )
        assert delete_not_user_solution_response.status_code == 403

        # Deleting his own solution
        conftest.logout_user(client)
        client2 = conftest.get_logged_user(student_user2.username)
        delete_share_response = client2.post('/share',
                                             data=json.dumps({
                                                 'solutionId':
                                                 solution.id,
                                                 'act':
                                                 'delete',
                                             }),
                                             content_type='application/json')
        assert delete_share_response.status_code == 200

        # Entering not shared solution after deletion
        conftest.logout_user(client2)
        client = conftest.get_logged_user(student_user.username)
        not_shared_solution_response = client.get(
            f'{routes.SHARED}/{shared_url}', )
        assert not_shared_solution_response.status_code == 404