示例#1
0
    def test_tokens_are_expired(self):
        owner_name, opponent_name = 'owner', 'opponent'
        owner, opponent = UserFactory(username=owner_name), UserFactory(
            username=opponent_name)
        dialog = Dialog.objects.create(owner=owner, opponent=opponent)

        self.assertFalse(dialog.tokens_are_expired())
示例#2
0
    def test_refresh_tokens_not_expired_tokens_should_raise(
            self, mock_are_expired):
        mock_are_expired.return_value = True
        owner, opponent = UserFactory(username='******'), UserFactory(
            username='******')
        dialog = Dialog.objects.create(owner=owner, opponent=opponent)

        with self.assertRaises(Exception):
            dialog.refresh_tokens()
示例#3
0
    def test_get_or_create_dialog_should_create_dialog_when_it_doesnt_exist(
            self):
        owner, opponent = UserFactory(), UserFactory()

        dialog = Dialog.objects.get_or_create_dialog_with_users(
            owner, opponent)

        self.assertEqual(dialog.owner, owner)
        self.assertEqual(dialog.opponent, opponent)
示例#4
0
    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')
示例#5
0
    def test_token_is_valid(self, mock_tokens_are_expired):
        mock_tokens_are_expired.return_value = False

        owner_name, opponent_name = 'owner', 'opponent'
        owner, opponent = UserFactory(username=owner_name), UserFactory(
            username=opponent_name)
        dialog = Dialog.objects.create(owner=owner, opponent=opponent)

        self.assertTrue(dialog.token_is_valid(dialog.opponent_token))
        mock_tokens_are_expired.assert_called_once()
        self.assertTrue(dialog.token_is_valid(dialog.owner_token))
示例#6
0
    def test_creation_assigns_secret_key_and_tokens(self, mock_gen_tokens):
        mock_gen_tokens.return_value = ('one', 'two', 'three')
        owner_name, opponent_name = 'owner', 'opponent'
        owner, opponent = UserFactory(username=owner_name), UserFactory(
            username=opponent_name)
        dialog = Dialog.objects.create(owner=owner, opponent=opponent)

        self.assertEqual(dialog.secret_key, 'one')
        self.assertEqual(dialog.owner_token, 'two')
        self.assertEqual(dialog.opponent_token, 'three')
        mock_gen_tokens.assert_called_once_with(owner_name, opponent_name)
示例#7
0
 def test_get_votes_count(self):
     """
     Get votes count should return a tuple indicating the upvote and downvote count
     for the given SubmissionVote object
     """
     sec_user = UserFactory()
     third_user = UserFactory()
     s = Submission.objects.create(language=self.python_language, challenge=self.challenge, author=self.auth_user, code="")
     SubmissionVote.objects.create(author=self.auth_user, submission=s, is_upvote=False)
     SubmissionVote.objects.create(author=sec_user, submission=s, is_upvote=True)
     SubmissionVote.objects.create(author=third_user, submission=s, is_upvote=False)
     self.assertEqual((1, 2), s.get_votes_count())
示例#8
0
    def test_get_or_create_dialog_should_get_existing_dialog(self):
        # Should get the existing dialog regardless of how we send the users
        owner, opponent = UserFactory(), UserFactory()

        dialog = Dialog.objects.create(owner=owner, opponent=opponent)

        self.assertEqual(
            dialog,
            Dialog.objects.get_or_create_dialog_with_users(owner, opponent))
        self.assertEqual(
            dialog,
            Dialog.objects.get_or_create_dialog_with_users(opponent, owner))
示例#9
0
 def setUp(self):
     self.create_user_and_auth_token()
     self.first_user = UserFactory()
     self.first_us_token = f'Token {self.first_user.auth_token.key}'
     self.second_user = UserFactory()
     self.dialog = Dialog.objects.create(owner=self.auth_user,
                                         opponent=self.second_user)
     self.auth_us_messages = [
         Message.objects.create(text='whatup',
                                sender=self.auth_user,
                                dialog=self.dialog) for _ in range(5)
     ]
示例#10
0
    def test_tokens_are_expired_expired_token_should_return_false(self):
        owner_name, opponent_name = 'owner', 'opponent'
        owner, opponent = UserFactory(username=owner_name), UserFactory(
            username=opponent_name)
        dialog = Dialog.objects.create(owner=owner, opponent=opponent)
        dialog.opponent_token = jwt.encode(
            {
                'exp': datetime.utcnow() - timedelta(minutes=1),
                'username': opponent_name
            }, dialog.secret_key)
        dialog.save()

        self.assertTrue(dialog.tokens_are_expired())
示例#11
0
    def test_refresh_tokens_expired_tokens_should_refresh(
            self, mock_are_expired):
        mock_are_expired.return_value = False
        owner, opponent = UserFactory(username='******'), UserFactory(
            username='******')
        dialog = Dialog.objects.create(owner=owner, opponent=opponent)
        with patch('private_chat.models.generate_dialog_tokens') as mock_gen:
            mock_gen.return_value = '1', '2', '3'

            dialog.refresh_tokens()
            self.assertEqual(dialog.secret_key, '1')
            self.assertEqual(dialog.owner_token, '2')
            self.assertEqual(dialog.opponent_token, '3')
            mock_are_expired.assert_called_once()
            mock_gen.assert_called_once_with('own', 'opn')
示例#12
0
 def test_upvote_creation_creates_notification(self):
     other_user = UserFactory()
     SubmissionVote.objects.create(author=other_user, submission=self.submission, is_upvote=True, to_notify=True)
     self.assertEqual(Notification.objects.count(), 1)
     notif = Notification.objects.first()
     self.assertEqual(notif.recipient, self.submission.author)
     self.assertEqual(notif.type, RECEIVE_SUBMISSION_UPVOTE_NOTIFICATION)
示例#13
0
    def test_run_grader_task_correctly_sets_on_compile_failure(
            self, mock_run_grader):
        Proficiency.objects.create(name='starter', needed_percentage=0)
        user = UserFactory()
        submission = SubmissionFactory(author=user)
        test_case_count = 5
        test_folder_name = '/tank/'
        code = 'print("hello world")'
        lang = "python3"
        submission_id = submission.id
        mock_run_grader.return_value = {
            GRADER_TEST_RESULTS_RESULTS_KEY: 'batman',
            GRADER_COMPILE_FAILURE: "FAILED MISERABLY"
        }

        run_grader_task(test_case_count=test_case_count,
                        test_folder_name=test_folder_name,
                        code=code,
                        lang=lang,
                        submission_id=submission_id)

        # since we have a compile failure, the submission's compiled should be set to false
        submission.refresh_from_db()
        self.assertFalse(submission.compiled)
        self.assertFalse(submission.pending)
        self.assertEqual(submission.compile_error_message, "FAILED MISERABLY")
示例#14
0
    def test_get_votes_with_delete_returns_expected(self):
        sec_user = UserFactory()
        third_user = UserFactory()
        s = Submission.objects.create(language=self.python_language, challenge=self.challenge, author=self.auth_user,
                                      code="")
        sv1 = SubmissionVote.objects.create(author=self.auth_user, submission=s, is_upvote=False)
        sv2 = SubmissionVote.objects.create(author=sec_user, submission=s, is_upvote=True)
        sv3 = SubmissionVote.objects.create(author=third_user, submission=s, is_upvote=False)
        self.assertEqual((1, 2), s.get_votes_count())

        sv3.delete()
        self.assertEqual((1, 1), s.get_votes_count())
        sv2.delete()
        self.assertEqual((0, 1), s.get_votes_count())
        sv1.delete()
        self.assertEqual((0, 0), s.get_votes_count())
示例#15
0
 def test_fetch_messages_created_before(self):
     other_user = UserFactory()
     other_dialog = Dialog.objects.create(owner=self.first_user,
                                          opponent=other_user)
     for i in range(40):
         msg = Message.objects.create(dialog=self.dialog,
                                      sender=self.first_user,
                                      text='What the f you mean?',
                                      created=random_datetime())
         Message.objects.create(dialog=other_dialog,
                                sender=self.first_user,
                                text='What huh',
                                created=random_datetime())
     message_count = 5
     expected_result = [
         msg.id
         for msg in Message.objects.filter(dialog=msg.dialog,
                                           created__lt=msg.created).all()
         [:message_count]
     ]
     received_result = [
         msg.id
         for msg in Message.fetch_messages_from_dialog_created_before(
             message=msg, message_count=message_count)
     ]
     self.assertEqual(expected_result, received_result)
示例#16
0
    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)
示例#17
0
 def test_like_should_create_notification(self):
     nw_item = NewsfeedItem.objects.create(
         author=self.auth_user,
         type=NW_ITEM_TEXT_POST,
         content={'content': 'Hello I like turtles'})
     nw_item.like(UserFactory())
     self.assertEqual(Notification.objects.count(), 1)
     self.assertEqual(Notification.objects.first().type,
                      RECEIVE_NW_ITEM_LIKE_NOTIFICATION)
示例#18
0
    def test_add_comment_adds_comment_and_creates_notification(self):
        f_submission: Submission = SubmissionFactory(author=self.auth_user, challenge=self.challenge, result_score=50)
        us = UserFactory()
        f_submission.add_comment(author=us, content='Hello')

        self.assertEqual(f_submission.comments.count(), 1)
        self.assertEqual(f_submission.comments.first().author, us)
        self.assertEqual(f_submission.comments.first().content, 'Hello')
        self.assertEqual(Notification.objects.count(), 1)
        self.assertEqual(Notification.objects.first().type, RECEIVE_SUBMISSION_COMMENT_NOTIFICATION)
示例#19
0
    def test_token_is_valid_forged_token_should_return_false(self):
        # creating a separate token with the same username should not be a valid token
        owner_name, opponent_name = 'owner', 'opponent'
        owner, opponent = UserFactory(username=owner_name), UserFactory(
            username=opponent_name)
        dialog = Dialog.objects.create(owner=owner, opponent=opponent)
        owner_token = jwt.encode(
            {
                'exp': datetime.utcnow() + timedelta(days=1),
                'username': owner_name
            }, dialog.secret_key)
        opponent_token = jwt.encode(
            {
                'exp': datetime.utcnow() + timedelta(days=1),
                'username': opponent_name
            }, dialog.secret_key)

        self.assertFalse(dialog.token_is_valid(opponent_token))
        self.assertFalse(dialog.token_is_valid(owner_token))
示例#20
0
    def test_add_reply_adds_reply(self):
        f_submission: Submission = SubmissionFactory(author=self.auth_user, challenge=self.challenge, result_score=50)
        sb_comment = SubmissionComment.objects.create(submission=f_submission, author=self.auth_user, content='aa')

        sb_comment.add_reply(author=UserFactory(), content='Light it up')
        self.assertEqual(sb_comment.replies.count(), 1)
        self.assertEqual(sb_comment.replies.first().content, 'Light it up')
        # should also create a notification
        self.assertEqual(Notification.objects.count(), 1)
        self.assertEqual(Notification.objects.first().type, RECEIVE_SUBMISSION_COMMENT_REPLY_NOTIFICATION)
示例#21
0
    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()
示例#22
0
    def test_fetch_top_submissions(self):
        """
        The method should return the top submissions for a given challenge,
            selecting the top submission for each user
        """
        """ Arrange """
        f_submission = SubmissionFactory(author=self.auth_user, challenge=self.challenge, result_score=50)
        # Second user with submissions
        s_user = UserFactory()
        SubmissionFactory(author=s_user, challenge=self.challenge)  # should get ignored
        top_submission = SubmissionFactory(author=s_user, challenge=self.challenge, result_score=51)
        # Third user with equal to first submission
        t_user = UserFactory()
        tr_sub = SubmissionFactory(challenge=self.challenge, author=t_user, result_score=50)

        expected_submissions = [top_submission, f_submission, tr_sub]  # ordered by score, then by date (oldest first)

        received_submissions = list(Submission.fetch_top_submissions_for_challenge(self.challenge.id))
        self.assertEqual(expected_submissions, received_submissions)
示例#23
0
 def test_create_comment_doesnt_create_notification_if_specified(self):
     self.nw_item = NewsfeedItem.objects.create(
         author=self.auth_user,
         type=NW_ITEM_TEXT_POST,
         content={'content': 'Hello I like turtles'})
     sec_user = UserFactory()
     self.nw_item.add_comment(author=sec_user,
                              content='HelloHello',
                              to_notify=False)
     self.assertEqual(Notification.objects.count(), 0)
示例#24
0
    def test_creates_a_notification_for_every_user(self, mock_create_notif):
        # create 11 users
        users = []
        for i in range(11):
            users.append(UserFactory())

        notify_users_for_new_challenge('sample_challenge')

        for i in range(11):
            self.assertEqual(
                mock_create_notif.mock_calls[i],
                mock.call(recipient=users[i], challenge='sample_challenge'))
示例#25
0
    def setUp(self):
        challenge_cat = MainCategory.objects.create(name='Tests')
        self.python_language = Language.objects.create(name="Python")

        self.sub_cat = SubCategory.objects.create(name='tests', meta_category=challenge_cat)
        Proficiency.objects.create(name='starter', needed_percentage=0)
        self.c1 = ChallengeFactory(category=self.sub_cat)

        self.create_user_and_auth_token()
        submission_user = UserFactory()
        self.submission = Submission.objects.create(language=self.python_language, challenge=self.c1, author=submission_user,
                                                    code="", pending=False)
示例#26
0
 def test_create_comment_adds_notification(self):
     self.nw_item = NewsfeedItem.objects.create(
         author=self.auth_user,
         type=NW_ITEM_TEXT_POST,
         content={'content': 'Hello I like turtles'})
     sec_user = UserFactory()
     comment = self.nw_item.add_comment(author=sec_user,
                                        content='HelloHello')
     self.assertEqual(Notification.objects.count(), 1)
     notif = Notification.objects.first()
     self.assertEqual(notif.type, RECEIVE_NW_ITEM_COMMENT_NOTIFICATION)
     self.assertEqual(notif.recipient, self.nw_item.author)
 def test_add_reply(self):
     sec_user = UserFactory()
     new_comment = self.challenge.add_comment(author=sec_user, content='Frozen')
     new_reply = new_comment.add_reply(author=self.auth_user, content='Stone')
     self.assertEqual(new_reply.replies.count(), 0)
     self.assertEqual(new_comment.replies.count(), 1)
     self.assertEqual(new_comment.replies.first(), new_reply)
     self.assertEqual(new_reply.content, 'Stone')
     self.assertEqual(new_reply.parent, new_comment)
     self.assertEqual(new_reply.author, self.auth_user)
     # assert it creates a notification
     self.assertEqual(Notification.objects.count(), 1)
     self.assertEqual(Notification.objects.first().type, RECEIVE_CHALLENGE_COMMENT_REPLY_NOTIFICATION)
     self.assertEqual(Notification.objects.first().recipient, sec_user)
示例#28
0
 def setUp(self):
     self.create_user_and_auth_token()
     self.second_user = UserFactory()
     self.nw_item = NewsfeedItem.objects.create(
         author=self.auth_user,
         type=NW_ITEM_TEXT_POST,
         content={'content': 'Hello I like turtles'})
     self.comment_1 = NewsfeedItemComment.objects.create(
         author=self.auth_user, content='name', newsfeed_item=self.nw_item)
     self.comment_2 = NewsfeedItemComment.objects.create(
         author=self.auth_user, content='name', newsfeed_item=self.nw_item)
     self.comment_3 = NewsfeedItemComment.objects.create(
         author=self.auth_user,
         content='Drop the top',
         newsfeed_item=self.nw_item)
示例#29
0
 def setUp(self):
     self.c1 = MainCategory.objects.create(name='Tank')
     self.sub1 = SubCategory.objects.create(name='Unit', meta_category=self.c1)
     # create two different challenges
     self.chal = Challenge.objects.create(name='Hello', difficulty=1, score=200, test_case_count=5, category=self.sub1,
                       description=ChallengeDescFactory(), test_file_name='tank')
     self.chal2 = Challenge.objects.create(name='Hello2', difficulty=1, score=200, test_case_count=5, category=self.sub1,
                       description=ChallengeDescFactory(), test_file_name='tank2')
     self.max_challenge_score = 400
     self.starter_prof = Proficiency.objects.create(name='starter', needed_percentage=0)
     self.mid_prof = Proficiency.objects.create(name='mid', needed_percentage=50)
     self.mid_prof_award = SubcategoryProficiencyAward.objects.create(subcategory=self.sub1, proficiency=self.mid_prof,
                                                                 xp_reward=1000)
     self.top_prof = Proficiency.objects.create(name='top', needed_percentage=100)
     self.user: User = UserFactory()
     self.user.save()
示例#30
0
    def test_create_comment(self):
        # create a solved submission for auth_user to give him access
        Submission.objects.create(language=self.python_language, challenge=self.c1, author=self.auth_user,
                                  result_score=self.c1.score, pending=False, code="")

        us = UserFactory()
        Submission.objects.create(language=self.python_language, challenge=self.c1,
                                  author=us, code="", result_score=self.c1.score, pending=False)

        response = self.client.post(f'/challenges/{self.c1.id}/submissions/{self.submission.id}/comments',
                                    HTTP_AUTHORIZATION=self.auth_token,
                                    data={'content': 'Hello World'})

        self.assertEqual(response.status_code, 201)
        self.assertEqual(self.submission.comments.count(), 1)
        self.assertEqual(self.submission.comments.first().author, self.auth_user)
        self.assertEqual(self.submission.comments.first().content, 'Hello World')
        self.assertEqual(Notification.objects.count(), 1)
        self.assertEqual(Notification.objects.first().type, RECEIVE_SUBMISSION_COMMENT_NOTIFICATION)