class ForumNotification(TestCase): def setUp(self): self.user1 = ProfileFactory().user self.user2 = ProfileFactory().user self.staff = StaffProfileFactory().user self.assertTrue(self.staff.has_perm('forum.change_topic')) self.category1 = CategoryFactory(position=1) self.forum11 = ForumFactory(category=self.category1, position_in_category=1) self.forum12 = ForumFactory(category=self.category1, position_in_category=2) for group in self.staff.groups.all(): self.forum12.groups.add(group) self.forum12.save() def test_no_dead_notif_on_moving(self): NewTopicSubscription.objects.get_or_create_active( self.user1, self.forum11) self.assertTrue( self.client.login(username=self.user2.username, password='******')) result = self.client.post(reverse('topic-new') + '?forum={0}'.format(self.forum11.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': "En tout cas l'un abonnement", 'tags': '' }, follow=False) self.assertEqual(result.status_code, 302) topic = Topic.objects.filter(title='Super sujet').first() subscription = NewTopicSubscription.objects.get_existing( self.user1, self.forum11, True) self.assertIsNotNone(subscription, 'There must be an active subscription for now') self.assertIsNotNone(subscription.last_notification, 'There must be a notification for now') self.assertFalse(subscription.last_notification.is_read) self.client.logout() self.assertTrue( self.client.login(username=self.staff.username, password='******')) data = {'move': '', 'forum': self.forum12.pk, 'topic': topic.pk} response = self.client.post(reverse('topic-edit'), data, follow=False) self.assertEqual(302, response.status_code) subscription = NewTopicSubscription.objects.get_existing( self.user1, self.forum11, True) self.assertIsNotNone(subscription, 'There must still be an active subscription') self.assertIsNotNone( subscription.last_notification, 'There must still be a notification as object is not removed.') self.assertEqual( subscription.last_notification, Notification.objects.filter(sender=self.user2).first()) self.assertTrue(subscription.last_notification.is_read, 'As forum is not reachable, notification is read')
class ForumNotification(TestCase): def setUp(self): self.user1 = ProfileFactory().user self.user2 = ProfileFactory().user self.to_be_changed_staff = StaffProfileFactory().user self.staff = StaffProfileFactory().user self.assertTrue(self.staff.has_perm('forum.change_topic')) self.category1 = CategoryFactory(position=1) self.forum11 = ForumFactory(category=self.category1, position_in_category=1) self.forum12 = ForumFactory(category=self.category1, position_in_category=2) for group in self.staff.groups.all(): self.forum12.groups.add(group) self.forum12.save() def test_ping_unknown(self): overridden_zds_app['comment']['enable_pings'] = True self.assertTrue(self.client.login(username=self.user2.username, password='******')) result = self.client.post( reverse('topic-new') + '?forum={0}'.format(self.forum11.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': '@anUnExistingUser is pinged, also a special chars user @{}', 'tags': '' }, follow=False) self.assertEqual(result.status_code, 302, 'Request must succeed') self.assertEqual(0, PingSubscription.objects.count(), 'As one user is pinged, only one subscription is created.') def test_no_auto_ping(self): overridden_zds_app['comment']['enable_pings'] = True self.assertTrue(self.client.login(username=self.user2.username, password='******')) result = self.client.post( reverse('topic-new') + '?forum={0}'.format(self.forum11.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': '@{} is pinged, not @{}'.format(self.user1.username, self.user2.username), 'tags': '' }, follow=False) self.assertEqual(result.status_code, 302) self.assertEqual(1, PingSubscription.objects.count(), 'As one user is pinged, only one subscription is created.') def test_no_reping_on_edition(self): """ to be more accurate : on edition, only ping **new** members """ overridden_zds_app['comment']['enable_pings'] = True self.assertTrue(self.client.login(username=self.user2.username, password='******')) result = self.client.post( reverse('topic-new') + '?forum={0}'.format(self.forum11.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': '@{} is pinged'.format(self.user1.username), 'tags': '' }, follow=False) self.assertEqual(result.status_code, 302) self.assertEqual(1, PingSubscription.objects.count(), 'As one user is pinged, only one subscription is created.') self.assertEqual(1, Notification.objects.count()) user3 = ProfileFactory().user post = Topic.objects.last().last_message result = self.client.post( reverse('post-edit') + '?message={0}'.format(post.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': '@{} is pinged even twice @{}'.format(self.user1.username, self.user1.username), 'tags': '' }, follow=False) self.assertEqual(result.status_code, 302) self.assertEqual(1, PingSubscription.objects.count(), 'No added subscription.') self.assertEqual(1, Notification.objects.count()) result = self.client.post( reverse('post-edit') + '?message={0}'.format(post.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': '@{} is pinged even twice @{} and add @{}'.format(self.user1.username, self.user1.username, user3.username), 'tags': '' }, follow=False) self.assertEqual(result.status_code, 302) self.assertEqual(2, PingSubscription.objects.count()) self.assertEqual(2, Notification.objects.count()) def test_loose_ping_on_edition(self): """ to be more accurate : on edition, only ping **new** members """ overridden_zds_app['comment']['enable_pings'] = True self.assertTrue(self.client.login(username=self.user2.username, password='******')) result = self.client.post( reverse('topic-new') + '?forum={0}'.format(self.forum11.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': '@{} is pinged'.format(self.user1.username), 'tags': '' }, follow=False) self.assertEqual(result.status_code, 302) self.assertEqual(1, PingSubscription.objects.count(), 'As one user is pinged, only one subscription is created.') self.assertEqual(1, Notification.objects.count()) post = Topic.objects.last().last_message result = self.client.post( reverse('post-edit') + '?message={0}'.format(post.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': '@ {} is no more pinged '.format(self.user1.username), 'tags': '' }, follow=False) self.assertEqual(result.status_code, 302) self.assertEqual(1, PingSubscription.objects.count(), 'No added subscription.') self.assertFalse(PingSubscription.objects.first().is_active) self.assertEqual(1, Notification.objects.count()) self.assertTrue(Notification.objects.first().is_read) def test_no_dead_notif_on_moving(self): NewTopicSubscription.objects.get_or_create_active(self.user1, self.forum11) self.assertTrue(self.client.login(username=self.user2.username, password='******')) result = self.client.post( reverse('topic-new') + '?forum={0}'.format(self.forum11.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': "En tout cas l'un abonnement", 'tags': '' }, follow=False) self.assertEqual(result.status_code, 302) topic = Topic.objects.filter(title='Super sujet').first() subscription = NewTopicSubscription.objects.get_existing(self.user1, self.forum11, True) self.assertIsNotNone(subscription, 'There must be an active subscription for now') self.assertIsNotNone(subscription.last_notification, 'There must be a notification for now') self.assertFalse(subscription.last_notification.is_read) self.client.logout() self.assertTrue(self.client.login(username=self.staff.username, password='******')) data = { 'move': '', 'forum': self.forum12.pk, 'topic': topic.pk } response = self.client.post(reverse('topic-edit'), data, follow=False) self.assertEqual(302, response.status_code) subscription = NewTopicSubscription.objects.get_existing(self.user1, self.forum11, True) self.assertIsNotNone(subscription, 'There must still be an active subscription') self.assertIsNotNone(subscription.last_notification, 'There must still be a notification as object is not removed.') self.assertEqual(subscription.last_notification, Notification.objects.filter(sender=self.user2).first()) self.assertTrue(subscription.last_notification.is_read, 'As forum is not reachable, notification is read') def test_no_ping_on_private_forum(self): self.assertTrue(self.client.login(username=self.staff.username, password='******')) result = self.client.post( reverse('topic-new') + '?forum={0}'.format(self.forum12.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': 'ping @{}'.format(self.user1.username), 'tags': '' }, follow=False) self.assertEqual(result.status_code, 302) topic = Topic.objects.filter(title='Super sujet').first() subscription = PingSubscription.objects.get_existing(self.user1, topic.last_message, True) self.assertIsNone(subscription, 'There must be no active subscription for now') def test_no_dead_ping_notif_on_moving_to_private_forum(self): self.assertTrue(self.client.login(username=self.user2.username, password='******')) result = self.client.post( reverse('topic-new') + '?forum={0}'.format(self.forum11.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': 'ping @{}'.format(self.user1.username), 'tags': '' }, follow=False) self.assertEqual(result.status_code, 302) topic = Topic.objects.filter(title='Super sujet').first() subscription = PingSubscription.objects.get_existing(self.user1, topic.last_message, True) self.assertIsNotNone(subscription, 'There must be an active subscription for now') self.assertIsNotNone(subscription.last_notification, 'There must be a notification for now') self.assertFalse(subscription.last_notification.is_read) self.client.logout() self.assertTrue(self.client.login(username=self.staff.username, password='******')) data = { 'move': '', 'forum': self.forum12.pk, 'topic': topic.pk } response = self.client.post(reverse('topic-edit'), data, follow=False) self.assertEqual(302, response.status_code) subscription = PingSubscription.objects.get_existing(self.user1, topic.last_message, True) self.assertIsNotNone(subscription, 'There must still be an active subscription') self.assertIsNotNone(subscription.last_notification, 'There must still be a notification as object is not removed.') self.assertEqual(subscription.last_notification, Notification.objects.filter(sender=self.user2).first()) self.assertTrue(subscription.last_notification.is_read, 'As forum is not reachable, notification is read') def test_no_more_notif_on_losing_all_groups(self): NewTopicSubscription.objects.get_or_create_active(self.to_be_changed_staff, self.forum12) self.assertTrue(self.client.login(username=self.staff.username, password='******')) self.client.post( reverse('topic-new') + '?forum={0}'.format(self.forum12.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': "En tout cas l'un abonnement", 'tags': '' }, follow=False) subscription = NewTopicSubscription.objects.get_existing(self.to_be_changed_staff, self.forum12, True) self.assertIsNotNone(subscription, 'There must be an active subscription for now') self.to_be_changed_staff.groups.clear() self.to_be_changed_staff.save() subscription = NewTopicSubscription.objects.get_existing(self.to_be_changed_staff, self.forum12, False) self.assertIsNotNone(subscription, 'There must be an active subscription for now') self.assertFalse(subscription.is_active) def test_no_more_notif_on_losing_one_group(self): NewTopicSubscription.objects.get_or_create_active(self.to_be_changed_staff, self.forum12) self.assertTrue(self.client.login(username=self.staff.username, password='******')) self.client.post( reverse('topic-new') + '?forum={0}'.format(self.forum12.pk), { 'title': 'Super sujet', 'subtitle': 'Pour tester les notifs', 'text': "En tout cas l'un abonnement", 'tags': '' }, follow=False) subscription = NewTopicSubscription.objects.get_existing(self.to_be_changed_staff, self.forum12, True) self.assertIsNotNone(subscription, 'There must be an active subscription for now') self.to_be_changed_staff.groups.remove(list(self.to_be_changed_staff.groups.all())[0]) self.to_be_changed_staff.save() subscription = NewTopicSubscription.objects.get_existing(self.to_be_changed_staff, self.forum12, False) self.assertIsNotNone(subscription, 'There must be an active subscription for now') self.assertFalse(subscription.is_active)
class ForumNotification(TestCase): def setUp(self): self.user1 = ProfileFactory().user self.user2 = ProfileFactory().user self.to_be_changed_staff = StaffProfileFactory().user self.staff = StaffProfileFactory().user self.assertTrue(self.staff.has_perm("forum.change_topic")) self.category1 = ForumCategoryFactory(position=1) self.forum11 = ForumFactory(category=self.category1, position_in_category=1) self.forum12 = ForumFactory(category=self.category1, position_in_category=2) for group in self.staff.groups.all(): self.forum12.groups.add(group) self.forum12.save() def test_ping_unknown(self): self.client.force_login(self.user2) result = self.client.post( reverse("topic-new") + f"?forum={self.forum11.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": "@anUnExistingUser is pinged, also a special chars user @{}", "tags": "", }, follow=False, ) self.assertEqual(result.status_code, 302, "Request must succeed") self.assertEqual( 0, PingSubscription.objects.count(), "As one user is pinged, only one subscription is created.") def test_no_auto_ping(self): self.client.force_login(self.user2) result = self.client.post( reverse("topic-new") + f"?forum={self.forum11.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": f"@{self.user1.username} is pinged, not @{self.user2.username}", "tags": "", }, follow=False, ) self.assertEqual(result.status_code, 302) self.assertEqual( 1, PingSubscription.objects.count(), "As one user is pinged, only one subscription is created.") def test_edit_with_more_than_max_ping(self): overridden_zds_app["comment"]["max_pings"] = 2 pinged_users = [ ProfileFactory(), ProfileFactory(), ProfileFactory(), ProfileFactory() ] self.client.force_login(self.user2) self.client.post( reverse("topic-new") + f"?forum={self.forum11.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": "@{} @{} are pinged, not @{} @{}".format( *[a.user.username for a in pinged_users]), "tags": "", }, follow=False, ) topic = Topic.objects.last() post = topic.last_message self.assertEqual(2, PingSubscription.objects.count()) self.assertTrue( PingSubscription.objects.get_existing(pinged_users[0].user, post, True)) self.assertTrue( PingSubscription.objects.get_existing(pinged_users[1].user, post, True)) self.assertFalse( PingSubscription.objects.get_existing(pinged_users[2].user, post, True)) self.assertFalse( PingSubscription.objects.get_existing(pinged_users[3].user, post, True)) self.client.post( reverse("topic-edit") + f"?topic={topic.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": "@{} @{} are pinged".format(pinged_users[1].user.username, pinged_users[3].user.username), "tags": "", }, follow=False, ) self.assertTrue( PingSubscription.objects.get_existing(pinged_users[3].user, post, True)) self.assertTrue( PingSubscription.objects.get_existing(pinged_users[1].user, post, True)) self.assertFalse( PingSubscription.objects.get_existing(pinged_users[0].user, post, True)) def test_no_reping_on_edition(self): """ to be more accurate : on edition, only ping **new** members """ self.client.force_login(self.user2) result = self.client.post( reverse("topic-new") + f"?forum={self.forum11.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": f"@{self.user1.username} is pinged", "tags": "", }, follow=False, ) self.assertEqual(result.status_code, 302) self.assertEqual( 1, PingSubscription.objects.count(), "As one user is pinged, only one subscription is created.") self.assertEqual(1, Notification.objects.count()) user3 = ProfileFactory().user post = Topic.objects.last().last_message result = self.client.post( reverse("post-edit") + f"?message={post.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": f"@{self.user1.username} is pinged even twice @{self.user1.username}", "tags": "", }, follow=False, ) self.assertEqual(result.status_code, 302) self.assertEqual(1, PingSubscription.objects.count(), "No added subscription.") self.assertEqual(1, Notification.objects.count()) result = self.client.post( reverse("post-edit") + f"?message={post.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": "@{} is pinged even twice @{} and add @{}".format( self.user1.username, self.user1.username, user3.username), "tags": "", }, follow=False, ) self.assertEqual(result.status_code, 302) self.assertEqual(2, PingSubscription.objects.count()) self.assertEqual(2, Notification.objects.count()) def test_loose_ping_on_edition(self): """ to be more accurate : on edition, only ping **new** members """ self.client.force_login(self.user2) result = self.client.post( reverse("topic-new") + f"?forum={self.forum11.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": f"@{self.user1.username} is pinged", "tags": "", }, follow=False, ) self.assertEqual(result.status_code, 302) self.assertEqual( 1, PingSubscription.objects.count(), "As one user is pinged, only one subscription is created.") self.assertEqual(1, Notification.objects.count()) post = Topic.objects.last().last_message result = self.client.post( reverse("post-edit") + f"?message={post.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": f"@ {self.user1.username} is no more pinged ", "tags": "", }, follow=False, ) self.assertEqual(result.status_code, 302) self.assertEqual(1, PingSubscription.objects.count(), "No added subscription.") self.assertFalse(PingSubscription.objects.first().is_active) self.assertEqual(1, Notification.objects.count()) self.assertTrue(Notification.objects.first().is_read) def test_no_dead_notif_on_moving(self): NewTopicSubscription.objects.get_or_create_active( self.user1, self.forum11) self.client.force_login(self.user2) result = self.client.post( reverse("topic-new") + f"?forum={self.forum11.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": "En tout cas l'un abonnement", "tags": "", }, follow=False, ) self.assertEqual(result.status_code, 302) topic = Topic.objects.filter(title="Super sujet").first() subscription = NewTopicSubscription.objects.get_existing( self.user1, self.forum11, True) self.assertIsNotNone(subscription, "There must be an active subscription for now") self.assertIsNotNone(subscription.last_notification, "There must be a notification for now") self.assertFalse(subscription.last_notification.is_read) self.client.logout() self.client.force_login(self.staff) data = {"move": "", "forum": self.forum12.pk, "topic": topic.pk} response = self.client.post(reverse("topic-edit"), data, follow=False) self.assertEqual(302, response.status_code) subscription = NewTopicSubscription.objects.get_existing( self.user1, self.forum11, True) self.assertIsNotNone(subscription, "There must still be an active subscription") self.assertIsNotNone( subscription.last_notification, "There must still be a notification as object is not removed.") self.assertEqual( subscription.last_notification, Notification.objects.filter(sender=self.user2).first()) self.assertTrue(subscription.last_notification.is_read, "As forum is not reachable, notification is read") def test_no_ping_on_private_forum(self): self.client.force_login(self.staff) result = self.client.post( reverse("topic-new") + f"?forum={self.forum12.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": f"ping @{self.user1.username}", "tags": "", }, follow=False, ) self.assertEqual(result.status_code, 302) topic = Topic.objects.filter(title="Super sujet").first() subscription = PingSubscription.objects.get_existing( self.user1, topic.last_message, True) self.assertIsNone(subscription, "There must be no active subscription for now") def test_no_dead_ping_notif_on_moving_to_private_forum(self): self.client.force_login(self.user2) result = self.client.post( reverse("topic-new") + f"?forum={self.forum11.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": f"ping @{self.user1.username}", "tags": "", }, follow=False, ) self.assertEqual(result.status_code, 302) topic = Topic.objects.filter(title="Super sujet").first() subscription = PingSubscription.objects.get_existing( self.user1, topic.last_message, True) self.assertIsNotNone(subscription, "There must be an active subscription for now") self.assertIsNotNone(subscription.last_notification, "There must be a notification for now") self.assertFalse(subscription.last_notification.is_read) self.client.logout() self.client.force_login(self.staff) data = {"move": "", "forum": self.forum12.pk, "topic": topic.pk} response = self.client.post(reverse("topic-edit"), data, follow=False) self.assertEqual(302, response.status_code) subscription = PingSubscription.objects.get_existing( self.user1, topic.last_message, True) self.assertIsNotNone(subscription, "There must still be an active subscription") self.assertIsNotNone( subscription.last_notification, "There must still be a notification as object is not removed.") self.assertEqual( subscription.last_notification, Notification.objects.filter(sender=self.user2).first()) self.assertTrue(subscription.last_notification.is_read, "As forum is not reachable, notification is read") def test_no_more_notif_on_losing_all_groups(self): NewTopicSubscription.objects.get_or_create_active( self.to_be_changed_staff, self.forum12) self.client.force_login(self.staff) self.client.post( reverse("topic-new") + f"?forum={self.forum12.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": "En tout cas l'un abonnement", "tags": "", }, follow=False, ) subscription = NewTopicSubscription.objects.get_existing( self.to_be_changed_staff, self.forum12, True) self.assertIsNotNone(subscription, "There must be an active subscription for now") self.to_be_changed_staff.groups.clear() self.to_be_changed_staff.save() subscription = NewTopicSubscription.objects.get_existing( self.to_be_changed_staff, self.forum12, False) self.assertIsNotNone(subscription, "There must be an active subscription for now") self.assertFalse(subscription.is_active) def test_no_more_notif_on_losing_one_group(self): NewTopicSubscription.objects.get_or_create_active( self.to_be_changed_staff, self.forum12) self.client.force_login(self.staff) self.client.post( reverse("topic-new") + f"?forum={self.forum12.pk}", { "title": "Super sujet", "subtitle": "Pour tester les notifs", "text": "En tout cas l'un abonnement", "tags": "", }, follow=False, ) subscription = NewTopicSubscription.objects.get_existing( self.to_be_changed_staff, self.forum12, True) self.assertIsNotNone(subscription, "There must be an active subscription for now") self.to_be_changed_staff.groups.remove( list(self.to_be_changed_staff.groups.all())[0]) self.to_be_changed_staff.save() subscription = NewTopicSubscription.objects.get_existing( self.to_be_changed_staff, self.forum12, False) self.assertIsNotNone(subscription, "There must be an active subscription for now") self.assertFalse(subscription.is_active)