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

Added an attempt to set UPLOAD_FAILED in except block, and logging if… #197

Merged
merged 1 commit into from
May 7, 2024
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
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="[email protected]",
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
Loading