def test_deserialize_ignores_read_only_fields(self):
        new_user = UserFactory(); new_user.save()
        desc = ChallengeDescFactory()
        new_c = Challenge.objects.create(name='Stay Callin', difficulty=5, score=10, description=desc,
                                                  test_case_count=2, category=self.sub_cat)
        challenge_comment = ChallengeComment.objects.create(challenge=self.challenge,
                                                            author=self.auth_user, content='Hello World')
        ser = ChallengeCommentSerializer(data={'id': 2014, 'content': 'change', 'author_id': new_user.id, 'parent_id': challenge_comment.id,
                                               'challenge_id': new_c.id})
        self.assertTrue(ser.is_valid())
        new_comment = ser.save(author=self.auth_user, challenge=self.challenge)

        self.assertNotEqual(new_comment.id, 2014)
        self.assertEqual(new_comment.author, self.auth_user)
        self.assertIsNone(new_comment.parent)
        self.assertEqual(new_comment.content, 'change')
        self.assertEqual(new_comment.challenge, self.challenge)
    def test_returns_400_if_submission_is_not_made_by_user(
            self, mock_create_post):
        other_user = UserFactory()
        other_user.save()
        other_subm = Submission.objects.create(language=self.python_language,
                                               challenge=self.challenge,
                                               author=other_user,
                                               code="")
        response = self.client.post('/social/posts',
                                    HTTP_AUTHORIZATION=self.auth_token,
                                    data={
                                        'post_type':
                                        NW_ITEM_CHALLENGE_COMPLETION_POST,
                                        'submission_id': other_subm.id,
                                    })

        self.assertEqual(response.status_code, 400)
        mock_create_post.assert_not_called()
        mock_create_post.assert_not_called()
Exemple #3
0
class SubmissionCommentCreateReplyView(APITestCase, TestHelperMixin):
    def setUp(self):
        self.base_set_up()
        self.second_user = UserFactory()
        self.second_user.save()
        self.second_token = 'Token {}'.format(self.second_user.auth_token.key)

        self.comment = SubmissionComment.objects.create(
            submission=self.submission,
            author=self.second_user,
            content='Hello, my name is boris')

    def test_can_create_reply(self, mock_add_reply):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments/{self.comment.id}',
            HTTP_AUTHORIZATION=self.auth_token,
            data={'content': 'When the night call ye'})

        self.assertEqual(response.status_code, 201)
        mock_add_reply.assert_called_once_with(
            author=self.auth_user,
            content='When the night call ye',
            to_notify=True)

    def test_is_created_if_user_has_solved_challenge(self, mock_add_reply):
        # Create a solved Submission
        Submission.objects.create(language=self.python_language,
                                  challenge=self.challenge,
                                  author=self.second_user,
                                  code="",
                                  result_score=self.challenge.score,
                                  pending=False)
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments/{self.comment.id}',
            HTTP_AUTHORIZATION=self.second_token,
            data={'content': 'When the night call ye'})

        self.assertEqual(response.status_code, 201)
        mock_add_reply.assert_called_once_with(
            author=self.second_user,
            content='When the night call ye',
            to_notify=True)

    def test_returns_401_if_user_has_not_solved_challenge(
            self, mock_add_reply):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments/{self.comment.id}',
            HTTP_AUTHORIZATION=self.second_token,
            data={'content': 'When the night call ye'})
        self.assertEqual(response.status_code, 401)
        mock_add_reply.assert_not_called()

    def test_returns_404_if_invalid_challenge(self, mock_add_reply):
        response = self.client.post(
            f'/challenges/111/submissions/{self.submission.id}/comments/{self.comment.id}',
            HTTP_AUTHORIZATION=self.auth_token,
            data={'content': 'When the night call ye'})

        self.assertEqual(response.status_code, 404)
        mock_add_reply.assert_not_called()

    def test_returns_404_if_invalid_submission(self, mock_add_reply):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/111/comments/{self.comment.id}',
            HTTP_AUTHORIZATION=self.auth_token,
            data={'content': 'When the night call ye'})

        self.assertEqual(response.status_code, 404)
        mock_add_reply.assert_not_called()

    def test_returns_404_if_invalid_comment(self, mock_add_reply):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments/111',
            HTTP_AUTHORIZATION=self.auth_token,
            data={'content': 'When the night call ye'})

        self.assertEqual(response.status_code, 404)
        mock_add_reply.assert_not_called()

    def test_invalid_challenge_submission_relation_returns_400(
            self, mock_add_reply):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{SubmissionFactory(author=self.second_user).id}/comments/{self.comment.id}',
            HTTP_AUTHORIZATION=self.auth_token,
            data={'content': 'When the night call ye'})

        self.assertEqual(response.status_code, 400)
        mock_add_reply.assert_not_called()

    def test_invalid_submission_comment_relation_returns_400(
            self, mock_add_reply):
        new_comment = SubmissionCommentFactory(
            author=self.auth_user,
            submission=SubmissionFactory(author=self.auth_user))
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments/{new_comment.id}',
            HTTP_AUTHORIZATION=self.auth_token,
            data={'content': 'When the night call ye'})

        self.assertEqual(response.status_code, 400)
        mock_add_reply.assert_not_called()

    def test_requires_authentication(self, mock_add_reply):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments/{self.comment.id}',
            data={'content': 'When the night call ye'})
        self.assertEqual(response.status_code, 401)
        mock_add_reply.assert_not_called()
Exemple #4
0
class SubmissionCommentCreateView(APITestCase, TestHelperMixin):
    def setUp(self):
        self.base_set_up()
        self.second_user = UserFactory()
        self.second_user.save()
        self.second_token = 'Token {}'.format(self.second_user.auth_token.key)

    def test_can_comment_if_fully_solved(self, mock_add_comment):
        self.submission = Submission.objects.create(
            language=self.python_language,
            challenge=self.challenge,
            author=self.second_user,
            code="",
            result_score=self.challenge.score,
            pending=False)
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments',
            HTTP_AUTHORIZATION=self.second_token,
            data={'content': 'Nice man!'})

        self.assertEqual(response.status_code, 201)
        mock_add_comment.assert_called_once_with(author=self.second_user,
                                                 content='Nice man!',
                                                 to_notify=True)

    def test_can_comment_if_author(self, mock_add_comment):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments',
            HTTP_AUTHORIZATION=self.auth_token,
            data={'content': 'Nice man!'})

        self.assertEqual(response.status_code, 201)
        mock_add_comment.assert_called_once_with(author=self.auth_user,
                                                 content='Nice man!',
                                                 to_notify=True)

    def test_cant_comment_if_short_content(self, mock_add_comment):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments',
            HTTP_AUTHORIZATION=self.auth_token,
            data={'content': 'n'})
        self.assertEqual(response.status_code, 400)
        mock_add_comment.assert_not_called()

    def test_cant_comment_if_long_content(self, mock_add_comment):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments',
            HTTP_AUTHORIZATION=self.auth_token,
            data={'content': 'N' * 600})
        self.assertEqual(response.status_code, 400)
        mock_add_comment.assert_not_called()

    def test_cant_comment_if_user_hasnt_solved_challenge(
            self, mock_add_comment):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments',
            HTTP_AUTHORIZATION=self.second_token,
            data={'content': 'Nice man!'})
        self.assertEqual(response.status_code, 401)
        mock_add_comment.assert_not_called()

    def test_invalid_challenge_returns_404(self, mock_add_comment):
        response = self.client.post(
            f'/challenges/111/submissions/{self.submission.id}/comments',
            HTTP_AUTHORIZATION=self.second_token,
            data={'content': 'Nice man!'})
        self.assertEqual(response.status_code, 404)
        mock_add_comment.assert_not_called()

    def test_invalid_submission_returns_404(self, mock_add_comment):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/111/comments',
            HTTP_AUTHORIZATION=self.second_token,
            data={'content': 'Nice man!'})
        self.assertEqual(response.status_code, 404)
        mock_add_comment.assert_not_called()

    def test_invalid_challenge_submission_relation_returns_400(
            self, mock_add_comment):
        response = self.client.post(
            f'/challenges/{ChallengeFactory().id}/submissions/{self.submission.id}/comments',
            HTTP_AUTHORIZATION=self.second_token,
            data={'content': 'Nice man!'})
        self.assertEqual(response.status_code, 400)
        mock_add_comment.assert_not_called()

    def test_requires_authentication(self, mock_add_comment):
        response = self.client.post(
            f'/challenges/{self.challenge.id}/submissions/{self.submission.id}/comments',
            data={'content': 'Nice man!'})
        self.assertEqual(response.status_code, 401)
        mock_add_comment.assert_not_called()