Skip to content

Commit

Permalink
update EmailLastSeen on Topic/Post create, we do not track updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Feb 11, 2025
1 parent f981573 commit 7c972f3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lacommunaute/forum_conversation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from lacommunaute.forum_conversation.signals import post_create
from lacommunaute.forum_member.shortcuts import get_forum_member_display_name
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.users.models import User
from lacommunaute.users.enums import EmailLastSeenKind
from lacommunaute.users.models import EmailLastSeen, User


class TopicQuerySet(models.QuerySet):
Expand Down Expand Up @@ -155,6 +156,9 @@ def save(self, *args, **kwargs):
super().save(*args, **kwargs)
if created:
post_create.send(sender=self.__class__, instance=self)
EmailLastSeen.objects.seen(
email=self.poster.email if self.poster else self.username, kind=EmailLastSeenKind.POST
)


class CertifiedPost(DatedModel):
Expand Down
26 changes: 26 additions & 0 deletions lacommunaute/forum_conversation/tests/tests_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
)
from lacommunaute.forum_conversation.models import Post, Topic
from lacommunaute.forum_member.shortcuts import get_forum_member_display_name
from lacommunaute.users.enums import EmailLastSeenKind
from lacommunaute.users.factories import UserFactory
from lacommunaute.users.models import EmailLastSeen


@pytest.fixture(name="forum")
Expand All @@ -39,6 +41,30 @@ def test_is_certified(self):
self.assertTrue(topic.last_post.is_certified)


class TestPostModel:
@pytest.mark.parametrize(
"topic", [lambda: TopicFactory(with_post=True), lambda: AnonymousTopicFactory(with_post=True)]
)
def test_email_last_seen_when_create(self, db, topic):
topic = topic()
email_last_seen = EmailLastSeen.objects.get()
assert email_last_seen.last_seen_kind == EmailLastSeenKind.POST

if topic.first_post.poster:
assert email_last_seen.email == topic.first_post.poster.email
else:
assert email_last_seen.email == topic.first_post.username

@pytest.mark.parametrize(
"topic", [lambda: TopicFactory(with_post=True), lambda: AnonymousTopicFactory(with_post=True)]
)
def test_email_last_seen_when_update(self, db, topic):
topic = topic()
EmailLastSeen.objects.all().delete()
topic.first_post.save()
assert EmailLastSeen.objects.count() == 0


class TestTopicManager:
def test_unanswered(self, db, forum):
TopicFactory(forum=forum, posts_count=0)
Expand Down
20 changes: 20 additions & 0 deletions lacommunaute/forum_conversation/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
from lacommunaute.forum_moderation.factories import BlockedDomainNameFactory, BlockedEmailFactory
from lacommunaute.forum_upvote.factories import UpVoteFactory
from lacommunaute.notification.factories import NotificationFactory
from lacommunaute.users.enums import EmailLastSeenKind
from lacommunaute.users.factories import UserFactory
from lacommunaute.users.models import EmailLastSeen
from lacommunaute.utils.testing import parse_response_to_soup


Expand All @@ -39,6 +41,12 @@
assign_perm = get_class("forum_permission.shortcuts", "assign_perm")


def check_email_last_seen(email):
return EmailLastSeen.objects.filter(
email=email, last_seen_kind__in=[EmailLastSeenKind.POST, EmailLastSeenKind.LOGGED]
).exists()


class TopicCreateViewTest(TestCase):
@classmethod
def setUpTestData(cls):
Expand Down Expand Up @@ -116,6 +124,8 @@ def test_topic_create_as_anonymous_user(self, *args):
self.assertTrue(topic.approved)
self.assertTrue(topic.first_post.approved)

self.assertTrue(check_email_last_seen(self.post_data["username"]))

def test_topic_create_as_unapproved_anonymous_user(self, *args):
self.post_data["username"] = faker.email()
BlockedEmailFactory(email=self.post_data["username"])
Expand All @@ -133,6 +143,8 @@ def test_topic_create_as_unapproved_anonymous_user(self, *args):
)
self.assertEqual(Topic.objects.count(), 0)

self.assertFalse(check_email_last_seen(self.post_data["username"]))

def test_topic_create_with_nonfr_content(self, *args):
self.client.force_login(self.poster)
self.post_data["content"] = "популярные лучшие песни слушать онлайн"
Expand Down Expand Up @@ -183,6 +195,8 @@ def test_topic_create_as_anonymous_user_with_blocked_domain_name(self, *args):
)
self.assertEqual(Topic.objects.count(), 0)

self.assertFalse(check_email_last_seen(self.post_data["username"]))

def test_topic_create_as_authenticated_user(self, *args):
self.client.force_login(self.poster)

Expand All @@ -196,6 +210,8 @@ def test_topic_create_as_authenticated_user(self, *args):
self.assertTrue(Topic.objects.first().approved)
self.assertTrue(Topic.objects.first().posts.first().approved)

self.assertTrue(check_email_last_seen(self.poster.email))

def test_tags_checkbox_are_displayed(self, *args):
Tag.objects.bulk_create([Tag(name=f"tag_x{i}", slug=f"tag_x{i}") for i in range(2)])
self.client.force_login(self.poster)
Expand Down Expand Up @@ -329,6 +345,7 @@ def test_selected_tags_are_checked(self):

def test_update_by_anonymous_user(self):
topic = AnonymousTopicFactory(with_post=True, forum=self.forum)
EmailLastSeen.objects.all().delete()
session = self.client.session
session["_anonymous_forum_key"] = topic.first_post.anonymous_key
session.save()
Expand Down Expand Up @@ -359,6 +376,9 @@ def test_update_by_anonymous_user(self):
),
)

self.assertFalse(check_email_last_seen("[email protected]"))
self.assertFalse(check_email_last_seen(topic.first_post.username))

def test_topic_update_with_nonfr_content(self, *args):
self.client.force_login(self.poster)
post_data = {"subject": "s", "content": "популярные лучшие песни слушать онлайн"}
Expand Down
31 changes: 31 additions & 0 deletions lacommunaute/forum_conversation/tests/tests_views_htmx.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.test import RequestFactory, TestCase
Expand All @@ -6,6 +7,7 @@
from machina.core.db.models import get_model
from machina.core.loading import get_class

from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum_conversation.factories import CertifiedPostFactory, PostFactory, TopicFactory
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.models import CertifiedPost, Topic
Expand All @@ -15,7 +17,9 @@
from lacommunaute.forum_moderation.models import BlockedPost
from lacommunaute.forum_upvote.factories import UpVoteFactory
from lacommunaute.notification.factories import NotificationFactory
from lacommunaute.users.enums import EmailLastSeenKind
from lacommunaute.users.factories import UserFactory
from lacommunaute.users.models import EmailLastSeen


faker = Faker(settings.LANGUAGE_CODE)
Expand Down Expand Up @@ -424,6 +428,33 @@ def test_create_post_with_blocked_domain_name(self):
assert blocked_post.block_reason == BlockedPostReason.BLOCKED_DOMAIN


class TestPostFeedCreateView:
@pytest.mark.parametrize("logged", [True, False])
def test_email_last_seen_is_updated(self, client, db, logged):
topic = TopicFactory(with_post=True, forum=ForumFactory(with_public_perms=True))
url = reverse(
"forum_conversation_extension:post_create",
kwargs={
"forum_pk": topic.forum.pk,
"forum_slug": topic.forum.slug,
"pk": topic.pk,
"slug": topic.slug,
},
)
data = {"content": faker.paragraph(nb_sentences=5)}

if logged:
client.force_login(topic.poster)
else:
data["username"] = "[email protected]"

response = client.post(url, data=data)
assert response.status_code == 200

email = topic.poster.email if logged else data["username"]
assert EmailLastSeen.objects.filter(email=email, last_seen_kind=EmailLastSeenKind.POST).exists()


# vincentporte : not to futur self, rewrite it in pytest style
class CertifiedPostViewTest(TestCase):
@classmethod
Expand Down

0 comments on commit 7c972f3

Please sign in to comment.