"""Tests for statement_attachment invoice 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 StatementAttachmentFailureReason from moneyhub.constants.constants import StatementAttachmentFileType from moneyhub.constants.constants import StatementAttachmentStatus from moneyhub.constants.constants import StatementAttachmentType from moneyhub.schemas.statement_attachment import StatementAttachmentDetailSchema from moneyhub.schemas.statement_attachment import StatementAttachmentInvoiceDetailSchema from tests.utils.factories import StatementAttachmentFactory @patch('moneyhub.handlers.statement_attachment_invoice.logic') def test_get_invoice_statement_attachments(mock_logic, fixture_client): """Test getting invoice statement attachments.""" account_id = 24601 payment_entity_id = 2 statement_period_id = 123 mock_logic.get_invoices_by_payment_entity.return_value = [ StatementAttachmentInvoiceDetailSchema( account_id=account_id, account_name='Best Name', contract_id=1001, statement_attachment_id=1, statement_attachment_status=StatementAttachmentStatus.IN_PROGRESS, statement_attachment_type=StatementAttachmentType.DISTRIBUTION_FEE_INVOICE, failure_reason=StatementAttachmentFailureReason.MISSING_TAX_INFORMATION, statement_period_id=statement_period_id, file_location=None, file_type=StatementAttachmentFileType.PDF, invoice_number=None, number_format=NumberFormat.US, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) ] endpoint_url = f'/invoices/statement-period/{statement_period_id}/?payment_entity_id={payment_entity_id}' # noqa: E501 result = fixture_client.get(endpoint_url) assert result.status_code == 200 assert result.json() == [ { 'account_id': account_id, 'subaccount_id': None, 'account_name': 'Best Name', 'contract_id': 1001, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF, 'number_format': NumberFormat.US.value, 'invoice_number': None, 'statement_attachment_id': 1, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE, 'failure_reason': StatementAttachmentFailureReason.MISSING_TAX_INFORMATION, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', } ] mock_logic.get_invoices_by_payment_entity.assert_called_with( payment_entity_id, statement_period_id) @patch('moneyhub.handlers.statement_attachment_invoice.logic') def test_create_invoice_statement_attachments(mock_logic, fixture_client): """Test creating invoice statement attachments.""" account_id = 24601 payment_entity_id = 2 statement_period_id = 123 correlation_id = 'abcd-1234' orchard_identity_id = 'me' mock_logic.create_invoice_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.DISTRIBUTION_FEE_INVOICE, statement_period_id=statement_period_id, file_location=None, file_type=StatementAttachmentFileType.PDF, number_format=NumberFormat.US, invoice_number=None, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) ] endpoint_url = f'/invoices/payment-entity/{payment_entity_id}/statement-period/{statement_period_id}' # noqa: E501 result = fixture_client.post( endpoint_url, headers={CORRELATION_ID_HEADER: correlation_id}) assert result.status_code == 201 for item in result.json(): assert (StatementAttachmentDetailSchema(**item) == StatementAttachmentDetailSchema( account_id=account_id, contract_id=1001, file_location=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.DISTRIBUTION_FEE_INVOICE, failure_reason=None, statement_period_id=statement_period_id, created_at='2010-09-08T07:06:05', created_by='me', )) mock_logic.create_invoice_documents.assert_called_with( payment_entity_id, statement_period_id, orchard_identity_id, correlation_id) @patch('moneyhub.handlers.statement_attachment_invoice.logic') def test_retry_invoice_statement_attachments(mock_logic, fixture_client): """Test retrying invoice statement attachments.""" account_id = 24601 payment_entity_id = 2 statement_period_id = 123 correlation_id = 'abcd-1234' mock_logic.retry_invoice_documents.return_value = [ StatementAttachmentFactory.build( account_id=account_id, contract_id=1001, statement_attachment_id=1, statement_attachment_status=StatementAttachmentStatus.RETRY, statement_attachment_type=StatementAttachmentType.DISTRIBUTION_FEE_INVOICE, statement_period_id=statement_period_id, file_location=None, number_format=NumberFormat.US, file_type=StatementAttachmentFileType.PDF, invoice_number=None, created_at=datetime(2010, 9, 8, 7, 6, 5), created_by='me', ) ] endpoint_url = f'/invoices/payment-entity/{payment_entity_id}/statement-period/{statement_period_id}/retry' # noqa: E501 result = fixture_client.post( endpoint_url, headers={CORRELATION_ID_HEADER: correlation_id}) assert result.status_code == 201 for item in result.json(): assert (StatementAttachmentDetailSchema(**item) == StatementAttachmentDetailSchema( account_id=account_id, contract_id=1001, file_location=None, file_type=StatementAttachmentFileType.PDF, invoice_number=None, number_format=NumberFormat.US, statement_attachment_id=1, statement_attachment_status=StatementAttachmentStatus.RETRY, statement_attachment_type=StatementAttachmentType.DISTRIBUTION_FEE_INVOICE, failure_reason=None, statement_period_id=statement_period_id, created_at='2010-09-08T07:06:05', created_by='me', )) mock_logic.retry_invoice_documents.assert_called_with( payment_entity_id, statement_period_id, correlation_id)