Skip to content

Commit

Permalink
notify forum upvoters on new topics, refactor tests in pytest style
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Nov 28, 2024
1 parent e73499c commit 5a13f7f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
13 changes: 12 additions & 1 deletion lacommunaute/forum_conversation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ def get_absolute_url(self, with_fqdn=False):

return absolute_url

def mails_to_notify(self):
def mails_to_notify_topic_head(self):
forum_upvoters_qs = User.objects.filter(id__in=self.forum.upvotes.all().values("voter_id")).values_list(
"email", flat=True
)
return [email for email in forum_upvoters_qs]

def mails_to_notify_replies(self):
# we want to notify stakeholders of the topic, except the last poster.
# stakeholders are:
# - authenticated users who upvoted one of the posts of the topic
Expand Down Expand Up @@ -101,6 +107,11 @@ def mails_to_notify(self):

return [email for email in stakeholders_qs if email != last_poster_email]

def mails_to_notify(self):
if self.last_post.is_topic_head:
return self.mails_to_notify_topic_head()
return self.mails_to_notify_replies()

@property
def poster_email(self):
return self.first_post.username or self.first_post.poster.email
Expand Down
47 changes: 26 additions & 21 deletions lacommunaute/forum_conversation/tests/tests_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import IntegrityError
Expand All @@ -7,13 +8,13 @@
from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum_conversation.factories import (
AnonymousPostFactory,
AnonymousTopicFactory,
CertifiedPostFactory,
PostFactory,
TopicFactory,
)
from lacommunaute.forum_conversation.models import Post, Topic
from lacommunaute.forum_member.shortcuts import get_forum_member_display_name
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.users.factories import UserFactory


Expand Down Expand Up @@ -127,36 +128,40 @@ def test_topic_types(self):
self.assertEqual(1, Topic.TOPIC_STICKY)
self.assertEqual(2, Topic.TOPIC_ANNOUNCE)

def test_mails_to_notify_sorted_authenticated_posters(self):
topic = TopicFactory(with_post=True)
self.assertEqual(topic.mails_to_notify(), [])

post = PostFactory(topic=topic)
self.assertEqual(topic.mails_to_notify(), [topic.poster.email])
@pytest.fixture(name="upvoters")
def upvoters_fixture():
return UserFactory.create_batch(3)

PostFactory(topic=topic)
self.assertEqual(topic.mails_to_notify(), sorted([topic.poster.email, post.poster.email]))

def test_mails_to_notify_authenticated_upvoters(self):
upvoter = UserFactory()
topic = TopicFactory(with_post=True)
UpVote.objects.create(content_object=topic.first_post, voter=upvoter)
class TestModelMethods:
def test_forum_upvoters_are_notified_for_new_topic(self, db, upvoters):
topic = TopicFactory(forum=ForumFactory(upvoted_by=upvoters), with_post=True)
assert topic.mails_to_notify() == [upvoter.email for upvoter in upvoters]

self.assertEqual(topic.mails_to_notify(), [upvoter.email])
def test_forum_upvoters_are_not_notified_on_replies(self, db, upvoters):
topic = TopicFactory(forum=ForumFactory(upvoted_by=upvoters), with_post=True, answered=True)
assert not set([upvoter.email for upvoter in upvoters]).intersection(set(topic.mails_to_notify()))

def test_mails_to_notify_anonymous_poster(self):
def test_posts_upvoters_are_notified_on_replies(self, db, upvoters):
topic = TopicFactory(with_post=True)
PostFactory(topic=topic, upvoted_by=upvoters)
assert set([upvoter.email for upvoter in upvoters]).issubset(set(topic.mails_to_notify()))

def test_authenticated_posters_are_notified_on_replies_except_last_one(self, db):
topic = TopicFactory(with_post=True)
anonymous_post = AnonymousPostFactory(topic=topic)
PostFactory(topic=topic)
assert topic.mails_to_notify() == [topic.first_post.poster.email]

self.assertEqual(topic.mails_to_notify(), sorted([topic.poster.email, anonymous_post.username]))
def test_anonymous_posters_are_notified_on_replies_except_last_one(self, db):
topic = AnonymousTopicFactory(with_post=True)
AnonymousPostFactory(topic=topic)
assert topic.mails_to_notify() == [topic.first_post.username]

def test_mails_to_notify_deduplication(self):
def test_notify_replies_deduplication(self, db):
topic = TopicFactory(with_post=True)
UpVote.objects.create(content_object=topic.first_post, voter=topic.poster)

PostFactory(topic=topic)
self.assertEqual(topic.mails_to_notify(), [topic.poster.email])
PostFactory(topic=topic, upvoted_by=[topic.first_post.poster])
assert topic.mails_to_notify() == [topic.first_post.poster.email]


class CertifiedPostModelTest(TestCase):
Expand Down

0 comments on commit 5a13f7f

Please sign in to comment.