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

Updated code to pull name and email from auth'd user #16

Merged
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
10 changes: 5 additions & 5 deletions regtech_mail_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -77,10 +77,10 @@ async def get_debug_info(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()

Expand Down
20 changes: 1 addition & 19 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
}
Expand Down Expand Up @@ -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
Loading