From 313a7ecc1b2fa347398b4fdffa5231399d8c3d88 Mon Sep 17 00:00:00 2001 From: Bianca Yang Date: Wed, 13 Dec 2023 18:58:37 -0800 Subject: [PATCH] feat: capture event when we send verifiation emails (#19301) * capture event when we send verifiation emails * add test * pr feedback --------- Co-authored-by: Bianca Yang --- posthog/api/test/test_user.py | 10 +++++++++- posthog/tasks/email.py | 6 ++++++ posthog/tasks/test/test_email.py | 8 +++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/posthog/api/test/test_user.py b/posthog/api/test/test_user.py index 1e0f1098b32bd..7113d50e5f7b5 100644 --- a/posthog/api/test/test_user.py +++ b/posthog/api/test/test_user.py @@ -1047,7 +1047,15 @@ def test_user_can_request_verification_email(self, mock_capture): "user verified email", properties={"$set": ANY}, ) - self.assertEqual(mock_capture.call_count, 2) + + mock_capture.assert_any_call( + self.user.distinct_id, + "verification email sent", + groups={ + "organization": str(self.team.organization_id), + }, + ) + self.assertEqual(mock_capture.call_count, 3) def test_cant_verify_if_email_is_not_configured(self): with self.settings(CELERY_TASK_ALWAYS_EAGER=True): diff --git a/posthog/tasks/email.py b/posthog/tasks/email.py index bd7f60188b166..6ab43b973f509 100644 --- a/posthog/tasks/email.py +++ b/posthog/tasks/email.py @@ -2,6 +2,7 @@ from datetime import datetime from typing import List, Optional +import posthoganalytics import structlog from django.conf import settings from django.utils import timezone @@ -119,6 +120,11 @@ def send_email_verification(user_id: int, token: str) -> None: ) message.add_recipient(user.pending_email if user.pending_email is not None else user.email) message.send() + posthoganalytics.capture( + user.distinct_id, + "verification email sent", + groups={"organization": str(user.current_organization.id)}, # type: ignore + ) @app.task( diff --git a/posthog/tasks/test/test_email.py b/posthog/tasks/test/test_email.py index a728879586aad..571132fd1ca84 100644 --- a/posthog/tasks/test/test_email.py +++ b/posthog/tasks/test/test_email.py @@ -97,12 +97,18 @@ def test_send_password_reset(self, MockEmailMessage: MagicMock) -> None: assert mocked_email_messages[0].send.call_count == 1 assert mocked_email_messages[0].html_body - def test_send_email_verification(self, MockEmailMessage: MagicMock) -> None: + @patch("posthoganalytics.capture") + def test_send_email_verification(self, mock_capture: MagicMock, MockEmailMessage: MagicMock) -> None: mocked_email_messages = mock_email_messages(MockEmailMessage) org, user = create_org_team_and_user("2022-01-02 00:00:00", "admin@posthog.com") token = email_verification_token_generator.make_token(self.user) send_email_verification(user.id, token) + mock_capture.assert_called_once_with( + user.distinct_id, + "verification email sent", + groups={"organization": str(user.current_organization_id)}, + ) assert len(mocked_email_messages) == 1 assert mocked_email_messages[0].send.call_count == 1 assert mocked_email_messages[0].html_body