Skip to content

Commit

Permalink
Updated code to pull name and email from auth'd user (#16)
Browse files Browse the repository at this point in the history
* Updated code to pull name and email from auth'd user

* Linting!

* Updated to fix sender if name is missing

* Added the .get_secret_value() call here to get this working
jcadam14 authored Feb 23, 2024
1 parent 95ce4cf commit ac492a0
Showing 3 changed files with 119 additions and 24 deletions.
10 changes: 5 additions & 5 deletions regtech_mail_api/api.py
Original file line number Diff line number Diff line change
@@ -43,8 +43,8 @@
mailer = SmtpMailer(
settings.smtp_host, # type: ignore
settings.smtp_port,
settings.smtp_username, # type: ignore
settings.smtp_password, # type: ignore
settings.smtp_username.get_secret_value(), # type: ignore
settings.smtp_password.get_secret_value(), # type: ignore
settings.smtp_use_tls,
)
case EmailMailerType.MOCK:
@@ -64,10 +64,10 @@ def read_root(request: Request):
async def send_email(request: Request):
headers = request.headers
subject = headers["X-Mail-Subject"]
sender_addr = headers["X-Mail-Sender-Address"]
sender_name = headers["X-Mail-Sender-Name"]
sender_addr = request.user.email
sender_name = request.user.name

sender = f"{sender_name} <{sender_addr}>" if sender_addr else sender_name
sender = f"{sender_name} <{sender_addr}>" if sender_name else sender_addr

form_data = await request.form()

20 changes: 1 addition & 19 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ def auth_mock(mocker: MockerFixture) -> Mock:
@pytest.fixture
def authed_user_mock(auth_mock: Mock) -> Mock:
claims = {
"name": "Test User",
"preferred_username": "test_user",
"email": "[email protected]",
}
@@ -78,22 +79,3 @@ def test_authed_endpoints(
client = TestClient(app_fixture)
res = client.get("/")
assert res.status_code == 200

client = TestClient(app_fixture)
res = client.post(
"/send",
json=email_json,
headers={
"X-Mail-Subject": "Institution Profile Change",
"X-Mail-Sender-Address": "[email protected]",
"X-Mail-Sender-Name": "Jane Doe",
},
data={
"lei": "1234567890ABCDEFGHIJ",
"institution_name_1": "Fintech 1",
"tin_1": "12-3456789",
"rssd_1": "1234567",
},
)
assert res.status_code == 200
assert res.json() == email_json
113 changes: 113 additions & 0 deletions tests/test_send.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import pytest

from fastapi import FastAPI
from fastapi.testclient import TestClient
from pytest_mock import MockerFixture
from unittest.mock import Mock

from regtech_api_commons.models.auth import AuthenticatedUser
from starlette.authentication import AuthCredentials


@pytest.fixture
def app_fixture(mocker: MockerFixture) -> FastAPI:
from api import app

return app


@pytest.fixture
def auth_mock(mocker: MockerFixture) -> Mock:
return mocker.patch(
"regtech_api_commons.oauth2.oauth2_backend.BearerTokenAuthBackend.authenticate"
)


@pytest.fixture
def user_no_profile_mock(auth_mock: Mock) -> Mock:
claims = {
"email": "[email protected]",
}
auth_mock.return_value = (
AuthCredentials(["authenticated"]),
AuthenticatedUser.from_claim(claims),
)
return auth_mock


@pytest.fixture
def full_user_mock(auth_mock: Mock) -> Mock:
claims = {
"name": "Test User",
"email": "[email protected]",
}
auth_mock.return_value = (
AuthCredentials(["authenticated"]),
AuthenticatedUser.from_claim(claims),
)
return auth_mock


class TestEmailApiSend:

def test_send_no_profile(
self, mocker: MockerFixture, app_fixture: FastAPI, user_no_profile_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]",
"sender": "[email protected]",
"to": ["[email protected]"],
"cc": None,
"bcc": None,
}
}

client = TestClient(app_fixture)
res = client.post(
"/send",
headers={
"X-Mail-Subject": "Institution Profile Change",
},
data={
"lei": "1234567890ABCDEFGHIJ",
"institution_name_1": "Fintech 1",
"tin_1": "12-3456789",
"rssd_1": "1234567",
},
)
assert res.status_code == 200
assert res.json() == email_json

def test_send(
self, mocker: MockerFixture, app_fixture: FastAPI, full_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]",
"sender": "Test User <[email protected]>",
"to": ["[email protected]"],
"cc": None,
"bcc": None,
}
}

client = TestClient(app_fixture)
res = client.post(
"/send",
headers={
"X-Mail-Subject": "Institution Profile Change",
},
data={
"lei": "1234567890ABCDEFGHIJ",
"institution_name_1": "Fintech 1",
"tin_1": "12-3456789",
"rssd_1": "1234567",
},
)
assert res.status_code == 200
assert res.json() == email_json

0 comments on commit ac492a0

Please sign in to comment.