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):
cache.clear()
self.user = utils.create_user()
self.category = utils.create_category()
self.topic = utils.create_topic(category=self.category, user=self.user)
utils.create_comment(topic=self.topic)
utils.create_comment(topic=self.topic)
utils.create_comment(topic=self.topic)
def test_flag_open(self):
"""
Open flags
"""
comment = utils.create_comment(topic=self.topic)
comment2 = utils.create_comment(topic=self.topic)
CommentFlag.objects.create(comment=comment2, is_closed=True)
flag_ = CommentFlag.objects.create(comment=comment)
utils.login(self)
response = self.client.get(reverse('spirit:admin:flag:opened'))
self.assertEqual(list(response.context['flags']), [flag_, ])
def test_indexing_text_include_comments(self):
"""
Should include topic title and all comments
"""
category = utils.create_category()
topic = utils.create_topic(category, title='my title')
utils.create_comment(topic=topic, comment_html='<span>foo</span>')
utils.create_comment(topic=topic, comment_html='<b>bar</b>')
rebuild_index()
self.assertEqual(len(self.topic_sqs.all()), 1)
self.assertEqual(
len(self.topic_sqs.filter(text='my title foo bar')), 1)
self.assertEqual(
len(self.topic_sqs.filter(text='bar')), 1)
self.assertEqual(
len(self.topic_sqs.filter(text='<b>')), 0)
self.assertEqual(
len(self.topic_sqs.filter(text='span')), 0)
</b>
def test_private_detail(self):
"""
private topic detail
"""
utils.login(self)
private = utils.create_private_topic(user=self.user)
comment1 = utils.create_comment(topic=private.topic)
comment2 = utils.create_comment(topic=private.topic)
category = utils.create_category()
topic2 = utils.create_topic(category=category)
utils.create_comment(topic=topic2)
response = self.client.get(reverse('spirit:topic:private:detail', kwargs={'topic_id': private.topic.pk,
'slug': private.topic.slug}))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['topic'], private.topic)
self.assertEqual(list(response.context['comments']), [comment1, comment2])
def test_profile_likes(self):
"""
profile user's likes
"""
utils.login(self)
comment = utils.create_comment(user=self.user, topic=self.topic)
comment2 = utils.create_comment(user=self.user2, topic=self.topic)
like = CommentLike.objects.create(user=self.user2, comment=comment)
CommentLike.objects.create(user=self.user, comment=comment2)
response = self.client.get(reverse(
"spirit:user:likes",
kwargs={'pk': self.user2.pk, 'slug': self.user2.st.slug}))
self.assertEqual(response.status_code, 200)
self.assertEqual(list(response.context['comments']), [like.comment, ])
self.assertEqual(response.context['p_user'], self.user2)
def test_comment_decrease_likes_count(self):
"""
Decrease like_count on remove comment like
"""
comment = utils.create_comment(topic=self.topic, likes_count=1)
comment.decrease_likes_count()
self.assertEqual(Comment.objects.get(pk=comment.pk).likes_count, 0)
def setUp(self):
utils.cache_clear()
self.user = utils.create_user()
self.category = utils.create_category()
self.topic = utils.create_topic(category=self.category, user=self.user)
self.comment = utils.create_comment(topic=self.topic)
def test_comment_increase_likes_count(self):
"""
Increase like_count on comment like
"""
comment = utils.create_comment(topic=self.topic)
comment.increase_likes_count()
self.assertEqual(Comment.objects.get(pk=comment.pk).likes_count, 1)
def test_profile_likes_order(self):
"""
comments ordered by date
"""
comment_a = utils.create_comment(user=self.user, topic=self.topic)
comment_b = utils.create_comment(user=self.user, topic=self.topic)
comment_c = utils.create_comment(user=self.user, topic=self.topic)
like_a = CommentLike.objects.create(user=self.user2, comment=comment_a)
CommentLike.objects.create(user=self.user2, comment=comment_b)
like_c = CommentLike.objects.create(user=self.user2, comment=comment_c)
CommentLike.objects.filter(pk=like_a.pk).update(date=timezone.now() - datetime.timedelta(days=10))
CommentLike.objects.filter(pk=like_c.pk).update(date=timezone.now() - datetime.timedelta(days=5))
utils.login(self)
response = self.client.get(reverse(
"spirit:user:likes",
kwargs={'pk': self.user2.pk, 'slug': self.user2.st.slug}))
self.assertEqual(list(response.context['comments']), [comment_b, comment_c, comment_a])
comment = utils.create_comment(user=subscriber, topic=self.topic)
comment_posted(comment=comment, mentions=None)
self.assertEqual(len(TopicNotification.objects.all()), 1)
self.assertTrue(TopicNotification.objects.get(user=subscriber, topic=self.topic).is_read)
# Should notify subscribers
user = utils.create_user()
comment = utils.create_comment(user=user, topic=self.topic)
comment_posted(comment=comment, mentions=None)
self.assertEqual(len(TopicNotification.objects.all()), 2)
self.assertFalse(TopicNotification.objects.get(user=subscriber, topic=self.topic).is_read)
# Should notify mentions
mentioned = utils.create_user()
mentions = {mentioned.username: mentioned, }
comment = utils.create_comment(user=user, topic=self.topic)
comment_posted(comment=comment, mentions=mentions)
self.assertEqual(TopicNotification.objects.get(user=mentioned, comment=comment).action, MENTION)
self.assertFalse(TopicNotification.objects.get(user=mentioned, comment=comment).is_read)
# Should mark the topic as unread
user_unread = utils.create_user()
topic = utils.create_topic(self.category)
topic_unread_creator = TopicUnread.objects.create(user=user, topic=topic, is_read=True)
topic_unread_subscriber = TopicUnread.objects.create(user=user_unread, topic=topic, is_read=True)
comment = utils.create_comment(user=user, topic=topic)
comment_posted(comment=comment, mentions=None)
self.assertTrue(TopicUnread.objects.get(pk=topic_unread_creator.pk).is_read)
self.assertFalse(TopicUnread.objects.get(pk=topic_unread_subscriber.pk).is_read)
# Should increase topic's comment counter
topic = utils.create_topic(self.category)