Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
bots: Use realm.host for bot email domain if possible.
Mateusz Mandera2283aa8a6268
diff --git a/zerver/models.py b/zerver/models.pyindex e464754fc8d..f08b941c684 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -546,7 +546,12 @@ class Realm(models.Model):is_active=True)def get_bot_domain(self) -> str:- return get_fake_email_domain()+ try:+ # Check that realm.host can be used to form valid email addresses.+ validate_email(f"bot@{self.host}")+ return self.host+ except ValidationError:+ return get_fake_email_domain()def get_notifications_stream(self) -> Optional['Stream']:if self.notifications_stream is not None and not self.notifications_stream.deactivated:diff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.pyindex 413c7f27831..0d2e2034310 100644--- a/zerver/tests/test_bots.py+++ b/zerver/tests/test_bots.py@@ -105,7 +105,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):self.assert_json_error(result, 'Bad name or username')self.assert_num_bots_equal(0)- @override_settings(FAKE_EMAIL_DOMAIN="invaliddomain")+ @override_settings(FAKE_EMAIL_DOMAIN="invaliddomain", REALM_HOSTS={"zulip": "127.0.0.1"})def test_add_bot_with_invalid_fake_email_domain(self) -> None:self.login('hamlet')self.assert_num_bots_equal(0)@@ -204,6 +204,26 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):self.assertEqual(bot['bot_owner_id'], self.example_user('hamlet').id)self.assertEqual(bot['user_id'], self.get_bot_user(email).id)+ @override_settings(FAKE_EMAIL_DOMAIN="fakedomain.com", REALM_HOSTS={"zulip": "127.0.0.1"})+ def test_add_bot_with_fake_email_domain(self) -> None:+ self.login('hamlet')+ self.assert_num_bots_equal(0)+ self.create_bot()+ self.assert_num_bots_equal(1)++ email = '[email protected]'+ self.get_bot_user(email)++ @override_settings(FAKE_EMAIL_DOMAIN="fakedomain.com", REALM_HOSTS={"zulip": "zulip.example.com"})+ def test_add_bot_host_used_as_domain_if_valid(self) -> None:+ self.login('hamlet')+ self.assert_num_bots_equal(0)+ self.create_bot()+ self.assert_num_bots_equal(1)++ email = '[email protected]'+ self.get_bot_user(email)+def test_add_bot_with_username_in_use(self) -> None:self.login('hamlet')self.assert_num_bots_equal(0)