2020-10-20 14:00:53 +01:00
|
|
|
import json
|
2020-10-26 17:14:08 +00:00
|
|
|
import uuid
|
2020-10-20 14:00:53 +01:00
|
|
|
|
2020-10-20 13:33:51 +01:00
|
|
|
import pytest
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
from app.clients.cbc_proxy import CBCProxyClient, CBCProxyException
|
2020-10-20 13:33:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
|
|
|
2020-10-22 12:22:11 +01:00
|
|
|
def test_cbc_proxy_lambda_client_has_correct_region(cbc_proxy):
|
|
|
|
|
assert cbc_proxy._lambda_client._client_config.region_name == 'eu-west-2'
|
2020-10-20 13:33:51 +01:00
|
|
|
|
|
|
|
|
|
2020-10-22 12:22:11 +01:00
|
|
|
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
|
2020-10-20 13:33:51 +01:00
|
|
|
|
|
|
|
|
assert key == 'cbc-proxy-aws-access-key-id'
|
|
|
|
|
assert secret == 'cbc-proxy-aws-secret-access-key'
|
2020-10-20 14:00:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cbc_proxy_create_and_send_invokes_function(mocker, cbc_proxy):
|
|
|
|
|
identifier = 'my-identifier'
|
|
|
|
|
headline = 'my-headline'
|
|
|
|
|
description = 'my-description'
|
|
|
|
|
|
2020-10-28 11:26:38 +00:00
|
|
|
sent = 'a-passed-through-sent-value'
|
|
|
|
|
expires = 'a-passed-through-expires-value'
|
|
|
|
|
|
2020-10-23 16:44:11 +01:00
|
|
|
# a single area which is a square including london
|
|
|
|
|
areas = [{
|
|
|
|
|
'description': 'london',
|
|
|
|
|
'polygon': [
|
|
|
|
|
[51.12, -1.2],
|
|
|
|
|
[51.12, 1.2],
|
|
|
|
|
[51.74, 1.2],
|
|
|
|
|
[51.74, -1.2],
|
|
|
|
|
[51.12, -1.2],
|
|
|
|
|
],
|
|
|
|
|
}]
|
|
|
|
|
|
2020-10-20 14:00:53 +01:00
|
|
|
ld_client_mock = mocker.patch.object(
|
|
|
|
|
cbc_proxy,
|
2020-10-22 12:22:11 +01:00
|
|
|
'_lambda_client',
|
2020-10-20 14:00:53 +01:00
|
|
|
create=True,
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-20 15:18:11 +01:00
|
|
|
ld_client_mock.invoke.return_value = {
|
|
|
|
|
'StatusCode': 200,
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 14:00:53 +01:00
|
|
|
cbc_proxy.create_and_send_broadcast(
|
|
|
|
|
identifier=identifier,
|
|
|
|
|
headline=headline,
|
|
|
|
|
description=description,
|
2020-10-23 16:44:11 +01:00
|
|
|
areas=areas,
|
2020-10-28 11:26:38 +00:00
|
|
|
sent=sent, expires=expires,
|
2020-10-20 14:00:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
2020-10-27 14:44:04 +00:00
|
|
|
assert payload['message_type'] == 'alert'
|
2020-10-20 14:00:53 +01:00
|
|
|
assert payload['headline'] == headline
|
|
|
|
|
assert payload['description'] == description
|
2020-10-23 16:44:11 +01:00
|
|
|
assert payload['areas'] == areas
|
2020-10-28 11:26:38 +00:00
|
|
|
assert payload['sent'] == sent
|
|
|
|
|
assert payload['expires'] == expires
|
2020-10-20 15:18:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cbc_proxy_create_and_send_handles_invoke_error(mocker, cbc_proxy):
|
|
|
|
|
identifier = 'my-identifier'
|
|
|
|
|
headline = 'my-headline'
|
|
|
|
|
description = 'my-description'
|
|
|
|
|
|
2020-10-28 11:26:38 +00:00
|
|
|
sent = 'a-passed-through-sent-value'
|
|
|
|
|
expires = 'a-passed-through-expires-value'
|
|
|
|
|
|
2020-10-23 16:44:11 +01:00
|
|
|
# a single area which is a square including london
|
|
|
|
|
areas = [{
|
|
|
|
|
'description': 'london',
|
|
|
|
|
'polygon': [
|
|
|
|
|
[51.12, -1.2],
|
|
|
|
|
[51.12, 1.2],
|
|
|
|
|
[51.74, 1.2],
|
|
|
|
|
[51.74, -1.2],
|
|
|
|
|
[51.12, -1.2],
|
|
|
|
|
],
|
|
|
|
|
}]
|
|
|
|
|
|
2020-10-20 15:18:24 +01:00
|
|
|
ld_client_mock = mocker.patch.object(
|
|
|
|
|
cbc_proxy,
|
2020-10-22 12:22:11 +01:00
|
|
|
'_lambda_client',
|
2020-10-20 15:18:24 +01:00
|
|
|
create=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
ld_client_mock.invoke.return_value = {
|
|
|
|
|
'StatusCode': 400,
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
with pytest.raises(CBCProxyException) as e:
|
2020-10-20 15:18:24 +01:00
|
|
|
cbc_proxy.create_and_send_broadcast(
|
|
|
|
|
identifier=identifier,
|
|
|
|
|
headline=headline,
|
|
|
|
|
description=description,
|
2020-10-23 16:44:11 +01:00
|
|
|
areas=areas,
|
2020-10-28 11:26:38 +00:00
|
|
|
sent=sent, expires=expires,
|
2020-10-20 15:18:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
|
2020-10-28 11:26:38 +00:00
|
|
|
sent = 'a-passed-through-sent-value'
|
|
|
|
|
expires = 'a-passed-through-expires-value'
|
|
|
|
|
|
2020-10-23 16:44:11 +01:00
|
|
|
# a single area which is a square including london
|
|
|
|
|
areas = [{
|
|
|
|
|
'description': 'london',
|
|
|
|
|
'polygon': [
|
|
|
|
|
[51.12, -1.2],
|
|
|
|
|
[51.12, 1.2],
|
|
|
|
|
[51.74, 1.2],
|
|
|
|
|
[51.74, -1.2],
|
|
|
|
|
[51.12, -1.2],
|
|
|
|
|
],
|
|
|
|
|
}]
|
|
|
|
|
|
2020-10-20 15:18:24 +01:00
|
|
|
ld_client_mock = mocker.patch.object(
|
|
|
|
|
cbc_proxy,
|
2020-10-22 12:22:11 +01:00
|
|
|
'_lambda_client',
|
2020-10-20 15:18:24 +01:00
|
|
|
create=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
ld_client_mock.invoke.return_value = {
|
|
|
|
|
'StatusCode': 200,
|
|
|
|
|
'FunctionError': 'something',
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
with pytest.raises(CBCProxyException) as e:
|
2020-10-20 15:18:24 +01:00
|
|
|
cbc_proxy.create_and_send_broadcast(
|
|
|
|
|
identifier=identifier,
|
|
|
|
|
headline=headline,
|
|
|
|
|
description=description,
|
2020-10-23 16:44:11 +01:00
|
|
|
areas=areas,
|
2020-10-28 11:26:38 +00:00
|
|
|
sent=sent, expires=expires,
|
2020-10-20 15:18:24 +01:00
|
|
|
)
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
assert e.match('Function exited with unhandled exception')
|
2020-10-20 15:18:24 +01:00
|
|
|
|
|
|
|
|
ld_client_mock.invoke.assert_called_once_with(
|
|
|
|
|
FunctionName='bt-ee-1-proxy',
|
|
|
|
|
InvocationType='RequestResponse',
|
|
|
|
|
Payload=mocker.ANY,
|
|
|
|
|
)
|
2020-10-26 17:14:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cbc_proxy_send_canary_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_canary(
|
|
|
|
|
identifier=identifier,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
ld_client_mock.invoke.assert_called_once_with(
|
|
|
|
|
FunctionName='canary',
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cbc_proxy_send_canary_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,
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
with pytest.raises(CBCProxyException) as e:
|
2020-10-26 17:14:08 +00:00
|
|
|
cbc_proxy.send_canary(
|
|
|
|
|
identifier=identifier,
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
assert e.match('Could not invoke lambda')
|
2020-10-26 17:14:08 +00:00
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
with pytest.raises(CBCProxyException) as e:
|
2020-10-26 17:14:08 +00:00
|
|
|
cbc_proxy.send_canary(
|
|
|
|
|
identifier=identifier,
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
assert e.match('Function exited with unhandled exception')
|
2020-10-26 17:14:08 +00:00
|
|
|
|
|
|
|
|
ld_client_mock.invoke.assert_called_once_with(
|
|
|
|
|
FunctionName='canary',
|
|
|
|
|
InvocationType='RequestResponse',
|
|
|
|
|
Payload=mocker.ANY,
|
|
|
|
|
)
|
2020-10-27 14:44:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
with pytest.raises(CBCProxyException) as e:
|
2020-10-27 14:44:04 +00:00
|
|
|
cbc_proxy.send_link_test(
|
|
|
|
|
identifier=identifier,
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
assert e.match('Could not invoke lambda')
|
2020-10-27 14:44:04 +00:00
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
with pytest.raises(CBCProxyException) as e:
|
2020-10-27 14:44:04 +00:00
|
|
|
cbc_proxy.send_link_test(
|
|
|
|
|
identifier=identifier,
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
assert e.match('Function exited with unhandled exception')
|
2020-10-27 14:44:04 +00:00
|
|
|
|
|
|
|
|
ld_client_mock.invoke.assert_called_once_with(
|
|
|
|
|
FunctionName='bt-ee-1-proxy',
|
|
|
|
|
InvocationType='RequestResponse',
|
|
|
|
|
Payload=mocker.ANY,
|
|
|
|
|
)
|