"""Functional tests for fetching statement periods.""" from oto import status as response_code import pytest from collaborator.constants.statement_period import StatementPeriodStatus from collaborator.models.ows import ows_abacus_account from tests.testutils import db, mock_auth @pytest.fixture(autouse=True) def _mock_vendor_currency(mocker): mocker.patch.object( ows_abacus_account, "get_abacus_account_metadata", return_value={"currency_code": "USD"}, ) @db.test_schema_default_seed def test_get_participations_for_collaborator_with_collaborators_profile( auth_client, mocker, mock_account ): """Test get participations for a collaborator.""" mock_auth(mocker, mock_account.id) result = auth_client.get( "/statement-period-participations", query_string={ "collaborator_id": 1, }, ) assert result.json == { "items": [ { "statement_period_id": 2, "collaborator_id": 1, "opening_balance": {"amount": -50.0, "currency": "USD"}, "closing_balance": {"amount": 1184.0, "currency": "USD"}, "revenues_total": {"amount": 1234.0, "currency": "USD"}, "expenses_total": {"amount": 0.0, "currency": "USD"}, "payments_total": {"amount": 0.0, "currency": "USD"}, }, { "statement_period_id": 1, "collaborator_id": 1, "opening_balance": {"amount": 0.0, "currency": "USD"}, "closing_balance": {"amount": -50.0, "currency": "USD"}, "revenues_total": {"amount": 100.0, "currency": "USD"}, "expenses_total": {"amount": -100.0, "currency": "USD"}, "payments_total": {"amount": -50.0, "currency": "USD"}, }, ], "pagination": {"type": "standard", "total_records": 2}, } assert result.status_code == 200 @db.test_schema_default_seed def test_get_participations_for_collaborator_with_moneyhub_profile( moneyhub_auth_client, mocker ): """Test get participations for a collaborator.""" collaborator_id = 1 mock_auth(mocker, collaborator_id) result = moneyhub_auth_client.get( "/statement-period-participations", query_string={ "collaborator_id": collaborator_id, }, ) assert result.json == { "items": [ { "statement_period_id": 1, "collaborator_id": 1, "opening_balance": {"amount": 0.0, "currency": "USD"}, "closing_balance": {"amount": -50.0, "currency": "USD"}, "revenues_total": {"amount": 100.0, "currency": "USD"}, "expenses_total": {"amount": -100.0, "currency": "USD"}, "payments_total": {"amount": -50.0, "currency": "USD"}, }, ], "pagination": {"type": "standard", "total_records": 1}, } assert result.status_code == 200 @db.test_schema_default_seed def test_get_participations_for_collaborator_with_no_periods( moneyhub_auth_client, mocker ): """Test get participations for a collaborator.""" mock_auth(mocker, 3) result = moneyhub_auth_client.get( "/statement-period-participations", query_string={ "collaborator_id": 3, }, ) assert result.json == { "items": [], "pagination": {"type": "standard", "total_records": 0}, } assert result.status_code == 200 @db.test_schema_default_seed def test_get_participations_for_statement_period(auth_client, mocker, mock_account): """Test get participations for a statement period.""" mock_auth(mocker, mock_account.id) result = auth_client.get( "/statement-period-participations", query_string={ "statement_period_id": 2, }, ) assert result.json == { "items": [ { "statement_period_id": 2, "collaborator_id": 2, "opening_balance": {"amount": 0, "currency": "USD"}, "closing_balance": {"amount": 0, "currency": "USD"}, "revenues_total": {"amount": 0, "currency": "USD"}, "expenses_total": {"amount": 0, "currency": "USD"}, "payments_total": {"amount": 0, "currency": "USD"}, }, { "statement_period_id": 2, "collaborator_id": 1, "opening_balance": {"amount": -50, "currency": "USD"}, "closing_balance": {"amount": 1184, "currency": "USD"}, "revenues_total": {"amount": 1234, "currency": "USD"}, "expenses_total": {"amount": 0, "currency": "USD"}, "payments_total": {"amount": 0, "currency": "USD"}, }, ], "pagination": {"type": "standard", "total_records": 2}, } assert result.status_code == 200 @db.test_schema_default_seed def test_get_participations_for_collaborator_and_statement_period( auth_client, mocker, mock_account ): """Test get participation for a collaborator and statement period.""" mock_auth(mocker, mock_account.id) result = auth_client.get( "/statement-period-participations", query_string={ "collaborator_id": 1, "statement_period_id": 2, }, ) assert result.json == { "items": [ { "statement_period_id": 2, "collaborator_id": 1, "opening_balance": {"amount": -50, "currency": "USD"}, "closing_balance": {"amount": 1184, "currency": "USD"}, "revenues_total": {"amount": 1234, "currency": "USD"}, "expenses_total": {"amount": 0, "currency": "USD"}, "payments_total": {"amount": 0, "currency": "USD"}, } ], "pagination": {"type": "standard", "total_records": 1}, } assert result.status_code == 200 @db.test_schema_default_seed def test_get_participations_no_collaborator_or_statement_period( auth_client, mocker, mock_account ): """Test not specifying collaborator or statement period.""" mock_auth(mocker, mock_account.id) result = auth_client.get("/statement-period-participations") assert result.status_code == 400 @db.test_schema_default_seed def test_get_participations_with_status(auth_client, mocker, mock_account): """Test filtering by status.""" mock_auth(mocker, mock_account.id) result = auth_client.get( "/statement-period-participations", query_string={ "collaborator_id": 1, # TODO: remove .value once on Python 3.12+ — str(StrEnum) returns the value directly "status": StatementPeriodStatus.CLOSED.value, }, ) assert result.json == { "items": [ { "statement_period_id": 1, "collaborator_id": 1, "opening_balance": {"amount": 0.0, "currency": "USD"}, "closing_balance": {"amount": -50.0, "currency": "USD"}, "revenues_total": {"amount": 100.0, "currency": "USD"}, "expenses_total": {"amount": -100.0, "currency": "USD"}, "payments_total": {"amount": -50.0, "currency": "USD"}, } ], "pagination": {"type": "standard", "total_records": 1}, } assert result.status_code == 200 @db.test_schema_default_seed def test_get_participations_with_limit_offset(auth_client, mocker, mock_account): """Test not specifying a limit and offset.""" mock_auth(mocker, mock_account.id) result = auth_client.get( "/statement-period-participations", query_string={ "statement_period_id": 2, "limit": 1, "offset": 1, }, ) assert result.json == { "items": [ { "statement_period_id": 2, "collaborator_id": 1, "opening_balance": {"amount": -50, "currency": "USD"}, "closing_balance": {"amount": 1184, "currency": "USD"}, "revenues_total": {"amount": 1234, "currency": "USD"}, "expenses_total": {"amount": 0, "currency": "USD"}, "payments_total": {"amount": 0, "currency": "USD"}, } ], "pagination": {"type": "standard", "total_records": 2}, } assert result.status_code == 200 @pytest.mark.parametrize( "term, expected", [ ( None, [ { "statement_period_id": 2, "collaborator_id": 2, "opening_balance": {"amount": 0, "currency": "USD"}, "closing_balance": {"amount": 0, "currency": "USD"}, "revenues_total": {"amount": 0, "currency": "USD"}, "expenses_total": {"amount": 0, "currency": "USD"}, "payments_total": {"amount": 0, "currency": "USD"}, }, { "statement_period_id": 2, "collaborator_id": 1, "opening_balance": {"amount": -50, "currency": "USD"}, "closing_balance": {"amount": 1184, "currency": "USD"}, "revenues_total": {"amount": 1234, "currency": "USD"}, "expenses_total": {"amount": 0, "currency": "USD"}, "payments_total": {"amount": 0, "currency": "USD"}, }, ], ), ( "another", [ { "statement_period_id": 2, "collaborator_id": 2, "opening_balance": {"amount": 0, "currency": "USD"}, "closing_balance": {"amount": 0, "currency": "USD"}, "revenues_total": {"amount": 0, "currency": "USD"}, "expenses_total": {"amount": 0, "currency": "USD"}, "payments_total": {"amount": 0, "currency": "USD"}, }, ], ), ], ) @db.test_schema_default_seed def test_get_participations_with_term( auth_client, mocker, mock_account, term, expected ): """Test get participations for a statement period by term.""" mock_auth(mocker, mock_account.id) result = auth_client.get( "/statement-period-participations", query_string={ "statement_period_id": 2, "term": term, }, ) assert result.json == { "items": expected, "pagination": {"type": "standard", "total_records": len(expected)}, } assert result.status_code == 200 def test_get_statement_periods_error(auth_client, mock_account): """Test get statement period failure case.""" result = auth_client.get( "/statement-period-participations", query_string={"vendor_id": mock_account.id, "statement_period_id": 2}, headers=[ ("orchard-identity-id", "7ec441df-7b21-4a71-a9da"), ("orchard-profile-id", "90003"), ("orchard-profile-type", "BogusProfile"), ], ) assert result.status_code == response_code.FORBIDDEN @db.test_schema_default_seed def test_get_statement_periods_no_access(auth_client, mock_account, mocker): """Test getting participations for an unauthorized collaborator.""" mock_auth(mocker, 0) result = auth_client.get( "/statement-period-participations", query_string={"collaborator_id": 3} ) assert result.status_code == response_code.FORBIDDEN