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

142 all filings endpoint #341

Merged
merged 11 commits into from
Oct 11, 2024
8 changes: 8 additions & 0 deletions src/sbl_filing_api/entities/repos/submission_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ async def get_filing(session: AsyncSession, lei: str, filing_period: str) -> Fil
return result[0] if result else None


async def get_filings(session: AsyncSession, leis: list[str], filing_period: str) -> list[FilingDAO]:
stmt = select(FilingDAO).filter(FilingDAO.lei.in_(leis), FilingDAO.filing_period == filing_period)
result = (await session.scalars(stmt)).all()
if result:
result = await populate_missing_tasks(session, result)
return result if result else []


async def get_period_filings(session: AsyncSession, filing_period: str) -> List[FilingDAO]:
filings = await query_helper(session, FilingDAO, filing_period=filing_period)
if filings:
Expand Down
9 changes: 9 additions & 0 deletions src/sbl_filing_api/routers/filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from multiprocessing import Manager
from regtech_api_commons.api.router_wrapper import Router
from regtech_api_commons.api.exceptions import RegTechHttpException
from regtech_api_commons.models.auth import AuthenticatedUser

from sbl_filing_api.entities.models.model_enums import UserActionType
from sbl_filing_api.services import submission_processor
from sbl_filing_api.services.multithread_handler import handle_submission
Expand Down Expand Up @@ -58,6 +60,13 @@ async def get_filing(request: Request, response: Response, lei: str, period_code
response.status_code = status.HTTP_204_NO_CONTENT


@router.get("/periods/{period_code}/filings", response_model=List[FilingDTO])
@requires("authenticated")
async def get_filings(request: Request, period_code: str):
user: AuthenticatedUser = request.user
return await repo.get_filings(request.state.db_session, user.institutions, period_code)


@router.post("/institutions/{lei}/filings/{period_code}", response_model=FilingDTO)
@requires("authenticated")
async def post_filing(request: Request, lei: str, period_code: str):
Expand Down
92 changes: 92 additions & 0 deletions tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,98 @@ def get_filing_mock(mocker: MockerFixture) -> Mock:
return mock


@pytest.fixture
def get_filings_mock(mocker: MockerFixture) -> Mock:
mock = mocker.patch("sbl_filing_api.entities.repos.submission_repo.get_filings")
mock.return_value = [
FilingDAO(
id=1,
lei="1234567890ABCDEFGH00",
filing_period="2024",
institution_snapshot_id="v1",
contact_info=ContactInfoDAO(
id=1,
filing=1,
first_name="test_first_name_1",
last_name="test_last_name_1",
hq_address_street_1="address street 1",
hq_address_street_2="address street 2",
hq_address_city="Test City",
hq_address_state="TS",
hq_address_zip="12345",
phone_number="112-345-6789",
email="[email protected]",
),
creator_id=1,
creator=UserActionDAO(
id=1,
user_id="123456-7890-ABCDEF-GHIJ",
user_name="test submitter",
user_email="[email protected]",
action_type=UserActionType.SUBMIT,
timestamp=datetime.now(),
),
),
FilingDAO(
id=1,
lei="1234567890ABCDEFGH01",
filing_period="2024",
institution_snapshot_id="v1",
contact_info=ContactInfoDAO(
id=1,
filing=1,
first_name="test_first_name_1",
last_name="test_last_name_1",
hq_address_street_1="address street 1",
hq_address_street_2="address street 2",
hq_address_city="Test City",
hq_address_state="TS",
hq_address_zip="12345",
phone_number="112-345-6789",
email="[email protected]",
),
creator_id=1,
creator=UserActionDAO(
id=1,
user_id="123456-7890-ABCDEF-GHIJ",
user_name="test submitter",
user_email="[email protected]",
action_type=UserActionType.SUBMIT,
timestamp=datetime.now(),
),
),
FilingDAO(
id=1,
lei="1234567890ZXWVUTSR00",
filing_period="2024",
institution_snapshot_id="v1",
contact_info=ContactInfoDAO(
id=1,
filing=1,
first_name="test_first_name_1",
last_name="test_last_name_1",
hq_address_street_1="address street 1",
hq_address_street_2="address street 2",
hq_address_city="Test City",
hq_address_state="TS",
hq_address_zip="12345",
phone_number="112-345-6789",
email="[email protected]",
),
creator_id=1,
creator=UserActionDAO(
id=1,
user_id="123456-7890-ABCDEF-GHIJ",
user_name="test submitter",
user_email="[email protected]",
action_type=UserActionType.SUBMIT,
timestamp=datetime.now(),
),
),
]
return mock


@pytest.fixture
def post_filing_mock(mocker: MockerFixture) -> Mock:
mock = mocker.patch("sbl_filing_api.entities.repos.submission_repo.create_new_filing")
Expand Down
18 changes: 18 additions & 0 deletions tests/api/routers/test_filing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ def test_get_filing(self, app_fixture: FastAPI, get_filing_mock: Mock, authed_us
res = client.get("/v1/filing/institutions/1234567890ABCDEFGH00/filings/2024/")
assert res.status_code == 204

def test_unauthed_get_filings(self, app_fixture: FastAPI, get_filing_mock: Mock):
client = TestClient(app_fixture)
res = client.get("/v1/filing/periods/2024/filings")
assert res.status_code == 403

def test_get_filings(self, app_fixture: FastAPI, get_filings_mock: Mock, authed_user_mock: Mock):
client = TestClient(app_fixture)
res = client.get("/v1/filing/periods/2024/filings")
leis = ["1234567890ABCDEFGH00", "1234567890ABCDEFGH01", "1234567890ZXWVUTSR00"]
get_filings_mock.assert_called_with(ANY, leis, "2024")
assert res.status_code == 200
for i in range(len(res.json())):
assert res.json()[i]["lei"] == leis[i]

get_filings_mock.return_value = []
res = client.get("/v1/filing/periods/2024/filings")
assert res.json() == []

def test_unauthed_post_filing(self, app_fixture: FastAPI):
client = TestClient(app_fixture)
res = client.post("/v1/filing/institutions/ZXWVUTSRQP/filings/2024/")
Expand Down
33 changes: 33 additions & 0 deletions tests/entities/repos/test_submission_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,39 @@ async def test_get_filing(self, query_session: AsyncSession, mocker: MockerFixtu
tasks_populated_filings.append(filings[0].id)
assert set(tasks_populated_filings) == set([1, 2])

async def test_get_filings(self, query_session: AsyncSession, mocker: MockerFixture):
spy_populate_missing_tasks = mocker.patch(
"sbl_filing_api.entities.repos.submission_repo.populate_missing_tasks", wraps=repo.populate_missing_tasks
)
res = await repo.get_filings(query_session, leis=["1234567890", "ABCDEFGHIJ"], filing_period="2024")
assert res[0].id == 1
assert res[0].filing_period == "2024"
assert res[0].lei == "1234567890"
assert len(res[0].tasks) == 2
assert FilingTaskState.NOT_STARTED in set([t.state for t in res[0].tasks])
tasks1 = set([task_progress.task for task_progress in res[0].tasks])
assert len(tasks1) == 2
assert "Task-1" in set([task.name for task in tasks1])
assert "Task-2" in set([task.name for task in tasks1])

assert res[1].id == 2
assert res[1].filing_period == "2024"
assert res[1].lei == "ABCDEFGHIJ"
assert len(res[1].tasks) == 2
assert FilingTaskState.NOT_STARTED in set([t.state for t in res[1].tasks])
tasks2 = set([task_progress.task for task_progress in res[1].tasks])
assert len(tasks2) == 2
assert "Task-1" in set([task.name for task in tasks2])
assert "Task-2" in set([task.name for task in tasks2])

tasks_populated_filings = []
for call in spy_populate_missing_tasks.call_args_list:
args, _ = call
filings = args[1]
assert all([isinstance(f, FilingDAO) for f in filings])
tasks_populated_filings.extend([f.id for f in filings])
assert set(tasks_populated_filings) == set([1, 2])

async def test_get_period_filings(self, query_session: AsyncSession, mocker: MockerFixture):
results = await repo.get_period_filings(query_session, filing_period="2024")
assert len(results) == 3
Expand Down
Loading