Skip to content

Commit

Permalink
Merge branch 'mixed_branch' into 460-remove-millimicroseconds-from-th…
Browse files Browse the repository at this point in the history
…e-signature-confirmation-id
  • Loading branch information
jcadam14 committed Oct 29, 2024
2 parents 5081e62 + a2138dd commit d4f59f4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 47 deletions.
6 changes: 6 additions & 0 deletions src/sbl_filing_api/entities/models/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Dict, Any, List
from pydantic import BaseModel, ConfigDict, Field, model_validator
from sbl_filing_api.entities.models.model_enums import FilingType, FilingTaskState, SubmissionState, UserActionType
from sbl_filing_api.entities.models.dao import SubmissionDAO


class UserActionDTO(BaseModel):
Expand All @@ -28,6 +29,11 @@ class SubmissionDTO(BaseModel):
submitter: UserActionDTO
accepter: UserActionDTO | None = None

def from_orm(cls, obj: SubmissionDAO):
obj_dict = obj.__dict__.copy()
obj_dict["id"] = obj_dict["counter"]
return cls.parse_obj(obj_dict)


class FilingTaskDTO(BaseModel):
model_config = ConfigDict(from_attributes=True)
Expand Down
6 changes: 6 additions & 0 deletions src/sbl_filing_api/entities/repos/submission_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ async def get_submission(session: AsyncSession, submission_id: int) -> Submissio
return result[0] if result else None


async def get_submission_by_counter(session: AsyncSession, lei: str, filing_period: str, counter: int) -> SubmissionDAO:
filing = await get_filing(session, lei=lei, filing_period=filing_period)
result = await query_helper(session, SubmissionDAO, filing=filing.id, counter=counter)
return result[0] if result else None


async def get_filing(session: AsyncSession, lei: str, filing_period: str) -> FilingDAO:
result = await query_helper(session, FilingDAO, lei=lei, filing_period=filing_period)
if result:
Expand Down
24 changes: 12 additions & 12 deletions src/sbl_filing_api/routers/filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,24 +256,24 @@ async def get_submission_latest(request: Request, lei: str, period_code: str):
return Response(status_code=status.HTTP_204_NO_CONTENT)


@router.get("/institutions/{lei}/filings/{period_code}/submissions/{id}", response_model=SubmissionDTO | None)
@router.get("/institutions/{lei}/filings/{period_code}/submissions/{counter}", response_model=SubmissionDTO | None)
@requires("authenticated")
async def get_submission(request: Request, response: Response, id: int):
result = await repo.get_submission(request.state.db_session, id)
async def get_submission(request: Request, response: Response, counter: int, lei: str, period_code: str):
result = await repo.get_submission_by_counter(request.state.db_session, lei, period_code, counter)
if result:
return result
response.status_code = status.HTTP_404_NOT_FOUND


@router.put("/institutions/{lei}/filings/{period_code}/submissions/{id}/accept", response_model=SubmissionDTO)
@router.put("/institutions/{lei}/filings/{period_code}/submissions/{counter}/accept", response_model=SubmissionDTO)
@requires("authenticated")
async def accept_submission(request: Request, id: int, lei: str, period_code: str):
submission = await repo.get_submission(request.state.db_session, id)
async def accept_submission(request: Request, counter: int, lei: str, period_code: str):
submission = await repo.get_submission_by_counter(request.state.db_session, lei, period_code, counter)
if not submission:
raise RegTechHttpException(
status_code=status.HTTP_404_NOT_FOUND,
name="Submission Not Found",
detail=f"Submission ID {id} does not exist, cannot accept a non-existing submission.",
detail=f"Submission {counter} for LEI {lei} in filing period {period_code} does not exist, cannot accept a non-existing submission.",
)
if (
submission.state != SubmissionState.VALIDATION_SUCCESSFUL
Expand All @@ -282,7 +282,7 @@ async def accept_submission(request: Request, id: int, lei: str, period_code: st
raise RegTechHttpException(
status_code=status.HTTP_403_FORBIDDEN,
name="Submission Action Forbidden",
detail=f"Submission {id} for LEI {lei} in filing period {period_code} is not in an acceptable state. Submissions must be validated successfully or with only warnings to be accepted.",
detail=f"Submission {counter} for LEI {lei} in filing period {period_code} is not in an acceptable state. Submissions must be validated successfully or with only warnings to be accepted.",
)

accepter = await repo.add_user_action(
Expand Down Expand Up @@ -383,12 +383,12 @@ async def get_latest_submission_report(request: Request, lei: str, period_code:


@router.get(
"/institutions/{lei}/filings/{period_code}/submissions/{id}/report",
"/institutions/{lei}/filings/{period_code}/submissions/{counter}/report",
responses={200: {"content": {"text/plain; charset=utf-8": {}}}},
)
@requires("authenticated")
async def get_submission_report(request: Request, response: Response, lei: str, period_code: str, id: int):
sub = await repo.get_submission(request.state.db_session, id)
async def get_submission_report(request: Request, response: Response, lei: str, period_code: str, counter: int):
sub = await repo.get_submission_by_counter(request.state.db_session, lei, period_code, counter)
if sub and sub.state in [
SubmissionState.VALIDATION_SUCCESSFUL,
SubmissionState.VALIDATION_WITH_ERRORS,
Expand All @@ -410,7 +410,7 @@ async def get_submission_report(request: Request, response: Response, lei: str,
raise RegTechHttpException(
status_code=status.HTTP_404_NOT_FOUND,
name="Report Not Found",
detail=f"Report for ({id}) does not exist.",
detail=f"Report for ({lei}-{period_code}-{counter}) does not exist.",
)


Expand Down
57 changes: 23 additions & 34 deletions tests/api/routers/test_filing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def test_unauthed_get_submission_by_id(self, mocker: MockerFixture, app_fixture:
res = client.get("/v1/filing/institutions/123456790/filings/2024/submissions/1")
assert res.status_code == 403

async def test_get_submission_by_id(self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock):
async def test_get_submission_by_counter(self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock):
user_action_submit = UserActionDAO(
id=2,
user_id="123456-7890-ABCDEF-GHIJ",
Expand All @@ -252,11 +252,11 @@ async def test_get_submission_by_id(self, mocker: MockerFixture, app_fixture: Fa
action_type=UserActionType.SUBMIT,
timestamp=datetime.datetime.now(),
)
mock = mocker.patch("sbl_filing_api.entities.repos.submission_repo.get_submission")
mock = mocker.patch("sbl_filing_api.entities.repos.submission_repo.get_submission_by_counter")
mock.return_value = SubmissionDAO(
id=1,
filing=1,
counter=1,
counter=2,
state=SubmissionState.VALIDATION_WITH_ERRORS,
validation_ruleset_version="v1",
submission_time=datetime.datetime.now(),
Expand All @@ -267,13 +267,13 @@ async def test_get_submission_by_id(self, mocker: MockerFixture, app_fixture: Fa

client = TestClient(app_fixture)

res = client.get("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/1")
mock.assert_called_with(ANY, 1)
res = client.get("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/2")
mock.assert_called_with(ANY, "1234567890ZXWVUTSR00", "2024", 2)
assert res.status_code == 200

mock.return_value = None
res = client.get("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/1")
mock.assert_called_with(ANY, 1)
mock.assert_called_with(ANY, "1234567890ZXWVUTSR00", "2024", 1)
assert res.status_code == 404

def test_authed_upload_file(
Expand Down Expand Up @@ -915,11 +915,11 @@ async def test_accept_submission(self, mocker: MockerFixture, app_fixture: FastA
action_type=UserActionType.ACCEPT,
timestamp=datetime.datetime.now(),
)
mock = mocker.patch("sbl_filing_api.entities.repos.submission_repo.get_submission")
mock = mocker.patch("sbl_filing_api.entities.repos.submission_repo.get_submission_by_counter")
mock.return_value = SubmissionDAO(
id=1,
filing=1,
counter=1,
counter=3,
state=SubmissionState.VALIDATION_WITH_ERRORS,
validation_ruleset_version="v1",
submission_time=datetime.datetime.now(),
Expand All @@ -935,7 +935,7 @@ async def test_accept_submission(self, mocker: MockerFixture, app_fixture: FastA
update_mock.return_value = SubmissionDAO(
id=1,
filing=1,
counter=2,
counter=4,
state=SubmissionState.SUBMISSION_ACCEPTED,
validation_ruleset_version="v1",
submission_time=datetime.datetime.now(),
Expand All @@ -947,15 +947,15 @@ async def test_accept_submission(self, mocker: MockerFixture, app_fixture: FastA
)

client = TestClient(app_fixture)
res = client.put("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/1/accept")
res = client.put("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/3/accept")
assert res.status_code == 403
assert (
res.json()["error_detail"]
== "Submission 1 for LEI 1234567890ZXWVUTSR00 in filing period 2024 is not in an acceptable state. Submissions must be validated successfully or with only warnings to be accepted."
== "Submission 3 for LEI 1234567890ZXWVUTSR00 in filing period 2024 is not in an acceptable state. Submissions must be validated successfully or with only warnings to be accepted."
)

mock.return_value.state = SubmissionState.VALIDATION_SUCCESSFUL
res = client.put("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/1/accept")
res = client.put("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/4/accept")
update_mock.assert_called_once()
update_accepter_mock.assert_called_once_with(
ANY,
Expand All @@ -979,7 +979,10 @@ async def test_accept_submission(self, mocker: MockerFixture, app_fixture: FastA
mock.return_value = None
res = client.put("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/1/accept")
assert res.status_code == 404
assert res.json()["error_detail"] == "Submission ID 1 does not exist, cannot accept a non-existing submission."
assert (
res.json()["error_detail"]
== "Submission 1 for LEI 1234567890ZXWVUTSR00 in filing period 2024 does not exist, cannot accept a non-existing submission."
)

async def test_good_sign_filing(
self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock, get_filing_mock: Mock
Expand Down Expand Up @@ -1183,7 +1186,7 @@ async def test_get_latest_sub_report(
assert res.status_code == 404

async def test_get_sub_report(self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock):
sub_mock = mocker.patch("sbl_filing_api.entities.repos.submission_repo.get_submission")
sub_mock = mocker.patch("sbl_filing_api.entities.repos.submission_repo.get_submission_by_counter")
sub_mock.return_value = SubmissionDAO(
id=2,
counter=4,
Expand All @@ -1207,40 +1210,26 @@ async def test_get_sub_report(self, mocker: MockerFixture, app_fixture: FastAPI,
file_mock.return_value = [c for c in file_content]

client = TestClient(app_fixture)
res = client.get("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/2/report")
sub_mock.assert_called_with(ANY, 2)
res = client.get("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/4/report")
sub_mock.assert_called_with(ANY, "1234567890ZXWVUTSR00", "2024", 4)
file_mock.assert_called_with("2024", "1234567890ZXWVUTSR00", "4" + submission_processor.REPORT_QUALIFIER)
assert res.status_code == 200
assert res.text == "Test"
assert res.headers["content-type"] == "text/csv; charset=utf-8"
assert res.headers["content-disposition"] == 'attachment; filename="4_validation_report.csv"'
assert res.headers["Cache-Control"] == "no-store"

sub_mock.return_value = SubmissionDAO(
id=2,
submitter=UserActionDAO(
id=1,
user_id="1234-5678-ABCD-EFGH",
user_name="Test Submitter User",
user_email="[email protected]",
action_type=UserActionType.SUBMIT,
timestamp=datetime.datetime.now(),
),
filing=1,
state=SubmissionState.VALIDATION_IN_PROGRESS,
validation_ruleset_version="v1",
submission_time=datetime.datetime.now(),
filename="file1.csv",
)
sub_mock.return_value.state = SubmissionState.VALIDATION_IN_PROGRESS

client = TestClient(app_fixture)
res = client.get("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/1/report")
res = client.get("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/4/report")
sub_mock.assert_called_with(ANY, "1234567890ZXWVUTSR00", "2024", 4)
assert res.status_code == 404

sub_mock.return_value = []
client = TestClient(app_fixture)
res = client.get("/v1/filing/institutions/1234567890ZXWVUTSR00/filings/2024/submissions/1/report")
sub_mock.assert_called_with(ANY, 1)
sub_mock.assert_called_with(ANY, "1234567890ZXWVUTSR00", "2024", 1)
assert res.status_code == 404

def test_contact_info_invalid_email(self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock):
Expand Down
8 changes: 8 additions & 0 deletions tests/entities/repos/test_submission_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ async def test_get_submission(self, query_session: AsyncSession):
assert res.state == SubmissionState.SUBMISSION_UPLOADED
assert res.validation_ruleset_version == "v1"

async def test_get_submission_by_counter(self, query_session: AsyncSession):
res = await repo.get_submission_by_counter(query_session, "ABCDEFGHIJ", "2024", 2)
assert res.id == 3
assert res.filing == 2
assert res.state == SubmissionState.SUBMISSION_UPLOADED
assert res.validation_ruleset_version == "v1"
assert res.filename == "file3.csv"

async def test_get_submissions(self, query_session: AsyncSession):
res = await repo.get_submissions(query_session)
assert len(res) == 4
Expand Down
3 changes: 2 additions & 1 deletion tests/migrations/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ def test_migrations_to_6ec12afa5b37(alembic_runner: MigrationContext, alembic_en
assert "counter" in set([c["name"] for c in inspector.get_columns("submission")])
assert counter_constraint["name"] == "unique_filing_counter"
assert set(counter_constraint["column_names"]) == set(["filing", "counter"])



def test_migrations_to_63138f5cf036(alembic_runner: MigrationContext, alembic_engine: Engine):
alembic_runner.migrate_up_to("63138f5cf036")

Expand Down

0 comments on commit d4f59f4

Please sign in to comment.