From 62951fa0396c26f232cec09083db3ff4dff3eb7e Mon Sep 17 00:00:00 2001 From: Toby Lorne Date: Tue, 20 Oct 2020 15:18:24 +0100 Subject: [PATCH] clients: cbc_proxy tests for handling lambda response Signed-off-by: Toby Lorne --- app/clients/cbc_proxy.py | 1 + tests/app/clients/test_cbc_proxy.py | 64 ++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/app/clients/cbc_proxy.py b/app/clients/cbc_proxy.py index 81f3c7f46..05acadeea 100644 --- a/app/clients/cbc_proxy.py +++ b/app/clients/cbc_proxy.py @@ -2,6 +2,7 @@ import json import boto3 + # Noop = no operation class CBCProxyNoopClient: diff --git a/tests/app/clients/test_cbc_proxy.py b/tests/app/clients/test_cbc_proxy.py index 123c1c4ea..0d049e60a 100644 --- a/tests/app/clients/test_cbc_proxy.py +++ b/tests/app/clients/test_cbc_proxy.py @@ -29,7 +29,6 @@ def test_cbc_proxy_ld_client_has_correct_keys(cbc_proxy): 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' @@ -64,3 +63,66 @@ def test_cbc_proxy_create_and_send_invokes_function(mocker, cbc_proxy): 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, + '_ld_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, + '_ld_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, + )