open graph: Uploaded realm icon URL is absolute for some backends.

The `LocalUploadBackend` returns a relative URL, while the `S3UploadBackend`
returns an absolute URL. This commit switches to using `urljoin` to obtain the
absolute URL, instead of simply joining strings.
This commit is contained in:
Puneeth Chaganti
2019-05-16 17:37:12 +05:30
committed by Tim Abbott
parent 263657dcb6
commit bdf8183f55
2 changed files with 18 additions and 2 deletions

View File

@@ -1,3 +1,5 @@
from urllib.parse import urljoin
from typing import Any, Dict, Optional
from django.http import HttpRequest
from django.conf import settings
@@ -137,7 +139,7 @@ def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
context['OPEN_GRAPH_URL'] = '%s%s' % (realm_uri, request.path)
if realm is not None and realm.icon_source == realm.ICON_UPLOADED:
context['OPEN_GRAPH_IMAGE'] = '%s%s' % (realm_uri, realm_icon)
context['OPEN_GRAPH_IMAGE'] = urljoin(realm_uri, realm_icon)
return context

View File

@@ -176,7 +176,21 @@ class OpenGraphTest(ZulipTestCase):
decoded = response.content.decode('utf-8')
bs = BeautifulSoup(decoded, features='lxml')
open_graph_image = bs.select_one('meta[property="og:image"]').get('content')
self.assertTrue(open_graph_image.endswith(realm_icon))
self.assertEqual(open_graph_image, '%s%s' % (realm.uri, realm_icon))
def test_login_page_realm_icon_absolute_url(self) -> None:
realm = get_realm('zulip')
realm.icon_source = 'U'
realm.save(update_fields=['icon_source'])
icon_url = "https://foo.s3.amazonaws.com/%s/realm/icon.png?version=%s" % (realm.id, 1)
with patch('zerver.lib.realm_icon.upload_backend.get_realm_icon_url', return_value=icon_url):
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')
self.assertEqual(open_graph_image, icon_url)
def test_no_realm_api_page_og_url(self) -> None:
response = self.client_get('/api/', subdomain='')