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:
Sahil Batra
2023-11-07 12:42:19 +05:30
committed by Tim Abbott
parent 7da68259a6
commit a6fa875c23
5 changed files with 16 additions and 14 deletions

View File

@@ -80,7 +80,7 @@ def do_delete_user(user_profile: UserProfile, *, acting_user: Optional[UserProfi
replacement_user = create_user( replacement_user = create_user(
force_id=user_id, force_id=user_id,
email=Address( 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, ).addr_spec,
password=None, password=None,
realm=realm, 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. # is cleaner by using the same re-assignment approach for them together with Messages.
random_token = secrets.token_hex(16) random_token = secrets.token_hex(16)
temp_replacement_user = create_user( 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, password=None,
realm=realm, realm=realm,
full_name=f"Deleted User {user_id} (temp)", 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( replacement_user = create_user(
force_id=user_id, 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, password=None,
realm=realm, realm=realm,
full_name=f"Deleted User {user_id}", full_name=f"Deleted User {user_id}",

View File

@@ -70,7 +70,7 @@ def copy_default_settings(
def get_display_email_address(user_profile: UserProfile) -> str: def get_display_email_address(user_profile: UserProfile) -> str:
if not user_profile.email_address_is_realm_public(): if not user_profile.email_address_is_realm_public():
return Address( 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 ).addr_spec
return user_profile.delivery_email return user_profile.delivery_email

View File

@@ -796,7 +796,9 @@ def get_cross_realm_dicts() -> List[APIUserDict]:
def get_data_for_inaccessible_user(realm: Realm, user_id: int) -> 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. # We just set date_joined field to UNIX epoch.
user_date_joined = timestamp_to_datetime(0) user_date_joined = timestamp_to_datetime(0)

View File

@@ -995,7 +995,7 @@ class Realm(models.Model): # type: ignore[django-manager-missing] # django-stub
) )
def get_bot_domain(self) -> str: 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"]: def get_notifications_stream(self) -> Optional["Stream"]:
if self.notifications_stream is not None and not self.notifications_stream.deactivated: if self.notifications_stream is not None and not self.notifications_stream.deactivated:
@@ -5194,11 +5194,11 @@ class InvalidFakeEmailDomainError(Exception):
pass pass
def get_fake_email_domain(realm: Realm) -> str: def get_fake_email_domain(realm_host: str) -> str:
try: try:
# Check that realm.host can be used to form valid email addresses. # Check that realm.host can be used to form valid email addresses.
validate_email(Address(username="bot", domain=realm.host).addr_spec) validate_email(Address(username="bot", domain=realm_host).addr_spec)
return realm.host return realm_host
except ValidationError: except ValidationError:
pass pass

View File

@@ -2820,27 +2820,27 @@ class DeleteUserTest(ZulipTestCase):
class FakeEmailDomainTest(ZulipTestCase): class FakeEmailDomainTest(ZulipTestCase):
def test_get_fake_email_domain(self) -> None: def test_get_fake_email_domain(self) -> None:
realm = get_realm("zulip") 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"): 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"}) @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: def test_get_fake_email_domain_realm_host_is_ip_addr(self) -> None:
realm = get_realm("zulip") 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"}) @override_settings(FAKE_EMAIL_DOMAIN="invaliddomain", REALM_HOSTS={"zulip": "127.0.0.1"})
def test_invalid_fake_email_domain(self) -> None: def test_invalid_fake_email_domain(self) -> None:
realm = get_realm("zulip") realm = get_realm("zulip")
with self.assertRaises(InvalidFakeEmailDomainError): 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"}) @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: def test_invalid_fake_email_domain_ip(self) -> None:
with self.assertRaises(InvalidFakeEmailDomainError): with self.assertRaises(InvalidFakeEmailDomainError):
realm = get_realm("zulip") realm = get_realm("zulip")
get_fake_email_domain(realm) get_fake_email_domain(realm.host)
class TestBulkRegenerateAPIKey(ZulipTestCase): class TestBulkRegenerateAPIKey(ZulipTestCase):