mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 18:31:13 -05:00
clients: cbc_proxy sends message_type
When we ask the CBC Proxy to send a message, we should specify that we want to send a real message, when we want a real message We will do this by specifying the message_type which can have 4 types, 3 of which represent a real message: | Name | Effect | | ------ | ------------------------ | | alert | Create an alert | | update | Update an existing alert | | cancel | Cancel an existing alert | | test | Send a link test | We will use message_type to represent the table above Signed-off-by: Toby Lorne <toby.lornewelch-richards@digital.cabinet-office.gov.uk> Co-authored-by: Richard <richard.baker@digital.cabinet-office.gov.uk> Co-authored-by: Pea <pea.tyczynska@digital.cabinet-office.gov.uk>
This commit is contained in:
@@ -32,6 +32,12 @@ class CBCProxyNoopClient:
|
|||||||
):
|
):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def send_link_test(
|
||||||
|
self,
|
||||||
|
identifier,
|
||||||
|
):
|
||||||
|
pass
|
||||||
|
|
||||||
def create_and_send_broadcast(
|
def create_and_send_broadcast(
|
||||||
self,
|
self,
|
||||||
identifier, headline, description, areas
|
identifier, headline, description, areas
|
||||||
@@ -83,11 +89,33 @@ class CBCProxyClient:
|
|||||||
if 'FunctionError' in result:
|
if 'FunctionError' in result:
|
||||||
raise Exception('Function exited with unhandled exception')
|
raise Exception('Function exited with unhandled exception')
|
||||||
|
|
||||||
|
def send_link_test(
|
||||||
|
self,
|
||||||
|
identifier,
|
||||||
|
):
|
||||||
|
payload_bytes = bytes(json.dumps({
|
||||||
|
'message_type': 'test',
|
||||||
|
'identifier': identifier,
|
||||||
|
}), encoding='utf8')
|
||||||
|
|
||||||
|
result = self._lambda_client.invoke(
|
||||||
|
FunctionName='bt-ee-1-proxy',
|
||||||
|
InvocationType='RequestResponse',
|
||||||
|
Payload=payload_bytes,
|
||||||
|
)
|
||||||
|
|
||||||
|
if result['StatusCode'] > 299:
|
||||||
|
raise Exception('Could not invoke lambda')
|
||||||
|
|
||||||
|
if 'FunctionError' in result:
|
||||||
|
raise Exception('Function exited with unhandled exception')
|
||||||
|
|
||||||
def create_and_send_broadcast(
|
def create_and_send_broadcast(
|
||||||
self,
|
self,
|
||||||
identifier, headline, description, areas,
|
identifier, headline, description, areas,
|
||||||
):
|
):
|
||||||
payload_bytes = bytes(json.dumps({
|
payload_bytes = bytes(json.dumps({
|
||||||
|
'message_type': 'alert',
|
||||||
'identifier': identifier,
|
'identifier': identifier,
|
||||||
'headline': headline,
|
'headline': headline,
|
||||||
'description': description,
|
'description': description,
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ def test_cbc_proxy_create_and_send_invokes_function(mocker, cbc_proxy):
|
|||||||
payload = json.loads(payload_bytes)
|
payload = json.loads(payload_bytes)
|
||||||
|
|
||||||
assert payload['identifier'] == identifier
|
assert payload['identifier'] == identifier
|
||||||
|
assert payload['message_type'] == 'alert'
|
||||||
assert payload['headline'] == headline
|
assert payload['headline'] == headline
|
||||||
assert payload['description'] == description
|
assert payload['description'] == description
|
||||||
assert payload['areas'] == areas
|
assert payload['areas'] == areas
|
||||||
@@ -251,3 +252,89 @@ def test_cbc_proxy_send_canary_handles_function_error(mocker, cbc_proxy):
|
|||||||
InvocationType='RequestResponse',
|
InvocationType='RequestResponse',
|
||||||
Payload=mocker.ANY,
|
Payload=mocker.ANY,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_cbc_proxy_send_link_test_invokes_function(mocker, cbc_proxy):
|
||||||
|
identifier = str(uuid.uuid4())
|
||||||
|
|
||||||
|
ld_client_mock = mocker.patch.object(
|
||||||
|
cbc_proxy,
|
||||||
|
'_lambda_client',
|
||||||
|
create=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
ld_client_mock.invoke.return_value = {
|
||||||
|
'StatusCode': 200,
|
||||||
|
}
|
||||||
|
|
||||||
|
cbc_proxy.send_link_test(
|
||||||
|
identifier=identifier,
|
||||||
|
)
|
||||||
|
|
||||||
|
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['message_type'] == 'test'
|
||||||
|
|
||||||
|
|
||||||
|
def test_cbc_proxy_send_link_test_handles_invoke_error(mocker, cbc_proxy):
|
||||||
|
identifier = str(uuid.uuid4())
|
||||||
|
|
||||||
|
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.send_link_test(
|
||||||
|
identifier=identifier,
|
||||||
|
)
|
||||||
|
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_cbc_proxy_send_link_test_handles_function_error(mocker, cbc_proxy):
|
||||||
|
identifier = str(uuid.uuid4())
|
||||||
|
|
||||||
|
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.send_link_test(
|
||||||
|
identifier=identifier,
|
||||||
|
)
|
||||||
|
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user