Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_comment_move(self):
"""
comment move to another topic
"""
utils.login(self)
self.user.st.is_moderator = True
self.user.save()
Topic.objects.filter(pk=self.topic.pk).update(comment_count=2)
comment = utils.create_comment(user=self.user, topic=self.topic)
comment2 = utils.create_comment(user=self.user, topic=self.topic)
to_topic = utils.create_topic(category=self.category)
form_data = {'topic': to_topic.pk,
'comments': [comment.pk, comment2.pk], }
response = self.client.post(reverse('spirit:comment:move', kwargs={'topic_id': self.topic.pk, }),
form_data)
expected_url = self.topic.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302)
self.assertEqual(Comment.objects.filter(topic=to_topic.pk).count(), 2)
self.assertEqual(Comment.objects.filter(topic=self.topic.pk).count(), 0)
self.assertEqual(Topic.objects.get(pk=self.topic.pk).comment_count, 0)
def test_category_detail_view_removed_topics(self):
"""
should not display removed topics or from other categories
"""
subcategory_removed = utils.create_subcategory(self.category_1, is_removed=True)
utils.create_topic(category=subcategory_removed)
utils.create_topic(category=self.category_1, is_removed=True)
utils.create_topic(category=self.category_2)
response = self.client.get(reverse('spirit:category:detail', kwargs={'pk': self.category_1.pk,
'slug': self.category_1.slug}))
self.assertEqual(list(response.context['topics']), [])
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)
self.comment = utils.create_comment(topic=self.topic)
self.topic_notification = TopicNotification.objects.create(
user=self.user, topic=self.topic,
comment=self.comment, is_active=True,
action=COMMENT, is_read=True)
self.topic_notification2 = TopicNotification.objects.create(
user=self.user2, topic=self.topic,
comment=self.comment, is_active=True,
action=COMMENT, is_read=True)
def test_topic_is_visited(self):
"""
Should return True when the topic has been visited
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
self.assertFalse(Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().is_visited)
CommentBookmark.objects.create(topic=topic, user=self.user)
self.assertTrue(Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().is_visited)
def test_topic_active_view_dont_show_not_global(self):
"""
Should not display non-global categories topics
"""
# Global subcategories from non-global categories should be displayed
category_non_global = utils.create_category(is_global=False)
subcategory_global = utils.create_category(parent=category_non_global)
utils.create_topic(category=category_non_global)
topic = utils.create_topic(category=subcategory_global)
response = self.client.get(reverse('spirit:topic:index-active'))
self.assertEqual(list(response.context['topics']), [topic])
def test_indexing_text_template(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>')
self.assertEqual(
render_to_string(
'search/indexes/spirit_topic/topic_text.txt',
context={'object': topic}),
'my title\n\nbar\n\nfoo\n\n')
def test_topic_viewed(self):
"""
* Should update/create the comment bookmark
* Should mark the topic notification as read
* Should create or mark the topic (unread) as read
* Should increase the view_counter
"""
req = RequestFactory().get('/?page=1')
req.user = self.user
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
comment = utils.create_comment(topic=topic)
notification = TopicNotification.objects.create(user=topic.user, topic=topic, comment=comment, is_read=False)
unread = TopicUnread.objects.create(user=topic.user, topic=topic, is_read=False)
utils_topic.topic_viewed(req, topic)
self.assertEqual(len(CommentBookmark.objects.filter(user=self.user, topic=topic)), 1)
self.assertTrue(TopicNotification.objects.get(pk=notification.pk).is_read)
self.assertTrue(TopicUnread.objects.get(pk=unread.pk).is_read)
self.assertEqual(Topic.objects.get(pk=topic.pk).view_count, 1)
def test_topic_notification_list_dont_show_topic_removed_or_no_access(self):
"""
dont show private topics if user has no access or is removed
"""
TopicNotification.objects.all().delete()
category = utils.create_category()
category_removed = utils.create_category(is_removed=True)
subcategory = utils.create_category(parent=category_removed)
subcategory_removed = utils.create_category(
parent=category, is_removed=True)
topic_a = utils.create_private_topic()
topic_b = utils.create_topic(category=category, is_removed=True)
topic_c = utils.create_topic(category=category_removed)
topic_d = utils.create_topic(category=subcategory)
topic_e = utils.create_topic(category=subcategory_removed)
TopicNotification.objects.create(
user=self.user, topic=topic_a.topic,
comment=self.comment, is_active=True, action=COMMENT)
TopicNotification.objects.create(
user=self.user, topic=topic_b,
comment=self.comment, is_active=True, action=COMMENT)
TopicNotification.objects.create(
user=self.user, topic=topic_c,
comment=self.comment, is_active=True, action=COMMENT)
TopicNotification.objects.create(
user=self.user, topic=topic_d,
comment=self.comment, is_active=True, action=COMMENT)
TopicNotification.objects.create(
user=self.user, topic=topic_e,
comment=self.comment, is_active=True, action=COMMENT)
self.assertEqual(
def test_topic_active_view_pinned(self):
"""
Show globally pinned topics first, regular pinned topics are shown as regular topics
"""
category = utils.create_category()
topic_a = utils.create_topic(category=category)
topic_b = utils.create_topic(category=category, is_pinned=True)
topic_c = utils.create_topic(category=category)
topic_d = utils.create_topic(category=category, is_globally_pinned=True)
# show globally pinned first
Topic.objects.filter(pk=topic_d.pk).update(last_active=timezone.now() - datetime.timedelta(days=10))
response = self.client.get(reverse('spirit:topic:index-active'))
self.assertEqual(list(response.context['topics']), [topic_d, topic_c, topic_b, topic_a])