mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
Remove unused send_broadcast_message task
We only call send_broadcast_event now
This commit is contained in:
@@ -7,36 +7,6 @@ from app import notify_celery
|
|||||||
from app.dao.broadcast_message_dao import dao_get_broadcast_message_by_id, dao_get_broadcast_event_by_id
|
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}'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@notify_celery.task(name="send-broadcast-event")
|
@notify_celery.task(name="send-broadcast-event")
|
||||||
@statsd(namespace="tasks")
|
@statsd(namespace="tasks")
|
||||||
def send_broadcast_event(broadcast_event_id, provider='stub-1'):
|
def send_broadcast_event(broadcast_event_id, provider='stub-1'):
|
||||||
|
|||||||
@@ -5,71 +5,10 @@ from requests import RequestException
|
|||||||
|
|
||||||
from app.dao.templates_dao import dao_update_template
|
from app.dao.templates_dao import dao_update_template
|
||||||
from app.models import BROADCAST_TYPE, BroadcastStatusType, BroadcastEventMessageType
|
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
|
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')
|
@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(sample_service):
|
||||||
template = create_template(sample_service, BROADCAST_TYPE)
|
template = create_template(sample_service, BROADCAST_TYPE)
|
||||||
|
|||||||
Reference in New Issue
Block a user