mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
separate cbc proxy into separate clients
this is a pretty big and convoluted refactor unfortunately.
Previously:
There was one global `cbc_proxy_client` object in apps. This class has
the information about how to invoke the bt-ee lambda, and handles all
calls to lambda. This includes calls to the canary too (which is a
separate lambda).
The future:
There's one global `cbc_proxy_client`. This knows about the different
provider functions and lambdas, and you'll need to ask this client for a
proxy for your chosen provider. call cbc_proxy_client.get_proxy('ee')`
and it'll return you a proxy that knows what ee's lambda function is,
how to transform any content in a way that is exclusive to ee, and in
future how to parse any response from ee.
The present:
I also cleaned up some duplicate tests.
I'm really not sure about the names of some of these variables - in
particular `cbc_proxy_client` isn't a client - it's more of a java style
factory, where you call a function on it to get the client of your
choice.
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import json
|
||||
import uuid
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from app.clients.cbc_proxy import CBCProxyClient, CBCProxyException
|
||||
from app.clients.cbc_proxy import CBCProxyClient, CBCProxyException, CBCProxyEE, CBCProxyCanary
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def cbc_proxy(client, mocker):
|
||||
def cbc_proxy_client(client, mocker):
|
||||
client = CBCProxyClient()
|
||||
current_app = mocker.Mock(config={
|
||||
'CBC_PROXY_AWS_ACCESS_KEY_ID': 'cbc-proxy-aws-access-key-id',
|
||||
@@ -17,19 +18,39 @@ def cbc_proxy(client, mocker):
|
||||
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'
|
||||
@pytest.fixture
|
||||
def cbc_proxy_ee(cbc_proxy_client):
|
||||
return cbc_proxy_client.get_proxy('ee')
|
||||
|
||||
|
||||
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
|
||||
@pytest.mark.parametrize('provider_name, expected_provider_class', [
|
||||
('ee', CBCProxyEE),
|
||||
('canary', CBCProxyCanary),
|
||||
])
|
||||
def test_cbc_proxy_client_returns_correct_client(provider_name, expected_provider_class):
|
||||
mock_lambda = Mock()
|
||||
cbc_proxy_client = CBCProxyClient()
|
||||
cbc_proxy_client._lambda_client = mock_lambda
|
||||
|
||||
ret = cbc_proxy_client.get_proxy(provider_name)
|
||||
|
||||
assert type(ret) == expected_provider_class
|
||||
assert ret._lambda_client == mock_lambda
|
||||
|
||||
|
||||
def test_cbc_proxy_lambda_client_has_correct_region(cbc_proxy_ee):
|
||||
assert cbc_proxy_ee._lambda_client._client_config.region_name == 'eu-west-2'
|
||||
|
||||
|
||||
def test_cbc_proxy_lambda_client_has_correct_keys(cbc_proxy_ee):
|
||||
key = cbc_proxy_ee._lambda_client._request_signer._credentials.access_key
|
||||
secret = cbc_proxy_ee._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):
|
||||
def test_cbc_proxy_create_and_send_invokes_function(mocker, cbc_proxy_ee):
|
||||
identifier = 'my-identifier'
|
||||
headline = 'my-headline'
|
||||
description = 'my-description'
|
||||
@@ -50,7 +71,7 @@ def test_cbc_proxy_create_and_send_invokes_function(mocker, cbc_proxy):
|
||||
}]
|
||||
|
||||
ld_client_mock = mocker.patch.object(
|
||||
cbc_proxy,
|
||||
cbc_proxy_ee,
|
||||
'_lambda_client',
|
||||
create=True,
|
||||
)
|
||||
@@ -59,7 +80,7 @@ def test_cbc_proxy_create_and_send_invokes_function(mocker, cbc_proxy):
|
||||
'StatusCode': 200,
|
||||
}
|
||||
|
||||
cbc_proxy.create_and_send_broadcast(
|
||||
cbc_proxy_ee.create_and_send_broadcast(
|
||||
identifier=identifier,
|
||||
headline=headline,
|
||||
description=description,
|
||||
@@ -86,7 +107,7 @@ def test_cbc_proxy_create_and_send_invokes_function(mocker, cbc_proxy):
|
||||
assert payload['expires'] == expires
|
||||
|
||||
|
||||
def test_cbc_proxy_create_and_send_handles_invoke_error(mocker, cbc_proxy):
|
||||
def test_cbc_proxy_create_and_send_handles_invoke_error(mocker, cbc_proxy_ee):
|
||||
identifier = 'my-identifier'
|
||||
headline = 'my-headline'
|
||||
description = 'my-description'
|
||||
@@ -107,7 +128,7 @@ def test_cbc_proxy_create_and_send_handles_invoke_error(mocker, cbc_proxy):
|
||||
}]
|
||||
|
||||
ld_client_mock = mocker.patch.object(
|
||||
cbc_proxy,
|
||||
cbc_proxy_ee,
|
||||
'_lambda_client',
|
||||
create=True,
|
||||
)
|
||||
@@ -117,7 +138,7 @@ def test_cbc_proxy_create_and_send_handles_invoke_error(mocker, cbc_proxy):
|
||||
}
|
||||
|
||||
with pytest.raises(CBCProxyException) as e:
|
||||
cbc_proxy.create_and_send_broadcast(
|
||||
cbc_proxy_ee.create_and_send_broadcast(
|
||||
identifier=identifier,
|
||||
headline=headline,
|
||||
description=description,
|
||||
@@ -134,7 +155,7 @@ def test_cbc_proxy_create_and_send_handles_invoke_error(mocker, cbc_proxy):
|
||||
)
|
||||
|
||||
|
||||
def test_cbc_proxy_create_and_send_handles_function_error(mocker, cbc_proxy):
|
||||
def test_cbc_proxy_create_and_send_handles_function_error(mocker, cbc_proxy_ee):
|
||||
identifier = 'my-identifier'
|
||||
headline = 'my-headline'
|
||||
description = 'my-description'
|
||||
@@ -155,7 +176,7 @@ def test_cbc_proxy_create_and_send_handles_function_error(mocker, cbc_proxy):
|
||||
}]
|
||||
|
||||
ld_client_mock = mocker.patch.object(
|
||||
cbc_proxy,
|
||||
cbc_proxy_ee,
|
||||
'_lambda_client',
|
||||
create=True,
|
||||
)
|
||||
@@ -166,7 +187,7 @@ def test_cbc_proxy_create_and_send_handles_function_error(mocker, cbc_proxy):
|
||||
}
|
||||
|
||||
with pytest.raises(CBCProxyException) as e:
|
||||
cbc_proxy.create_and_send_broadcast(
|
||||
cbc_proxy_ee.create_and_send_broadcast(
|
||||
identifier=identifier,
|
||||
headline=headline,
|
||||
description=description,
|
||||
@@ -183,11 +204,13 @@ def test_cbc_proxy_create_and_send_handles_function_error(mocker, cbc_proxy):
|
||||
)
|
||||
|
||||
|
||||
def test_cbc_proxy_send_canary_invokes_function(mocker, cbc_proxy):
|
||||
def test_cbc_proxy_send_canary_invokes_function(mocker, cbc_proxy_client):
|
||||
identifier = str(uuid.uuid4())
|
||||
|
||||
canary_client = cbc_proxy_client.get_proxy('canary')
|
||||
|
||||
ld_client_mock = mocker.patch.object(
|
||||
cbc_proxy,
|
||||
canary_client,
|
||||
'_lambda_client',
|
||||
create=True,
|
||||
)
|
||||
@@ -196,7 +219,7 @@ def test_cbc_proxy_send_canary_invokes_function(mocker, cbc_proxy):
|
||||
'StatusCode': 200,
|
||||
}
|
||||
|
||||
cbc_proxy.send_canary(
|
||||
canary_client.send_canary(
|
||||
identifier=identifier,
|
||||
)
|
||||
|
||||
@@ -213,66 +236,11 @@ def test_cbc_proxy_send_canary_invokes_function(mocker, cbc_proxy):
|
||||
assert payload['identifier'] == identifier
|
||||
|
||||
|
||||
def test_cbc_proxy_send_canary_handles_invoke_error(mocker, cbc_proxy):
|
||||
def test_cbc_proxy_send_link_test_invokes_function(mocker, cbc_proxy_ee):
|
||||
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(CBCProxyException) as e:
|
||||
cbc_proxy.send_canary(
|
||||
identifier=identifier,
|
||||
)
|
||||
|
||||
assert e.match('Could not invoke lambda')
|
||||
|
||||
ld_client_mock.invoke.assert_called_once_with(
|
||||
FunctionName='canary',
|
||||
InvocationType='RequestResponse',
|
||||
Payload=mocker.ANY,
|
||||
)
|
||||
|
||||
|
||||
def test_cbc_proxy_send_canary_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(CBCProxyException) as e:
|
||||
cbc_proxy.send_canary(
|
||||
identifier=identifier,
|
||||
)
|
||||
|
||||
assert e.match('Function exited with unhandled exception')
|
||||
|
||||
ld_client_mock.invoke.assert_called_once_with(
|
||||
FunctionName='canary',
|
||||
InvocationType='RequestResponse',
|
||||
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,
|
||||
cbc_proxy_ee,
|
||||
'_lambda_client',
|
||||
create=True,
|
||||
)
|
||||
@@ -281,7 +249,7 @@ def test_cbc_proxy_send_link_test_invokes_function(mocker, cbc_proxy):
|
||||
'StatusCode': 200,
|
||||
}
|
||||
|
||||
cbc_proxy.send_link_test(
|
||||
cbc_proxy_ee.send_link_test(
|
||||
identifier=identifier,
|
||||
)
|
||||
|
||||
@@ -297,58 +265,3 @@ def test_cbc_proxy_send_link_test_invokes_function(mocker, cbc_proxy):
|
||||
|
||||
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(CBCProxyException) 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,
|
||||
)
|
||||
|
||||
|
||||
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(CBCProxyException) 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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user