Merge pull request #2970 from alphagov/fix-govuk-link

Bring in latest version of utils to fix link breaking bug
This commit is contained in:
David McDonald
2020-09-14 15:40:14 +01:00
committed by GitHub
6 changed files with 29 additions and 113 deletions

View File

@@ -4,37 +4,7 @@ from notifications_utils.statsd_decorators import statsd
from app import notify_celery
from app.dao.broadcast_message_dao import dao_get_broadcast_message_by_id, dao_get_broadcast_event_by_id
@notify_celery.task(name="send-broadcast-message")
@statsd(namespace="tasks")
def send_broadcast_message(broadcast_message_id, provider='stub-1'):
# imports of schemas from tasks have to happen within functions to prevent
# `AttributeError: 'DummySession' object has no attribute 'query'` errors in unrelated tests
from app.schemas import template_schema
broadcast_message = dao_get_broadcast_message_by_id(broadcast_message_id)
current_app.logger.info(
f'sending broadcast_message {broadcast_message_id} '
f'status {broadcast_message.status} to {provider}'
)
payload = {
"template": template_schema.dump(broadcast_message.template).data,
"broadcast_message": broadcast_message.serialize(),
}
resp = requests.post(
f'{current_app.config["CBC_PROXY_URL"]}/broadcasts/{provider}',
json=payload
)
resp.raise_for_status()
current_app.logger.info(
f'broadcast_message {broadcast_message.id} '
f'status {broadcast_message.status} sent to {provider}'
)
from app.dao.broadcast_message_dao import dao_get_broadcast_event_by_id
@notify_celery.task(name="send-broadcast-event")

View File

@@ -29,7 +29,7 @@ notifications-python-client==5.7.0
# PaaS
awscli-cwlogs>=1.4,<1.5
git+https://github.com/alphagov/notifications-utils.git@40.9.0#egg=notifications-utils==40.9.0
git+https://github.com/alphagov/notifications-utils.git@42.0.0#egg=notifications-utils==42.0.0
# gds-metrics requires prometheseus 0.2.0, override that requirement as 0.7.1 brings significant performance gains
prometheus-client==0.8.0

View File

@@ -31,25 +31,25 @@ notifications-python-client==5.7.0
# PaaS
awscli-cwlogs>=1.4,<1.5
git+https://github.com/alphagov/notifications-utils.git@40.9.0#egg=notifications-utils==40.9.0
git+https://github.com/alphagov/notifications-utils.git@42.0.0#egg=notifications-utils==42.0.0
# gds-metrics requires prometheseus 0.2.0, override that requirement as 0.7.1 brings significant performance gains
prometheus-client==0.8.0
gds-metrics==0.2.4
## The following requirements were added by pip freeze:
alembic==1.4.2
alembic==1.4.3
amqp==1.4.9
anyjson==0.3.3
attrs==20.2.0
awscli==1.18.133
awscli==1.18.137
bcrypt==3.2.0
billiard==3.3.0.23
bleach==3.1.4
blinker==1.4
boto==2.49.0
boto3==1.10.38
botocore==1.17.56
botocore==1.17.60
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
@@ -76,7 +76,7 @@ phonenumbers==8.11.2
pyasn1==0.4.8
pycparser==2.20
PyPDF2==1.26.0
pyrsistent==0.16.0
pyrsistent==0.17.3
python-dateutil==2.8.1
python-editor==1.0.4
python-json-logger==0.1.11

View File

@@ -13,7 +13,7 @@ def test_get_broadcast_message(admin_request, sample_service):
t = create_template(sample_service, BROADCAST_TYPE)
bm = create_broadcast_message(t, areas={
"areas": ['place A', 'region B'],
"simple_polygons": [[50.1, 1.2], [50.12, 1.2]]
"simple_polygons": [[[50.1, 1.2], [50.12, 1.2], [50.13, 1.2]]]
})
response = admin_request.get(
@@ -145,7 +145,7 @@ def test_update_broadcast_message_allows_edit_while_not_yet_live(admin_request,
t = create_template(sample_service, BROADCAST_TYPE)
bm = create_broadcast_message(
t,
areas={"areas": ['manchester'], "simple_polygons": [[50.12, 1.2], [50.13, 1.2], [50.14, 1.21]]},
areas={"areas": ['manchester'], "simple_polygons": [[[50.12, 1.2], [50.13, 1.2], [50.14, 1.21]]]},
status=status
)
@@ -154,7 +154,7 @@ def test_update_broadcast_message_allows_edit_while_not_yet_live(admin_request,
_data={
'starts_at': '2020-06-01 20:00:01',
'areas': ['london', 'glasgow'],
"simple_polygons": [[51.12, 0.2], [50.13, 0.4], [50.14, 0.45]]
"simple_polygons": [[[51.12, 0.2], [50.13, 0.4], [50.14, 0.45]]]
},
service_id=t.service_id,
broadcast_message_id=bm.id,
@@ -163,7 +163,7 @@ def test_update_broadcast_message_allows_edit_while_not_yet_live(admin_request,
assert response['starts_at'] == '2020-06-01T20:00:01.000000Z'
assert response['areas'] == ['london', 'glasgow']
assert response['simple_polygons'] == [[51.12, 0.2], [50.13, 0.4], [50.14, 0.45]]
assert response['simple_polygons'] == [[[51.12, 0.2], [50.13, 0.4], [50.14, 0.45]]]
assert response['updated_at'] is not None
@@ -191,7 +191,7 @@ def test_update_broadcast_message_sets_finishes_at_separately(admin_request, sam
t = create_template(sample_service, BROADCAST_TYPE)
bm = create_broadcast_message(
t,
areas={"areas": ['london'], "simple_polygons": [[50.12, 1.2], [50.13, 1.2], [50.14, 1.21]]}
areas={"areas": ['london'], "simple_polygons": [[[50.12, 1.2], [50.13, 1.2], [50.14, 1.21]]]}
)
response = admin_request.post(
@@ -237,7 +237,7 @@ def test_update_broadcast_message_doesnt_let_you_update_status(admin_request, sa
'broadcast_message.update_broadcast_message',
_data={
'areas': ['glasgow'],
"simple_polygons": [[55.86, -4.25], [55.85, -4.25], [55.87, -4.24]],
"simple_polygons": [[[55.86, -4.25], [55.85, -4.25], [55.87, -4.24]]],
'status': BroadcastStatusType.BROADCASTING},
service_id=t.service_id,
broadcast_message_id=bm.id,
@@ -252,7 +252,7 @@ def test_update_broadcast_message_doesnt_let_you_update_status(admin_request, sa
@pytest.mark.parametrize("incomplete_area_data", [
{"areas": ["cardiff"]},
{"simple_polygons": [[51.28, -3.11], [51.29, -3.12], [51.27, -3.10]]},
{"simple_polygons": [[[51.28, -3.11], [51.29, -3.12], [51.27, -3.10]]]},
])
def test_update_broadcast_message_doesnt_let_you_update_areas_but_not_polygons(
admin_request, sample_service, incomplete_area_data
@@ -348,7 +348,7 @@ def test_update_broadcast_message_status_stores_approved_by_and_approved_at_and_
bm = create_broadcast_message(
t,
status=BroadcastStatusType.PENDING_APPROVAL,
areas={"areas": ["london"], "simple_polygons": [[51.30, 0.7], [51.28, 0.8], [51.25, -0.7]]}
areas={"areas": ["london"], "simple_polygons": [[[51.30, 0.7], [51.28, 0.8], [51.25, -0.7]]]}
)
approver = create_user(email='approver@gov.uk')
sample_service.users.append(approver)
@@ -436,7 +436,7 @@ def test_update_broadcast_message_status_allows_platform_admin_to_approve_own_me
bm = create_broadcast_message(
t,
status=BroadcastStatusType.PENDING_APPROVAL,
areas={"areas": ["london"], "simple_polygons": [[51.30, 0.7], [51.28, 0.8], [51.25, -0.7]]}
areas={"areas": ["london"], "simple_polygons": [[[51.30, 0.7], [51.28, 0.8], [51.25, -0.7]]]}
)
mock_task = mocker.patch('app.celery.broadcast_message_tasks.send_broadcast_event.apply_async')
@@ -469,7 +469,7 @@ def test_update_broadcast_message_status_allows_trial_mode_services_to_approve_o
bm = create_broadcast_message(
t,
status=BroadcastStatusType.PENDING_APPROVAL,
areas={"areas": ["london"], "simple_polygons": [[51.30, 0.7], [51.28, 0.8], [51.25, -0.7]]}
areas={"areas": ["london"], "simple_polygons": [[[51.30, 0.7], [51.28, 0.8], [51.25, -0.7]]]}
)
mock_task = mocker.patch('app.celery.broadcast_message_tasks.send_broadcast_event.apply_async')

View File

@@ -3,77 +3,19 @@ import pytest
import requests_mock
from requests import RequestException
from app.dao.templates_dao import dao_update_template
from app.models import BROADCAST_TYPE, BroadcastStatusType, BroadcastEventMessageType
from app.celery.broadcast_message_tasks import send_broadcast_message, send_broadcast_event
from app.celery.broadcast_message_tasks import send_broadcast_event
from tests.app.db import create_template, create_broadcast_message, create_broadcast_event
def test_send_broadcast_message_sends_data_correctly(sample_service):
template = create_template(sample_service, BROADCAST_TYPE)
broadcast_message = create_broadcast_message(
template,
areas={"areas": ['london'], "simple_polygons": [[50.12, 1.2], [50.13, 1.2], [50.14, 1.21]]},
status=BroadcastStatusType.BROADCASTING
)
with requests_mock.Mocker() as request_mock:
request_mock.post("http://test-cbc-proxy/broadcasts/stub-1", json={'valid': 'true'}, status_code=200)
send_broadcast_message(broadcast_message_id=str(broadcast_message.id))
assert request_mock.call_count == 1
assert request_mock.request_history[0].method == 'POST'
assert request_mock.request_history[0].headers["Content-type"] == "application/json"
cbc_json = request_mock.request_history[0].json()
assert cbc_json['template']['id'] == str(template.id)
assert cbc_json['template']['template_type'] == BROADCAST_TYPE
assert cbc_json['broadcast_message']['areas'] == ['london']
def test_send_broadcast_message_sends_old_version_of_template(sample_service):
template = create_template(sample_service, BROADCAST_TYPE, content='first content')
broadcast_message = create_broadcast_message(
template,
areas={"areas": ['london'], "simple_polygons": [[50.12, 1.2], [50.13, 1.2], [50.14, 1.21]]},
status=BroadcastStatusType.BROADCASTING
)
template.content = 'second content'
dao_update_template(template)
assert template.version == 2
with requests_mock.Mocker() as request_mock:
request_mock.post("http://test-cbc-proxy/broadcasts/stub-1", json={'valid': 'true'}, status_code=200)
send_broadcast_message(broadcast_message_id=str(broadcast_message.id))
assert request_mock.call_count == 1
assert request_mock.request_history[0].method == 'POST'
assert request_mock.request_history[0].headers["Content-type"] == "application/json"
cbc_json = request_mock.request_history[0].json()
assert cbc_json['template']['id'] == str(template.id)
assert cbc_json['template']['version'] == 1
assert cbc_json['template']['content'] == 'first content'
def test_send_broadcast_message_errors(sample_service):
template = create_template(sample_service, BROADCAST_TYPE)
broadcast_message = create_broadcast_message(template, status=BroadcastStatusType.BROADCASTING)
with requests_mock.Mocker() as request_mock:
request_mock.post("http://test-cbc-proxy/broadcasts/stub-1", text='503 bad gateway', status_code=503)
# we're not retrying or anything for the moment - but this'll ensure any exception gets logged
with pytest.raises(RequestException) as ex:
send_broadcast_message(broadcast_message_id=str(broadcast_message.id))
assert ex.value.response.status_code == 503
@freeze_time('2020-08-01 12:00')
def test_send_broadcast_event_sends_data_correctly(sample_service):
template = create_template(sample_service, BROADCAST_TYPE)
broadcast_message = create_broadcast_message(template, areas=['london'], status=BroadcastStatusType.BROADCASTING)
broadcast_message = create_broadcast_message(
template,
areas={"areas": ['london'], "simple_polygons": [[[50.12, 1.2], [50.13, 1.2], [50.14, 1.21]]]},
status=BroadcastStatusType.BROADCASTING
)
event = create_broadcast_event(broadcast_message)
with requests_mock.Mocker() as request_mock:
@@ -89,7 +31,9 @@ def test_send_broadcast_event_sends_data_correctly(sample_service):
assert cbc_json['broadcast_message_id'] == str(broadcast_message.id)
assert cbc_json['sent_at'] == '2020-08-01T12:00:00.000000Z'
assert cbc_json['transmitted_starts_at'] is None
assert cbc_json['transmitted_areas'] == ['london']
assert cbc_json['transmitted_areas'] == {
"areas": ['london'], "simple_polygons": [[[50.12, 1.2], [50.13, 1.2], [50.14, 1.21]]]
}
def test_send_broadcast_event_sends_references(sample_service):

View File

@@ -1040,7 +1040,9 @@ def create_broadcast_event(
sent_at=sent_at or datetime.utcnow(),
message_type=message_type,
transmitted_content=transmitted_content or {'body': 'this is an emergency broadcast message'},
transmitted_areas=transmitted_areas or ['london'],
transmitted_areas=transmitted_areas or {
'areas': ['london'], 'simple_polygons': [[[50.12, 1.2], [50.13, 1.2], [50.14, 1.21]]]
},
transmitted_sender=transmitted_sender or 'www.notifications.service.gov.uk',
transmitted_starts_at=transmitted_starts_at,
transmitted_finishes_at=transmitted_finishes_at or datetime.utcnow(),