"""Tests for the report handlers.""" from collections import namedtuple from types import SimpleNamespace from unittest.mock import patch from collaborator.constants.header import ABACUS_PROFILE from tests.testutils import mock_auth @patch("collaborator.logic.reports.get_reports") def test_get_reports(mock_get_reports, mock_account, auth_client, mocker): """Test getting reports with no parameters.""" mock_auth(mocker, mock_account.id) mock_get_reports.return_value = SimpleNamespace(message="success") result = auth_client.get("/reports", query_string={"vendor_id": mock_account.id}) assert result.status_code == 200 mock_get_reports.assert_called_with( account=mock_account, sort_direction="ASC", limit=10, offset=0, ) @patch("collaborator.logic.reports.get_reports") def test_get_reports_params(mock_get_reports, mock_account, auth_client, mocker): """Test getting reports with query parameters.""" mock_auth(mocker, mock_account.id) mock_get_reports.return_value = SimpleNamespace(message="success") query_string = { "has_transaction": True, "status": "GENERATED", "sort_key": "amount", "sort_direction": "DESC", "limit": 20, "offset": 100, "report_run_uuid": "64cd5dbb-a384-416f-a1ce-cf583c0971d0", } result = auth_client.get( "/reports", query_string={**query_string, "vendor_id": mock_account.id} ) assert result.status_code == 200 mock_get_reports.assert_called_with(account=mock_account, **query_string) @patch("collaborator.logic.reports.get_reports") def test_get_reports_abacus_profile_with_report_run_uuid_only( mock_get_reports, make_auth_client, mocker ): """Test getting reports with AbacusProfile filtering by collaborator_dp_enabled.""" abacus_client = make_auth_client(ABACUS_PROFILE) mock_get_reports.return_value = SimpleNamespace(message="success") result = abacus_client.get( "/reports", query_string={ "report_run_uuid": "64cd5dbb-a384-416f-a1ce-cf583c0971d0", }, ) assert result.status_code == 200 mock_get_reports.assert_called_with( account=None, report_run_uuid="64cd5dbb-a384-416f-a1ce-cf583c0971d0", sort_direction="ASC", limit=10, offset=0, ) def test_get_reports_non_abacus_profile_with_report_run_uuid_only(auth_client, mocker): """Test getting reports with non-AbacusProfile and no vendor_id or collaborator_id.""" # auth_client uses CollaboratorsProfile by default # Need to mock auth even though we expect this to fail validation mock_auth(mocker, 12345) result = auth_client.get( "/reports", query_string={ "report_run_uuid": "64cd5dbb-a384-416f-a1ce-cf583c0971d0", }, ) assert result.status_code == 400 assert result.json["code"] == "missing_params" MockAggregation = namedtuple( "MockAggregation", ["total_count", "currency_agnostic_total_amount"] ) @patch("collaborator.logic.reports.get_report_contract_subtotal_aggregations") def test_get_report_contract_subtotal_aggregations( mock_get_aggregations, auth_client, mocker ): """Test getting report contract subtotal aggregations.""" mock_get_aggregations.return_value = MockAggregation( total_count=3, currency_agnostic_total_amount=750.25 ) result = auth_client.get( "/reports/contract-subtotal-aggregations", query_string={"report_run_id": 1, "collaborator_dp_enabled": True}, ) assert result.status_code == 200 assert result.json == { "total_count": 3, "currency_agnostic_total_amount": 750.25, } mock_get_aggregations.assert_called_with( report_run_id=1, collaborator_dp_enabled=True )