mirror of
https://github.com/zulip/zulip.git
synced 2025-11-11 01:16:19 +00:00
models: Refactor get_fake_email_domain to take realm.host as arg.
This commit updates get_fake_email_domain to accept realm.host as argument instead of the Realm object since we only use realm.host to get the fake email domain. This is a preparatory commit for the limited guest feature as we would be sending the fake email of the message sender in message event object to a guest user who cannot access the sender and there we would need to compute the fake email.
This commit is contained in:
@@ -80,7 +80,7 @@ def do_delete_user(user_profile: UserProfile, *, acting_user: Optional[UserProfi
|
||||
replacement_user = create_user(
|
||||
force_id=user_id,
|
||||
email=Address(
|
||||
username=f"deleteduser{user_id}", domain=get_fake_email_domain(realm)
|
||||
username=f"deleteduser{user_id}", domain=get_fake_email_domain(realm.host)
|
||||
).addr_spec,
|
||||
password=None,
|
||||
realm=realm,
|
||||
@@ -186,7 +186,7 @@ def do_delete_user_preserving_messages(user_profile: UserProfile) -> None:
|
||||
# is cleaner by using the same re-assignment approach for them together with Messages.
|
||||
random_token = secrets.token_hex(16)
|
||||
temp_replacement_user = create_user(
|
||||
email=f"temp_deleteduser{random_token}@{get_fake_email_domain(realm)}",
|
||||
email=f"temp_deleteduser{random_token}@{get_fake_email_domain(realm.host)}",
|
||||
password=None,
|
||||
realm=realm,
|
||||
full_name=f"Deleted User {user_id} (temp)",
|
||||
@@ -206,7 +206,7 @@ def do_delete_user_preserving_messages(user_profile: UserProfile) -> None:
|
||||
|
||||
replacement_user = create_user(
|
||||
force_id=user_id,
|
||||
email=f"deleteduser{user_id}@{get_fake_email_domain(realm)}",
|
||||
email=f"deleteduser{user_id}@{get_fake_email_domain(realm.host)}",
|
||||
password=None,
|
||||
realm=realm,
|
||||
full_name=f"Deleted User {user_id}",
|
||||
|
||||
@@ -70,7 +70,7 @@ def copy_default_settings(
|
||||
def get_display_email_address(user_profile: UserProfile) -> str:
|
||||
if not user_profile.email_address_is_realm_public():
|
||||
return Address(
|
||||
username=f"user{user_profile.id}", domain=get_fake_email_domain(user_profile.realm)
|
||||
username=f"user{user_profile.id}", domain=get_fake_email_domain(user_profile.realm.host)
|
||||
).addr_spec
|
||||
return user_profile.delivery_email
|
||||
|
||||
|
||||
@@ -796,7 +796,9 @@ def get_cross_realm_dicts() -> List[APIUserDict]:
|
||||
|
||||
|
||||
def get_data_for_inaccessible_user(realm: Realm, user_id: int) -> APIUserDict:
|
||||
fake_email = Address(username=f"user{user_id}", domain=get_fake_email_domain(realm)).addr_spec
|
||||
fake_email = Address(
|
||||
username=f"user{user_id}", domain=get_fake_email_domain(realm.host)
|
||||
).addr_spec
|
||||
|
||||
# We just set date_joined field to UNIX epoch.
|
||||
user_date_joined = timestamp_to_datetime(0)
|
||||
|
||||
@@ -995,7 +995,7 @@ class Realm(models.Model): # type: ignore[django-manager-missing] # django-stub
|
||||
)
|
||||
|
||||
def get_bot_domain(self) -> str:
|
||||
return get_fake_email_domain(self)
|
||||
return get_fake_email_domain(self.host)
|
||||
|
||||
def get_notifications_stream(self) -> Optional["Stream"]:
|
||||
if self.notifications_stream is not None and not self.notifications_stream.deactivated:
|
||||
@@ -5194,11 +5194,11 @@ class InvalidFakeEmailDomainError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def get_fake_email_domain(realm: Realm) -> str:
|
||||
def get_fake_email_domain(realm_host: str) -> str:
|
||||
try:
|
||||
# Check that realm.host can be used to form valid email addresses.
|
||||
validate_email(Address(username="bot", domain=realm.host).addr_spec)
|
||||
return realm.host
|
||||
validate_email(Address(username="bot", domain=realm_host).addr_spec)
|
||||
return realm_host
|
||||
except ValidationError:
|
||||
pass
|
||||
|
||||
|
||||
@@ -2820,27 +2820,27 @@ class DeleteUserTest(ZulipTestCase):
|
||||
class FakeEmailDomainTest(ZulipTestCase):
|
||||
def test_get_fake_email_domain(self) -> None:
|
||||
realm = get_realm("zulip")
|
||||
self.assertEqual("zulip.testserver", get_fake_email_domain(realm))
|
||||
self.assertEqual("zulip.testserver", get_fake_email_domain(realm.host))
|
||||
|
||||
with self.settings(EXTERNAL_HOST="example.com"):
|
||||
self.assertEqual("zulip.example.com", get_fake_email_domain(realm))
|
||||
self.assertEqual("zulip.example.com", get_fake_email_domain(realm.host))
|
||||
|
||||
@override_settings(FAKE_EMAIL_DOMAIN="fakedomain.com", REALM_HOSTS={"zulip": "127.0.0.1"})
|
||||
def test_get_fake_email_domain_realm_host_is_ip_addr(self) -> None:
|
||||
realm = get_realm("zulip")
|
||||
self.assertEqual("fakedomain.com", get_fake_email_domain(realm))
|
||||
self.assertEqual("fakedomain.com", get_fake_email_domain(realm.host))
|
||||
|
||||
@override_settings(FAKE_EMAIL_DOMAIN="invaliddomain", REALM_HOSTS={"zulip": "127.0.0.1"})
|
||||
def test_invalid_fake_email_domain(self) -> None:
|
||||
realm = get_realm("zulip")
|
||||
with self.assertRaises(InvalidFakeEmailDomainError):
|
||||
get_fake_email_domain(realm)
|
||||
get_fake_email_domain(realm.host)
|
||||
|
||||
@override_settings(FAKE_EMAIL_DOMAIN="127.0.0.1", REALM_HOSTS={"zulip": "127.0.0.1"})
|
||||
def test_invalid_fake_email_domain_ip(self) -> None:
|
||||
with self.assertRaises(InvalidFakeEmailDomainError):
|
||||
realm = get_realm("zulip")
|
||||
get_fake_email_domain(realm)
|
||||
get_fake_email_domain(realm.host)
|
||||
|
||||
|
||||
class TestBulkRegenerateAPIKey(ZulipTestCase):
|
||||
|
||||
Reference in New Issue
Block a user