Skip to content

Commit

Permalink
Squash #1979 - Remove exception test for now - update intergration te…
Browse files Browse the repository at this point in the history
…st to test for saved notification_id value
  • Loading branch information
MackHalliday committed Oct 22, 2024
1 parent b5d4bcf commit ca8fa9c
Showing 1 changed file with 4 additions and 85 deletions.
89 changes: 4 additions & 85 deletions tests/lambda_functions/va_profile/test_va_profile_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from unittest.mock import Mock, patch

import jwt
from botocore.exceptions import ClientError
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.x509 import Certificate, load_pem_x509_certificate
Expand All @@ -25,7 +24,6 @@
from lambda_functions.va_profile.va_profile_opt_in_out_lambda import (
generate_jwt,
jwt_is_valid,
send_comp_and_pen_opt_in_confirmation,
va_profile_opt_in_out_lambda_handler,
)

Expand Down Expand Up @@ -667,86 +665,6 @@ def test_va_profile_opt_in_out_lambda_handler_audit_id_mismatch(jwt_encoded, put
put_mock.assert_called_once_with('txAuditId', expected_put_body)


@patch(f'{LAMBDA_MODULE}.boto3.client')
@patch(f'{LAMBDA_MODULE}.os.getenv')
@patch(f'{LAMBDA_MODULE}.HTTPSConnection')
@patch(f'{LAMBDA_MODULE}.generate_jwt')
@patch(f'{LAMBDA_MODULE}.logger')
def test_send_comp_and_pen_opt_in_confirmation_boto3_exception(
mock_logger, mock_generate_jwt, mock_https, mock_getenv, mock_boto3_client
):
# Mock boto3 to raise a ClientError
client_error = ClientError({}, 'DescribeParameters')
mock_boto3_client.get_parameter.side_effect = client_error

va_profile_id = 12345
send_comp_and_pen_opt_in_confirmation(va_profile_id)

# Check the logger call with the string representation of the ClientError
mock_logger.critical.assert_called_once_with('Error fetching SSM parameters: %s', str(client_error))


@patch(f'{LAMBDA_MODULE}.boto3.client')
@patch(f'{LAMBDA_MODULE}.os.getenv')
@patch(f'{LAMBDA_MODULE}.HTTPSConnection')
@patch(f'{LAMBDA_MODULE}.generate_jwt')
@patch(f'{LAMBDA_MODULE}.logger')
def test_send_comp_and_pen_opt_in_confirmation_missing_env_vars(
mock_logger, mock_generate_jwt, mock_https, mock_getenv, mock_boto3_client
):
# Mock environment variables to be None
mock_getenv.side_effect = [None, 'service_id', 'template_id']

va_profile_id = 12345
send_comp_and_pen_opt_in_confirmation(va_profile_id)

mock_logger.critical.assert_called_once_with(
'Missing one or more required environment variables in va_profile_opt_in_lambda'
)


@patch(f'{LAMBDA_MODULE}.boto3.client')
@patch(f'{LAMBDA_MODULE}.os.getenv')
@patch(f'{LAMBDA_MODULE}.HTTPSConnection')
@patch(f'{LAMBDA_MODULE}.generate_jwt')
@patch(f'{LAMBDA_MODULE}.logger')
def test_send_comp_and_pen_opt_in_confirmation_value_error(
mock_logger, mock_generate_jwt, mock_https, mock_getenv, mock_boto3_client
):
mock_getenv.side_effect = ['sender_id', 'service_id', 'template_id']
mock_boto3_client.return_value.get_parameter.side_effect = ValueError('Some value error')

va_profile_id = 12345
send_comp_and_pen_opt_in_confirmation(va_profile_id)

mock_logger.exception.assert_called_once_with(
'Configuration error while attempting to send Comp and Pen opt-in confirmation SMS notification: %s',
{'Some value error'},
)


@patch(f'{LAMBDA_MODULE}.boto3.client')
@patch(f'{LAMBDA_MODULE}.os.getenv')
@patch(f'{LAMBDA_MODULE}.HTTPSConnection')
@patch(f'{LAMBDA_MODULE}.generate_jwt')
@patch(f'{LAMBDA_MODULE}.logger')
def test_send_comp_and_pen_opt_in_confirmation_general_exception(
mock_logger, mock_generate_jwt, mock_https, mock_getenv, mock_boto3_client
):
# Mock a general exception during the HTTPS request
mock_getenv.side_effect = ['sender_id', 'service_id', 'template_id']
mock_https.side_effect = Exception('General error')

va_profile_id = 12345
send_comp_and_pen_opt_in_confirmation(va_profile_id)

mock_logger.exception.assert_called_once_with(
'An error occurred while attempting to send Comp and Pen opt-in confirmation SMS notification to vaProfileId %s: %s',
va_profile_id,
'General error',
)


@pytest.mark.serial
@pytest.mark.parametrize(
'mock_date,expected_month',
Expand Down Expand Up @@ -778,14 +696,14 @@ def test_va_profile_opt_in_out_lambda_handler(
mock_datetime.now.return_value = mock_date
mock_datetime.side_effect = lambda *args, **kwargs: datetime(*args, **kwargs)

# Mock SSL context and HTTPS request handling
# Mock POST request HTTPS request handling
mock_https_instance = Mock()
patch(
'lambda_functions.va_profile.va_profile_opt_in_out_lambda.HTTPSConnection', return_value=mock_https_instance
).start()
mock_response = Mock()
mock_response.status = 200
mock_response.read.return_value = b'{"success": true}'
mock_response.status = 201
mock_response.read.return_value = b'{"id":"e7b8cdda-858e-4b6f-a7df-93a71a2edb1e"}'
mock_https_instance.getresponse.return_value = mock_response

mock_ssm = patch('boto3.client').start()
Expand Down Expand Up @@ -865,6 +783,7 @@ def test_va_profile_opt_in_out_lambda_handler(
VAProfileLocalCache.va_profile_id == va_profile_id,
VAProfileLocalCache.communication_item_id == 5,
VAProfileLocalCache.communication_channel_id == 1,
VAProfileLocalCache.notification_id == 'e7b8cdda-858e-4b6f-a7df-93a71a2edb1e',
)
assert notify_db_session.session.execute(stmt).rowcount == 1
notify_db_session.session.commit()
Expand Down

0 comments on commit ca8fa9c

Please sign in to comment.