From f3f172991be0f6cf6b1fff246aed1fef87c63472 Mon Sep 17 00:00:00 2001 From: Puneeth Chaganti Date: Fri, 26 Apr 2019 18:32:16 +0530 Subject: [PATCH] open graph: Use uploaded realm icon for open graph images. --- templates/zerver/meta_tags.html | 10 +++++++++- zerver/context_processors.py | 3 +++ zerver/tests/test_middleware.py | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/templates/zerver/meta_tags.html b/templates/zerver/meta_tags.html index c7efc8e0a2..5432e7e3fd 100644 --- a/templates/zerver/meta_tags.html +++ b/templates/zerver/meta_tags.html @@ -16,7 +16,12 @@ {% endif %} +{% if OPEN_GRAPH_IMAGE %} + +{% else %} +{% endif %} + @@ -27,5 +32,8 @@ {% endif %} - +{% if OPEN_GRAPH_IMAGE %} + +{% else %} +{% endif %} diff --git a/zerver/context_processors.py b/zerver/context_processors.py index abe2b9a4ff..44d73b76c7 100644 --- a/zerver/context_processors.py +++ b/zerver/context_processors.py @@ -133,6 +133,9 @@ def zulip_default_context(request: HttpRequest) -> Dict[str, Any]: 'allow_search_engine_indexing': allow_search_engine_indexing, } + if realm is not None and realm.icon_source == realm.ICON_UPLOADED: + context['OPEN_GRAPH_IMAGE'] = '%s%s' % (realm_uri, realm_icon) + return context def login_context(request: HttpRequest) -> Dict[str, Any]: diff --git a/zerver/tests/test_middleware.py b/zerver/tests/test_middleware.py index 801594cbe7..a2890cb82e 100644 --- a/zerver/tests/test_middleware.py +++ b/zerver/tests/test_middleware.py @@ -4,6 +4,7 @@ from typing import List from bs4 import BeautifulSoup from django.test import override_settings from unittest.mock import Mock, patch +from zerver.lib.realm_icon import get_realm_icon_url from zerver.lib.test_classes import ZulipTestCase from zerver.middleware import is_slow_query, write_log_line from zerver.models import get_realm @@ -167,3 +168,19 @@ class OpenGraphTest(ZulipTestCase): ['Welcome to Clojurians Zulip - the place where the Clojure community meets', 'note-1', 'note-2', 'note-3', 'Enjoy!'], []) + + def test_login_page_realm_icon(self) -> None: + realm = get_realm('zulip') + realm.icon_source = 'U' + realm.save(update_fields=['icon_source']) + realm_icon = get_realm_icon_url(realm) + + response = self.client_get('/login/') + self.assertEqual(response.status_code, 200) + + decoded = response.content.decode('utf-8') + bs = BeautifulSoup(decoded, features='lxml') + open_graph_image = bs.select_one('meta[property="og:image"]').get('content') + twitter_image = bs.select_one('meta[name="twitter:image"]').get('content') + self.assertTrue(open_graph_image.endswith(realm_icon)) + self.assertTrue(twitter_image.endswith(realm_icon))