From 8ca48b23ffcf9c2c497f7d9c18704d0e51c71501 Mon Sep 17 00:00:00 2001 From: Adam <41971533+jcadam14@users.noreply.github.com> Date: Tue, 19 Nov 2024 13:54:31 -0700 Subject: [PATCH] Created internal/public routers --- src/regtech_mail_api/api.py | 53 ++++---------------------------- src/regtech_mail_api/internal.py | 21 +++++++++++++ src/regtech_mail_api/public.py | 41 ++++++++++++++++++++++++ tests/test_authentication.py | 40 ++++++++++++------------ tests/test_send.py | 16 ++++++++-- 5 files changed, 101 insertions(+), 70 deletions(-) create mode 100644 src/regtech_mail_api/internal.py create mode 100644 src/regtech_mail_api/public.py diff --git a/src/regtech_mail_api/api.py b/src/regtech_mail_api/api.py index 620719e..2036a48 100644 --- a/src/regtech_mail_api/api.py +++ b/src/regtech_mail_api/api.py @@ -1,15 +1,13 @@ -from fastapi import FastAPI, Request +from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.security import OAuth2AuthorizationCodeBearer -from regtech_mail_api.models import Email -from regtech_mail_api.settings import kc_settings, EmailApiSettings -from regtech_mail_api.mailer import create_mailer +from regtech_mail_api.settings import kc_settings +from regtech_mail_api.public import router as public_router +from regtech_mail_api.internal import router as internal_router -from starlette.authentication import requires from starlette.middleware.authentication import AuthenticationMiddleware -from regtech_api_commons.api.router_wrapper import Router from regtech_api_commons.oauth2.oauth2_backend import BearerTokenAuthBackend from regtech_api_commons.oauth2.oauth2_admin import OAuth2Admin @@ -33,44 +31,5 @@ allow_headers=["*"], ) -settings = EmailApiSettings() - -router = Router() - - -@router.get("/welcome") -@requires("authenticated") -def read_root(request: Request): - return {"message": "Welcome to the Email API"} - - -@router.post("/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} - - -app.include_router(router) +app.include_router(internal_router, prefix="/internal") +app.include_router(public_router, prefix="/public") diff --git a/src/regtech_mail_api/internal.py b/src/regtech_mail_api/internal.py new file mode 100644 index 0000000..ce3866e --- /dev/null +++ b/src/regtech_mail_api/internal.py @@ -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" diff --git a/src/regtech_mail_api/public.py b/src/regtech_mail_api/public.py new file mode 100644 index 0000000..4b0876d --- /dev/null +++ b/src/regtech_mail_api/public.py @@ -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} diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 1615dad..ef5ac5d 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -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": "test@cfpb.gov", - "to": ["cases@localhost.localdomain"], - "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 diff --git a/tests/test_send.py b/tests/test_send.py index 4a96b4c..f8da09d 100644 --- a/tests/test_send.py +++ b/tests/test_send.py @@ -68,7 +68,7 @@ def test_send_no_profile( client = TestClient(app_fixture) res = client.post( - "/send", + "/public/case/send", headers={ "case-type": "Institution Profile Change", }, @@ -82,7 +82,7 @@ def test_send_no_profile( assert res.status_code == 200 assert res.json() == email_json - def test_send( + def test_case_send( self, mocker: MockerFixture, app_fixture: FastAPI, full_user_mock: Mock ): email_json = { @@ -98,7 +98,7 @@ def test_send( client = TestClient(app_fixture) res = client.post( - "/send", + "/public/case/send", headers={ "case-type": "Institution Profile Change", }, @@ -111,3 +111,13 @@ def test_send( ) assert res.status_code == 200 assert res.json() == email_json + + def test_confirmation_send( + self, mocker: MockerFixture, app_fixture: FastAPI, full_user_mock: Mock + ): + client = TestClient(app_fixture) + res = client.post( + "/internal/confirmation/send" + ) + assert res.status_code == 200 + assert res.json() == "Called internal send" \ No newline at end of file