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 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_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_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_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 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_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_topic_notification_create_has_access(self): """ create notification for private topic if user has access """ TopicNotification.objects.all().delete() private = utils.create_private_topic(user=self.user) utils.create_comment(topic=private.topic) utils.login(self) form_data = { 'is_active': True, } response = self.client.post( reverse('spirit:topic:notification:create', kwargs={ 'topic_id': private.topic.pk, }), form_data) self.assertRedirects(response, private.topic.get_absolute_url(), status_code=302) self.assertEqual(len(TopicNotification.objects.all()), 1)
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 response = self.client.get( reverse('spirit:topic:detail', kwargs={ 'pk': topic.pk, 'slug': topic.slug })) self.assertEqual(response.status_code, 200) self.assertEqual(list(response.context['comments']), [comment1, comment2])
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_notification_mark_as_read(self): """ Mark notification as read """ private = utils.create_private_topic() comment = utils.create_comment(topic=private.topic) TopicNotification.objects.create(user=private.user, topic=private.topic, comment=comment, is_read=False) TopicNotification.mark_as_read(user=private.user, topic=private.topic) notification = TopicNotification.objects.get(user=private.user, topic=private.topic) self.assertTrue(notification.is_read)
def test_topic_notification_notify_new_mentions_unactive(self): """ set is_read=False when user gets mentioned even if is_active=False """ (TopicNotification.objects.filter( pk=self.topic_notification.pk).update(is_active=False)) mentions = {self.user.username: self.user} comment = utils.create_comment(topic=self.topic_notification.topic) TopicNotification.notify_new_mentions(comment=comment, mentions=mentions) self.assertEqual( TopicNotification.objects.get( pk=self.topic_notification.pk).action, MENTION) self.assertFalse( TopicNotification.objects.get( pk=self.topic_notification.pk).is_read)
def test_topic_private_bulk_create(self): """ Create notifications for a bunch of users """ TopicNotification.objects.all().delete() user = utils.create_user() user2 = utils.create_user() topic = utils.create_topic(self.category) comment = utils.create_comment(topic=topic) TopicNotification.bulk_create(users=[user, user2], comment=comment) self.assertEqual(len(TopicNotification.objects.all()), 2) notification = TopicNotification.objects.get(user=user, topic=comment.topic) self.assertTrue(notification.is_active) self.assertFalse(notification.is_read) self.assertEqual(notification.comment, comment)
def test_topic_notification_list_paginate(self): """ topic notification list paginated """ topic2 = utils.create_topic(self.category) comment2 = utils.create_comment(topic=topic2) topic_notification2 = TopicNotification.objects.create( user=self.user, topic=topic2, comment=comment2, is_active=True, action=COMMENT) utils.login(self) response = self.client.get(reverse('spirit:topic:notification:index')) self.assertEqual(list(response.context['notifications']), [ topic_notification2, ])
def test_topic_notification_notify_new_mentions(self): """ Should notify mentions """ topic = utils.create_topic(self.category) mentions = {self.user.username: self.user} comment = utils.create_comment(topic=topic) TopicNotification.notify_new_mentions(comment=comment, mentions=mentions) self.assertEqual( TopicNotification.objects.get(user=self.user, comment=comment).action, MENTION) self.assertFalse( TopicNotification.objects.get(user=self.user, comment=comment).is_read) self.assertTrue( TopicNotification.objects.get(user=self.user, comment=comment).is_active)
def test_topic_notification_notify_new_comment_unactive(self): """ Should do nothing if notification is unactive """ 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=subscriber, topic=topic, comment=comment, is_active=False, is_read=True) TopicNotification.notify_new_comment(comment) notification = TopicNotification.objects.get(user=subscriber, topic=topic) self.assertTrue(notification.is_read)
def test_notification(self): """ update notification """ category = utils.create_category() topic = utils.create_topic(category) comment = utils.create_comment(topic=topic) notification = TopicNotification.objects.create(user=self.user, topic=topic, comment=comment, is_active=True, action=COMMENT) form_data = { 'is_active': True, } form = NotificationForm(data=form_data, instance=notification) self.assertEqual(form.is_valid(), True)
def test_topic_notification_create_maybe(self): """ Should create a notification if does not exists """ user = utils.create_user() topic = utils.create_topic(self.category) comment = utils.create_comment(topic=topic) TopicNotification.create_maybe(user=user, comment=comment) notification = TopicNotification.objects.get(user=user, topic=topic) self.assertTrue(notification.is_active) self.assertTrue(notification.is_read) self.assertEqual(notification.action, COMMENT) # Creating it again should do nothing (TopicNotification.objects.filter(user=user, topic=topic).update(is_active=False)) TopicNotification.create_maybe(user=user, comment=comment) self.assertFalse( TopicNotification.objects.get(user=user, topic=topic).is_active)
def test_topic_notification_ajax_limit(self): """ get first N notifications """ user = utils.create_user() 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) 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.assertGreater( TopicNotification.objects.filter(user=self.user).count(), 1) self.assertEqual(len(res['n']), 1)