mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
Merge pull request #3293 from alphagov/site-b-link-test-178738544
Trigger separate link test for failover lambda
This commit is contained in:
@@ -1,10 +1,8 @@
|
|||||||
import uuid
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from sqlalchemy.schema import Sequence
|
|
||||||
|
|
||||||
from app import cbc_proxy_client, db, notify_celery, zendesk_client
|
from app import cbc_proxy_client, notify_celery, zendesk_client
|
||||||
from app.clients.cbc_proxy import CBCProxyRetryableException
|
from app.clients.cbc_proxy import CBCProxyRetryableException
|
||||||
from app.config import QueueNames
|
from app.config import QueueNames
|
||||||
from app.dao.broadcast_message_dao import (
|
from app.dao.broadcast_message_dao import (
|
||||||
@@ -251,12 +249,4 @@ def send_broadcast_provider_message(self, broadcast_event_id, provider):
|
|||||||
|
|
||||||
@notify_celery.task(name='trigger-link-test')
|
@notify_celery.task(name='trigger-link-test')
|
||||||
def trigger_link_test(provider):
|
def trigger_link_test(provider):
|
||||||
identifier = str(uuid.uuid4())
|
cbc_proxy_client.get_proxy(provider).send_link_test()
|
||||||
formatted_seq_number = None
|
|
||||||
if provider == BroadcastProvider.VODAFONE:
|
|
||||||
sequence = Sequence('broadcast_provider_message_number_seq')
|
|
||||||
sequential_number = db.session.connection().execute(sequence)
|
|
||||||
formatted_seq_number = format_sequential_number(sequential_number)
|
|
||||||
message = f"Sending a link test to CBC proxy for provider {provider}. Identifier in payload is {identifier}"
|
|
||||||
current_app.logger.info(message)
|
|
||||||
cbc_proxy_client.get_proxy(provider).send_link_test(identifier, formatted_seq_number)
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import json
|
import json
|
||||||
|
import uuid
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
import boto3
|
import boto3
|
||||||
import botocore
|
import botocore
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from notifications_utils.template import non_gsm_characters
|
from notifications_utils.template import non_gsm_characters
|
||||||
|
from sqlalchemy.schema import Sequence
|
||||||
|
|
||||||
from app.config import BroadcastProvider
|
from app.config import BroadcastProvider
|
||||||
from app.utils import DATETIME_FORMAT, format_sequential_number
|
from app.utils import DATETIME_FORMAT, format_sequential_number
|
||||||
@@ -76,12 +78,14 @@ class CBCProxyClientBase(ABC):
|
|||||||
def __init__(self, lambda_client):
|
def __init__(self, lambda_client):
|
||||||
self._lambda_client = lambda_client
|
self._lambda_client = lambda_client
|
||||||
|
|
||||||
def send_link_test(
|
def send_link_test(self):
|
||||||
|
self._send_link_test(self.lambda_name)
|
||||||
|
self._send_link_test(self.failover_lambda_name)
|
||||||
|
|
||||||
|
def _send_link_test(
|
||||||
self,
|
self,
|
||||||
identifier,
|
lambda_name,
|
||||||
sequential_number
|
): pass
|
||||||
):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def create_and_send_broadcast(
|
def create_and_send_broadcast(
|
||||||
self, identifier, headline, description, areas, sent, expires, channel, message_number=None
|
self, identifier, headline, description, areas, sent, expires, channel, message_number=None
|
||||||
@@ -118,6 +122,10 @@ class CBCProxyClientBase(ABC):
|
|||||||
def _invoke_lambda(self, lambda_name, payload):
|
def _invoke_lambda(self, lambda_name, payload):
|
||||||
payload_bytes = bytes(json.dumps(payload), encoding='utf8')
|
payload_bytes = bytes(json.dumps(payload), encoding='utf8')
|
||||||
try:
|
try:
|
||||||
|
current_app.logger.info(
|
||||||
|
f"Calling lambda {lambda_name} with payload {str(payload)[:1000]}"
|
||||||
|
)
|
||||||
|
|
||||||
result = self._lambda_client.invoke(
|
result = self._lambda_client.invoke(
|
||||||
FunctionName=lambda_name,
|
FunctionName=lambda_name,
|
||||||
InvocationType='RequestResponse',
|
InvocationType='RequestResponse',
|
||||||
@@ -155,10 +163,9 @@ class CBCProxyOne2ManyClient(CBCProxyClientBase):
|
|||||||
LANGUAGE_ENGLISH = 'en-GB'
|
LANGUAGE_ENGLISH = 'en-GB'
|
||||||
LANGUAGE_WELSH = 'cy-GB'
|
LANGUAGE_WELSH = 'cy-GB'
|
||||||
|
|
||||||
def send_link_test(
|
def _send_link_test(
|
||||||
self,
|
self,
|
||||||
identifier,
|
lambda_name,
|
||||||
sequential_number=None,
|
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
link test - open up a connection to a specific provider, and send them an xml payload with a <msgType> of
|
link test - open up a connection to a specific provider, and send them an xml payload with a <msgType> of
|
||||||
@@ -166,11 +173,11 @@ class CBCProxyOne2ManyClient(CBCProxyClientBase):
|
|||||||
"""
|
"""
|
||||||
payload = {
|
payload = {
|
||||||
'message_type': 'test',
|
'message_type': 'test',
|
||||||
'identifier': identifier,
|
'identifier': str(uuid.uuid4()),
|
||||||
'message_format': 'cap'
|
'message_format': 'cap'
|
||||||
}
|
}
|
||||||
|
|
||||||
self._invoke_lambda_with_failover(payload=payload)
|
self._invoke_lambda(lambda_name=lambda_name, payload=payload)
|
||||||
|
|
||||||
def create_and_send_broadcast(
|
def create_and_send_broadcast(
|
||||||
self, identifier, headline, description, areas, sent, expires, channel, message_number=None
|
self, identifier, headline, description, areas, sent, expires, channel, message_number=None
|
||||||
@@ -231,23 +238,27 @@ class CBCProxyVodafone(CBCProxyClientBase):
|
|||||||
LANGUAGE_ENGLISH = 'English'
|
LANGUAGE_ENGLISH = 'English'
|
||||||
LANGUAGE_WELSH = 'Welsh'
|
LANGUAGE_WELSH = 'Welsh'
|
||||||
|
|
||||||
def send_link_test(
|
def _send_link_test(
|
||||||
self,
|
self,
|
||||||
identifier,
|
lambda_name,
|
||||||
sequential_number,
|
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
link test - open up a connection to a specific provider, and send them an xml payload with a <msgType> of
|
link test - open up a connection to a specific provider, and send them an xml payload with a <msgType> of
|
||||||
test.
|
test.
|
||||||
"""
|
"""
|
||||||
|
from app import db
|
||||||
|
sequence = Sequence('broadcast_provider_message_number_seq')
|
||||||
|
sequential_number = db.session.connection().execute(sequence)
|
||||||
|
formatted_seq_number = format_sequential_number(sequential_number)
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
'message_type': 'test',
|
'message_type': 'test',
|
||||||
'identifier': identifier,
|
'identifier': str(uuid.uuid4()),
|
||||||
'message_number': sequential_number,
|
'message_number': formatted_seq_number,
|
||||||
'message_format': 'ibag'
|
'message_format': 'ibag'
|
||||||
}
|
}
|
||||||
|
|
||||||
self._invoke_lambda_with_failover(payload=payload)
|
self._invoke_lambda(lambda_name=lambda_name, payload=payload)
|
||||||
|
|
||||||
def create_and_send_broadcast(
|
def create_and_send_broadcast(
|
||||||
self, identifier, message_number, headline, description, areas, sent, expires, channel
|
self, identifier, message_number, headline, description, areas, sent, expires, channel
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import uuid
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from unittest.mock import ANY, Mock, call
|
from unittest.mock import ANY, Mock, call
|
||||||
|
|
||||||
@@ -561,29 +560,14 @@ def test_send_broadcast_provider_message_delays_retry_exponentially(
|
|||||||
['vodafone', 'Vodafone'],
|
['vodafone', 'Vodafone'],
|
||||||
])
|
])
|
||||||
def test_trigger_link_tests_invokes_cbc_proxy_client(
|
def test_trigger_link_tests_invokes_cbc_proxy_client(
|
||||||
mocker, provider, provider_capitalised
|
mocker, provider, provider_capitalised, client,
|
||||||
):
|
):
|
||||||
mock_send_link_test = mocker.patch(
|
mock_send_link_test = mocker.patch(
|
||||||
f'app.clients.cbc_proxy.CBCProxy{provider_capitalised}.send_link_test',
|
f'app.clients.cbc_proxy.CBCProxy{provider_capitalised}.send_link_test',
|
||||||
)
|
)
|
||||||
|
|
||||||
trigger_link_test(provider)
|
trigger_link_test(provider)
|
||||||
|
assert mock_send_link_test.called_once()
|
||||||
assert mock_send_link_test.called
|
|
||||||
# the 0th argument of the call to send_link_test
|
|
||||||
identifier = mock_send_link_test.mock_calls[0][1][0]
|
|
||||||
|
|
||||||
try:
|
|
||||||
uuid.UUID(identifier)
|
|
||||||
except BaseException:
|
|
||||||
pytest.fail(f"{identifier} is not a valid uuid")
|
|
||||||
|
|
||||||
# testing sequential number:
|
|
||||||
if provider == 'vodafone':
|
|
||||||
assert type(mock_send_link_test.mock_calls[0][1][1]) is str
|
|
||||||
assert len(mock_send_link_test.mock_calls[0][1][1]) == 8
|
|
||||||
else:
|
|
||||||
assert not mock_send_link_test.mock_calls[0][1][1]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('retry_count, expected_delay', [
|
@pytest.mark.parametrize('retry_count, expected_delay', [
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from unittest.mock import Mock, call
|
|||||||
import pytest
|
import pytest
|
||||||
from botocore.exceptions import ClientError as BotoClientError
|
from botocore.exceptions import ClientError as BotoClientError
|
||||||
|
|
||||||
|
from app import db
|
||||||
from app.clients.cbc_proxy import (
|
from app.clients.cbc_proxy import (
|
||||||
CBCProxyClient,
|
CBCProxyClient,
|
||||||
CBCProxyEE,
|
CBCProxyEE,
|
||||||
@@ -81,6 +82,14 @@ def test_cbc_proxy_lambda_client_has_correct_keys(cbc_proxy_ee):
|
|||||||
assert secret == 'cbc-proxy-aws-secret-access-key'
|
assert secret == 'cbc-proxy-aws-secret-access-key'
|
||||||
|
|
||||||
|
|
||||||
|
def test_cbc_proxy_send_link_test(mocker, cbc_proxy_ee):
|
||||||
|
mock_send_link_test = mocker.patch.object(cbc_proxy_ee, '_send_link_test')
|
||||||
|
cbc_proxy_ee.send_link_test()
|
||||||
|
|
||||||
|
mock_send_link_test.assert_any_call(cbc_proxy_ee.lambda_name)
|
||||||
|
mock_send_link_test.assert_any_call(cbc_proxy_ee.failover_lambda_name)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('description, expected_language', (
|
@pytest.mark.parametrize('description, expected_language', (
|
||||||
('my-description', 'en-GB'),
|
('my-description', 'en-GB'),
|
||||||
('mŷ-description', 'cy-GB'),
|
('mŷ-description', 'cy-GB'),
|
||||||
@@ -559,7 +568,7 @@ def test_cbc_proxy_create_and_send_tries_failover_lambda_on_function_error_and_r
|
|||||||
def test_cbc_proxy_one_2_many_send_link_test_invokes_function(mocker, cbc_proxy_client, cbc):
|
def test_cbc_proxy_one_2_many_send_link_test_invokes_function(mocker, cbc_proxy_client, cbc):
|
||||||
cbc_proxy = cbc_proxy_client.get_proxy(cbc)
|
cbc_proxy = cbc_proxy_client.get_proxy(cbc)
|
||||||
|
|
||||||
identifier = str(uuid.uuid4())
|
mocker.patch('app.clients.cbc_proxy.uuid.uuid4', return_value=123)
|
||||||
|
|
||||||
ld_client_mock = mocker.patch.object(
|
ld_client_mock = mocker.patch.object(
|
||||||
cbc_proxy,
|
cbc_proxy,
|
||||||
@@ -571,9 +580,8 @@ def test_cbc_proxy_one_2_many_send_link_test_invokes_function(mocker, cbc_proxy_
|
|||||||
'StatusCode': 200,
|
'StatusCode': 200,
|
||||||
}
|
}
|
||||||
|
|
||||||
cbc_proxy.send_link_test(
|
cbc_proxy._send_link_test(
|
||||||
identifier=identifier,
|
lambda_name=f'{cbc}-1-proxy'
|
||||||
sequential_number='0000007b',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
ld_client_mock.invoke.assert_called_once_with(
|
ld_client_mock.invoke.assert_called_once_with(
|
||||||
@@ -586,14 +594,18 @@ def test_cbc_proxy_one_2_many_send_link_test_invokes_function(mocker, cbc_proxy_
|
|||||||
payload_bytes = kwargs['Payload']
|
payload_bytes = kwargs['Payload']
|
||||||
payload = json.loads(payload_bytes)
|
payload = json.loads(payload_bytes)
|
||||||
|
|
||||||
assert payload['identifier'] == identifier
|
assert payload['identifier'] == '123'
|
||||||
assert payload['message_type'] == 'test'
|
assert payload['message_type'] == 'test'
|
||||||
assert 'message_number' not in payload
|
assert 'message_number' not in payload
|
||||||
assert payload['message_format'] == 'cap'
|
assert payload['message_format'] == 'cap'
|
||||||
|
|
||||||
|
|
||||||
def test_cbc_proxy_vodafone_send_link_test_invokes_function(mocker, cbc_proxy_vodafone):
|
def test_cbc_proxy_vodafone_send_link_test_invokes_function(mocker, cbc_proxy_vodafone):
|
||||||
identifier = str(uuid.uuid4())
|
mocker.patch('app.clients.cbc_proxy.uuid.uuid4', return_value=123)
|
||||||
|
|
||||||
|
db.session.connection().execute(
|
||||||
|
'ALTER SEQUENCE broadcast_provider_message_number_seq RESTART WITH 1'
|
||||||
|
)
|
||||||
|
|
||||||
ld_client_mock = mocker.patch.object(
|
ld_client_mock = mocker.patch.object(
|
||||||
cbc_proxy_vodafone,
|
cbc_proxy_vodafone,
|
||||||
@@ -605,9 +617,8 @@ def test_cbc_proxy_vodafone_send_link_test_invokes_function(mocker, cbc_proxy_vo
|
|||||||
'StatusCode': 200,
|
'StatusCode': 200,
|
||||||
}
|
}
|
||||||
|
|
||||||
cbc_proxy_vodafone.send_link_test(
|
cbc_proxy_vodafone._send_link_test(
|
||||||
identifier=identifier,
|
lambda_name='vodafone-1-proxy'
|
||||||
sequential_number='0000007b',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
ld_client_mock.invoke.assert_called_once_with(
|
ld_client_mock.invoke.assert_called_once_with(
|
||||||
@@ -620,7 +631,7 @@ def test_cbc_proxy_vodafone_send_link_test_invokes_function(mocker, cbc_proxy_vo
|
|||||||
payload_bytes = kwargs['Payload']
|
payload_bytes = kwargs['Payload']
|
||||||
payload = json.loads(payload_bytes)
|
payload = json.loads(payload_bytes)
|
||||||
|
|
||||||
assert payload['identifier'] == identifier
|
assert payload['identifier'] == '123'
|
||||||
assert payload['message_type'] == 'test'
|
assert payload['message_type'] == 'test'
|
||||||
assert payload['message_number'] == '0000007b'
|
assert payload['message_number'] == '00000001'
|
||||||
assert payload['message_format'] == 'ibag'
|
assert payload['message_format'] == 'ibag'
|
||||||
|
|||||||
Reference in New Issue
Block a user