Skip to content

Commit

Permalink
Squash #1979 - Use request library
Browse files Browse the repository at this point in the history
  • Loading branch information
MackHalliday committed Oct 17, 2024
1 parent 666da6c commit 8255c18
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ fileignoreconfig:
- filename: tests/app/celery/test_service_callback_tasks.py
checksum: 70575434f7a4fedd43d4c9164bc899a606768526d432c364db372524eec26542
- filename: lambda_functions/va_profile/va_profile_opt_in_out_lambda.py
checksum: b59639f4e85eb3dbc5ebbc868c64a7909ebbb8665dbfc33871938cd1133bd4e8
checksum: 1531ff785bfea4669ff7f85f8582576436d8f3abdcb2a0d193bdc102b43009e4
- filename: tests/app/conftest.py
checksum: a80aa727586db82ed1b50bdb81ddfe1379e649a9dfc1ece2c36047486b41b83d
- filename: tests/app/notifications/test_process_notifications_for_profile_v3.py
checksum: 4e15e63d349635131173ffdd7aebcd547621db08de877ef926d3a41fde72d065
- filename: tests/app/v2/notifications/test_post_notifications.py
checksum: 3181930a13e3679bb2f17eaa3f383512eb9caf4ed5d5e14496ca4193c6083965
- filename: tests/lambda_functions/va_profile/test_va_profile_integration.py
checksum: 8e0d06aa33972250e59a15d6e2e65a19083dc1ba4f1b7d974ca6a8f2a681c188
checksum: d9ddbda44de0bf5e037fb9407f66079d33ab4a857fed616e715bda34fd8f42e6
version: "1.0"
32 changes: 19 additions & 13 deletions lambda_functions/va_profile/va_profile_opt_in_out_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import boto3
import jwt
import psycopg2
import requests
from botocore.exceptions import ClientError, ValidationError
from cryptography.x509 import Certificate, load_pem_x509_certificate

Expand Down Expand Up @@ -491,22 +490,29 @@ def send_comp_and_pen_opt_in_confirmation(va_profile_id: int) -> None:
next_month = (now.month % 12) + 1
month_personalisation = calendar.month_name[next_month]

sms_data = {
'template_id': confirmation_opt_in_template_id,
'recipient_identifier': {'id_type': 'VAPROFILEID', 'id_value': va_profile_id},
'sms_sender_id': comp_and_pen_sms_sender_id,
'personalisation': {'month': month_personalisation},
}
sms_data = json.dumps(
{
'template_id': confirmation_opt_in_template_id,
'recipient_identifier': {'id_type': 'VAPROFILEID', 'id_value': va_profile_id},
'sms_sender_id': comp_and_pen_sms_sender_id,
'personalisation': {'month': month_personalisation},
}
)

try:
logger.debug('Sending opt-in confirmation SMS to vaProfileId %s' % va_profile_id)

requests.post(
f'https://{NOTIFY_ENVIRONMENT}.api.notifications.va.gov/v2/notifications/sms',
json=sms_data,
headers={'Authorization': f'Bearer {comp_and_pen_opt_in_api_key}', 'Content-Type': 'application/json'},
timeout=10,
)
conn = HTTPSConnection(f'{NOTIFY_ENVIRONMENT}.api.notifications.va.gov')

headers = {'Authorization': f'Bearer {comp_and_pen_opt_in_api_key}', 'Content-Type': 'application/json'}

conn.request('POST', '/v2/notifications/sms', body=sms_data, headers=headers)

response = conn.getresponse()
response_data = response.read().decode()

logger.info(f'SMS sent successfully. Response status: {response.status}, Response data: {response_data}')
conn.close()

# TODO - #1979 Add better Exception handling
except Exception as e:
Expand Down
27 changes: 17 additions & 10 deletions tests/lambda_functions/va_profile/test_va_profile_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,12 @@ def test_va_profile_opt_in_out_lambda_handler_comp_and_pen_confirmation(
mocker.patch('lambda_functions.va_profile.va_profile_opt_in_out_lambda.datetime', mock.Mock(wraps=datetime))
mocker.patch('lambda_functions.va_profile.va_profile_opt_in_out_lambda.datetime.now', return_value=mock_date)

post_mock = mocker.patch('requests.post')
mock_https = mocker.patch('http.client.HTTPSConnection', autospec=True)
mock_https_instance = mock_https.return_value
mock_response = mocker.Mock()
mock_response.status = 200
mock_response.read.return_value = b'{"success": true}'
mock_https_instance.getresponse.return_value = mock_response

mock_ssm = mocker.patch('boto3.client')
mock_ssm_instance = mock_ssm.return_value
Expand Down Expand Up @@ -787,15 +792,17 @@ def test_va_profile_opt_in_out_lambda_handler_comp_and_pen_confirmation(
assert response_body['put_body'] == expected_put_body

# Assert POST request to VANotify was made with correct parameters, including the expected month
post_mock.assert_called_once_with(
'https://test.api.notifications.va.gov/v2/notifications/sms',
json={
'template_id': 'mock_template_id',
'recipient_identifier': {'id_type': 'VAPROFILEID', 'id_value': va_profile_id},
'sms_sender_id': 'mock_sms_sender_id',
'personalisation': {'month': expected_month}, # Check for correct month
},
timeout=10,
mock_https_instance.request.assert_called_once_with(
'POST',
'/v2/notifications/sms',
body=json.dumps(
{
'template_id': 'mock_template_id',
'recipient_identifier': {'id_type': 'VAPROFILEID', 'id_value': va_profile_id},
'sms_sender_id': 'mock_sms_sender_id',
'personalisation': {'month': expected_month}, # Check for correct month
}
),
headers={'Authorization': 'Bearer mock_va_notify_api_key', 'Content-Type': 'application/json'},
)

Expand Down

0 comments on commit 8255c18

Please sign in to comment.