generated from cfpb/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Updated code to pull name and email from auth'd user (#16)
* 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
Showing
3 changed files
with
119 additions
and
24 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 |
---|---|---|
|
@@ -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 |
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,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 |