mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 23:26:23 -05:00
Merge pull request #3035 from alphagov/broadcast-event-response
Send broadcast events per provider
This commit is contained in:
@@ -1,13 +1,38 @@
|
||||
import uuid
|
||||
from unittest.mock import call, ANY
|
||||
|
||||
from freezegun import freeze_time
|
||||
import pytest
|
||||
|
||||
from app.models import BROADCAST_TYPE, BroadcastStatusType, BroadcastEventMessageType
|
||||
from app.celery.broadcast_message_tasks import send_broadcast_event
|
||||
from tests.app.db import create_template, create_broadcast_message, create_broadcast_event
|
||||
from app.models import BROADCAST_TYPE, BroadcastStatusType, BroadcastEventMessageType, BroadcastProviderMessageStatus
|
||||
from app.celery.broadcast_message_tasks import send_broadcast_event, send_broadcast_provider_message, trigger_link_test
|
||||
|
||||
from tests.app.db import (
|
||||
create_template,
|
||||
create_broadcast_message,
|
||||
create_broadcast_event,
|
||||
create_broadcast_provider_message
|
||||
)
|
||||
from tests.conftest import set_config
|
||||
|
||||
|
||||
def test_send_broadcast_event_queues_up_for_active_providers(mocker, notify_api):
|
||||
mock_send_broadcast_provider_message = mocker.patch(
|
||||
'app.celery.broadcast_message_tasks.send_broadcast_provider_message',
|
||||
)
|
||||
|
||||
event_id = uuid.uuid4()
|
||||
with set_config(notify_api, 'ENABLED_CBCS', ['ee', 'vodafone']):
|
||||
send_broadcast_event(event_id)
|
||||
|
||||
assert mock_send_broadcast_provider_message.apply_async.call_args_list == [
|
||||
call(kwargs={'broadcast_event_id': event_id, 'provider': 'ee'}, queue='notify-internal-tasks'),
|
||||
call(kwargs={'broadcast_event_id': event_id, 'provider': 'vodafone'}, queue='notify-internal-tasks')
|
||||
]
|
||||
|
||||
|
||||
@freeze_time('2020-08-01 12:00')
|
||||
def test_create_broadcast_event_sends_data_correctly(mocker, sample_service):
|
||||
def test_send_broadcast_provider_message_sends_data_correctly(mocker, sample_service):
|
||||
template = create_template(sample_service, BROADCAST_TYPE)
|
||||
broadcast_message = create_broadcast_message(
|
||||
template,
|
||||
@@ -23,13 +48,18 @@ def test_create_broadcast_event_sends_data_correctly(mocker, sample_service):
|
||||
event = create_broadcast_event(broadcast_message)
|
||||
|
||||
mock_create_broadcast = mocker.patch(
|
||||
'app.cbc_proxy_client.create_and_send_broadcast',
|
||||
'app.clients.cbc_proxy.CBCProxyEE.create_and_send_broadcast',
|
||||
)
|
||||
|
||||
send_broadcast_event(broadcast_event_id=str(event.id))
|
||||
assert event.get_provider_message('ee') is None
|
||||
|
||||
send_broadcast_provider_message(provider='ee', broadcast_event_id=str(event.id))
|
||||
|
||||
broadcast_provider_message = event.get_provider_message('ee')
|
||||
assert broadcast_provider_message.status == BroadcastProviderMessageStatus.SENDING
|
||||
|
||||
mock_create_broadcast.assert_called_once_with(
|
||||
identifier=str(event.id),
|
||||
identifier=str(broadcast_provider_message.id),
|
||||
headline='GOV.UK Notify Broadcast',
|
||||
description='this is an emergency broadcast message',
|
||||
areas=[{
|
||||
@@ -46,7 +76,7 @@ def test_create_broadcast_event_sends_data_correctly(mocker, sample_service):
|
||||
)
|
||||
|
||||
|
||||
def test_update_broadcast_event_sends_references(mocker, sample_service):
|
||||
def test_send_broadcast_provider_message_sends_update_with_references(mocker, sample_service):
|
||||
template = create_template(sample_service, BROADCAST_TYPE, content='content')
|
||||
|
||||
broadcast_message = create_broadcast_message(
|
||||
@@ -61,28 +91,34 @@ def test_update_broadcast_event_sends_references(mocker, sample_service):
|
||||
)
|
||||
|
||||
alert_event = create_broadcast_event(broadcast_message, message_type=BroadcastEventMessageType.ALERT)
|
||||
create_broadcast_provider_message(alert_event, 'ee')
|
||||
update_event = create_broadcast_event(broadcast_message, message_type=BroadcastEventMessageType.UPDATE)
|
||||
|
||||
mock_update_broadcast = mocker.patch(
|
||||
'app.cbc_proxy_client.update_and_send_broadcast',
|
||||
'app.clients.cbc_proxy.CBCProxyEE.update_and_send_broadcast',
|
||||
)
|
||||
|
||||
send_broadcast_event(broadcast_event_id=str(update_event.id))
|
||||
send_broadcast_provider_message(provider='ee', broadcast_event_id=str(update_event.id))
|
||||
|
||||
broadcast_provider_message = update_event.get_provider_message('ee')
|
||||
assert broadcast_provider_message.status == BroadcastProviderMessageStatus.SENDING
|
||||
|
||||
mock_update_broadcast.assert_called_once_with(
|
||||
identifier=str(update_event.id),
|
||||
identifier=str(broadcast_provider_message.id),
|
||||
headline="GOV.UK Notify Broadcast",
|
||||
description='this is an emergency broadcast message',
|
||||
areas=[{
|
||||
"polygon": [[50.12, 1.2], [50.13, 1.2], [50.14, 1.21]],
|
||||
}],
|
||||
references=[alert_event.reference],
|
||||
previous_provider_messages=[
|
||||
alert_event.get_provider_message('ee')
|
||||
],
|
||||
sent=update_event.sent_at_as_cap_datetime_string,
|
||||
expires=update_event.transmitted_finishes_at_as_cap_datetime_string,
|
||||
)
|
||||
|
||||
|
||||
def test_cancel_broadcast_event_sends_references(mocker, sample_service):
|
||||
def test_send_broadcast_provider_message_sends_cancel_with_references(mocker, sample_service):
|
||||
template = create_template(sample_service, BROADCAST_TYPE, content='content')
|
||||
|
||||
broadcast_message = create_broadcast_message(
|
||||
@@ -100,26 +136,35 @@ def test_cancel_broadcast_event_sends_references(mocker, sample_service):
|
||||
update_event = create_broadcast_event(broadcast_message, message_type=BroadcastEventMessageType.UPDATE)
|
||||
cancel_event = create_broadcast_event(broadcast_message, message_type=BroadcastEventMessageType.CANCEL)
|
||||
|
||||
create_broadcast_provider_message(alert_event, 'ee')
|
||||
create_broadcast_provider_message(update_event, 'ee')
|
||||
|
||||
mock_cancel_broadcast = mocker.patch(
|
||||
'app.cbc_proxy_client.cancel_broadcast',
|
||||
'app.clients.cbc_proxy.CBCProxyEE.cancel_broadcast',
|
||||
)
|
||||
|
||||
send_broadcast_event(broadcast_event_id=str(cancel_event.id))
|
||||
send_broadcast_provider_message(provider='ee', broadcast_event_id=str(cancel_event.id))
|
||||
|
||||
broadcast_provider_message = cancel_event.get_provider_message('ee')
|
||||
assert broadcast_provider_message.status == BroadcastProviderMessageStatus.SENDING
|
||||
|
||||
mock_cancel_broadcast.assert_called_once_with(
|
||||
identifier=str(cancel_event.id),
|
||||
identifier=str(broadcast_provider_message.id),
|
||||
headline="GOV.UK Notify Broadcast",
|
||||
description='this is an emergency broadcast message',
|
||||
areas=[{
|
||||
"polygon": [[50.12, 1.2], [50.13, 1.2], [50.14, 1.21]],
|
||||
}],
|
||||
references=[alert_event.reference, update_event.reference],
|
||||
previous_provider_messages=[
|
||||
alert_event.get_provider_message('ee'),
|
||||
update_event.get_provider_message('ee')
|
||||
],
|
||||
sent=cancel_event.sent_at_as_cap_datetime_string,
|
||||
expires=cancel_event.transmitted_finishes_at_as_cap_datetime_string,
|
||||
)
|
||||
|
||||
|
||||
def test_send_broadcast_event_errors(mocker, sample_service):
|
||||
def test_send_broadcast_provider_message_errors(mocker, sample_service):
|
||||
template = create_template(sample_service, BROADCAST_TYPE)
|
||||
|
||||
broadcast_message = create_broadcast_message(
|
||||
@@ -136,17 +181,17 @@ def test_send_broadcast_event_errors(mocker, sample_service):
|
||||
event = create_broadcast_event(broadcast_message)
|
||||
|
||||
mock_create_broadcast = mocker.patch(
|
||||
'app.cbc_proxy_client.create_and_send_broadcast',
|
||||
'app.clients.cbc_proxy.CBCProxyEE.create_and_send_broadcast',
|
||||
side_effect=Exception('oh no'),
|
||||
)
|
||||
|
||||
with pytest.raises(Exception) as ex:
|
||||
send_broadcast_event(broadcast_event_id=str(event.id))
|
||||
send_broadcast_provider_message(provider='ee', broadcast_event_id=str(event.id))
|
||||
|
||||
assert ex.match('oh no')
|
||||
|
||||
mock_create_broadcast.assert_called_once_with(
|
||||
identifier=str(event.id),
|
||||
identifier=ANY,
|
||||
headline="GOV.UK Notify Broadcast",
|
||||
description='this is an emergency broadcast message',
|
||||
areas=[{
|
||||
@@ -159,3 +204,22 @@ def test_send_broadcast_event_errors(mocker, sample_service):
|
||||
sent=event.sent_at_as_cap_datetime_string,
|
||||
expires=event.transmitted_finishes_at_as_cap_datetime_string,
|
||||
)
|
||||
|
||||
|
||||
def test_trigger_link_tests_invokes_cbc_proxy_client(
|
||||
mocker,
|
||||
):
|
||||
mock_send_link_test = mocker.patch(
|
||||
'app.clients.cbc_proxy.CBCProxyEE.send_link_test',
|
||||
)
|
||||
|
||||
trigger_link_test('ee')
|
||||
|
||||
assert mock_send_link_test.called
|
||||
# the 0th argument of the call to send_link_test
|
||||
identifier = mock_send_link_test.mock_calls[0][1][0]
|
||||
|
||||
try:
|
||||
uuid.UUID(identifier)
|
||||
except BaseException:
|
||||
pytest.fail(f"{identifier} is not a valid uuid")
|
||||
|
||||
@@ -19,6 +19,7 @@ from app.celery.scheduled_tasks import (
|
||||
check_for_missing_rows_in_completed_jobs,
|
||||
check_for_services_with_high_failure_rates_or_sending_to_tv_numbers,
|
||||
switch_current_sms_provider_on_slow_delivery,
|
||||
trigger_link_tests,
|
||||
)
|
||||
from app.config import QueueNames, Config
|
||||
from app.dao.jobs_dao import dao_get_job_by_id
|
||||
@@ -30,8 +31,8 @@ from app.models import (
|
||||
NOTIFICATION_DELIVERED,
|
||||
NOTIFICATION_PENDING_VIRUS_CHECK,
|
||||
)
|
||||
from tests.conftest import set_config
|
||||
from tests.app import load_example_csv
|
||||
|
||||
from tests.app.db import (
|
||||
create_notification,
|
||||
create_template,
|
||||
@@ -560,9 +561,10 @@ def test_check_for_services_with_high_failure_rates_or_sending_to_tv_numbers(
|
||||
|
||||
def test_send_canary_to_cbc_proxy_invokes_cbc_proxy_client(
|
||||
mocker,
|
||||
notify_api
|
||||
):
|
||||
mock_send_canary = mocker.patch(
|
||||
'app.cbc_proxy_client.send_canary',
|
||||
'app.clients.cbc_proxy.CBCProxyCanary.send_canary',
|
||||
)
|
||||
|
||||
scheduled_tasks.send_canary_to_cbc_proxy()
|
||||
@@ -577,20 +579,17 @@ def test_send_canary_to_cbc_proxy_invokes_cbc_proxy_client(
|
||||
pytest.fail(f"{identifier} is not a valid uuid")
|
||||
|
||||
|
||||
def test_trigger_link_tests_invokes_cbc_proxy_client(
|
||||
mocker,
|
||||
def test_trigger_link_tests_calls_for_all_providers(
|
||||
mocker, notify_api
|
||||
):
|
||||
mock_send_link_test = mocker.patch(
|
||||
'app.cbc_proxy_client.send_link_test',
|
||||
mock_trigger_link_test = mocker.patch(
|
||||
'app.celery.scheduled_tasks.trigger_link_test',
|
||||
)
|
||||
|
||||
scheduled_tasks.trigger_link_tests()
|
||||
with set_config(notify_api, 'ENABLED_CBCS', ['ee', 'vodafone']):
|
||||
trigger_link_tests()
|
||||
|
||||
mock_send_link_test.assert_called
|
||||
# the 0th argument of the call to send_link_test
|
||||
identifier = mock_send_link_test.mock_calls[0][1][0]
|
||||
|
||||
try:
|
||||
uuid.UUID(identifier)
|
||||
except BaseException:
|
||||
pytest.fail(f"{identifier} is not a valid uuid")
|
||||
assert mock_trigger_link_test.apply_async.call_args_list == [
|
||||
call(kwargs={'provider': 'ee'}, queue='notify-internal-tasks'),
|
||||
call(kwargs={'provider': 'vodafone'}, queue='notify-internal-tasks')
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user