Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo n.º 3
0
    def test_staff_and_user_comments(
        exercise: Exercise,
        student_user: User,
        staff_user: User,
    ):
        solution = conftest.create_solution(exercise, student_user)

        client = conftest.get_logged_user(staff_user.username)
        # Enabling user comments option
        conftest.enable_users_comments()
        # Creating a comment
        comment_response = client.post('/comments',
                                       data=json.dumps({
                                           'fileId':
                                           solution.files[0].id,
                                           'act':
                                           'create',
                                           'kind':
                                           'text',
                                           'comment':
                                           'try again',
                                           'line':
                                           1,
                                       }),
                                       content_type='application/json')
        assert comment_response.status_code == 200

        # Creating another comment
        another_comment_response = client.post(
            '/comments',
            data=json.dumps({
                'fileId': solution.files[0].id,
                'act': 'create',
                'kind': 'text',
                'comment': 'hey',
                'line': 1,
            }),
            content_type='application/json',
        )
        assert another_comment_response.status_code == 200

        # Unknown act comment
        unknown_comment_response = client.post(
            '/comments',
            data=json.dumps({
                'fileId': solution.files[0].id,
                'act': 'unknown',
                'kind': 'text',
                'comment': 'hey',
                'line': 1,
            }),
            content_type='application/json',
        )
        assert unknown_comment_response.status_code == 400

        # Not existing fileId comment
        file_id_comment_response = client.post(
            '/comments',
            data=json.dumps({
                'fileId': 99,
                'act': 'create',
                'kind': 'text',
                'comment': 'hey',
                'line': 1,
            }),
            content_type='application/json',
        )
        assert file_id_comment_response.status_code == 404

        # Removing the second comment
        json_response_another_comment = json.loads(
            another_comment_response.get_data(as_text=True), )
        delete_response = client.get('/comments',
                                     query_string={
                                         'fileId':
                                         solution.files[0].id,
                                         'act':
                                         'delete',
                                         'commentId':
                                         json_response_another_comment['id'],
                                     },
                                     content_type='application/json')
        assert delete_response.status_code == 200

        conftest.logout_user(client)
        client2 = conftest.get_logged_user(student_user.username)
        # Trying to remove a comment
        json_response_comment = json.loads(
            comment_response.get_data(as_text=True), )
        delete_response = client2.get('/comments',
                                      query_string={
                                          'fileId': solution.files[0].id,
                                          'act': 'delete',
                                          'commentId':
                                          json_response_comment['id'],
                                      },
                                      content_type='application/json')
        assert delete_response.status_code == 403
Exemplo n.º 4
0
    def test_notes(
        staff_user: User,
        student_user: User,
    ):
        staff_user2 = conftest.create_staff_user(index=1)

        private_note, _ = conftest.create_note(
            creator=staff_user,
            user=student_user,
            note_text='private note',
            privacy=0,
        )
        staff_note, _ = conftest.create_note(
            creator=staff_user,
            user=student_user,
            note_text='staff note',
            privacy=1,
        )
        user_note, _ = conftest.create_note(
            creator=staff_user,
            user=student_user,
            note_text='user note',
            privacy=2,
        )
        public_note, _ = conftest.create_note(
            creator=staff_user,
            user=student_user,
            note_text='public note',
            privacy=3,
        )

        client = conftest.get_logged_user(staff_user2.username)
        # Trying to remove a private note of another staff client
        private_note_response = client.get(
            f'/notes/{student_user.id}',
            query_string={'noteId': private_note.id},
            data=json.dumps({'act': 'delete'}),
            content_type='application/json',
        )
        assert private_note_response.status_code == 403

        # Removing a staff note of another staff user
        staff_note_response = client.get(
            f'/notes/{student_user.id}',
            query_string={'noteId': staff_note.id},
            data=json.dumps({'act': 'delete'}),
            content_type='application/json',
        )
        assert staff_note_response.status_code == 200

        # Another staff user can see only the remaining user and public comment
        user_page_notes = client.get(
            f'notes/{student_user.id}',
            query_string={'act': 'fetch'},
            content_type='application/json',
        )
        json_user_page_notes = json.loads(
            user_page_notes.get_data(as_text=True), )
        assert len(json_user_page_notes) == 2

        # Staff trying unknown act
        unknown_act_note_response = client.post(
            f'/notes/{student_user.id}',
            data=json.dumps({'act': 'unknown'}),
            content_type='application/json',
        )
        assert unknown_act_note_response.status_code == 400

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

        # User can see only the remaining user and public comment
        user_page_notes = client2.get(
            f'notes/{student_user.id}',
            query_string={'act': 'fetch'},
            content_type='application/json',
        )
        json_user_page_notes = json.loads(
            user_page_notes.get_data(as_text=True), )
        assert len(json_user_page_notes) == 2

        # Trying to remove a public note
        public_note_response = client2.get(
            f'/notes/{student_user.id}',
            query_string={'noteId': public_note.id},
            data=json.dumps({'act': 'delete'}),
            content_type='application/json',
        )
        assert public_note_response.status_code == 403
Exemplo n.º 5
0
    def test_user_commented_after_check(
        solution: Solution,
        student_user: User,
        staff_user: User,
    ):
        client = conftest.get_logged_user(student_user.username)

        # Marking the solution as checked
        solutions.mark_as_checked(solution.id, staff_user.id)
        solution = Solution.get_by_id(solution.id)

        # Sending comments after solution checked
        comment_response = client.post('/comments',
                                       data=json.dumps({
                                           'fileId':
                                           solution.files[0].id,
                                           'act':
                                           'create',
                                           'kind':
                                           'text',
                                           'comment':
                                           'new one',
                                           'line':
                                           1,
                                       }),
                                       content_type='application/json')
        new_comment_response = client.post('/comments',
                                           data=json.dumps({
                                               'fileId':
                                               solution.files[0].id,
                                               'act':
                                               'create',
                                               'kind':
                                               'text',
                                               'comment':
                                               'another one',
                                               'line':
                                               2,
                                           }),
                                           content_type='application/json')
        assert comment_response.status_code == 200
        assert new_comment_response.status_code == 200
        assert len(list(notifications.get(staff_user))) == 1

        conftest.logout_user(client)
        client2 = conftest.get_logged_user(staff_user.username)

        # Sending a comment after student user commented
        staff_comment_response = client2.post(
            '/comments',
            data=json.dumps({
                'fileId': solution.files[0].id,
                'act': 'create',
                'kind': 'text',
                'comment': 'FINE',
                'line': 1,
            }),
            content_type='application/json',
        )
        assert staff_comment_response.status_code == 200
        assert len(list(notifications.get(student_user))) == 2

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

        # User student comments again
        another_comment_response = client.post(
            '/comments',
            data=json.dumps({
                'fileId': solution.files[0].id,
                'act': 'create',
                'kind': 'text',
                'comment': 'OK',
                'line': 3,
            }),
            content_type='application/json',
        )
        assert another_comment_response.status_code == 200
        assert len(list(notifications.get(staff_user))) == 2