"""Tests for statement_attachment handlers.""" from datetime import datetime from unittest.mock import patch from moneyhub.constants.constants import CORRELATION_ID_HEADER from moneyhub.constants.constants import NumberFormat from moneyhub.constants.constants import StatementAttachmentFileType from moneyhub.constants.constants import StatementAttachmentStatus from moneyhub.constants.constants import StatementAttachmentType from moneyhub.constants.error import MISSING_PROFILE_HEADERS from moneyhub.schemas.statement_attachment import InternalAttachmentPostSchema from tests.utils.factories import StatementAttachmentFactory @patch('moneyhub.handlers.statement_attachment.logic') def test_get_statement_attachment(mock_logic, fixture_client): """Test getting a statement attachments by ID.""" statement_attachment_id = 1234 mock_logic.get_statement_attachment_by_id.return_value = StatementAttachmentFactory.build( statement_attachment_id=statement_attachment_id, created_at='2010-09-08T07:06:05') result = fixture_client.get(f'/statement-attachment/{statement_attachment_id}') assert result.status_code == 200 assert result.json() == { 'account_id': 1, 'subaccount_id': None, 'contract_id': 1, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.CSV, 'invoice_number': '1234', 'number_format': NumberFormat.US, 'statement_attachment_id': statement_attachment_id, 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': 1, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', 'filters': None, } mock_logic.get_statement_attachment_by_id.assert_called_with( statement_attachment_id, 'MoneyhubProfile', 1111) @patch('moneyhub.handlers.statement_attachment.logic') def test_get_statement_attachments(mock_logic, fixture_client): """Get statement attachments by an account_id and statement period id.""" account_id = 1 statement_period_id = 1 contract_id = 123 mock_logic.get_statement_attachments_by_account_and_statement_periods.return_value = [ StatementAttachmentFactory.build( account_id=account_id, contract_id=123, statement_attachment_id=111, statement_attachment_status='in_progress', statement_attachment_type='distribution_fee_invoice', statement_period_id=statement_period_id, file_location=None, file_type=StatementAttachmentFileType.PDF, number_format=NumberFormat.US, invoice_number='6575765765', created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) ] endpoint_url = (f'/statement-attachment/account/{account_id}/statement-period/' f'{statement_period_id}?contract_id={contract_id}') result = fixture_client.get(endpoint_url) assert result.status_code == 200 assert result.json() == [ { 'account_id': 1, 'subaccount_id': None, 'contract_id': contract_id, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF, 'invoice_number': '6575765765', 'number_format': NumberFormat.US, 'statement_attachment_id': 111, 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': 1, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', 'filters': None, } ] mock_logic.get_statement_attachments_by_account_and_statement_periods.assert_called_with( account_id, [statement_period_id], contract_id, None ) @patch('moneyhub.handlers.statement_attachment.logic') def test_get_statement_attachments_subaccount(mock_logic, fixture_client): """Get subaccountstatement attachments by an account_id and statement period id.""" account_id = 1 statement_period_id = 1 subaccount_id = 124 contract_id = 123 mock_logic.get_statement_attachments_by_account_and_statement_periods.return_value = [ StatementAttachmentFactory.build( account_id=account_id, subaccount_id=subaccount_id, contract_id=123, statement_attachment_id=111, statement_attachment_status='in_progress', statement_attachment_type='distribution_fee_invoice', statement_period_id=statement_period_id, file_location=None, file_type=StatementAttachmentFileType.PDF, number_format=NumberFormat.US, invoice_number='6575765765', created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) ] endpoint_url = (f'/statement-attachment/account/{account_id}/statement-period/' f'{statement_period_id}?contract_id={contract_id}' f'&subaccount_id={subaccount_id}') result = fixture_client.get(endpoint_url) assert result.status_code == 200 assert result.json() == [ { 'account_id': 1, 'subaccount_id': subaccount_id, 'contract_id': contract_id, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF, 'invoice_number': '6575765765', 'number_format': NumberFormat.US, 'statement_attachment_id': 111, 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': 1, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', 'filters': None, } ] mock_logic.get_statement_attachments_by_account_and_statement_periods.assert_called_with( account_id, [statement_period_id], contract_id, subaccount_id ) @patch('moneyhub.handlers.statement_attachment.logic') def test_get_statement_attachments_with_no_statement_period_parameter(mock_logic, fixture_client): """Get statement attachments by an account_id and statement period id.""" account_id = 1 contract_id = 123 mock_logic.get_statement_attachments_by_account_and_statement_periods.return_value = [ StatementAttachmentFactory.build( account_id=account_id, contract_id=123, statement_attachment_id=111, statement_attachment_status='in_progress', statement_attachment_type='distribution_fee_invoice', statement_period_id=111, file_location=None, file_type=StatementAttachmentFileType.PDF, invoice_number='6575765765', number_format=NumberFormat.US, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ), StatementAttachmentFactory.build( account_id=account_id, contract_id=123, statement_attachment_id=112, statement_attachment_status='in_progress', statement_attachment_type='distribution_fee_invoice', statement_period_id=112, file_location=None, file_type=StatementAttachmentFileType.PDF, invoice_number='6575765765', number_format=NumberFormat.US, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) ] endpoint_url = f'/statement-attachment/account/{account_id}?contract_id={contract_id}' result = fixture_client.get(endpoint_url) print(result.text) assert result.status_code == 200 assert result.json() == [ { 'account_id': 1, 'subaccount_id': None, 'contract_id': contract_id, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF, 'invoice_number': '6575765765', 'number_format': NumberFormat.US, 'statement_attachment_id': 111, 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': 111, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', 'filters': None, }, { 'account_id': 1, 'subaccount_id': None, 'contract_id': contract_id, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF, 'invoice_number': '6575765765', 'number_format': NumberFormat.US, 'statement_attachment_id': 112, 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': 112, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', 'filters': None, } ] mock_logic.get_statement_attachments_by_account_and_statement_periods.assert_called_with( account_id, None, contract_id, None ) @patch('moneyhub.handlers.statement_attachment.logic') def test_create_revenue_detail_reports(mock_logic, fixture_client): """Test creating revenue detail reports.""" account_id = 123 statement_period_id = 456 correlation_id = 'abc' orchard_identity_id = 'me' mock_logic.create_revenue_detail_reports.return_value = [ StatementAttachmentFactory.build( account_id=account_id, contract_id=123, statement_attachment_id=111, statement_attachment_status='in_progress', statement_attachment_type='revenue_detail', statement_period_id=statement_period_id, file_location=None, invoice_number=None, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) ] endpoint_url = (f'/statement-attachment/account/{account_id}/statement-period/' f'{statement_period_id}/revenue-detail') result = fixture_client.post( endpoint_url, headers={CORRELATION_ID_HEADER: correlation_id}) assert result.status_code == 200 assert result.json() == [ { 'account_id': account_id, 'subaccount_id': None, 'contract_id': 123, 'statement_attachment_id': 111, 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'revenue_detail', 'failure_reason': None, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.CSV, 'invoice_number': None, 'number_format': NumberFormat.US, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', } ] mock_logic.create_revenue_detail_reports.assert_called_with( account_id, statement_period_id, orchard_identity_id, correlation_id, None, None) @patch('moneyhub.handlers.statement_attachment.logic') def test_create_revenue_detail_reports_subaccount(mock_logic, fixture_client): """Test creating subaccount revenue detail reports.""" account_id = 123 subaccount_id = 12344 statement_period_id = 456 correlation_id = 'abc' orchard_identity_id = 'me' mock_logic.create_revenue_detail_reports.return_value = [ StatementAttachmentFactory.build( account_id=account_id, subaccount_id=subaccount_id, contract_id=123, statement_attachment_id=111, statement_attachment_status='in_progress', statement_attachment_type='revenue_detail', statement_period_id=statement_period_id, file_location=None, invoice_number=None, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) ] endpoint_url = (f'/statement-attachment/account/{account_id}/statement-period/' f'{statement_period_id}/revenue-detail?&subaccount_id={subaccount_id}') result = fixture_client.post( endpoint_url, headers={CORRELATION_ID_HEADER: correlation_id}) assert result.status_code == 200 assert result.json() == [ { 'account_id': account_id, 'subaccount_id': subaccount_id, 'contract_id': 123, 'statement_attachment_id': 111, 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'revenue_detail', 'failure_reason': None, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.CSV, 'invoice_number': None, 'number_format': NumberFormat.US, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', } ] mock_logic.create_revenue_detail_reports.assert_called_with( account_id, statement_period_id, orchard_identity_id, correlation_id, None, subaccount_id) @patch('moneyhub.handlers.statement_attachment.logic') def test_create_statement_period_collection_summary_documents(mock_logic, fixture_client): """Test creating collection summary documents.""" account_id = 24601 statement_period_id = 123 correlation_id = 'abcd-1234' orchard_identity_id = 'me' mock_logic.create_collection_summary_documents.return_value = [ StatementAttachmentFactory.build( account_id=account_id, contract_id=1001, statement_attachment_id=1, statement_attachment_status=StatementAttachmentStatus.IN_PROGRESS, statement_attachment_type=StatementAttachmentType.COLLECTION_SUMMARY_PERFORMER, statement_period_id=statement_period_id, file_location=None, file_type=StatementAttachmentFileType.PDF, invoice_number=None, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) ] endpoint_url = f'/statement-attachment/account/{account_id}/statement-period/{statement_period_id}/collection-summary' # noqa: E501 result = fixture_client.post( endpoint_url, headers={CORRELATION_ID_HEADER: correlation_id}) assert result.status_code == 201 assert result.json() == [ { 'account_id': account_id, 'subaccount_id': None, 'contract_id': 1001, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF, 'invoice_number': None, 'number_format': NumberFormat.US, 'statement_attachment_id': 1, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS, 'statement_attachment_type': StatementAttachmentType.COLLECTION_SUMMARY_PERFORMER, 'failure_reason': None, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', } ] mock_logic.create_collection_summary_documents.assert_called_with( account_id, statement_period_id, orchard_identity_id, correlation_id) @patch('moneyhub.handlers.statement_attachment.logic') def test_create_legacy_revenue_report(mock_logic, fixture_client): """Test creating legacy reports.""" account_id = 24601 statement_period_id = 123 statement_period_ids = [123] statement_period_ids_str = str(statement_period_id) report_type = StatementAttachmentType.LEGACY_REVENUE_DETAIL_PHYSICAL file_type = StatementAttachmentFileType.TXT filters = { 'transaction_type_ids': [1, 2, 3], 'transaction_type_group_ids': [4, 5, 6], 'exclude_transaction_type_group_ids': None, 'exclude_transaction_type_ids': None, 'variant': 'physical' } number_format = 'us' correlation_id = 'abcd-1234' orchard_identity_id = 'me' mock_logic.create_legacy_revenue_report.return_value = [ StatementAttachmentFactory.build( account_id=account_id, contract_id=1001, statement_attachment_id=1, statement_attachment_status=StatementAttachmentStatus.IN_PROGRESS, statement_attachment_type=report_type, statement_period_id=statement_period_id, statement_period_ids=statement_period_ids_str, file_location=None, file_type=file_type, number_format=number_format, invoice_number=None, created_at=datetime(2010, 9, 8, 7, 6, 5), filters=filters, created_by='me', ) ] params = { 'file_type': file_type.value, 'number_format': number_format, 'statement_period_ids': statement_period_ids, 'report_type': report_type.value, } endpoint_url = ( f'/statement-attachment/account/{account_id}/statement-period/{statement_period_id}/legacy-revenue-report' # noqa: E501 ) result = fixture_client.post( endpoint_url, headers={CORRELATION_ID_HEADER: correlation_id}, params=params, json=filters, ) assert result.status_code == 201 assert result.json() == [ { 'account_id': account_id, 'subaccount_id': None, 'contract_id': 1001, 'file_location': None, 'displayed_file_name': None, 'file_type': file_type, 'number_format': number_format, 'invoice_number': None, 'statement_attachment_id': 1, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': report_type, 'failure_reason': None, 'filters': filters, 'statement_period_id': statement_period_id, 'statement_period_ids': statement_period_ids_str, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', } ] mock_logic.create_legacy_revenue_report.assert_called_with( account_id, None, statement_period_id, orchard_identity_id, report_type, file_type, number_format, correlation_id, None, filters, statement_period_ids) @patch('moneyhub.handlers.statement_attachment.logic') def test_create_legacy_revenue_report_subaccount(mock_logic, fixture_client): """Test creating legacy reports for a subaccount.""" account_id = 24601 statement_period_id = 123 statement_period_ids = [123, 456] statement_period_ids_str = '123, 456' subaccount_id = 1234 report_type = StatementAttachmentType.LEGACY_REVENUE_DETAIL_PHYSICAL file_type = StatementAttachmentFileType.TXT number_format = 'us' correlation_id = 'abcd-1234' orchard_identity_id = 'me' mock_logic.create_legacy_revenue_report.return_value = [ StatementAttachmentFactory.build( account_id=account_id, subaccount_id=subaccount_id, contract_id=1001, statement_attachment_id=1, statement_attachment_status=StatementAttachmentStatus.IN_PROGRESS, statement_attachment_type=report_type, statement_period_id=statement_period_id, statement_period_ids=statement_period_ids_str, file_location=None, file_type=file_type, number_format=number_format, invoice_number=None, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) ] params = { 'file_type': file_type.value, 'number_format': number_format, 'statement_period_ids': statement_period_ids, 'report_type': report_type.value, 'subaccount_id': subaccount_id } endpoint_url = ( f'/statement-attachment/account/{account_id}/statement-period/{statement_period_id}/legacy-revenue-report' # noqa: E501 ) result = fixture_client.post( endpoint_url, headers={CORRELATION_ID_HEADER: correlation_id}, params=params ) assert result.status_code == 201 assert result.json() == [ { 'account_id': account_id, 'subaccount_id': subaccount_id, 'contract_id': 1001, 'file_location': None, 'displayed_file_name': None, 'file_type': file_type, 'number_format': number_format, 'invoice_number': None, 'statement_attachment_id': 1, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS, 'statement_attachment_type': report_type, 'failure_reason': None, 'statement_period_id': statement_period_id, 'statement_period_ids': statement_period_ids_str, 'filters': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', } ] mock_logic.create_legacy_revenue_report.assert_called_with( account_id, None, statement_period_id, orchard_identity_id, report_type, file_type, number_format, correlation_id, subaccount_id, None, statement_period_ids) @patch('moneyhub.handlers.statement_attachment.logic') def test_create_statement_attachments(mock_logic, fixture_client): """Test creating multiple statement attachments.""" statement_period_id = 1 orchard_identity_id = 'me' mock_logic.create_statement_attachments.return_value = [ StatementAttachmentFactory.build( account_id=1, contract_id=123, statement_attachment_id=1, statement_attachment_status='in_progress', statement_attachment_type='distribution_fee_invoice', statement_period_id=statement_period_id, file_location=None, file_type=StatementAttachmentFileType.PDF, invoice_number='1987_2021_0000000001', number_format=NumberFormat.US, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) ] endpoint_url = f'/statement-attachment/statement-period/{statement_period_id}' result = fixture_client.post(endpoint_url) assert result.status_code == 201 assert result.json() == [ { 'account_id': 1, 'subaccount_id': None, 'contract_id': 123, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF, 'invoice_number': '1987_2021_0000000001', 'number_format': NumberFormat.US, 'statement_attachment_id': 1, 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', 'filters': None, } ] mock_logic.create_statement_attachments.assert_called_with( statement_period_id, orchard_identity_id) @patch('moneyhub.handlers.statement_attachment.logic') def test_create_internal_attachment(mock_logic, fixture_client): """Test creating internal statement attachment.""" statement_period_id = 1 account_id = 1 orchard_identity_id = 'me' file_location = 'https://fakefile.pdf' file_type = StatementAttachmentFileType.PDF upload_date = datetime(2010, 9, 8, 7, 6, 5) payload = InternalAttachmentPostSchema( file_type=file_type, file_location=file_location, upload_date=upload_date ) mock_logic.create_internal_attachment.return_value = StatementAttachmentFactory.build( account_id=account_id, contract_id=123, statement_attachment_id=1, statement_attachment_status=StatementAttachmentStatus.COMPLETE, statement_attachment_type=StatementAttachmentType.INTERNAL_UPLOAD, statement_period_id=statement_period_id, file_location=file_location, file_type=file_type, invoice_number=None, created_at=upload_date, created_by='me', ) endpoint_url = (f'/statement-attachment/account/{account_id}/statement-period' f'/{statement_period_id}/internal-attachment') result = fixture_client.post( endpoint_url, content=payload.model_dump_json(), headers={'Content-Type': 'application/json'}) assert result.status_code == 201 assert result.json() == { 'account_id': 1, 'subaccount_id': None, 'contract_id': 123, 'file_location': file_location, 'displayed_file_name': None, 'file_type': file_type, 'invoice_number': None, 'statement_attachment_id': 1, 'statement_attachment_status': 'complete', 'statement_attachment_type': 'internal_upload', 'failure_reason': None, 'number_format': NumberFormat.US, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', } mock_logic.create_internal_attachment.assert_called_with( account_id, statement_period_id, file_type, file_location, orchard_identity_id, upload_date, None) @patch('moneyhub.handlers.statement_attachment.logic') def test_create_internal_attachment_already_existing(mock_logic, fixture_client): """Test creating internal statement attachment that already exists.""" statement_period_id = 1 account_id = 1 orchard_identity_id = 'me' file_location = 'https://fakefile.pdf' file_type = StatementAttachmentFileType.PDF upload_date = datetime(2010, 9, 8, 7, 6, 5) payload = InternalAttachmentPostSchema( file_type=file_type, file_location=file_location, upload_date=upload_date ) mock_logic.create_internal_attachment.return_value = None endpoint_url = (f'/statement-attachment/account/{account_id}/statement-period' f'/{statement_period_id}/internal-attachment') result = fixture_client.post( endpoint_url, content=payload.model_dump_json(), headers={'Content-Type': 'application/json'}) assert result.status_code == 201 assert not result.json() mock_logic.create_internal_attachment.assert_called_with( account_id, statement_period_id, file_type, file_location, orchard_identity_id, upload_date, None) @patch('moneyhub.handlers.statement_attachment.logic') def test_regenerate_statement_attachments(mock_logic, fixture_client): """Test creating multiple statement attachments.""" statement_period_id = 123 account_id = 24601 correlation_id = 'abcd' mock_logic.regenerate_statement_attachments.return_value = [ StatementAttachmentFactory.build( account_id=1, contract_id=123, statement_attachment_id=1, statement_attachment_status='in_progress', statement_attachment_type='distribution_fee_invoice', statement_period_id=statement_period_id, file_location=None, file_type=StatementAttachmentFileType.PDF, invoice_number='1987_2021_0000000001', number_format=NumberFormat.US, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ), ] url = f'/statement-attachment/statement-period/{statement_period_id}/regenerate' body = { 'account_id': account_id, 'statement_attachment_type': StatementAttachmentType.REVENUE_DETAIL, } result = fixture_client.post(url, json=body, headers={CORRELATION_ID_HEADER: correlation_id}) assert result.status_code == 200 assert result.json() == [ { 'account_id': 1, 'subaccount_id': None, 'contract_id': 123, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF, 'invoice_number': '1987_2021_0000000001', 'number_format': NumberFormat.US, 'statement_attachment_id': 1, 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', } ] mock_logic.regenerate_statement_attachments.assert_called_with( statement_period_id, account_id, StatementAttachmentType.REVENUE_DETAIL, correlation_id) @patch('moneyhub.handlers.statement_attachment.logic') def test_delete_statement_attachment(mock_logic, fixture_client): """Test deleting a statement attachment.""" statement_period_id = 123 url = f'/statement-attachment/{statement_period_id}' result = fixture_client.delete(url) assert result.status_code == 204 mock_logic.delete_statement_attachment.assert_called_once_with(statement_period_id) @patch('moneyhub.handlers.statement_attachment.logic') def test_update_statement_attachment(mock_logic, fixture_client): """Test to update statement attachments by statement attachment id.""" statement_attachment_id = 123 put_body = {'statement_attachment_status': StatementAttachmentStatus.COMPLETE} mock_logic.update_statement_attachment.return_value = StatementAttachmentFactory.build( # noqa: E501 account_id=1, contract_id=123, statement_attachment_id=statement_attachment_id, statement_attachment_status='complete', statement_attachment_type='distribution_fee_invoice', statement_period_id=1, file_location=None, file_type=StatementAttachmentFileType.PDF, invoice_number='1987_2021_0000000001', number_format=NumberFormat.US, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) endpoint_url = f'/statement-attachment/{statement_attachment_id}' result = fixture_client.put(endpoint_url, json=put_body) assert result.status_code == 200 assert result.json() == { 'account_id': 1, 'subaccount_id': None, 'contract_id': 123, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF, 'invoice_number': '1987_2021_0000000001', 'number_format': NumberFormat.US, 'statement_attachment_id': statement_attachment_id, 'statement_attachment_status': 'complete', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': 1, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', } mock_logic.update_statement_attachment.assert_called_with( statement_attachment_id, **put_body ) @patch('moneyhub.handlers.statement_attachment.logic') def test_generate_statement_attachments(mock_logic, fixture_client): """Generate statement attachments by statement_period_id.""" statement_period_id = 1 correlation_id = '123' expected_result = { 'status': 'OK', 'messages_sent': 123, } mock_logic.trigger_statement_attachment_generation.return_value = expected_result endpoint_url = f'/statement-attachment/statement-period/{statement_period_id}/generate' result = fixture_client.post( endpoint_url, headers={CORRELATION_ID_HEADER: correlation_id} ) assert result.status_code == 201 assert result.json() == expected_result mock_logic.trigger_statement_attachment_generation.assert_called_with( statement_period_id, None, correlation_id ) @patch('moneyhub.handlers.statement_attachment.logic') def test_get_invoice_presigned_url( mock_logic, fixture_client, request_engine ): """Test getting invoice's presigned url to download.""" profile_id = '98787' profile_type = 'MoneyhubProfile' statement_attachment_id = 1 mock_url = 's3://invoice-files/2021/invoice_1.pdf' mock_logic.get_invoice_presigned_url.return_value = mock_url endpoint_url = f'/statement-attachment/{statement_attachment_id}/download' result = fixture_client.get( endpoint_url, headers={ 'orchard-profile-id': profile_id, 'orchard-profile-type': profile_type } ) assert result.status_code == 200 assert result.json() == {'url': 's3://invoice-files/2021/invoice_1.pdf'} mock_logic.get_invoice_presigned_url.assert_called_with( statement_attachment_id, profile_type, int(profile_id) ) @patch('moneyhub.handlers.statement_attachment.logic') def test_get_invoice_presigned_url_missing_headers( mock_logic, fixture_client, request_engine ): """Test getting invoice's presigned url to download when missing headers.""" statement_attachment_id = 1 result = fixture_client.get( f'/statement-attachment/{statement_attachment_id}/download') assert result.status_code == 400 assert result.json() == {'detail': MISSING_PROFILE_HEADERS} mock_logic.get_invoice_presigned_url.assert_not_called()