Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrige la perte de forums et sujets suivis lors de l'éviction d'un groupe #6620

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions zds/member/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ class NonAsciiUserFactory(UserFactory):
username = factory.Sequence("ïéàçÊÀ{}".format)


class MultipleGroupsUserFactory(UserFactory):
"""
Factory that creates a User with multiple groups

WARNING: Don't try to directly use `MultipleGroupsUserFactory` because it won't create the associated Profile.
Use `MultipleGroupsProfileFactory` instead.
"""

@factory.post_generation
def groups(self, create, extracted, **kwargs):
for group_name in ("first_group", "second_group"):
group = Group(name=group_name)
group.save()
self.groups.add(group)


class ProfileFactory(factory.django.DjangoModelFactory):
"""
Use this factory when you need a complete Profile for a standard User.
Expand Down Expand Up @@ -153,3 +169,11 @@ class NonAsciiProfileFactory(ProfileFactory):
"""

user = factory.SubFactory(NonAsciiUserFactory)


class MultipleGroupsProfileFactory(ProfileFactory):
"""
Use this factory when you need a complete Profile for a User with multiple groups.
"""

user = factory.SubFactory(MultipleGroupsUserFactory)
6 changes: 5 additions & 1 deletion zds/notification/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def remove_group_subscription_on_quitting_groups(*, sender, instance, action, pk
)
return

for forum in Forum.objects.filter(groups__pk__in=list(pk_set)):
all_groups_pk = instance.groups.values_list("pk", flat=True)
removed_groups_pk = pk_set
kept_groups_pk = set(all_groups_pk) - set(removed_groups_pk)

for forum in Forum.objects.filter(groups__pk__in=removed_groups_pk).exclude(groups__pk__in=kept_groups_pk):
subscriptions = []

forum_subscription = NewTopicSubscription.objects.get_existing(instance, forum, True)
Expand Down
39 changes: 38 additions & 1 deletion zds/notification/tests/tests_tricky.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from zds.forum.tests.factories import ForumCategoryFactory, ForumFactory
from zds.forum.models import Topic
from zds.gallery.tests.factories import UserGalleryFactory
from zds.member.tests.factories import StaffProfileFactory, ProfileFactory
from zds.member.tests.factories import StaffProfileFactory, ProfileFactory, MultipleGroupsProfileFactory
from zds.notification.models import (
NewTopicSubscription,
TopicAnswerSubscription,
Expand Down Expand Up @@ -40,13 +40,19 @@ def setUp(self):
self.user2 = ProfileFactory().user
self.to_be_changed_staff = StaffProfileFactory().user
self.staff = StaffProfileFactory().user
self.multi_groups_user = MultipleGroupsProfileFactory().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)
self.forum13 = ForumFactory(category=self.category1, position_in_category=3)
for group in self.staff.groups.all():
self.forum12.groups.add(group)
self.forum13.groups.add(group)
self.forum12.save()
for group in self.multi_groups_user.groups.all():
self.forum13.groups.add(group)
self.forum13.save()

def test_ping_unknown(self):
self.client.force_login(self.user2)
Expand Down Expand Up @@ -340,6 +346,37 @@ def test_no_more_new_topic_notif_on_losing_one_group(self):
self.assertFalse(subscription.is_active)
self.assertTrue(subscription.last_notification.is_read, "As forum is not reachable, notification is read")

def test_keep_new_topic_notif_if_losing_only_one_group(self):
"""
Context: self.multi_groups_user is in two groups that both give access to self.forum13
If this user leaves one of the two groups, they should keep receiving new topic notifications.
"""

NewTopicSubscription.objects.get_or_create_active(self.multi_groups_user, self.forum13)
self.client.force_login(self.staff)
self.client.post(
reverse("forum:topic-new") + f"?forum={self.forum13.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.multi_groups_user, self.forum13, True)
self.assertIsNotNone(subscription, "There must be an active subscription for now")
self.assertIsNotNone(subscription.last_notification, "There must be a notification.")
self.assertFalse(subscription.last_notification.is_read, "The notification has not been read yet")

self.multi_groups_user.groups.remove(list(self.multi_groups_user.groups.all())[0])
self.multi_groups_user.save()

subscription = NewTopicSubscription.objects.get_existing(self.multi_groups_user, self.forum13, True)
self.assertIsNotNone(subscription, "There must still be an active subscription")
self.assertTrue(subscription.is_active)
self.assertFalse(subscription.last_notification.is_read, "The notification has not been read yet")

def test_no_more_topic_answer_notif_on_losing_all_groups(self):
self.client.force_login(self.to_be_changed_staff)
self.client.post(
Expand Down