Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

50 fix confirmation email for beta language #51

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/regtech_mail_api/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
}
lchen-2101 marked this conversation as resolved.
Show resolved Hide resolved


class ConfirmationRequest(BaseModel):
model_config = ConfigDict(from_attributes=True)
Expand All @@ -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):
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/regtech_mail_api/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@ def get_header(email):
header = "[CFPB BETA]"
if settings.environment:
header = f"[{settings.environment}]"
if settings.environment == "PROD":
header = ""
return header
100 changes: 96 additions & 4 deletions tests/test_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
):
Expand All @@ -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
Loading