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.
- Loading branch information
Showing
5 changed files
with
101 additions
and
70 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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from fastapi import Request | ||
|
||
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 | ||
|
||
settings = EmailApiSettings() | ||
|
||
router = Router() | ||
|
||
|
||
@router.post("/confirmation/send") | ||
async def send_email(request: Request): | ||
mailer = create_mailer() | ||
# build email | ||
to_list = ["contact email", "signer email"] | ||
email = Email("Subject", "Body", settings.from_addr, to=to_list) | ||
mailer.send(email) | ||
return "Called internal send" |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from fastapi import Request | ||
from starlette.authentication import requires | ||
|
||
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 | ||
|
||
settings = EmailApiSettings() | ||
|
||
router = Router() | ||
|
||
|
||
@router.post("/case/send") | ||
@requires("authenticated") | ||
async def send_email(request: Request): | ||
sender_addr = request.user.email | ||
sender_name = request.user.name if request.user.name else "" | ||
type = request.headers["case-type"] | ||
|
||
header = "[BETA]" | ||
if "cfpb" in sender_addr.lower().split("@")[-1]: | ||
header = "[CFPB BETA]" | ||
if settings.environment: | ||
header = f"[{settings.environment}]" | ||
|
||
subject = f"{header} SBL User Request for {type}" | ||
|
||
form_data = await request.form() | ||
|
||
body_lines = [f"{k}: {v}" for k, v in form_data.items()] | ||
email_body = f"Contact Email: {sender_addr}\n" | ||
email_body += f"Contact Name: {sender_name}\n\n" | ||
email_body += "\n".join(body_lines) | ||
|
||
email = Email(subject, email_body, settings.from_addr, to={settings.to}) | ||
|
||
create_mailer().send(email) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,33 +48,33 @@ class TestEmailApiAuthentication: | |
def test_unauthed_endpoints( | ||
self, mocker: MockerFixture, app_fixture: FastAPI, unauthed_user_mock: Mock | ||
): | ||
client = TestClient(app_fixture) | ||
res = client.get("/welcome") | ||
assert res.status_code == 403 | ||
|
||
client = TestClient(app_fixture) | ||
res = client.post( | ||
"/send", | ||
"/public/case/send", | ||
json={"data": "data"}, | ||
) | ||
assert res.status_code == 403 | ||
|
||
res = client.post( | ||
"/internal/confirmation/send" | ||
) | ||
assert res.status_code == 200 | ||
|
||
def test_authed_endpoints( | ||
self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock | ||
): | ||
email_json = { | ||
"email": { | ||
"subject": "Institution Profile Change", | ||
"body": "lei: 1234567890ABCDEFGHIJ\ninstitution_name_1: Fintech 1\ntin_1: 12-3456789\nrssd_1: 1234567", | ||
"from_addr": "[email protected]", | ||
"to": ["[email protected]"], | ||
"cc": None, | ||
"bcc": None, | ||
} | ||
} | ||
|
||
mock = mocker.patch("regtech_mail_api.api.send_email") | ||
mock.return_value = {"email": email_json} | ||
): | ||
client = TestClient(app_fixture) | ||
res = client.post( | ||
"/public/case/send", | ||
headers={ | ||
"case-type": "Institution Profile Change", | ||
}, | ||
data={ | ||
"lei": "1234567890ABCDEFGHIJ", | ||
"institution_name_1": "Fintech 1", | ||
"tin_1": "12-3456789", | ||
"rssd_1": "1234567", | ||
}, | ||
) | ||
client = TestClient(app_fixture) | ||
res = client.get("/welcome") | ||
assert res.status_code == 200 |
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