From e31767dda4b849540e0f50f4e637d31527b07f6e Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Tue, 24 Jan 2023 23:30:24 +0000 Subject: [PATCH] settings: Make DEFAULT_LOGO_URI/DEFAULT_AVATAR_URI use staticfiles. --- zerver/lib/avatar.py | 6 +++++- zerver/lib/realm_icon.py | 5 ++++- zerver/lib/realm_logo.py | 5 ++++- zerver/tests/test_upload.py | 41 +++++++++++++++++++++++++++++++++--- zproject/default_settings.py | 4 ++-- 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/zerver/lib/avatar.py b/zerver/lib/avatar.py index bf2e3830aa..a1b7aa89f9 100644 --- a/zerver/lib/avatar.py +++ b/zerver/lib/avatar.py @@ -2,6 +2,7 @@ import urllib from typing import Any, Dict, Optional from django.conf import settings +from django.contrib.staticfiles.storage import staticfiles_storage from zerver.lib.avatar_hash import ( gravatar_hash, @@ -106,7 +107,10 @@ def _get_unversioned_gravatar_url(email: str, medium: bool) -> str: gravitar_query_suffix = f"&s={MEDIUM_AVATAR_SIZE}" if medium else "" hash_key = gravatar_hash(email) return f"https://secure.gravatar.com/avatar/{hash_key}?d=identicon{gravitar_query_suffix}" - return settings.DEFAULT_AVATAR_URI + elif settings.DEFAULT_AVATAR_URI is not None: + return settings.DEFAULT_AVATAR_URI + else: + return staticfiles_storage.url("images/default-avatar.png") def _get_unversioned_avatar_url( diff --git a/zerver/lib/realm_icon.py b/zerver/lib/realm_icon.py index bcde9b8cff..31eb2023e2 100644 --- a/zerver/lib/realm_icon.py +++ b/zerver/lib/realm_icon.py @@ -1,4 +1,5 @@ from django.conf import settings +from django.contrib.staticfiles.storage import staticfiles_storage from zerver.lib.avatar_hash import gravatar_hash from zerver.lib.upload import upload_backend @@ -15,5 +16,7 @@ def get_realm_icon_url(realm: Realm) -> str: elif settings.ENABLE_GRAVATAR: hash_key = gravatar_hash(realm.string_id) return f"https://secure.gravatar.com/avatar/{hash_key}?d=identicon" + elif settings.DEFAULT_AVATAR_URI is not None: + return settings.DEFAULT_AVATAR_URI else: - return settings.DEFAULT_AVATAR_URI + "?version=0" + return staticfiles_storage.url("images/default-avatar.png") + "?version=0" diff --git a/zerver/lib/realm_logo.py b/zerver/lib/realm_logo.py index 94c44bce95..f89305bbdb 100644 --- a/zerver/lib/realm_logo.py +++ b/zerver/lib/realm_logo.py @@ -1,6 +1,7 @@ from typing import Any, Dict from django.conf import settings +from django.contrib.staticfiles.storage import staticfiles_storage from zerver.lib.upload import upload_backend from zerver.models import Realm @@ -23,7 +24,9 @@ def get_realm_logo_url(realm: Realm, night: bool) -> str: else: logo_version = realm.logo_version return upload_backend.get_realm_logo_url(realm.id, logo_version, night) - return settings.DEFAULT_LOGO_URI + "?version=0" + if settings.DEFAULT_LOGO_URI is not None: + return settings.DEFAULT_LOGO_URI + return staticfiles_storage.url("images/logo/zulip-org-logo.svg") + "?version=0" def get_realm_logo_data(realm: Realm, night: bool) -> Dict[str, Any]: diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py index b27a07aa6a..d8e39ab9b2 100644 --- a/zerver/tests/test_upload.py +++ b/zerver/tests/test_upload.py @@ -1123,6 +1123,18 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase): redirect_url = response["Location"] self.assertTrue(redirect_url.endswith(str(avatar_url(cordelia)) + "&foo=bar")) + def test_get_settings_avatar(self) -> None: + self.login("hamlet") + cordelia = self.example_user("cordelia") + cordelia.email = cordelia.delivery_email + cordelia.save() + with self.settings( + ENABLE_GRAVATAR=False, DEFAULT_AVATAR_URI="http://other.server/avatar.svg" + ): + response = self.client_get("/avatar/cordelia@zulip.com", {"foo": "bar"}) + redirect_url = response["Location"] + self.assertEqual(redirect_url, "http://other.server/avatar.svg?version=1&foo=bar") + def test_get_user_avatar(self) -> None: hamlet = self.example_user("hamlet") self.login_user(hamlet) @@ -1530,7 +1542,16 @@ class RealmIconTest(UploadSerializeMixin, ZulipTestCase): redirect_url = response["Location"] self.assertTrue(redirect_url.endswith(realm_icon_url(realm) + "&foo=bar")) - def test_get_realm_icon(self) -> None: + def test_get_settings_realm_icon(self) -> None: + self.login("hamlet") + with self.settings( + ENABLE_GRAVATAR=False, DEFAULT_AVATAR_URI="http://other.server/icon.svg" + ): + response = self.client_get("/json/realm/icon", {"foo": "bar"}) + redirect_url = response["Location"] + self.assertEqual(redirect_url, "http://other.server/icon.svg?foo=bar") + + def test_get_uploaded_realm_icon(self) -> None: self.login("hamlet") realm = get_realm("zulip") @@ -1675,9 +1696,22 @@ class RealmLogoTest(UploadSerializeMixin, ZulipTestCase): redirect_url = response["Location"] is_night_str = str(self.night).lower() self.assertEqual( - redirect_url, f"/static/images/logo/zulip-org-logo.svg?version=0&night={is_night_str}" + redirect_url, + f"http://testserver/static/images/logo/zulip-org-logo.svg?version=0&night={is_night_str}", ) + def test_get_settings_logo(self) -> None: + self.login("hamlet") + with self.settings(DEFAULT_LOGO_URI="http://other.server/logo.svg"): + response = self.client_get( + "/json/realm/logo", {"night": orjson.dumps(self.night).decode()} + ) + redirect_url = response["Location"] + self.assertEqual( + redirect_url, + f"http://other.server/logo.svg?night={str(self.night).lower()}", + ) + def test_get_realm_logo(self) -> None: user_profile = self.example_user("hamlet") self.login_user(user_profile) @@ -1710,7 +1744,8 @@ class RealmLogoTest(UploadSerializeMixin, ZulipTestCase): response = self.client_get("/json/realm/logo", {"night": orjson.dumps(self.night).decode()}) redirect_url = response["Location"] self.assertEqual( - redirect_url, f"/static/images/logo/zulip-org-logo.svg?version=0&night={is_night_str}" + redirect_url, + f"http://testserver/static/images/logo/zulip-org-logo.svg?version=0&night={is_night_str}", ) def test_valid_logos(self) -> None: diff --git a/zproject/default_settings.py b/zproject/default_settings.py index 9ea8e029fd..14c73fa8cb 100644 --- a/zproject/default_settings.py +++ b/zproject/default_settings.py @@ -133,8 +133,8 @@ SENTRY_DSN: Optional[str] = None # File uploads and avatars # TODO: Rename MAX_FILE_UPLOAD_SIZE to have unit in name. -DEFAULT_AVATAR_URI = "/static/images/default-avatar.png" -DEFAULT_LOGO_URI = "/static/images/logo/zulip-org-logo.svg" +DEFAULT_AVATAR_URI: Optional[str] = None +DEFAULT_LOGO_URI: Optional[str] = None S3_AVATAR_BUCKET = "" S3_AUTH_UPLOADS_BUCKET = "" S3_REGION: Optional[str] = None