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) # comment notification self.topic_notification = TopicNotification.objects.create( user=self.user, topic=self.topic, comment=self.comment, is_active=True, action=COMMENT) self.topic_notification2 = TopicNotification.objects.create( user=self.user2, topic=self.topic, comment=self.comment, is_active=True, action=COMMENT) # subscription to topic self.topic2 = utils.create_topic(self.category) self.comment2 = utils.create_comment(topic=self.topic2) self.topic_subscrption = TopicNotification.objects.create( user=self.user, topic=self.topic2, comment=self.comment2, is_active=True)
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.user2) self.topic2 = utils.create_topic(self.category)
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) response = self.client.get(reverse('spirit:topic:index-active')) self.assertEqual(list(response.context['topics']), [topic_b])
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_topic_notification_has_notifications_dont_count_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( len( TopicNotification.objects.filter(user=self.user, is_active=True, is_read=False)), 5) self.assertFalse(has_topic_notifications(self.user))
def test_topic_notification_mark_all_as_read(self): TopicNotification.objects.all().delete() user2 = utils.create_user() topic = utils.create_topic(self.category) comment = utils.create_comment(topic=topic) TopicNotification.objects.create(user=user2, topic=topic, comment=comment, is_active=True, action=MENTION) topic0 = utils.create_topic(self.category) comment0 = utils.create_comment(topic=topic0) TopicNotification.objects.create(user=self.user, topic=topic0, comment=comment0, is_active=True, action=COMMENT) topic1 = utils.create_topic(self.category) comment1 = utils.create_comment(topic=topic1) TopicNotification.objects.create(user=self.user, topic=topic1, comment=comment1, is_active=True, action=COMMENT) topic2 = utils.create_topic(self.category) comment2 = utils.create_comment(topic=topic2) TopicNotification.objects.create(user=self.user, topic=topic2, comment=comment2, is_active=True, action=MENTION) self.assertEqual( TopicNotification.objects.filter(user=self.user, is_read=False).count(), 3) self.assertEqual( TopicNotification.objects.filter(user=user2, is_read=False).count(), 1) utils.login(self) response = self.client.post( reverse('spirit:topic:notification:mark-all-as-read')) self.assertRedirects(response, reverse('spirit:topic:notification:index'), status_code=302) self.assertEqual( TopicNotification.objects.filter(user=self.user, is_read=False).count(), 0) self.assertEqual( TopicNotification.objects.filter(user=user2, is_read=False).count(), 1)
def test_topic_notification_notify_new_comment(self): """ Should set is_read=False to all notifiers/users """ creator = utils.create_user() subscriber = utils.create_user() topic = utils.create_topic(self.category) comment = utils.create_comment(user=creator, topic=topic) TopicNotification.objects.create(user=creator, topic=topic, comment=comment, is_active=True, is_read=True) TopicNotification.objects.create(user=subscriber, topic=topic, comment=comment, is_active=True, is_read=True) TopicNotification.notify_new_comment(comment) notification = TopicNotification.objects.get(user=subscriber, topic=topic) self.assertTrue(notification.is_active) self.assertFalse(notification.is_read) self.assertEqual(notification.action, COMMENT) # Author should not be notified of its own comment notification2 = TopicNotification.objects.get(user=creator, topic=topic) self.assertTrue(notification2.is_read)
def test_notification_creation(self): """ create notification """ # Should be ready to suscribe (true) form = NotificationCreationForm() self.assertEqual(form.fields['is_active'].initial, True) category = utils.create_category() topic = utils.create_topic(category) comment = utils.create_comment(topic=topic) form_data = { 'is_active': True, } form = NotificationCreationForm(data=form_data) form.user = self.user form.topic = topic self.assertEqual(form.is_valid(), True) TopicNotification.objects.create(user=self.user, topic=topic, comment=comment, is_active=True, action=COMMENT) form = NotificationCreationForm(data=form_data) form.user = self.user form.topic = topic self.assertEqual(form.is_valid(), False)
def test_topic_notification_ajax_escape(self): """ The receive username and topic title should be escaped """ user = utils.create_user(username="******") topic = utils.create_topic(self.category, title="<tag>Have you met Ted?</tag>") notification = TopicNotification.objects.create( user=self.user, topic=topic, comment=utils.create_comment(topic=topic, user=user), is_active=True, action=COMMENT) utils.login(self) response = self.client.get( reverse('spirit:topic:notification:index-ajax'), HTTP_X_REQUESTED_WITH='XMLHttpRequest') res = json.loads(response.content.decode('utf-8')) expected = { 'user': "******", 'action': notification.action, 'title': "<tag>Have you met Ted?</tag>", 'url': notification.get_absolute_url(), 'is_read': notification.is_read } self.assertDictEqual(res['n'][0], expected)
def test_topic_notification_ajax_order(self): """ order by is_read=False first then by date """ user = utils.create_user() for _ in range(10): topic = utils.create_topic(self.category, user=user) comment = utils.create_comment(topic=topic, user=user) TopicNotification.objects.create(user=self.user, topic=topic, comment=comment, is_active=True, action=COMMENT) TopicNotification.objects.filter(user=self.user).update(is_read=True) old_date = timezone.now() - datetime.timedelta(days=10) (TopicNotification.objects.filter( pk=self.topic_notification.pk).update(is_read=False, date=old_date)) utils.login(self) response = self.client.get( reverse('spirit:topic:notification:index-ajax'), HTTP_X_REQUESTED_WITH='XMLHttpRequest') res = json.loads(response.content.decode('utf-8')) self.assertFalse(res['n'][0]['is_read']) self.assertTrue(res['n'][1]['is_read'])
def test_topic_notification_list_unread(self): """ topic notification list """ topic = utils.create_topic(self.category, user=self.user2) comment = utils.create_comment(topic=topic, user=self.user2) topic_notification = TopicNotification.objects.create(user=self.user, topic=topic, comment=comment, is_active=True, action=COMMENT) utils.login(self) response = self.client.get( reverse('spirit:topic:notification:index-unread')) self.assertEqual(list(response.context['page']), [topic_notification, self.topic_notification]) # fake next page response = self.client.get( reverse('spirit:topic:notification:index-unread'), { 'p': to_page_key(value=topic_notification.date, pk=topic_notification.pk) }) self.assertEqual(list(response.context['page']), [ self.topic_notification, ])
def setUp(self): utils.cache_clear() self.user = utils.create_user() self.user.st.is_administrator = True self.user.st.save() self.category = utils.create_category() self.topic = utils.create_topic(self.category, user=self.user)
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_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())
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_detail_viewed(self): """ Calls utils.topic_viewed """ def mocked_topic_viewed(request, topic): self._user = request.user self._topic = topic 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
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_update_not_moderator(self): category = utils.create_category() topic = utils.create_topic(category) self.user.st.is_moderator = False self.user.st.save() form = TopicForm(self.user, data=None, instance=topic) self.assertNotIn('category', form.fields) form = TopicForm(self.user, data=None) self.assertIn('category', form.fields)
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])
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))) response = self.client.get(reverse('spirit:topic:index-active')) self.assertEqual(list(response.context['topics']), [topic_b, topic_c, topic_a])
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_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) response = self.client.get(reverse('spirit:topic:index-active')) self.assertEqual(list(response.context['topics']), [topic, topic2]) self.assertEqual(response.context['topics'][0].bookmark, bookmark)
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'} response = self.client.post( reverse('spirit:topic:update', kwargs={'pk': topic.pk}), form_data) self.assertEqual(response.status_code, 404)
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'} response = self.client.post( reverse('spirit:topic:update', kwargs={'pk': topic.pk}), form_data) self.assertRedirects(response, topic.get_absolute_url(), status_code=302)
def setUp(self): utils.cache_clear() self.user = 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)
def test_topic_detail_view_no_slug(self): """ no slug """ utils.login(self) category = utils.create_category() topic = utils.create_topic(category=category) response = self.client.get( reverse('spirit:topic:detail', kwargs={ 'pk': topic.pk, 'slug': '' })) self.assertRedirects(response, topic.get_absolute_url(), status_code=301)
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) response = self.client.get(reverse('spirit:topic:index-active')) self.assertEqual(list(response.context['topics']), [])
def test_topic_detail_view(self): """ should display topic with comments """ utils.login(self) category = utils.create_category() 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) response = self.client.get( reverse('spirit:topic:detail', kwargs={ 'pk': topic.pk, 'slug': topic.slug })) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['topic'], topic) self.assertEqual(list(response.context['comments']), [comment1, comment2])
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)
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_viewtopic(app): user = utils.create_user() category = utils.create_category() topic = utils.create_topic(category=category, user=user) PhpBBForumRefs.objects.create(category=category, phpbb_forum_id=1) PhpBBTopicRefs.objects.create(topic=topic, phpbb_forum_id=1, phpbb_topic_id=2) # Test success app.get('/forum/viewforum.php?f=1') app.get('/forum/viewtopic.php?f=1&t=2') # Test 404 app.get('/forum/viewforum.php?f=2', status=404) app.get('/forum/viewtopic.php?f=1&t=3', status=404) # Test NaN's app.get('/forum/viewtopic.php?f=NaN', status=404) app.get('/forum/viewtopic.php?t=42NaN', status=404) app.get('/forum/viewforum.php?f=42Nan', status=404)