generated from cfpb/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated confirmation send with expected email format
- Loading branch information
Showing
5 changed files
with
92 additions
and
17 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 |
---|---|---|
@@ -1,21 +1,58 @@ | ||
from datetime import datetime | ||
from fastapi import Request | ||
from pydantic import BaseModel, ConfigDict, EmailStr | ||
from textwrap import dedent | ||
from zoneinfo import ZoneInfo | ||
|
||
from regtech_api_commons.api.router_wrapper import Router | ||
|
||
from regtech_mail_api.settings import EmailApiSettings | ||
from regtech_mail_api.models import Email | ||
from regtech_mail_api.mailer import create_mailer | ||
from regtech_mail_api.mailer import create_mailer, get_header | ||
|
||
settings = EmailApiSettings() | ||
|
||
router = Router() | ||
|
||
|
||
class ConfirmationRequest(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
contact_email: EmailStr | ||
signer_email: EmailStr | ||
signer_name: str | ||
timestamp: datetime | ||
confirmation_id: str | ||
|
||
|
||
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 upload a new file to continue testing. | ||
""" | ||
|
||
|
||
@router.post("/confirmation/send") | ||
async def send_email(request: Request): | ||
async def send_email(request: Request, confirmation_request: ConfirmationRequest): | ||
mailer = create_mailer() | ||
# build email | ||
to_list = ["contact email", "signer email"] | ||
email = Email("Subject", "Body", settings.from_addr, to=to_list) | ||
|
||
timestamp_est = confirmation_request.timestamp.astimezone( | ||
ZoneInfo("America/New_York") | ||
) | ||
formatted_date = timestamp_est.strftime("%B %d, %Y at %-I:%M %p %Z") | ||
body_text = dedent( | ||
body_template.format( | ||
signer_name=confirmation_request.signer_name, | ||
formatted_date=formatted_date, | ||
confirmation_id=confirmation_request.confirmation_id, | ||
) | ||
) | ||
|
||
to_list = [confirmation_request.contact_email, confirmation_request.signer_email] | ||
email = Email( | ||
f"{get_header(confirmation_request.signer_email)} Small Business Lending Data Filing Confirmation", | ||
body_text, | ||
settings.from_addr, | ||
to=to_list, | ||
) | ||
mailer.send(email) | ||
return "Called internal send" | ||
return {"email": email} |
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import json | ||
import pytest | ||
|
||
from fastapi import FastAPI | ||
|
@@ -55,7 +56,18 @@ def test_unauthed_endpoints( | |
) | ||
assert res.status_code == 403 | ||
|
||
res = client.post("/internal/confirmation/send") | ||
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 | ||
|
||
def test_authed_endpoints( | ||
|
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import json | ||
import pytest | ||
|
||
from fastapi import FastAPI | ||
|
@@ -116,6 +117,27 @@ def test_confirmation_send( | |
self, mocker: MockerFixture, app_fixture: FastAPI, full_user_mock: Mock | ||
): | ||
client = TestClient(app_fixture) | ||
res = client.post("/internal/confirmation/send") | ||
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, | ||
} | ||
), | ||
) | ||
|
||
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\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 upload a new file to continue testing.\n", | ||
"from_addr": "[email protected]", | ||
"to": ["[email protected]", "[email protected]"], | ||
"cc": None, | ||
"bcc": None, | ||
} | ||
|
||
assert res.status_code == 200 | ||
assert res.json() == "Called internal send" | ||
assert res.json()["email"] == expected_email |