generated from cfpb/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 fix confirmation email for beta language (#51)
* Updated to swap between BETA and PROD headers and email text * Updates based on natalia's comments * Updates for date formatting * Updated for UTC in pytest
- Loading branch information
Showing
3 changed files
with
132 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,22 @@ | |
|
||
from regtech_mail_api.settings import EmailApiSettings | ||
from regtech_mail_api.models import Email | ||
from regtech_mail_api.mailer import create_mailer, get_header | ||
from regtech_mail_api.mailer import create_mailer | ||
|
||
settings = EmailApiSettings() | ||
|
||
router = Router() | ||
|
||
custom_months = { | ||
"January": "Jan.", | ||
"February": "Feb.", | ||
"August": "Aug.", | ||
"September": "Sept.", | ||
"October": "Oct.", | ||
"November": "Nov.", | ||
"December": "Dec.", | ||
} | ||
|
||
|
||
class ConfirmationRequest(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
|
@@ -24,12 +34,18 @@ class ConfirmationRequest(BaseModel): | |
confirmation_id: str | ||
|
||
|
||
body_template = """ | ||
prod_body_template = """ | ||
Congratulations! This email confirms that the filing submitted by {signer_name} on {formatted_date} was successful. The confirmation number is {confirmation_id}. | ||
To make any changes to the filing, please return to the Small Business Lending Data Filing Platform and follow the provided instructions. If you have any questions or need additional support, email our support staff at [email protected]. | ||
""" | ||
|
||
beta_body_template = """ | ||
Congratulations! This email confirms that the filing submitted by {signer_name} on {formatted_date} was successful. The confirmation number is {confirmation_id}. | ||
The beta platform is for testing purposes only and user-supplied data may be removed at any time. Take a moment to email our support staff at [email protected] with your feedback or return to the beta platform and upload a new file to continue testing. | ||
""" | ||
|
||
|
||
@router.post("/confirmation/send") | ||
async def send_email(request: Request, confirmation_request: ConfirmationRequest): | ||
|
@@ -38,7 +54,15 @@ async def send_email(request: Request, confirmation_request: ConfirmationRequest | |
timestamp_est = confirmation_request.timestamp.astimezone( | ||
ZoneInfo("America/New_York") | ||
) | ||
formatted_date = timestamp_est.strftime("%B %d, %Y at %-I:%M %p %Z") | ||
full_month = timestamp_est.strftime("%B") | ||
formatted_month = custom_months.get(full_month, full_month) | ||
am_pm = "a.m." if timestamp_est.strftime("%p") == "AM" else "p.m." | ||
formatted_date = ( | ||
f"{formatted_month} {timestamp_est.strftime("%d, %Y at %-I:%M")} {am_pm} EST" | ||
) | ||
body_template = ( | ||
prod_body_template if settings.environment == "PROD" else beta_body_template | ||
) | ||
body_text = dedent( | ||
body_template.format( | ||
signer_name=confirmation_request.signer_name, | ||
|
@@ -47,9 +71,14 @@ async def send_email(request: Request, confirmation_request: ConfirmationRequest | |
) | ||
) | ||
|
||
to_list = [confirmation_request.contact_email, confirmation_request.signer_email] | ||
to_list = ( | ||
[confirmation_request.contact_email, confirmation_request.signer_email] | ||
if settings.environment == "PROD" | ||
else [confirmation_request.signer_email] | ||
) | ||
header = "" if settings.environment == "PROD" else "[BETA] " | ||
email = Email( | ||
f"{get_header(confirmation_request.signer_email)} Small Business Lending Data Filing Confirmation", | ||
f"{header}Small Business Lending Data Filing Confirmation", | ||
body_text, | ||
settings.from_addr, | ||
to=to_list, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,13 @@ | |
from fastapi import FastAPI | ||
from fastapi.testclient import TestClient | ||
from pytest_mock import MockerFixture | ||
from unittest.mock import Mock | ||
from unittest.mock import Mock, patch | ||
|
||
from regtech_api_commons.models.auth import AuthenticatedUser | ||
from starlette.authentication import AuthCredentials | ||
|
||
from datetime import datetime, timezone | ||
|
||
|
||
@pytest.fixture | ||
def app_fixture(mocker: MockerFixture) -> FastAPI: | ||
|
@@ -113,6 +115,66 @@ def test_case_send( | |
assert res.status_code == 200 | ||
assert res.json() == email_json | ||
|
||
def test_email_dates( | ||
self, mocker: MockerFixture, app_fixture: FastAPI, full_user_mock: Mock | ||
): | ||
client = TestClient(app_fixture) | ||
res = client.post( | ||
"/internal/confirmation/send", | ||
data=json.dumps( | ||
{ | ||
"confirmation_id": "test", | ||
"signer_email": "[email protected]", | ||
"signer_name": "Test User", | ||
"contact_email": "[email protected]", | ||
"timestamp": datetime( | ||
2024, 3, 15, 10, 10, tzinfo=timezone.utc | ||
).timestamp() | ||
* 1000, | ||
} | ||
), | ||
) | ||
|
||
expected_email = { | ||
"subject": "[BETA] Small Business Lending Data Filing Confirmation", | ||
"body": "\nCongratulations! This email confirms that the filing submitted by Test User on March 15, 2024 at 6:10 a.m. EST was successful. The confirmation number is test.\n\nThe beta platform is for testing purposes only and user-supplied data may be removed at any time. Take a moment to email our support staff at [email protected] with your feedback or return to the beta platform and upload a new file to continue testing.\n", | ||
"from_addr": "[email protected]", | ||
"to": ["[email protected]"], | ||
"cc": None, | ||
"bcc": None, | ||
} | ||
|
||
assert res.status_code == 200 | ||
assert res.json()["email"] == expected_email | ||
|
||
res = client.post( | ||
"/internal/confirmation/send", | ||
data=json.dumps( | ||
{ | ||
"confirmation_id": "test", | ||
"signer_email": "[email protected]", | ||
"signer_name": "Test User", | ||
"contact_email": "[email protected]", | ||
"timestamp": datetime( | ||
2024, 9, 15, 17, 10, tzinfo=timezone.utc | ||
).timestamp() | ||
* 1000, | ||
} | ||
), | ||
) | ||
|
||
expected_email = { | ||
"subject": "[BETA] Small Business Lending Data Filing Confirmation", | ||
"body": "\nCongratulations! This email confirms that the filing submitted by Test User on Sept. 15, 2024 at 1:10 p.m. EST was successful. The confirmation number is test.\n\nThe beta platform is for testing purposes only and user-supplied data may be removed at any time. Take a moment to email our support staff at [email protected] with your feedback or return to the beta platform and upload a new file to continue testing.\n", | ||
"from_addr": "[email protected]", | ||
"to": ["[email protected]"], | ||
"cc": None, | ||
"bcc": None, | ||
} | ||
|
||
assert res.status_code == 200 | ||
assert res.json()["email"] == expected_email | ||
|
||
def test_confirmation_send( | ||
self, mocker: MockerFixture, app_fixture: FastAPI, full_user_mock: Mock | ||
): | ||
|
@@ -131,13 +193,43 @@ def test_confirmation_send( | |
) | ||
|
||
expected_email = { | ||
"subject": "[CFPB BETA] Small Business Lending Data Filing Confirmation", | ||
"body": "\nCongratulations! This email confirms that the filing submitted by Test User on November 20, 2024 at 1:51 PM EST was successful. The confirmation number is test.\n\nTo make any changes to the filing, please return to the Small Business Lending Data Filing Platform and follow the provided instructions. If you have any questions or need additional support, email our support staff at [email protected].\n", | ||
"subject": "[BETA] Small Business Lending Data Filing Confirmation", | ||
"body": "\nCongratulations! This email confirms that the filing submitted by Test User on Nov. 20, 2024 at 1:51 p.m. EST was successful. The confirmation number is test.\n\nThe beta platform is for testing purposes only and user-supplied data may be removed at any time. Take a moment to email our support staff at [email protected] with your feedback or return to the beta platform and upload a new file to continue testing.\n", | ||
"from_addr": "[email protected]", | ||
"to": ["[email protected]", "[email protected]"], | ||
"to": ["[email protected]"], | ||
"cc": None, | ||
"bcc": None, | ||
} | ||
|
||
assert res.status_code == 200 | ||
assert res.json()["email"] == expected_email | ||
|
||
mock_settings = mocker.MagicMock() | ||
|
||
with patch("regtech_mail_api.internal.settings") as mock_settings: | ||
mock_settings.environment = "PROD" | ||
mock_settings.from_addr = "[email protected]" | ||
expected_email = { | ||
"subject": "Small Business Lending Data Filing Confirmation", | ||
"body": "\nCongratulations! This email confirms that the filing submitted by Test User on Nov. 20, 2024 at 1:51 p.m. EST was successful. The confirmation number is test.\n\nTo make any changes to the filing, please return to the Small Business Lending Data Filing Platform and follow the provided instructions. If you have any questions or need additional support, email our support staff at [email protected].\n", | ||
"from_addr": "[email protected]", | ||
"to": ["[email protected]", "[email protected]"], | ||
"cc": None, | ||
"bcc": None, | ||
} | ||
|
||
res = client.post( | ||
"/internal/confirmation/send", | ||
data=json.dumps( | ||
{ | ||
"confirmation_id": "test", | ||
"signer_email": "[email protected]", | ||
"signer_name": "Test User", | ||
"contact_email": "[email protected]", | ||
"timestamp": 1732128696, | ||
} | ||
), | ||
) | ||
|
||
assert res.status_code == 200 | ||
assert res.json()["email"] == expected_email |