Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
unlock topic
"""
utils.login(self)
self.user.st.is_moderator = True
self.user.save()
category = utils.create_category()
topic = utils.create_topic(category, is_closed=True)
form_data = {}
response = self.client.post(
reverse('spirit:topic:moderate:unlock', kwargs={'pk': topic.pk, }),
form_data)
expected_url = topic.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302)
self.assertFalse(Topic.objects.get(pk=topic.pk).is_closed)
self.assertEqual(
len(Comment.objects.filter(user=self.user, topic=topic, action=UNCLOSED)),
1)
"""
topic pin
"""
utils.login(self)
self.user.st.is_moderator = True
self.user.save()
category = utils.create_category()
topic = utils.create_topic(category)
form_data = {}
response = self.client.post(
reverse('spirit:topic:moderate:global-pin', kwargs={'pk': topic.pk, }),
form_data)
expected_url = topic.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302)
self.assertTrue(Topic.objects.get(pk=topic.pk).is_globally_pinned)
self.assertEqual(
len(Comment.objects.filter(user=self.user, topic=topic, action=PINNED)),
1)
"""
utils.login(self)
self.user.st.is_moderator = True
self.user.save()
yesterday = timezone.now() - datetime.timedelta(days=1)
category = utils.create_category()
topic = utils.create_topic(category, is_removed=True, reindex_at=yesterday)
self.assertEqual(topic.reindex_at, yesterday)
response = self.client.post(
reverse('spirit:topic:moderate:undelete', kwargs={'pk': topic.pk}),
data={})
expected_url = topic.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302)
topic = Topic.objects.get(pk=topic.pk)
self.assertFalse(topic.is_removed)
self.assertGreater(topic.reindex_at, yesterday)
* 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_publish_count(self):
"""
Creating or deleting a topic updates the profile topic_count and comment_count
"""
utils.login(self)
category = utils.create_category()
form_data = {'comment': 'foo', 'title': 'foobar', 'category': category.pk}
response = self.client.post(reverse('spirit:topic:publish'),
form_data)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).topic_count, 1)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).comment_count, 1)
Topic.objects.get(user=self.user).delete()
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).topic_count, 0)
self.assertEqual(UserProfile.objects.get(pk=self.user.st.pk).comment_count, 0)
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)
comment = utils.create_comment(user=user, topic=topic)
comment_posted(comment=comment, mentions=None)
self.assertEqual(Topic.objects.get(pk=topic.pk).comment_count, 1)
comment_posted(comment=comment, mentions=None)
self.assertEqual(Topic.objects.get(pk=topic.pk).comment_count, 2)
"""
topic pin
"""
utils.login(self)
self.user.st.is_moderator = True
self.user.save()
category = utils.create_category()
topic = utils.create_topic(category)
form_data = {}
response = self.client.post(
reverse('spirit:topic:moderate:pin', kwargs={'pk': topic.pk, }),
form_data)
expected_url = topic.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302)
self.assertTrue(Topic.objects.get(pk=topic.pk).is_pinned)
self.assertEqual(
len(Comment.objects.filter(user=self.user, topic=topic, action=PINNED)),
1)
# 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)
comment = utils.create_comment(user=user, topic=topic)
comment_posted(comment=comment, mentions=None)
self.assertEqual(Topic.objects.get(pk=topic.pk).comment_count, 1)
comment_posted(comment=comment, mentions=None)
self.assertEqual(Topic.objects.get(pk=topic.pk).comment_count, 2)
def test_topic_decrease_comment_count(self):
"""
decrease_comment_count
"""
Topic.objects.filter(pk=self.topic.pk).update(comment_count=10)
self.topic.decrease_comment_count()
self.assertEqual(Topic.objects.get(pk=self.topic.pk).comment_count, 9)
# 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)
comment = utils.create_comment(user=user, topic=topic)
comment_posted(comment=comment, mentions=None)
self.assertEqual(Topic.objects.get(pk=topic.pk).comment_count, 1)
comment_posted(comment=comment, mentions=None)
self.assertEqual(Topic.objects.get(pk=topic.pk).comment_count, 2)