Skip to content

Commit

Permalink
Merge branch 'main' into feature/482_tin_lei_check
Browse files Browse the repository at this point in the history
  • Loading branch information
lchen-2101 committed Nov 29, 2024
2 parents 0e7cc94 + d4cc638 commit de8cc8e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/sbl_filing_api/routers/filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

from regtech_api_commons.api.dependencies import verify_user_lei_relation

from src.sbl_filing_api.services.request_handler import send_confirmation_email

from sbl_filing_api.services.request_action_validator import UserActionContext, validate_user_action, set_context

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -144,10 +146,12 @@ async def sign_filing(request: Request, lei: str, period_code: str):
action_type=UserActionType.SIGN,
),
)
filing.confirmation_id = (
lei + "-" + period_code + "-" + str(latest_sub.counter) + "-" + str(int(sig.timestamp.timestamp()))
)
sig_timestamp = int(sig.timestamp.timestamp())
filing.confirmation_id = lei + "-" + period_code + "-" + str(latest_sub.counter) + "-" + str(sig_timestamp)
filing.signatures.append(sig)
send_confirmation_email(
request.user.name, request.user.email, filing.contact_info.email, filing.confirmation_id, sig_timestamp
)
return await repo.upsert_filing(request.state.db_session, filing)


Expand Down
8 changes: 7 additions & 1 deletion src/sbl_filing_api/services/request_action_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ class UserActionContext(StrEnum):
FILING = "filing"
INSTITUTION = "institution"


class FiRequest:
"""
FI retrieval request to allow cache to work
"""

request: Request
lei: str

Expand All @@ -39,14 +41,17 @@ def __hash__(self):
def __eq__(self, other: "FiRequest"):
return self.lei == other.lei


@alru_cache(ttl=60 * 60)
async def get_institution_data(fi_request: FiRequest):
async with httpx.AsyncClient() as client:
res = await client.get(
settings.user_fi_api_url + fi_request.lei, headers={"authorization": fi_request.request.headers["authorization"]}
settings.user_fi_api_url + fi_request.lei,
headers={"authorization": fi_request.request.headers["authorization"]},
)
return res.json()


class ActionValidator(ABC):
"""
Abstract Callable class for action validations, __subclasses__ method leveraged to construct a registry
Expand Down Expand Up @@ -136,6 +141,7 @@ def set_context(requirements: Set[UserActionContext]):
lei: comes from request path param
period: filing period comes from request path param
"""

async def _set_context(request: Request):
lei = request.path_params.get("lei")
period = request.path_params.get("period_code")
Expand Down
31 changes: 31 additions & 0 deletions src/sbl_filing_api/services/request_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import logging

import httpx
from pydantic import EmailStr
from sbl_filing_api.config import settings

logger = logging.getLogger(__name__)


def send_confirmation_email(
user_full_name: str,
user_email: EmailStr,
contact_info_email: EmailStr,
confirmation_id: str,
timestamp: int,
):
confirmation_request = {
"confirmation_id": confirmation_id,
"signer_email": user_email,
"signer_name": user_full_name,
"contact_email": contact_info_email,
"timestamp": timestamp,
}
try:
res = httpx.post(settings.mail_api_url, json=confirmation_request)
if res.status_code != 200:
logger.error(res.text)
else:
logger.info(res.text)
except Exception:
logger.exception(f"Failed to send confirmation email for {user_full_name}")
8 changes: 8 additions & 0 deletions tests/api/routers/test_filing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,9 @@ async def test_good_sign_filing(
action_type=UserActionType.SIGN,
)

send_email_mock = mocker.patch("sbl_filing_api.routers.filing.send_confirmation_email")
send_email_mock.return_value = None

upsert_mock = mocker.patch("sbl_filing_api.entities.repos.submission_repo.upsert_filing")
updated_filing_obj = deepcopy(get_filing_mock.return_value)
upsert_mock.return_value = updated_filing_obj
Expand All @@ -1042,6 +1045,9 @@ async def test_good_sign_filing(
action_type=UserActionType.SIGN,
),
)
send_email_mock.assert_called_with("Test User", "[email protected]", "[email protected]", ANY, ANY)
assert send_email_mock.call_args.args[3].startswith("1234567890ABCDEFGH00-2024-5-")
assert float(send_email_mock.call_args.args[4]) == pytest.approx(int(dt.now().timestamp()), abs=1.5)
assert upsert_mock.call_args.args[1].confirmation_id.startswith("1234567890ABCDEFGH00-2024-5-")
assert res.status_code == 200
assert float(upsert_mock.call_args.args[1].confirmation_id.split("-")[3]) == pytest.approx(
Expand All @@ -1052,6 +1058,8 @@ async def test_errors_sign_filing(
self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock, get_filing_mock: Mock
):
sub_mock = mocker.patch("sbl_filing_api.entities.repos.submission_repo.get_latest_submission")
send_email_mock = mocker.patch("sbl_filing_api.services.request_handler.send_confirmation_email")
send_email_mock.return_value = None
sub_mock.return_value = SubmissionDAO(
id=1,
submitter=UserActionDAO(
Expand Down
31 changes: 31 additions & 0 deletions tests/services/test_request_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from unittest.mock import ANY
from pytest_mock import MockerFixture
from sbl_filing_api.services.request_handler import send_confirmation_email


def test_send_confirmation_email(mocker: MockerFixture, caplog):
# No errors
post_mock = mocker.patch("sbl_filing_api.services.request_handler.httpx.post")
post_mock.return_value.status_code = 200
send_confirmation_email("full_name", "[email protected]", "[email protected]", "confirmation", 12345)
post_mock.assert_called_with(
ANY,
json={
"confirmation_id": "confirmation",
"contact_email": "[email protected]",
"signer_email": "[email protected]",
"signer_name": "full_name",
"timestamp": 12345,
},
)

# With errors
post_mock.side_effect = None
post_mock.return_value.status_code = 400
post_mock.return_value.text = "Email_response"
send_confirmation_email("full_name", "[email protected]", "[email protected]", "confirmation", 12345)
assert "Email_response" in caplog.messages[0]

post_mock.side_effect = IOError("test")
send_confirmation_email("full_name", "[email protected]", "[email protected]", "confirmation", 12345)
assert "Failed to send confirmation email for full_name" in caplog.messages[1]

0 comments on commit de8cc8e

Please sign in to comment.