diff --git a/app/clients/cbc_proxy.py b/app/clients/cbc_proxy.py index 919c4a23c..3a82f640e 100644 --- a/app/clients/cbc_proxy.py +++ b/app/clients/cbc_proxy.py @@ -20,6 +20,10 @@ import boto3 # the preceeding Alert message in the references field +class CBCProxyException(Exception): + pass + + # Noop = no operation class CBCProxyNoopClient: @@ -72,72 +76,53 @@ class CBCProxyClient: aws_secret_access_key=app.config['CBC_PROXY_AWS_SECRET_ACCESS_KEY'], ) - def send_canary( - self, - identifier, - ): - payload_bytes = bytes(json.dumps({ - 'identifier': identifier, - }), encoding='utf8') + def _invoke_lambda(self, function_name, payload): + payload_bytes = bytes(json.dumps(payload), encoding='utf8') result = self._lambda_client.invoke( - FunctionName='canary', + FunctionName=function_name, InvocationType='RequestResponse', Payload=payload_bytes, ) if result['StatusCode'] > 299: - raise Exception('Could not invoke lambda') + raise CBCProxyException('Could not invoke lambda') if 'FunctionError' in result: - raise Exception('Function exited with unhandled exception') + raise CBCProxyException('Function exited with unhandled exception') + + return result + + def send_canary( + self, + identifier, + ): + self._invoke_lambda(function_name='canary', payload={'identifier': identifier}) def send_link_test( self, identifier, ): - payload_bytes = bytes(json.dumps({ - 'message_type': 'test', - 'identifier': identifier, - }), encoding='utf8') + payload = {'message_type': 'test', 'identifier': identifier} - 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') + self._invoke_lambda(function_name='bt-ee-1-proxy', payload=payload) def create_and_send_broadcast( self, identifier, headline, description, areas, sent, expires, ): - payload_bytes = bytes(json.dumps({ + payload = { 'message_type': 'alert', 'identifier': identifier, 'headline': headline, 'description': description, 'areas': areas, - 'sent': sent, 'expires': expires, - }), encoding='utf8') + 'sent': sent, + 'expires': expires, + } - 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') + self._invoke_lambda(function_name='bt-ee-1-proxy', payload=payload) # We have not implementated updating a broadcast def update_and_send_broadcast( diff --git a/tests/app/clients/test_cbc_proxy.py b/tests/app/clients/test_cbc_proxy.py index 3df22bdd4..d888d44b6 100644 --- a/tests/app/clients/test_cbc_proxy.py +++ b/tests/app/clients/test_cbc_proxy.py @@ -3,7 +3,7 @@ import uuid import pytest -from app.clients.cbc_proxy import CBCProxyClient +from app.clients.cbc_proxy import CBCProxyClient, CBCProxyException @pytest.fixture(scope='function') @@ -116,7 +116,7 @@ def test_cbc_proxy_create_and_send_handles_invoke_error(mocker, cbc_proxy): 'StatusCode': 400, } - with pytest.raises(Exception) as e: + with pytest.raises(CBCProxyException) as e: cbc_proxy.create_and_send_broadcast( identifier=identifier, headline=headline, @@ -165,7 +165,7 @@ def test_cbc_proxy_create_and_send_handles_function_error(mocker, cbc_proxy): 'FunctionError': 'something', } - with pytest.raises(Exception) as e: + with pytest.raises(CBCProxyException) as e: cbc_proxy.create_and_send_broadcast( identifier=identifier, headline=headline, @@ -174,7 +174,7 @@ def test_cbc_proxy_create_and_send_handles_function_error(mocker, cbc_proxy): sent=sent, expires=expires, ) - assert e.match('Function exited with unhandled exception') + assert e.match('Function exited with unhandled exception') ld_client_mock.invoke.assert_called_once_with( FunctionName='bt-ee-1-proxy', @@ -226,12 +226,12 @@ def test_cbc_proxy_send_canary_handles_invoke_error(mocker, cbc_proxy): 'StatusCode': 400, } - with pytest.raises(Exception) as e: + with pytest.raises(CBCProxyException) as e: cbc_proxy.send_canary( identifier=identifier, ) - assert e.match('Function exited with unhandled exception') + assert e.match('Could not invoke lambda') ld_client_mock.invoke.assert_called_once_with( FunctionName='canary', @@ -254,12 +254,12 @@ def test_cbc_proxy_send_canary_handles_function_error(mocker, cbc_proxy): 'FunctionError': 'something', } - with pytest.raises(Exception) as e: + with pytest.raises(CBCProxyException) as e: cbc_proxy.send_canary( identifier=identifier, ) - assert e.match('Could not invoke lambda') + assert e.match('Function exited with unhandled exception') ld_client_mock.invoke.assert_called_once_with( FunctionName='canary', @@ -312,12 +312,12 @@ def test_cbc_proxy_send_link_test_handles_invoke_error(mocker, cbc_proxy): 'StatusCode': 400, } - with pytest.raises(Exception) as e: + with pytest.raises(CBCProxyException) as e: cbc_proxy.send_link_test( identifier=identifier, ) - assert e.match('Function exited with unhandled exception') + assert e.match('Could not invoke lambda') ld_client_mock.invoke.assert_called_once_with( FunctionName='bt-ee-1-proxy', @@ -340,12 +340,12 @@ def test_cbc_proxy_send_link_test_handles_function_error(mocker, cbc_proxy): 'FunctionError': 'something', } - with pytest.raises(Exception) as e: + with pytest.raises(CBCProxyException) as e: cbc_proxy.send_link_test( identifier=identifier, ) - assert e.match('Could not invoke lambda') + assert e.match('Function exited with unhandled exception') ld_client_mock.invoke.assert_called_once_with( FunctionName='bt-ee-1-proxy',