From ade077ff73e4f61c7553aaaebb9716ec97fa680b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Tue, 30 Jan 2018 19:19:52 +0100 Subject: [PATCH] backend: Add bot config data to initial state data. --- zerver/models.py | 20 ++++++++++++++++---- zerver/tests/test_home.py | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/zerver/models.py b/zerver/models.py index e69208db02..9924e4963d 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -1929,11 +1929,23 @@ def get_bot_services(user_profile_id: str) -> List[Service]: return list(Service.objects.filter(user_profile__id=user_profile_id)) def get_service_dicts_for_bots(user_profile_id: str) -> List[Dict[str, Any]]: + user_profile = get_user_profile_by_id(user_profile_id) services = get_bot_services(user_profile_id) - service_dicts = [{'base_url': service.base_url, - 'interface': service.interface, - } - for service in services] + service_dicts = [] # type: List[Dict[Text, Any]] + if user_profile.bot_type == UserProfile.OUTGOING_WEBHOOK_BOT: + service_dicts = [{'base_url': service.base_url, + 'interface': service.interface, + } + for service in services] + elif user_profile.bot_type == UserProfile.EMBEDDED_BOT: + from zerver.lib.bot_config import get_bot_config, ConfigError + try: + service_dicts = [{'config_data': get_bot_config(user_profile), + 'service_name': services[0].name + }] + # A ConfigError just means that there are no config entries for user_profile. + except ConfigError: + pass return service_dicts def get_service_profile(user_profile_id: str, service_name: str) -> Service: diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py index 2bb5b524f7..bda68a2dee 100644 --- a/zerver/tests/test_home.py +++ b/zerver/tests/test_home.py @@ -186,8 +186,8 @@ class HomeTest(ZulipTestCase): with patch('zerver.lib.cache.cache_set') as cache_mock: result = self._get_home_page(stream='Denmark') - self.assert_length(queries, 41) - self.assert_length(cache_mock.call_args_list, 7) + self.assert_length(queries, 42) + self.assert_length(cache_mock.call_args_list, 8) html = result.content.decode('utf-8')