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())
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()
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)
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_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))
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)
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())
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))
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) ]
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())
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')
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)
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")
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())
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)
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_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)
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)
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))
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)
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()
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)
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)
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'))
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)
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)
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)
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()
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)