mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Merge pull request #3003 from alphagov/staging-preview-cbc
Non-production environments invoke CBC Proxy during broadcast event creation
This commit is contained in:
@@ -9,7 +9,7 @@ from tests.app.db import create_template, create_broadcast_message, create_broad
|
||||
|
||||
|
||||
@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)
|
||||
broadcast_message = create_broadcast_message(
|
||||
template,
|
||||
@@ -18,10 +18,20 @@ def test_send_broadcast_event_sends_data_correctly(sample_service):
|
||||
)
|
||||
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:
|
||||
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))
|
||||
|
||||
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.request_history[0].method == 'POST'
|
||||
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')
|
||||
broadcast_message = create_broadcast_message(template, areas=['london'], status=BroadcastStatusType.BROADCASTING)
|
||||
alert_event = create_broadcast_event(broadcast_message, message_type=BroadcastEventMessageType.ALERT)
|
||||
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:
|
||||
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))
|
||||
|
||||
assert not mock_create_broadcast.called
|
||||
|
||||
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"
|
||||
@@ -56,11 +72,15 @@ def test_send_broadcast_event_sends_references(sample_service):
|
||||
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)
|
||||
broadcast_message = create_broadcast_message(template, status=BroadcastStatusType.BROADCASTING)
|
||||
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:
|
||||
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
|
||||
@@ -68,3 +88,9 @@ def test_send_broadcast_event_errors(sample_service):
|
||||
send_broadcast_event(broadcast_event_id=str(event.id))
|
||||
|
||||
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',
|
||||
)
|
||||
|
||||
127
tests/app/clients/test_cbc_proxy.py
Normal file
127
tests/app/clients/test_cbc_proxy.py
Normal file
@@ -0,0 +1,127 @@
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from app.clients.cbc_proxy import CBCProxyClient
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def cbc_proxy(client, mocker):
|
||||
client = CBCProxyClient()
|
||||
current_app = mocker.Mock(config={
|
||||
'CBC_PROXY_AWS_ACCESS_KEY_ID': 'cbc-proxy-aws-access-key-id',
|
||||
'CBC_PROXY_AWS_SECRET_ACCESS_KEY': 'cbc-proxy-aws-secret-access-key',
|
||||
})
|
||||
client.init_app(current_app)
|
||||
return client
|
||||
|
||||
|
||||
def test_cbc_proxy_lambda_client_has_correct_region(cbc_proxy):
|
||||
assert cbc_proxy._lambda_client._client_config.region_name == 'eu-west-2'
|
||||
|
||||
|
||||
def test_cbc_proxy_lambda_client_has_correct_keys(cbc_proxy):
|
||||
key = cbc_proxy._lambda_client._request_signer._credentials.access_key
|
||||
secret = cbc_proxy._lambda_client._request_signer._credentials.secret_key
|
||||
|
||||
assert key == 'cbc-proxy-aws-access-key-id'
|
||||
assert secret == 'cbc-proxy-aws-secret-access-key'
|
||||
|
||||
|
||||
def test_cbc_proxy_create_and_send_invokes_function(mocker, cbc_proxy):
|
||||
identifier = 'my-identifier'
|
||||
headline = 'my-headline'
|
||||
description = 'my-description'
|
||||
|
||||
ld_client_mock = mocker.patch.object(
|
||||
cbc_proxy,
|
||||
'_lambda_client',
|
||||
create=True,
|
||||
)
|
||||
|
||||
ld_client_mock.invoke.return_value = {
|
||||
'StatusCode': 200,
|
||||
}
|
||||
|
||||
cbc_proxy.create_and_send_broadcast(
|
||||
identifier=identifier,
|
||||
headline=headline,
|
||||
description=description,
|
||||
)
|
||||
|
||||
ld_client_mock.invoke.assert_called_once_with(
|
||||
FunctionName='bt-ee-1-proxy',
|
||||
InvocationType='RequestResponse',
|
||||
Payload=mocker.ANY,
|
||||
)
|
||||
|
||||
kwargs = ld_client_mock.invoke.mock_calls[0][-1]
|
||||
payload_bytes = kwargs['Payload']
|
||||
payload = json.loads(payload_bytes)
|
||||
|
||||
assert payload['identifier'] == identifier
|
||||
assert payload['headline'] == headline
|
||||
assert payload['description'] == description
|
||||
|
||||
|
||||
def test_cbc_proxy_create_and_send_handles_invoke_error(mocker, cbc_proxy):
|
||||
identifier = 'my-identifier'
|
||||
headline = 'my-headline'
|
||||
description = 'my-description'
|
||||
|
||||
ld_client_mock = mocker.patch.object(
|
||||
cbc_proxy,
|
||||
'_lambda_client',
|
||||
create=True,
|
||||
)
|
||||
|
||||
ld_client_mock.invoke.return_value = {
|
||||
'StatusCode': 400,
|
||||
}
|
||||
|
||||
with pytest.raises(Exception) as e:
|
||||
cbc_proxy.create_and_send_broadcast(
|
||||
identifier=identifier,
|
||||
headline=headline,
|
||||
description=description,
|
||||
)
|
||||
|
||||
assert e.match('Could not invoke lambda')
|
||||
|
||||
ld_client_mock.invoke.assert_called_once_with(
|
||||
FunctionName='bt-ee-1-proxy',
|
||||
InvocationType='RequestResponse',
|
||||
Payload=mocker.ANY,
|
||||
)
|
||||
|
||||
|
||||
def test_cbc_proxy_create_and_send_handles_function_error(mocker, cbc_proxy):
|
||||
identifier = 'my-identifier'
|
||||
headline = 'my-headline'
|
||||
description = 'my-description'
|
||||
|
||||
ld_client_mock = mocker.patch.object(
|
||||
cbc_proxy,
|
||||
'_lambda_client',
|
||||
create=True,
|
||||
)
|
||||
|
||||
ld_client_mock.invoke.return_value = {
|
||||
'StatusCode': 200,
|
||||
'FunctionError': 'something',
|
||||
}
|
||||
|
||||
with pytest.raises(Exception) as e:
|
||||
cbc_proxy.create_and_send_broadcast(
|
||||
identifier=identifier,
|
||||
headline=headline,
|
||||
description=description,
|
||||
)
|
||||
|
||||
assert e.match('Function exited with unhandled exception')
|
||||
|
||||
ld_client_mock.invoke.assert_called_once_with(
|
||||
FunctionName='bt-ee-1-proxy',
|
||||
InvocationType='RequestResponse',
|
||||
Payload=mocker.ANY,
|
||||
)
|
||||
Reference in New Issue
Block a user