mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-18 08:02:31 -05:00
celery: broadcast msg create invokes cbc proxy
When we create a broadcast message, we should invoke the cbc proxy to send a cap message Either a function will be invoked within AWS, or a noop function call is made, depending on the environment We have only implemented CB message creation in the CBC Proxy, without polygons, therefore we: * only invoke the CBC Proxy during message creation * only send description, identifier, and hard-coded headline Signed-off-by: Toby Lorne <toby.lornewelch-richards@digital.cabinet-office.gov.uk> Co-authored-by: Pea <pea.tyczynska@digital.cabinet-office.gov.uk> Co-authored-by: Katie <katie.smith@digital.cabinet-office.gov.uk>
This commit is contained in:
@@ -2,8 +2,9 @@ import requests
|
|||||||
from flask import current_app
|
from flask import current_app
|
||||||
from notifications_utils.statsd_decorators import statsd
|
from notifications_utils.statsd_decorators import statsd
|
||||||
|
|
||||||
from app import notify_celery
|
from app import cbc_proxy_client, notify_celery
|
||||||
|
|
||||||
|
from app.models import BroadcastEventMessageType
|
||||||
from app.dao.broadcast_message_dao import dao_get_broadcast_event_by_id
|
from app.dao.broadcast_message_dao import dao_get_broadcast_event_by_id
|
||||||
|
|
||||||
|
|
||||||
@@ -12,6 +13,19 @@ from app.dao.broadcast_message_dao import dao_get_broadcast_event_by_id
|
|||||||
def send_broadcast_event(broadcast_event_id, provider='stub-1'):
|
def send_broadcast_event(broadcast_event_id, provider='stub-1'):
|
||||||
broadcast_event = dao_get_broadcast_event_by_id(broadcast_event_id)
|
broadcast_event = dao_get_broadcast_event_by_id(broadcast_event_id)
|
||||||
|
|
||||||
|
if broadcast_event.message_type == BroadcastEventMessageType.ALERT:
|
||||||
|
current_app.logger.info(
|
||||||
|
f'invoking cbc proxy to send '
|
||||||
|
f'broadcast_event {broadcast_event.reference} '
|
||||||
|
f'msgType {broadcast_event.message_type} to {provider}'
|
||||||
|
)
|
||||||
|
|
||||||
|
cbc_proxy_client.create_and_send_broadcast(
|
||||||
|
identifier=str(broadcast_event.id),
|
||||||
|
headline="GOV.UK Notify Broadcast",
|
||||||
|
description=broadcast_event.transmitted_content['body'],
|
||||||
|
)
|
||||||
|
|
||||||
current_app.logger.info(
|
current_app.logger.info(
|
||||||
f'sending broadcast_event {broadcast_event.reference} '
|
f'sending broadcast_event {broadcast_event.reference} '
|
||||||
f'msgType {broadcast_event.message_type} to {provider}'
|
f'msgType {broadcast_event.message_type} to {provider}'
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from tests.app.db import create_template, create_broadcast_message, create_broad
|
|||||||
|
|
||||||
|
|
||||||
@freeze_time('2020-08-01 12:00')
|
@freeze_time('2020-08-01 12:00')
|
||||||
def test_send_broadcast_event_sends_data_correctly(sample_service):
|
def test_send_broadcast_event_sends_data_correctly(mocker, sample_service):
|
||||||
template = create_template(sample_service, BROADCAST_TYPE)
|
template = create_template(sample_service, BROADCAST_TYPE)
|
||||||
broadcast_message = create_broadcast_message(
|
broadcast_message = create_broadcast_message(
|
||||||
template,
|
template,
|
||||||
@@ -18,10 +18,20 @@ def test_send_broadcast_event_sends_data_correctly(sample_service):
|
|||||||
)
|
)
|
||||||
event = create_broadcast_event(broadcast_message)
|
event = create_broadcast_event(broadcast_message)
|
||||||
|
|
||||||
|
mock_create_broadcast = mocker.patch(
|
||||||
|
'app.cbc_proxy_client.create_and_send_broadcast',
|
||||||
|
)
|
||||||
|
|
||||||
with requests_mock.Mocker() as request_mock:
|
with requests_mock.Mocker() as request_mock:
|
||||||
request_mock.post("http://test-cbc-proxy/broadcasts/events/stub-1", json={'valid': 'true'}, status_code=200)
|
request_mock.post("http://test-cbc-proxy/broadcasts/events/stub-1", json={'valid': 'true'}, status_code=200)
|
||||||
send_broadcast_event(broadcast_event_id=str(event.id))
|
send_broadcast_event(broadcast_event_id=str(event.id))
|
||||||
|
|
||||||
|
mock_create_broadcast.assert_called_once_with(
|
||||||
|
identifier=str(event.id),
|
||||||
|
headline="GOV.UK Notify Broadcast",
|
||||||
|
description='this is an emergency broadcast message',
|
||||||
|
)
|
||||||
|
|
||||||
assert request_mock.call_count == 1
|
assert request_mock.call_count == 1
|
||||||
assert request_mock.request_history[0].method == 'POST'
|
assert request_mock.request_history[0].method == 'POST'
|
||||||
assert request_mock.request_history[0].headers["Content-type"] == "application/json"
|
assert request_mock.request_history[0].headers["Content-type"] == "application/json"
|
||||||
@@ -36,16 +46,22 @@ def test_send_broadcast_event_sends_data_correctly(sample_service):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_send_broadcast_event_sends_references(sample_service):
|
def test_send_broadcast_event_sends_references(mocker, sample_service):
|
||||||
template = create_template(sample_service, BROADCAST_TYPE, content='content')
|
template = create_template(sample_service, BROADCAST_TYPE, content='content')
|
||||||
broadcast_message = create_broadcast_message(template, areas=['london'], status=BroadcastStatusType.BROADCASTING)
|
broadcast_message = create_broadcast_message(template, areas=['london'], status=BroadcastStatusType.BROADCASTING)
|
||||||
alert_event = create_broadcast_event(broadcast_message, message_type=BroadcastEventMessageType.ALERT)
|
alert_event = create_broadcast_event(broadcast_message, message_type=BroadcastEventMessageType.ALERT)
|
||||||
cancel_event = create_broadcast_event(broadcast_message, message_type=BroadcastEventMessageType.CANCEL)
|
cancel_event = create_broadcast_event(broadcast_message, message_type=BroadcastEventMessageType.CANCEL)
|
||||||
|
|
||||||
|
mock_create_broadcast = mocker.patch(
|
||||||
|
'app.cbc_proxy_client.create_and_send_broadcast',
|
||||||
|
)
|
||||||
|
|
||||||
with requests_mock.Mocker() as request_mock:
|
with requests_mock.Mocker() as request_mock:
|
||||||
request_mock.post("http://test-cbc-proxy/broadcasts/events/stub-1", json={'valid': 'true'}, status_code=200)
|
request_mock.post("http://test-cbc-proxy/broadcasts/events/stub-1", json={'valid': 'true'}, status_code=200)
|
||||||
send_broadcast_event(broadcast_event_id=str(cancel_event.id))
|
send_broadcast_event(broadcast_event_id=str(cancel_event.id))
|
||||||
|
|
||||||
|
assert not mock_create_broadcast.called
|
||||||
|
|
||||||
assert request_mock.call_count == 1
|
assert request_mock.call_count == 1
|
||||||
assert request_mock.request_history[0].method == 'POST'
|
assert request_mock.request_history[0].method == 'POST'
|
||||||
assert request_mock.request_history[0].headers["Content-type"] == "application/json"
|
assert request_mock.request_history[0].headers["Content-type"] == "application/json"
|
||||||
@@ -56,11 +72,15 @@ def test_send_broadcast_event_sends_references(sample_service):
|
|||||||
assert cbc_json['previous_event_references'] == [alert_event.reference]
|
assert cbc_json['previous_event_references'] == [alert_event.reference]
|
||||||
|
|
||||||
|
|
||||||
def test_send_broadcast_event_errors(sample_service):
|
def test_send_broadcast_event_errors(mocker, sample_service):
|
||||||
template = create_template(sample_service, BROADCAST_TYPE)
|
template = create_template(sample_service, BROADCAST_TYPE)
|
||||||
broadcast_message = create_broadcast_message(template, status=BroadcastStatusType.BROADCASTING)
|
broadcast_message = create_broadcast_message(template, status=BroadcastStatusType.BROADCASTING)
|
||||||
event = create_broadcast_event(broadcast_message)
|
event = create_broadcast_event(broadcast_message)
|
||||||
|
|
||||||
|
mock_create_broadcast = mocker.patch(
|
||||||
|
'app.cbc_proxy_client.create_and_send_broadcast',
|
||||||
|
)
|
||||||
|
|
||||||
with requests_mock.Mocker() as request_mock:
|
with requests_mock.Mocker() as request_mock:
|
||||||
request_mock.post("http://test-cbc-proxy/broadcasts/events/stub-1", text='503 bad gateway', status_code=503)
|
request_mock.post("http://test-cbc-proxy/broadcasts/events/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
|
# we're not retrying or anything for the moment - but this'll ensure any exception gets logged
|
||||||
@@ -68,3 +88,9 @@ def test_send_broadcast_event_errors(sample_service):
|
|||||||
send_broadcast_event(broadcast_event_id=str(event.id))
|
send_broadcast_event(broadcast_event_id=str(event.id))
|
||||||
|
|
||||||
assert ex.value.response.status_code == 503
|
assert ex.value.response.status_code == 503
|
||||||
|
|
||||||
|
mock_create_broadcast.assert_called_once_with(
|
||||||
|
identifier=str(event.id),
|
||||||
|
headline="GOV.UK Notify Broadcast",
|
||||||
|
description='this is an emergency broadcast message',
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user