Spirit/spirit/topic/tests.py

657 lines
26 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
2014-11-16 20:36:42 +00:00
from __future__ import unicode_literals
2014-05-09 21:34:48 +00:00
import datetime
import hashlib
2014-05-09 21:34:48 +00:00
from django.test import TestCase, RequestFactory, override_settings
2014-05-09 21:34:48 +00:00
from django.core.urlresolvers import reverse
from django.utils import timezone
2014-08-04 13:44:30 +00:00
from django.core.exceptions import ObjectDoesNotExist
2015-07-11 03:03:08 +00:00
2015-01-09 02:41:54 +00:00
from djconfig.utils import override_djconfig
2015-07-11 03:03:08 +00:00
from ..core.tests import utils
2015-07-21 02:57:04 +00:00
from . import utils as utils_topic
2015-07-11 03:03:08 +00:00
from ..comment.models import MOVED
from .models import Topic
from .forms import TopicForm
from ..comment.models import Comment
from ..comment.bookmark.models import CommentBookmark
2015-07-21 02:57:04 +00:00
from .notification.models import TopicNotification
from .unread.models import TopicUnread
2014-05-09 21:34:48 +00:00
class TopicViewTest(TestCase):
def setUp(self):
utils.cache_clear()
2014-05-09 21:34:48 +00:00
self.user = utils.create_user()
@override_settings(ST_TESTS_RATELIMIT_NEVER_EXPIRE=True)
2014-05-09 21:34:48 +00:00
def test_topic_publish(self):
"""
POST, create topic
"""
self.assertEqual(len(Topic.objects.all()), 0)
2014-05-09 21:34:48 +00:00
utils.login(self)
category = utils.create_category()
2015-09-08 14:52:16 +00:00
form_data = {'comment': 'foo', 'title': 'foobar', 'category': category.pk}
response = self.client.post(reverse('spirit:topic:publish'), form_data)
2014-05-09 21:34:48 +00:00
topic = Topic.objects.last()
expected_url = topic.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302)
self.assertEqual(len(Topic.objects.all()), 1)
2014-05-22 18:53:51 +00:00
2014-05-09 21:34:48 +00:00
# ratelimit
response = self.client.post(reverse('spirit:topic:publish'), form_data)
2014-05-09 21:34:48 +00:00
self.assertEqual(response.status_code, 200)
self.assertEqual(len(Topic.objects.all()), 1)
2014-05-09 21:34:48 +00:00
2014-05-22 18:53:51 +00:00
# get
2015-07-12 22:24:37 +00:00
response = self.client.get(reverse('spirit:topic:publish'))
2014-05-22 18:53:51 +00:00
self.assertEqual(response.status_code, 200)
@override_settings(ST_TESTS_RATELIMIT_NEVER_EXPIRE=True)
def test_topic_publish_validate(self):
"""
Should validate all forms even when errors
"""
self.assertEqual(len(Topic.objects.all()), 0)
utils.login(self)
no_data = {}
response = self.client.post(reverse('spirit:topic:publish'), no_data)
self.assertEqual(len(Topic.objects.all()), 0)
self.assertTrue(bool(response.context['form'].errors))
self.assertTrue(bool(response.context['cform'].errors))
self.assertEqual(len(list(response.context['messages'])), 0)
# No rate-limit
category = utils.create_category()
form_data = {'comment': 'foo', 'title': 'foobar', 'category': category.pk}
self.client.post(reverse('spirit:topic:publish'), form_data)
self.assertEqual(len(Topic.objects.all()), 1)
2014-05-22 18:53:51 +00:00
def test_topic_publish_long_title(self):
"""
POST, create topic with long title
"""
utils.login(self)
category = utils.create_category()
title = "a" * 255
2015-09-08 14:52:16 +00:00
form_data = {'comment': 'foo', 'title': title, 'category': category.pk}
2015-07-12 22:24:37 +00:00
response = self.client.post(reverse('spirit:topic:publish'),
2014-05-22 18:53:51 +00:00
form_data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(Topic.objects.all()), 1)
2014-05-22 18:59:21 +00:00
self.assertEqual(Topic.objects.last().slug, title[:50])
2014-05-22 18:53:51 +00:00
@override_settings(ST_TESTS_RATELIMIT_NEVER_EXPIRE=True)
2014-05-09 21:34:48 +00:00
def test_topic_publish_in_category(self):
"""
POST, create topic in category
"""
utils.login(self)
category = utils.create_category()
2015-09-08 14:52:16 +00:00
form_data = {'comment': 'foo', 'title': 'foobar', 'category': category.pk}
2015-07-12 22:24:37 +00:00
response = self.client.post(reverse('spirit:topic:publish', kwargs={'category_id': category.pk, }),
2014-05-09 21:34:48 +00:00
form_data)
topic = Topic.objects.last()
expected_url = topic.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302)
2014-05-22 18:53:51 +00:00
2014-05-09 21:34:48 +00:00
# ratelimit
2015-07-12 22:24:37 +00:00
response = self.client.post(reverse('spirit:topic:publish', kwargs={'category_id': category.pk, }),
2014-05-09 21:34:48 +00:00
form_data)
self.assertEqual(response.status_code, 200)
def test_topic_publish_in_subcategory(self):
"""
POST, create topic in subcategory
"""
utils.login(self)
category = utils.create_category()
subcategory = utils.create_subcategory(category)
2015-09-08 14:52:16 +00:00
form_data = {'comment': 'foo', 'title': 'foobar', 'category': subcategory.pk}
2015-07-12 22:24:37 +00:00
response = self.client.post(reverse('spirit:topic:publish', kwargs={'category_id': subcategory.pk, }),
2014-05-09 21:34:48 +00:00
form_data)
topic = Topic.objects.last()
expected_url = topic.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302)
def test_topic_publish_invalid_category(self):
"""
invalid topic category
"""
utils.login(self)
2015-07-12 22:24:37 +00:00
response = self.client.get(reverse('spirit:topic:publish', kwargs={'category_id': str(99), }))
2014-05-09 21:34:48 +00:00
self.assertEqual(response.status_code, 404)
@override_settings(ST_DOUBLE_POST_THRESHOLD_MINUTES=10)
def test_topic_publish_double_post(self):
"""
Should prevent double posts
"""
utils.login(self)
category = utils.create_category()
topic_title = 'title foobar'
# First post
self.client.post(
reverse('spirit:topic:publish'),
{'comment': 'foo', 'title': topic_title, 'category': category.pk})
self.assertEqual(len(Topic.objects.all()), 1)
# Double post
utils.cache_clear() # Clear rate limit
response = self.client.post(
reverse('spirit:topic:publish'),
{'comment': 'foo', 'title': topic_title, 'category': category.pk})
self.assertEqual(len(Topic.objects.all()), 1) # Prevented!
self.assertRedirects(
response,
expected_url=category.get_absolute_url(),
status_code=302,
target_status_code=200)
# New post
utils.cache_clear() # Clear rate limit
self.client.post(
reverse('spirit:topic:publish'),
{'comment': 'foo', 'title': 'new topic', 'category': category.pk})
self.assertEqual(len(Topic.objects.all()), 2)
@override_settings(ST_DOUBLE_POST_THRESHOLD_MINUTES=10)
def test_topic_publish_same_post_into_another_topic(self):
"""
Should not prevent from posting the same topic into another category
"""
utils.login(self)
category = utils.create_category()
category_another = utils.create_category()
topic_title = 'title foobar'
self.client.post(
reverse('spirit:topic:publish'),
{'comment': 'foo', 'title': topic_title, 'category': category.pk})
self.assertEqual(len(Topic.objects.all()), 1)
utils.cache_clear() # Clear rate limit
self.client.post(
reverse('spirit:topic:publish'),
{'comment': 'foo', 'title': topic_title, 'category': category_another.pk})
self.assertEqual(len(Topic.objects.all()), 2)
2014-05-09 21:34:48 +00:00
def test_topic_update(self):
"""
POST, update topic
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
form_data = {'title': 'foobar', }
2015-07-12 22:24:37 +00:00
response = self.client.post(reverse('spirit:topic:update', kwargs={'pk': topic.pk, }),
2014-05-09 21:34:48 +00:00
form_data)
self.assertRedirects(response, topic.get_absolute_url(), status_code=302)
2015-07-13 23:13:37 +00:00
def test_topic_update_create_moderation_action(self):
2014-05-09 21:34:48 +00:00
"""
POST, topic moved to category
"""
utils.login(self)
2015-03-30 06:07:50 +00:00
self.user.st.is_moderator = True
2014-05-09 21:34:48 +00:00
self.user.save()
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
category2 = utils.create_category()
form_data = {'title': 'foobar', 'category': category2.pk}
2015-07-12 22:24:37 +00:00
self.client.post(reverse('spirit:topic:update', kwargs={'pk': topic.pk, }),
form_data)
2015-07-13 23:13:37 +00:00
self.assertEqual(len(Comment.objects.filter(user=self.user, topic_id=topic.pk, action=MOVED)), 1)
2014-05-09 21:34:48 +00:00
def test_topic_update_invalid_user(self):
"""
POST, update topic
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category)
form_data = {'title': 'foobar', }
2015-07-12 22:24:37 +00:00
response = self.client.post(reverse('spirit:topic:update', kwargs={'pk': topic.pk, }),
2014-05-09 21:34:48 +00:00
form_data)
self.assertEqual(response.status_code, 404)
def test_topic_detail_view(self):
"""
should display topic with comments
2014-05-09 21:34:48 +00:00
"""
utils.login(self)
category = utils.create_category()
2014-05-09 21:34:48 +00:00
topic = utils.create_topic(category=category)
topic2 = utils.create_topic(category=category)
comment1 = utils.create_comment(topic=topic)
comment2 = utils.create_comment(topic=topic)
utils.create_comment(topic=topic2)
2015-07-12 22:24:37 +00:00
response = self.client.get(reverse('spirit:topic:detail', kwargs={'pk': topic.pk, 'slug': topic.slug}))
2014-05-22 18:53:51 +00:00
self.assertEqual(response.status_code, 200)
2014-05-09 21:34:48 +00:00
self.assertEqual(response.context['topic'], topic)
self.assertEqual(list(response.context['comments']), [comment1, comment2])
2014-05-09 21:34:48 +00:00
2015-01-09 02:41:54 +00:00
@override_djconfig(comments_per_page=2)
def test_topic_detail_view_paginate(self):
"""
should display topic with comments, page 1
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category)
comment1 = utils.create_comment(topic=topic)
comment2 = utils.create_comment(topic=topic)
utils.create_comment(topic=topic) # comment3
2015-07-12 22:24:37 +00:00
response = self.client.get(reverse('spirit:topic:detail', kwargs={'pk': topic.pk, 'slug': topic.slug}))
2015-01-09 02:41:54 +00:00
self.assertEqual(response.status_code, 200)
self.assertEqual(list(response.context['comments']), [comment1, comment2])
2015-01-09 02:41:54 +00:00
2015-07-21 02:57:04 +00:00
def test_topic_detail_viewed(self):
2014-05-09 21:34:48 +00:00
"""
2015-07-21 02:57:04 +00:00
Calls utils.topic_viewed
2014-05-09 21:34:48 +00:00
"""
2015-07-21 02:57:04 +00:00
def mocked_topic_viewed(request, topic):
self._user = request.user
self._topic = topic
2014-05-09 21:34:48 +00:00
2015-07-21 02:57:04 +00:00
org_viewed, utils_topic.topic_viewed = utils_topic.topic_viewed, mocked_topic_viewed
try:
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
response = self.client.get(reverse('spirit:topic:detail', kwargs={'pk': topic.pk, 'slug': topic.slug}))
self.assertEqual(response.status_code, 200)
self.assertEqual(self._topic, topic)
self.assertEqual(self._user, self.user)
finally:
utils_topic.topic_viewed = org_viewed
2014-05-09 21:34:48 +00:00
def test_topic_detail_view_invalid_slug(self):
"""
invalid slug
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category)
2015-07-12 22:24:37 +00:00
response = self.client.get(reverse('spirit:topic:detail', kwargs={'pk': topic.pk,
2014-05-22 18:53:51 +00:00
'slug': 'bar'}))
self.assertRedirects(response, topic.get_absolute_url(), status_code=301)
def test_topic_detail_view_no_slug(self):
"""
no slug
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category)
2015-07-12 22:24:37 +00:00
response = self.client.get(reverse('spirit:topic:detail', kwargs={'pk': topic.pk,
2014-05-22 18:53:51 +00:00
'slug': ''}))
2014-05-09 21:34:48 +00:00
self.assertRedirects(response, topic.get_absolute_url(), status_code=301)
def test_topic_active_view(self):
"""
topics ordered by activity
"""
category = utils.create_category()
topic_a = utils.create_topic(category=category)
topic_b = utils.create_topic(category=category, user=self.user, view_count=10)
topic_c = utils.create_topic(category=category)
Topic.objects.filter(pk=topic_a.pk).update(last_active=timezone.now() - datetime.timedelta(days=10))
Topic.objects.filter(pk=topic_c.pk).update(last_active=timezone.now() - datetime.timedelta(days=5))
2015-07-12 22:24:37 +00:00
response = self.client.get(reverse('spirit:topic:index-active'))
self.assertEqual(list(response.context['topics']), [topic_b, topic_c, topic_a])
2014-05-09 21:34:48 +00:00
def test_topic_active_view_pinned(self):
"""
2014-12-20 23:02:27 +00:00
Show globally pinned topics first, regular pinned topics are shown as regular topics
2014-05-09 21:34:48 +00:00
"""
category = utils.create_category()
topic_a = utils.create_topic(category=category)
2014-12-20 23:02:27 +00:00
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
2014-05-09 21:34:48 +00:00
Topic.objects.filter(pk=topic_d.pk).update(last_active=timezone.now() - datetime.timedelta(days=10))
2015-07-12 22:24:37 +00:00
response = self.client.get(reverse('spirit:topic:index-active'))
self.assertEqual(list(response.context['topics']), [topic_d, topic_c, topic_b, topic_a])
2014-05-09 21:34:48 +00:00
def test_topic_active_view_dont_show_private_or_removed(self):
"""
dont show private topics or removed
"""
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)
utils.create_private_topic()
utils.create_topic(category=category, is_removed=True)
utils.create_topic(category=category_removed)
utils.create_topic(category=subcategory)
utils.create_topic(category=subcategory_removed)
2014-05-09 21:34:48 +00:00
2015-07-12 22:24:37 +00:00
response = self.client.get(reverse('spirit:topic:index-active'))
self.assertEqual(list(response.context['topics']), [])
2014-05-09 21:34:48 +00:00
2015-09-13 20:13:56 +00:00
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])
2015-01-10 18:32:03 +00:00
def test_topic_active_view_bookmark(self):
"""
topics with bookmarks
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
bookmark = CommentBookmark.objects.create(topic=topic, user=self.user)
user2 = utils.create_user()
CommentBookmark.objects.create(topic=topic, user=user2)
topic2 = utils.create_topic(category=category, user=self.user)
CommentBookmark.objects.create(topic=topic2, user=self.user)
ten_days_ago = timezone.now() - datetime.timedelta(days=10)
Topic.objects.filter(pk=topic2.pk).update(last_active=ten_days_ago)
2015-07-12 22:24:37 +00:00
response = self.client.get(reverse('spirit:topic:index-active'))
self.assertEqual(list(response.context['topics']), [topic, topic2])
2015-01-10 18:32:03 +00:00
self.assertEqual(response.context['topics'][0].bookmark, bookmark)
2015-01-11 00:47:52 +00:00
@override_djconfig(topics_per_page=1)
def test_topic_active_view_paginate(self):
"""
topics ordered by activity paginated
"""
category = utils.create_category()
topic_a = utils.create_topic(category=category)
topic_b = utils.create_topic(category=category, user=self.user, view_count=10)
2015-07-12 22:24:37 +00:00
response = self.client.get(reverse('spirit:topic:index-active'))
self.assertEqual(list(response.context['topics']), [topic_b, ])
2015-01-11 00:47:52 +00:00
2014-05-09 21:34:48 +00:00
class TopicFormTest(TestCase):
def setUp(self):
utils.cache_clear()
2014-05-09 21:34:48 +00:00
self.user = utils.create_user()
def test_topic_publish(self):
"""
create topic
"""
category = utils.create_category()
subcategory = utils.create_subcategory(category)
form_data = {'comment': 'foo', 'title': 'foobar',
'category': subcategory.pk}
form = TopicForm(self.user, data=form_data)
self.assertEqual(form.is_valid(), True)
def test_topic_publish_invalid_closed_subcategory(self):
2014-05-09 21:34:48 +00:00
"""
invalid closed subcategory
2014-05-09 21:34:48 +00:00
"""
category = utils.create_category()
subcategory = utils.create_subcategory(category, is_closed=True)
form_data = {'comment': 'foo', 'title': 'foobar',
'category': subcategory.pk}
form = TopicForm(self.user, data=form_data)
self.assertEqual(form.is_valid(), False)
self.assertNotIn('category', form.cleaned_data)
def test_topic_publish_invalid_removed_subcategory(self):
"""
invalid removed subcategory
"""
category = utils.create_category()
subcategory = utils.create_subcategory(category, is_removed=True)
2014-05-09 21:34:48 +00:00
form_data = {'comment': 'foo', 'title': 'foobar',
'category': subcategory.pk}
form = TopicForm(self.user, data=form_data)
self.assertEqual(form.is_valid(), False)
self.assertNotIn('category', form.cleaned_data)
def test_topic_update(self):
"""
create update
"""
category = utils.create_category()
topic = utils.create_topic(category)
form_data = {'title': 'foobar', }
form = TopicForm(self.user, data=form_data, instance=topic)
self.assertEqual(form.is_valid(), True)
def test_topic_get_category(self):
"""
Should return the category
"""
category = utils.create_category()
form_data = {
'title': 'foobar',
'category': category.pk}
form = TopicForm(self.user, data=form_data)
self.assertTrue(form.is_valid())
self.assertEqual(form.get_category(), category)
def test_topic_get_topic_hash(self):
"""
Should return the topic hash
"""
category = utils.create_category()
title = 'title foobar'
form_data = {
'title': title,
'category': category.pk}
form = TopicForm(self.user, data=form_data)
self.assertTrue(form.is_valid())
self.assertEqual(
form.get_topic_hash(),
hashlib.md5(
'{}category-{}'
.format(title, category.pk)
.encode('utf-8')).hexdigest())
def test_topic_get_topic_hash_from_field(self):
"""
Should return the topic hash from form field
"""
category = utils.create_category()
topic_hash = '1' * 32
form_data = {
'title': 'foobar',
'category': category.pk,
'topic_hash': topic_hash}
form = TopicForm(self.user, data=form_data)
self.assertTrue(form.is_valid())
self.assertEqual(form.get_topic_hash(), topic_hash)
def test_topic_updates_reindex_at(self):
"""
Should update reindex_at field
"""
yesterday = timezone.now() - datetime.timedelta(days=1)
category = utils.create_category()
topic = utils.create_topic(category, reindex_at=yesterday)
self.assertEqual(
topic.reindex_at,
yesterday)
form_data = {'title': 'foobar'}
form = TopicForm(self.user, data=form_data, instance=topic)
self.assertEqual(form.is_valid(), True)
form.save()
self.assertGreater(
Topic.objects.get(pk=topic.pk).reindex_at,
yesterday)
2014-05-09 21:34:48 +00:00
2015-07-21 02:57:04 +00:00
class TopicUtilsTest(TestCase):
def setUp(self):
utils.cache_clear()
2015-07-21 02:57:04 +00:00
self.user = utils.create_user()
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)
2015-07-26 02:44:14 +00:00
comment = utils.create_comment(topic=topic)
notification = TopicNotification.objects.create(user=topic.user, topic=topic, comment=comment, is_read=False)
2015-07-21 02:57:04 +00:00
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)
class TopicModelsTest(TestCase):
2014-05-09 21:34:48 +00:00
def setUp(self):
utils.cache_clear()
2014-05-09 21:34:48 +00:00
self.user = utils.create_user()
self.category = utils.create_category()
self.topic = utils.create_topic(category=self.category, user=self.user)
2015-07-21 02:57:04 +00:00
def test_topic_increase_view_count(self):
2014-05-09 21:34:48 +00:00
"""
2015-07-21 02:57:04 +00:00
increase_view_count
2014-05-09 21:34:48 +00:00
"""
2015-07-21 02:57:04 +00:00
self.topic.increase_view_count()
2014-05-09 21:34:48 +00:00
self.assertEqual(Topic.objects.get(pk=self.topic.pk).view_count, 1)
2015-07-22 02:48:35 +00:00
def test_topic_increase_comment_count(self):
2014-05-09 21:34:48 +00:00
"""
2015-07-22 02:48:35 +00:00
increase_comment_count
2014-05-09 21:34:48 +00:00
"""
2015-07-22 02:48:35 +00:00
self.topic.increase_comment_count()
2014-05-09 21:34:48 +00:00
self.assertEqual(Topic.objects.get(pk=self.topic.pk).comment_count, 1)
2014-06-14 23:26:56 +00:00
self.assertGreater(Topic.objects.get(pk=self.topic.pk).last_active, self.topic.last_active)
2015-07-22 20:20:03 +00:00
def test_topic_decrease_comment_count(self):
2014-06-14 23:26:56 +00:00
"""
2015-07-22 20:20:03 +00:00
decrease_comment_count
2014-06-14 23:26:56 +00:00
"""
Topic.objects.filter(pk=self.topic.pk).update(comment_count=10)
2015-07-22 20:20:03 +00:00
self.topic.decrease_comment_count()
self.assertEqual(Topic.objects.get(pk=self.topic.pk).comment_count, 9)
2015-10-26 01:15:47 +00:00
def test_topic_new_comments_count(self):
"""
Should return the new replies count
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user, comment_count=1)
self.assertEqual(
Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().new_comments_count,
0
)
CommentBookmark.objects.create(topic=topic, user=self.user, comment_number=1)
self.assertEqual(
Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().new_comments_count,
0
)
Topic.objects.filter(pk=topic.pk).update(comment_count=2)
self.assertEqual(
Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().new_comments_count,
1
)
# topic.comment_count greater than bookmark.comment_number should return 0
Topic.objects.filter(pk=topic.pk).update(comment_count=0)
self.assertEqual(
Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().new_comments_count,
0
)
def test_topic_has_new_comments(self):
"""
Should return True when there are new replies
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user, comment_count=1)
self.assertFalse(Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().has_new_comments)
CommentBookmark.objects.create(topic=topic, user=self.user, comment_number=1)
self.assertFalse(Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().has_new_comments)
Topic.objects.filter(pk=topic.pk).update(comment_count=2)
self.assertTrue(Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().has_new_comments)
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_get_bookmark_url(self):
"""
Should return the bookmark url
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
# No bookmark
topic_with_bookmark = Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first()
self.assertEqual(topic_with_bookmark.get_bookmark_url(), topic_with_bookmark.get_absolute_url())
# With bookmark
CommentBookmark.objects.create(topic=topic, user=self.user, comment_number=1)
topic_with_bookmark2 = Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first()
self.assertEqual(topic_with_bookmark2.get_bookmark_url(), topic_with_bookmark2.bookmark.get_absolute_url())
# With bookmark and new comment
Topic.objects.filter(pk=topic.pk).update(comment_count=2)
topic_with_bookmark3 = Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first()
self.assertEqual(topic_with_bookmark3.get_bookmark_url(), topic_with_bookmark3.bookmark.get_new_comment_url())