Example #1
0
    def test_view_all_submissions(self):
        second_submission = Submission(language=self.python_language, challenge=self.challenge, author=self.auth_user, code="")
        second_submission.save()
        response = self.client.get(path='/challenges/{}/submissions/all'.format(self.challenge.id), HTTP_AUTHORIZATION=self.auth_token)

        self.assertEqual(response.status_code, 200)
        # Should order them by creation date descending
        self.assertEqual(LimitedSubmissionSerializer([second_submission, self.submission], many=True).data, response.data)
Example #2
0
 def list(self, request, *args, **kwargs):
     challenge_pk = kwargs.get('challenge_pk')
     return Response(data=LimitedSubmissionSerializer(
         Submission.objects.filter(challenge=challenge_pk,
                                   author=request.user).order_by(
                                       '-created_at')  # newest first
         .all(),
         many=True).data)
    def test_serialize_correctly_sets_user_has_upvoted(self):
        req_mock = MagicMock(user=self.auth_user)
        submission = Submission.objects.create(language=self.python_language, challenge=self.challenge, author=self.auth_user,
                                code="hust", result_score=10, pending=False)
        sv = SubmissionVote.objects.create(is_upvote=False, submission_id=submission.id, author_id=self.auth_user.id);

        received_submission = LimitedSubmissionSerializer(submission, context={'request': req_mock}).data
        self.assertEqual(received_submission['user_has_voted'], True)
        self.assertEqual(received_submission['user_has_upvoted'], False)
        self.assertEqual(received_submission['upvote_count'], 0)
        self.assertEqual(received_submission['downvote_count'], 1)
    def test_limited_serialization_should_not_serialize_code(self):

        s = Submission.objects.create(language=self.python_language, challenge=self.challenge, author=self.auth_user,
                                      code="BAGDAD")
        serializer = LimitedSubmissionSerializer(s)
        created_at_date = s.created_at.isoformat()[:-6] + 'Z'
        expected_json = (f'{{"id":{s.id},"challenge":{self.challenge.id},"author":"{self.auth_user.username}",'
                         f'"result_score":0,"pending":true,"created_at":"{created_at_date}",'
                         f'"compiled":true,"compile_error_message":"","language":"Python","timed_out":false,'
                         f'"user_has_voted":false,"user_has_upvoted":false,"upvote_count":0,"downvote_count":0}}')
        content = JSONRenderer().render(serializer.data)
        self.assertEqual(content.decode('utf-8').replace('\\n', '\n'), expected_json)
Example #5
0
    def test_get_top_submissions(self):
        better_submission = Submission.objects.create(language=self.python_language, challenge=self.challenge, author=self.auth_user, code="",
                                       result_score=50)
        # Second user with submissions
        _s_user = User.objects.create(username='******', password='******', email='*****@*****.**', score=123, role=self.base_role)
        _submission = Submission.objects.create(language=self.python_language, challenge=self.challenge, author=_s_user, code="", result_score=50)
        top_submission = Submission.objects.create(language=self.python_language, challenge=self.challenge, author=_s_user, code="", result_score=51)

        # Should return the two submissions, (both users' best submissions) ordered by score descending
        response = self.client.get(f'/challenges/{self.challenge.id}/submissions/top', HTTP_AUTHORIZATION=self.auth_token)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, LimitedSubmissionSerializer([top_submission, better_submission], many=True).data)
Example #6
0
    def list(self, request, *args, **kwargs):
        challenge_pk = kwargs.get('challenge_pk', '')
        try:
            Challenge.objects.get(pk=challenge_pk)
        except Challenge.DoesNotExist:
            return Response(
                data={'error': f'Invalid challenge id {challenge_pk}!'},
                status=400)

        top_submissions = Submission.fetch_top_submissions_for_challenge(
            challenge_id=challenge_pk)

        return Response(
            data=LimitedSubmissionSerializer(top_submissions, many=True).data)
Example #7
0
    def test_get_self_top_submission(self):
        """ Should return the user's top submission """
        new_user = User.objects.create(username='******', password='******', email='*****@*****.**', score=123, role=self.base_role)
        new_auth_token = 'Token {}'.format(new_user.auth_token.key)
        submission = Submission.objects.create(language=self.python_language, challenge=self.challenge, author=new_user,
                                                code="", result_score=5, pending=False)
        top_submission = Submission.objects.create(language=self.python_language, challenge=self.challenge, author=new_user,
                                                    code="", result_score=6, pending=False)
        sec_submission = Submission.objects.create(language=self.python_language, challenge=self.challenge, author=new_user,
                                                    code="", result_score=5, pending=False)

        response = self.client.get(f'/challenges/{self.challenge.id}/submissions/selfTop', HTTP_AUTHORIZATION=new_auth_token)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, LimitedSubmissionSerializer(top_submission).data)
Example #8
0
    def retrieve(self, request, *args, **kwargs):
        challenge_pk = kwargs.get('challenge_pk', '')
        try:
            Challenge.objects.get(pk=challenge_pk)
        except Challenge.DoesNotExist:
            return Response(
                data={'error': f'Invalid challenge id {challenge_pk}!'},
                status=400)

        top_submission = Submission.fetch_top_submission_for_challenge_and_user(
            challenge_id=challenge_pk, user_id=request.user.id)

        if top_submission is None:
            return Response(status=404)  # doesnt exist

        return Response(data=LimitedSubmissionSerializer(top_submission).data)