Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
utils.cache_clear()
self.user = utils.create_user()
self.user2 = utils.create_user()
self.category = utils.create_category()
self.topic = utils.create_topic(self.category, user=self.user)
self.comment = utils.create_comment(topic=self.topic)
self.comment2 = utils.create_comment(topic=self.topic)
# Single choice
self.poll = CommentPoll.objects.create(comment=self.comment, name='foo')
self.poll_choice = CommentPollChoice.objects.create(poll=self.poll, number=1, description="op1")
self.poll_choice2 = CommentPollChoice.objects.create(poll=self.poll, number=2, description="op2")
self.poll_vote = CommentPollVote.objects.create(voter=self.user, choice=self.poll_choice)
self.poll_vote2 = CommentPollVote.objects.create(voter=self.user2, choice=self.poll_choice)
# ...poor man prefetch
self.poll_choice.votes = [self.poll_vote]
self.poll.choices = [self.poll_choice, self.poll_choice2]
# Multi choice
self.poll_multi = CommentPoll.objects.create(comment=self.comment2, name='bar', choice_max=2)
self.poll_multi_choice = CommentPollChoice.objects.create(poll=self.poll_multi, number=1, description="op1")
self.poll_multi_choice2 = CommentPollChoice.objects.create(poll=self.poll_multi, number=2, description="op2")
def setUp(self):
utils.cache_clear()
self.user = utils.create_user()
self.category = utils.create_category()
self.topic = utils.create_topic(category=self.category)
self.user_comment = utils.create_comment(topic=self.topic, user=self.user, comment_html="")
self.user_poll = CommentPoll.objects.create(comment=self.user_comment, name='foo')
self.user_comment_with_polls = (
self.user_comment.__class__.objects
.filter(pk=self.user_comment.pk)
.with_polls(self.user)
.first())
self.request = RequestFactory().get('/')
self.request.user = self.user
def test_post_comment_update(self):
"""
* Should increase modified count
* Should render static polls
* Should create comment history
"""
# Should increase modified count
comment = utils.create_comment(user=self.user, topic=self.topic)
post_comment_update(comment=comment)
self.assertEqual(Comment.objects.get(pk=comment.pk).modified_count, 1)
post_comment_update(comment=comment)
self.assertEqual(Comment.objects.get(pk=comment.pk).modified_count, 2)
# Should render static polls
comment = utils.create_comment(user=self.user, topic=self.topic, comment_html='')
CommentPoll.objects.create(comment=comment, name='foo', title="my poll")
post_comment_update(comment=comment)
self.assertTrue('my poll' in comment.comment_html)
# Should create comment history
comment = utils.create_comment(user=self.user, topic=self.topic)
post_comment_update(comment=comment)
self.assertEqual(len(CommentHistory.objects.filter(comment_fk=comment)), 1)
post_comment_update(comment=comment)
self.assertEqual(len(CommentHistory.objects.filter(comment_fk=comment)), 2)
def test_poll_choice_increase_vote_count(self):
"""
Should increase the vote count of all choices for a given user and poll
"""
poll = CommentPoll.objects.create(comment=self.comment, name='percentage')
choice = CommentPollChoice.objects.create(poll=poll, number=1, description="foobar")
choice2 = CommentPollChoice.objects.create(poll=poll, number=2, description="foobar")
CommentPollVote.objects.create(choice=choice, voter=self.user)
CommentPollVote.objects.create(choice=choice2, voter=self.user)
user2 = utils.create_user()
CommentPollVote.objects.create(choice=choice, voter=user2)
CommentPollChoice.increase_vote_count(poll, self.user)
self.assertEqual(CommentPollChoice.objects.get(pk=self.choice.pk).vote_count, 0)
self.assertEqual(CommentPollChoice.objects.get(pk=choice.pk).vote_count, 1)
self.assertEqual(CommentPollChoice.objects.get(pk=choice2.pk).vote_count, 1)
CommentPollChoice.objects.filter(pk=choice.pk).update(is_removed=True)
CommentPollChoice.increase_vote_count(poll, self.user)
self.assertEqual(CommentPollChoice.objects.get(pk=choice.pk).vote_count, 1)
self.assertEqual(CommentPollChoice.objects.get(pk=choice2.pk).vote_count, 2)
def test_poll_close_post(self):
"""
POST, poll_close
"""
utils.login(self)
poll = CommentPoll.objects.create(comment=self.user_comment, name='foo')
response = self.client.post(reverse('spirit:comment:poll:close', kwargs={'pk': poll.pk, }),
{})
expected_url = poll.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302, target_status_code=302)
self.assertTrue(CommentPoll.objects.get(pk=poll.pk).is_closed)
# Single choice
self.poll = CommentPoll.objects.create(comment=self.comment, name='foo')
self.poll_choice = CommentPollChoice.objects.create(poll=self.poll, number=1, description="op1")
self.poll_choice2 = CommentPollChoice.objects.create(poll=self.poll, number=2, description="op2")
self.poll_vote = CommentPollVote.objects.create(voter=self.user, choice=self.poll_choice)
self.poll_vote2 = CommentPollVote.objects.create(voter=self.user2, choice=self.poll_choice)
# ...poor man prefetch
self.poll_choice.votes = [self.poll_vote]
self.poll.choices = [self.poll_choice, self.poll_choice2]
# Multi choice
self.poll_multi = CommentPoll.objects.create(comment=self.comment2, name='bar', choice_max=2)
self.poll_multi_choice = CommentPollChoice.objects.create(poll=self.poll_multi, number=1, description="op1")
self.poll_multi_choice2 = CommentPollChoice.objects.create(poll=self.poll_multi, number=2, description="op2")
self.poll_multi_choice3 = CommentPollChoice.objects.create(poll=self.poll_multi, number=3, description="op3")
self.poll_multi_vote = CommentPollVote.objects.create(voter=self.user, choice=self.poll_multi_choice)
self.poll_multi_vote2 = CommentPollVote.objects.create(voter=self.user, choice=self.poll_multi_choice2)
self.poll_multi_vote3 = CommentPollVote.objects.create(voter=self.user2, choice=self.poll_multi_choice)
# ...poor man prefetch
self.poll_multi_choice.votes = [self.poll_multi_vote]
self.poll_multi_choice2.votes = [self.poll_multi_vote2]
self.poll_multi.choices = [self.poll_multi_choice, self.poll_multi_choice2]
def test_pre_comment_update(self):
"""
* Should render static polls
* Should create comment history maybe
"""
# Should render static polls
comment = utils.create_comment(user=self.user, topic=self.topic, comment_html='')
CommentPoll.objects.create(comment=comment, name='foo', title="my poll")
pre_comment_update(comment=comment)
self.assertTrue('my poll' in comment.comment_html)
# Should create comment history maybe
comment = utils.create_comment(user=self.user, topic=self.topic)
pre_comment_update(comment=comment)
self.assertEqual(len(CommentHistory.objects.filter(comment_fk=comment)), 1)
pre_comment_update(comment=comment)
self.assertEqual(len(CommentHistory.objects.filter(comment_fk=comment)), 1)
def setUp(self):
utils.cache_clear()
self.user = utils.create_user()
self.user2 = utils.create_user()
self.category = utils.create_category()
self.topic = utils.create_topic(self.category, user=self.user)
self.topic2 = utils.create_topic(self.category, user=self.user2)
self.comment = utils.create_comment(topic=self.topic)
self.user_comment = utils.create_comment(topic=self.topic, user=self.user)
self.poll = CommentPoll.objects.create(comment=self.comment, name='foo')
self.poll_multi = CommentPoll.objects.create(comment=self.comment, name='bar', choice_max=2)
def test_poll_voters_secret(self):
"""
Should forbid view voters of secret poll when is not closed
"""
poll = CommentPoll.objects.create(comment=self.comment, name='foobar', mode=PollMode.SECRET)
poll_choice = CommentPollChoice.objects.create(poll=poll, number=1, description="op1")
utils.login(self)
response = self.client.get(reverse('spirit:comment:poll:voters', kwargs={'pk': poll_choice.pk, }))
self.assertEqual(response.status_code, 403)
def test_poll_is_closed(self):
"""
Should be true when close_at > now
"""
yesterday = timezone.now() - timezone.timedelta(days=1)
tomorrow = timezone.now() + timezone.timedelta(days=1)
poll_old = CommentPoll.objects.create(comment=self.comment, name='bar', close_at=yesterday)
poll_new = CommentPoll.objects.create(comment=self.comment, name='bar2', close_at=tomorrow)
self.assertFalse(self.poll.is_closed)
self.assertTrue(poll_old.is_closed)
self.assertFalse(poll_new.is_closed)