From 541f8f0e282e767249b20b7aa4396c5ab1e11dc7 Mon Sep 17 00:00:00 2001 From: davidenwang Date: Fri, 26 Apr 2024 14:22:52 -0700 Subject: [PATCH] feat(user): Add option to only return verified emails from get_email_addresses --- src/sentry/services/hybrid_cloud/user/impl.py | 10 ++++-- .../services/hybrid_cloud/user/service.py | 2 +- .../services/hybrid_cloud/user/test_impl.py | 33 +++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/sentry/services/hybrid_cloud/user/impl.py b/src/sentry/services/hybrid_cloud/user/impl.py index 3fe3705514cd7e..1471e52a7acd84 100644 --- a/src/sentry/services/hybrid_cloud/user/impl.py +++ b/src/sentry/services/hybrid_cloud/user/impl.py @@ -277,14 +277,18 @@ def trigger_user_consent_email_if_applicable(self, *, user_id: int) -> None: user.send_confirm_emails(is_new_user=True) def verify_user_emails( - self, *, user_id_emails: list[UserIdEmailArgs] + self, *, user_id_emails: list[UserIdEmailArgs], only_verified: bool = False ) -> dict[int, RpcVerifyUserEmail]: results = {} for user_id_email in user_id_emails: user_id = user_id_email["user_id"] email = user_id_email["email"] - exists = UserEmail.objects.filter(user_id=user_id, email__iexact=email).exists() - results[user_id] = RpcVerifyUserEmail(email=email, exists=exists) + + user_email_qs = UserEmail.objects.filter(user_id=user_id, email__iexact=email) + if only_verified: + user_email_qs = user_email_qs.filter(is_verified=True) + + results[user_id] = RpcVerifyUserEmail(email=email, exists=user_email_qs.exists()) return results def get_user_avatar(self, *, user_id: int) -> RpcAvatar | None: diff --git a/src/sentry/services/hybrid_cloud/user/service.py b/src/sentry/services/hybrid_cloud/user/service.py index 0739220be33e78..d34076fb731666 100644 --- a/src/sentry/services/hybrid_cloud/user/service.py +++ b/src/sentry/services/hybrid_cloud/user/service.py @@ -198,7 +198,7 @@ def trigger_user_consent_email_if_applicable(self, *, user_id: int) -> None: @rpc_method @abstractmethod def verify_user_emails( - self, *, user_id_emails: list[UserIdEmailArgs] + self, *, user_id_emails: list[UserIdEmailArgs], only_verified: bool = False ) -> dict[int, RpcVerifyUserEmail]: pass diff --git a/tests/sentry/services/hybrid_cloud/user/test_impl.py b/tests/sentry/services/hybrid_cloud/user/test_impl.py index 2b3c66ee50ed9c..c3adfc3078bc5d 100644 --- a/tests/sentry/services/hybrid_cloud/user/test_impl.py +++ b/tests/sentry/services/hybrid_cloud/user/test_impl.py @@ -2,6 +2,7 @@ from sentry.models.authidentity import AuthIdentity from sentry.models.authprovider import AuthProvider from sentry.models.user import User +from sentry.models.useremail import UserEmail from sentry.services.hybrid_cloud.user.service import user_service from sentry.testutils.cases import TestCase from sentry.testutils.silo import control_silo_test @@ -65,3 +66,35 @@ def test_get_user_with_ident(self): assert user2.id == fetched_user.id assert user1.id != fetched_user.id assert created is False + + def test_verify_user_emails(self): + user1 = self.create_user(email="test@email.com") + user2 = self.create_user(email="test2@email.com") + verified_emails = user_service.verify_user_emails( + user_id_emails=[ + {"user_id": user1.id, "email": "test@email.com"}, + {"user_id": user2.id, "email": "non-existent@email.com"}, + ], + only_verified=False, + ) + + # Tests that matching emails to user ids exist + assert verified_emails[user1.id].exists + assert not verified_emails[user2.id].exists + + def test_verify_user_emails_only_verified(self): + user1 = self.create_user(email="test@email.com") + user2 = self.create_user(email="test2@email.com") + UserEmail.objects.filter(user=user2, email="test2@email.com").update(is_verified=False) + + verified_emails = user_service.verify_user_emails( + user_id_emails=[ + {"user_id": user1.id, "email": "test@email.com"}, + {"user_id": user2.id, "email": "test2@email.com"}, + ], + only_verified=True, + ) + + # Tests that only verified emails are returned + assert verified_emails[user1.id].exists + assert not verified_emails[user2.id].exists