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

feat(user): Add option to only return verified emails from verify_user_emails #69812

Merged
merged 1 commit into from
Apr 29, 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
10 changes: 7 additions & 3 deletions src/sentry/services/hybrid_cloud/user/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/sentry/services/hybrid_cloud/user/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 33 additions & 0 deletions tests/sentry/services/hybrid_cloud/user/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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="[email protected]")
user2 = self.create_user(email="[email protected]")
verified_emails = user_service.verify_user_emails(
user_id_emails=[
{"user_id": user1.id, "email": "[email protected]"},
{"user_id": user2.id, "email": "[email protected]"},
],
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="[email protected]")
user2 = self.create_user(email="[email protected]")
UserEmail.objects.filter(user=user2, email="[email protected]").update(is_verified=False)

verified_emails = user_service.verify_user_emails(
user_id_emails=[
{"user_id": user1.id, "email": "[email protected]"},
{"user_id": user2.id, "email": "[email protected]"},
],
only_verified=True,
)

# Tests that only verified emails are returned
assert verified_emails[user1.id].exists
assert not verified_emails[user2.id].exists
Loading