Browse Source

feat(provisioning): new user flag (#60657)

During partner provisioning I need to enforce password reset if user is
created new. This is because other wise they won't have access to their
identification to sign in for the first time. Creating this flag so I
can later trigger that behavior knowing who is new and who is not.

https://github.com/getsentry/getsentry/pull/12167/ ensures this change
doesn't break getsentry.
Athena Moghaddam 1 year ago
parent
commit
a4231fa871

+ 4 - 4
src/sentry/services/hybrid_cloud/user/impl.py

@@ -1,7 +1,7 @@
 from __future__ import annotations
 
 import logging
-from typing import Any, Callable, Dict, List, MutableMapping, Optional
+from typing import Any, Callable, Dict, List, MutableMapping, Optional, Tuple
 from uuid import uuid4
 
 from django.db import router, transaction
@@ -179,11 +179,11 @@ class DatabaseBackedUserService(UserService):
 
     def get_or_create_user_by_email(
         self, *, email: str, ident: Optional[str] = None, referrer: Optional[str] = None
-    ) -> RpcUser:
+    ) -> Tuple[RpcUser, bool]:
         with transaction.atomic(router.db_for_write(User)):
             rpc_user = self.get_user_by_email(email=email, ident=ident)
             if rpc_user:
-                return rpc_user
+                return (rpc_user, False)
 
             # Create User if it doesn't exist
             user = User.objects.create(
@@ -195,7 +195,7 @@ class DatabaseBackedUserService(UserService):
                 sender=self, user=user, source="api", referrer=referrer or "unknown"
             )
             user.update(flags=F("flags").bitor(User.flags.newsletter_consent_prompt))
-            return serialize_rpc_user(user)
+            return (serialize_rpc_user(user), True)
 
     def get_user_by_email(
         self,

+ 2 - 2
src/sentry/services/hybrid_cloud/user/service.py

@@ -4,7 +4,7 @@
 # defined, because we want to reflect on type annotations and avoid forward references.
 
 from abc import abstractmethod
-from typing import Any, Dict, List, Optional
+from typing import Any, Dict, List, Optional, Tuple
 
 from sentry.hybridcloud.rpc.services.caching import back_with_silo_cache
 from sentry.services.hybrid_cloud.auth import AuthenticationContext
@@ -144,7 +144,7 @@ class UserService(RpcService):
         email: str,
         ident: Optional[str] = None,
         referrer: Optional[str] = None,
-    ) -> RpcUser:
+    ) -> Tuple[RpcUser, bool]:
         pass
 
     @rpc_method

+ 10 - 5
tests/sentry/services/hybrid_cloud/user/test_impl.py

@@ -14,11 +14,12 @@ class DatabaseBackedUserService(TestCase):
 
     def test_create_new_user(self):
         old_user_count = User.objects.all().count()
-        rpc_user = user_service.get_or_create_user_by_email(email="test@email.com")
+        rpc_user, created = user_service.get_or_create_user_by_email(email="test@email.com")
         user = User.objects.get(id=rpc_user.id)
         new_user_count = User.objects.all().count()
         assert new_user_count == old_user_count + 1
         assert user.flags.newsletter_consent_prompt
+        assert created
 
     def test_get_no_existing(self):
         rpc_user = user_service.get_user_by_email(email="test@email.com")
@@ -27,23 +28,26 @@ class DatabaseBackedUserService(TestCase):
     def test_get_or_create_user(self):
         user1 = self.create_user(email="test@email.com", username="1")
         user2 = self.create_user(email="test@email.com", username="2")
-        user = user_service.get_or_create_user_by_email(email="test@email.com")
+        user, created = user_service.get_or_create_user_by_email(email="test@email.com")
         assert user1.id == user.id
         assert user2.id != user.id
+        assert created is False
 
     def test_get_active_user(self):
         inactive_user = self.create_user(
             email="test@email.com", username="inactive", is_active=False
         )
         active_user = self.create_user(email="test@email.com", username="active")
-        user = user_service.get_or_create_user_by_email(email="test@email.com")
+        user, created = user_service.get_or_create_user_by_email(email="test@email.com")
         assert active_user.id == user.id
         assert inactive_user.id != user.id
+        assert created is False
 
     def test_get_user_ci(self):
         user = self.create_user(email="tESt@email.com")
-        fetched_user = user_service.get_or_create_user_by_email(email="TesT@email.com")
+        fetched_user, created = user_service.get_or_create_user_by_email(email="TesT@email.com")
         assert user.id == fetched_user.id
+        assert created is False
 
     def test_get_user_with_ident(self):
         user1 = self.create_user(email="test@email.com", username="1")
@@ -55,8 +59,9 @@ class DatabaseBackedUserService(TestCase):
             organization_id=org.id, provider="fly", config=config_data
         )
         AuthIdentity.objects.create(auth_provider=provider, user=user2, ident=partner_user_id)
-        fetched_user = user_service.get_or_create_user_by_email(
+        fetched_user, created = user_service.get_or_create_user_by_email(
             email="TesT@email.com", ident=partner_user_id
         )
         assert user2.id == fetched_user.id
         assert user1.id != fetched_user.id
+        assert created is False