mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 08:51:30 -05:00
Merge pull request #779 from alphagov/switch-to-cloudfront
use cloudfront instead of flask to serve static images
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
from urllib import parse
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
@@ -121,17 +122,53 @@ def provider_to_use(notification_type, notification_id):
|
|||||||
return clients.get_client_by_name_and_type(active_providers_in_order[0].identifier, notification_type)
|
return clients.get_client_by_name_and_type(active_providers_in_order[0].identifier, notification_type)
|
||||||
|
|
||||||
|
|
||||||
|
def get_logo_url(base_url, branding_path, logo_file):
|
||||||
|
"""
|
||||||
|
Get the complete URL for a given logo.
|
||||||
|
|
||||||
|
We have to convert the base_url into a static url. Our hosted environments all have their own cloudfront instances,
|
||||||
|
found at the static subdomain (eg https://static.notifications.service.gov.uk).
|
||||||
|
|
||||||
|
If running locally (dev environment), don't try and use cloudfront - just stick to the actual underlying source
|
||||||
|
({URL}/static/{PATH})
|
||||||
|
"""
|
||||||
|
base_url = parse.urlparse(base_url)
|
||||||
|
netloc = base_url.netloc
|
||||||
|
|
||||||
|
if base_url.netloc.startswith('localhost'):
|
||||||
|
netloc = 'localhost:6012'
|
||||||
|
path = '/static' + branding_path + logo_file
|
||||||
|
else:
|
||||||
|
if base_url.netloc.startswith('www'):
|
||||||
|
# strip "www."
|
||||||
|
netloc = base_url.netloc[4:]
|
||||||
|
|
||||||
|
netloc = 'static.' + netloc
|
||||||
|
path = branding_path + logo_file
|
||||||
|
|
||||||
|
logo_url = parse.ParseResult(
|
||||||
|
scheme=base_url.scheme,
|
||||||
|
netloc=netloc,
|
||||||
|
path=path,
|
||||||
|
params=base_url.params,
|
||||||
|
query=base_url.query,
|
||||||
|
fragment=base_url.fragment
|
||||||
|
)
|
||||||
|
return parse.urlunparse(logo_url)
|
||||||
|
|
||||||
|
|
||||||
def get_html_email_options(service):
|
def get_html_email_options(service):
|
||||||
govuk_banner = service.branding != BRANDING_ORG
|
govuk_banner = service.branding != BRANDING_ORG
|
||||||
if service.organisation:
|
if service.organisation:
|
||||||
logo = '{}{}{}'.format(
|
logo_url = get_logo_url(
|
||||||
current_app.config['ADMIN_BASE_URL'],
|
current_app.config['ADMIN_BASE_URL'],
|
||||||
current_app.config['BRANDING_PATH'],
|
current_app.config['BRANDING_PATH'],
|
||||||
service.organisation.logo
|
service.organisation.logo
|
||||||
)
|
)
|
||||||
|
|
||||||
branding = {
|
branding = {
|
||||||
'brand_colour': service.organisation.colour,
|
'brand_colour': service.organisation.colour,
|
||||||
'brand_logo': logo,
|
'brand_logo': logo_url,
|
||||||
'brand_name': service.organisation.name,
|
'brand_name': service.organisation.name,
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ class Config(object):
|
|||||||
SQLALCHEMY_TRACK_MODIFICATIONS = True
|
SQLALCHEMY_TRACK_MODIFICATIONS = True
|
||||||
PAGE_SIZE = 50
|
PAGE_SIZE = 50
|
||||||
SMS_CHAR_COUNT_LIMIT = 495
|
SMS_CHAR_COUNT_LIMIT = 495
|
||||||
BRANDING_PATH = '/static/images/email-template/crests/'
|
BRANDING_PATH = '/images/email-template/crests/'
|
||||||
TEST_MESSAGE_FILENAME = 'Test message'
|
TEST_MESSAGE_FILENAME = 'Test message'
|
||||||
|
|
||||||
NOTIFY_SERVICE_ID = 'd6aa2c68-a2d9-4437-ab19-3ae8eb202553'
|
NOTIFY_SERVICE_ID = 'd6aa2c68-a2d9-4437-ab19-3ae8eb202553'
|
||||||
|
|||||||
@@ -20,8 +20,9 @@ statsd==3.2.1
|
|||||||
jsonschema==2.5.1
|
jsonschema==2.5.1
|
||||||
Flask-Redis==0.1.0
|
Flask-Redis==0.1.0
|
||||||
|
|
||||||
git+https://github.com/alphagov/notifications-python-client.git@3.0.0#egg=notifications-python-client==3.0.0
|
# pin to minor version 3.1.x
|
||||||
|
notifications-python-client>=3.1,<3.2
|
||||||
|
|
||||||
git+https://github.com/alphagov/notifications-utils.git@12.1.1#egg=notifications-utils==12.1.1
|
git+https://github.com/alphagov/notifications-utils.git@12.2.0#egg=notifications-utils==12.2.0
|
||||||
|
|
||||||
git+https://github.com/alphagov/boto.git@2.43.0-patch3#egg=boto==2.43.0-patch3
|
git+https://github.com/alphagov/boto.git@2.43.0-patch3#egg=boto==2.43.0-patch3
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from unittest.mock import ANY
|
from unittest.mock import ANY
|
||||||
@@ -379,18 +380,35 @@ def test_get_html_email_renderer_with_branding_details(branding_type, govuk_bann
|
|||||||
assert options['brand_name'] == 'Justice League'
|
assert options['brand_name'] == 'Justice League'
|
||||||
|
|
||||||
|
|
||||||
def test_get_html_email_renderer_prepends_logo_path(notify_db, sample_service):
|
def test_get_html_email_renderer_prepends_logo_path(notify_api):
|
||||||
sample_service.branding = BRANDING_ORG
|
Service = namedtuple('Service', ['branding', 'organisation'])
|
||||||
org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League')
|
Organisation = namedtuple('Organisation', ['colour', 'name', 'logo'])
|
||||||
sample_service.organisation = org
|
|
||||||
notify_db.session.add_all([sample_service, org])
|
|
||||||
notify_db.session.commit()
|
|
||||||
|
|
||||||
renderer = send_to_providers.get_html_email_options(sample_service)
|
org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League')
|
||||||
|
service = Service(branding=BRANDING_ORG, organisation=org)
|
||||||
|
|
||||||
|
renderer = send_to_providers.get_html_email_options(service)
|
||||||
|
|
||||||
assert renderer['brand_logo'] == 'http://localhost:6012/static/images/email-template/crests/justice-league.png'
|
assert renderer['brand_logo'] == 'http://localhost:6012/static/images/email-template/crests/justice-league.png'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('base_url, expected_url', [
|
||||||
|
# don't change localhost to prevent errors when testing locally
|
||||||
|
('http://localhost:6012', 'http://localhost:6012/static/sub-path/filename.png'),
|
||||||
|
# on other environments, replace www with staging
|
||||||
|
('https://www.notifications.service.gov.uk', 'https://static.notifications.service.gov.uk/sub-path/filename.png'),
|
||||||
|
('https://www.notify.works', 'https://static.notify.works/sub-path/filename.png'),
|
||||||
|
('https://notify.works', 'https://static.notify.works/sub-path/filename.png'),
|
||||||
|
])
|
||||||
|
def test_get_logo_url_works_for_different_environments(base_url, expected_url):
|
||||||
|
branding_path = '/sub-path/'
|
||||||
|
logo_file = 'filename.png'
|
||||||
|
|
||||||
|
logo_url = send_to_providers.get_logo_url(base_url, branding_path, logo_file)
|
||||||
|
|
||||||
|
assert logo_url == expected_url
|
||||||
|
|
||||||
|
|
||||||
def test_should_not_set_billable_units_if_research_mode(notify_db, sample_service, sample_notification, mocker):
|
def test_should_not_set_billable_units_if_research_mode(notify_db, sample_service, sample_notification, mocker):
|
||||||
mocker.patch('app.mmg_client.send_sms')
|
mocker.patch('app.mmg_client.send_sms')
|
||||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
||||||
|
|||||||
Reference in New Issue
Block a user