Skip to content

Commit

Permalink
Added an attempt to set UPLOAD_FAILED in except block, and logging if… (
Browse files Browse the repository at this point in the history
#197)

Closes #180
  • Loading branch information
jcadam14 committed May 7, 2024
1 parent c1c0fa4 commit c823e0f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/sbl_filing_api/routers/filing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import logging

from concurrent.futures import ProcessPoolExecutor
from fastapi import Depends, Request, UploadFile, BackgroundTasks, status
Expand Down Expand Up @@ -31,6 +32,8 @@

from sbl_filing_api.routers.dependencies import verify_user_lei_relation

logger = logging.getLogger(__name__)


async def set_db(request: Request, session: Annotated[AsyncSession, Depends(get_session)]):
request.state.db_session = session
Expand Down Expand Up @@ -152,6 +155,7 @@ async def upload_file(
name="Filing Not Found",
detail=f"There is no Filing for LEI {lei} in period {period_code}, unable to submit file.",
)
submission = None
try:
submitter = await repo.add_user_action(
request.state.db_session,
Expand Down Expand Up @@ -186,6 +190,20 @@ async def upload_file(
return submission

except Exception as e:
if submission:
try:
submission.state = SubmissionState.UPLOAD_FAILED
submission = await repo.update_submission(request.state.db_session, submission)
except Exception as ex:
logger.error(
(
f"Error updating submission {submission.id} to {SubmissionState.UPLOAD_FAILED} state during error handling,"
f" the submission may be stuck in the {SubmissionState.SUBMISSION_STARTED} or {SubmissionState.SUBMISSION_UPLOADED} state."
),
ex,
exc_info=True,
stack_info=True,
)
raise RegTechHttpException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
name="Submission Unprocessable",
Expand Down
60 changes: 60 additions & 0 deletions tests/api/routers/test_filing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,66 @@ def test_submission_update_fail(
assert res.status_code == 500
assert res.json()["error_detail"] == "Error while trying to process SUBMIT User Action"

def test_submission_second_update_fail(
self,
mocker: MockerFixture,
app_fixture: FastAPI,
authed_user_mock: Mock,
submission_csv: str,
get_filing_mock: Mock,
):
return_sub = SubmissionDAO(
id=1,
filing=1,
state=SubmissionState.SUBMISSION_UPLOADED,
filename="submission.csv",
)

log_mock = mocker.patch("sbl_filing_api.routers.filing.logger.error")

mock_validate_file = mocker.patch("sbl_filing_api.services.submission_processor.validate_file_processable")
mock_validate_file.return_value = None

async_mock = AsyncMock(return_value=return_sub)
mocker.patch("sbl_filing_api.entities.repos.submission_repo.add_submission", side_effect=async_mock)

mock_upload = mocker.patch("sbl_filing_api.services.submission_processor.upload_to_storage")
mock_upload.return_value = None

mocker.patch(
"sbl_filing_api.entities.repos.submission_repo.update_submission",
side_effect=Exception("Can't connect to database"),
)

mock_add_submitter = mocker.patch("sbl_filing_api.entities.repos.submission_repo.add_user_action")
mock_add_submitter.side_effect = AsyncMock(
return_value=UserActionDAO(
id=2,
user_id="123456-7890-ABCDEF-GHIJ",
user_name="test submitter",
user_email="test@local.host",
action_type=UserActionType.SUBMIT,
timestamp=datetime.datetime.now(),
)
)

file = {"file": ("submission.csv", open(submission_csv, "rb"))}

client = TestClient(app_fixture)

res = client.post("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions", files=file)
log_mock.assert_called_with(
(
f"Error updating submission 1 to {SubmissionState.UPLOAD_FAILED} state during error handling,"
f" the submission may be stuck in the {SubmissionState.SUBMISSION_STARTED} or {SubmissionState.SUBMISSION_UPLOADED} state."
),
ANY,
exc_info=True,
stack_info=True,
)
assert res.status_code == 500
assert res.json()["error_detail"] == "Error while trying to process SUBMIT User Action"

async def test_unauthed_patch_filing(self, app_fixture: FastAPI):
client = TestClient(app_fixture)

Expand Down

0 comments on commit c823e0f

Please sign in to comment.