"""Unit tests for WorksheetPaymentCustom API.""" from decimal import Decimal from unittest.mock import patch from tests.utils.factories import WorksheetPaymentCustomFactory @patch('payment.logic.worksheet_payment_custom.WorksheetPaymentCustom') def test_create_worksheet_payment_custom_handler( mock_model, fixture_client, fresh_db, mock_accounts, mock_contracts, mock_statement_periods, ): """Test create_worksheet_payment_custom handler.""" test_data = { 'account_id': 1, 'contract_id': 1, 'currency_code': 'USD', 'amount': '100.00', 'withholding_tax_amount': '-10.00', 'withholding_tax_rate': '5.00', 'vat_amount': '20.00', 'vat_rate': '10.00', 'amount_after_withholding_and_vat': '110.00', 'activity_statement_period_id': 1, 'statement_period_id': 1, 'payment_name': 'test custom payment', } worksheet = WorksheetPaymentCustomFactory.create() mock_model.create.return_value = worksheet res = fixture_client.post('/payment/custom', json=test_data) assert res.status_code == 201 assert mock_model.create.called call_kwargs = mock_model.create.call_args[1] assert call_kwargs['account_id'] == 1 assert call_kwargs['contract_id'] == 1 assert call_kwargs['currency_code'] == 'USD' assert call_kwargs['amount'] == Decimal('100.00') assert call_kwargs['withholding_tax_amount'] == Decimal('-10.00') assert call_kwargs['vat_amount'] == Decimal('20.00') assert call_kwargs['withholding_tax_rate'] == Decimal('5.00') assert call_kwargs['vat_rate'] == Decimal('10.00') assert call_kwargs['amount_after_withholding_and_vat'] == Decimal('110.00') assert call_kwargs['activity_statement_period_id'] == 1 assert call_kwargs['statement_period_id'] == 1 assert call_kwargs['payment_name'] == 'test custom payment' @patch('payment.logic.worksheet_payment_custom.WorksheetPaymentCustom') def test_create_worksheet_payment_custom_handler_without_optional_fields( mock_model, fixture_client, fresh_db, mock_accounts, mock_contracts, mock_statement_periods, ): """Test create_worksheet_payment_custom handler without optional fields.""" test_data = { 'account_id': 1, 'contract_id': 1, 'currency_code': 'USD', 'amount': '100.00', 'amount_after_withholding_and_vat': '100.00', 'activity_statement_period_id': 1, 'statement_period_id': 1, 'payment_name': 'test custom payment', } worksheet = WorksheetPaymentCustomFactory.create() mock_model.create.return_value = worksheet res = fixture_client.post('/payment/custom', json=test_data) assert res.status_code == 201 def test_create_worksheet_payment_custom_handler_validation_failure( fixture_client, fresh_db, mock_accounts, mock_contracts, mock_statement_periods ): """Test create_worksheet_payment_custom handler with invalid data.""" test_data = { 'account_id': 1, 'contract_id': 1, 'currency_code': 'USD', 'amount': '-100.00', # Invalid: must be positive 'withholding_tax_amount': '-10.00', 'vat_amount': '20.00', 'withholding_tax_rate': '5.00', 'vat_rate': '10.00', 'amount_after_withholding_and_vat': '0.00', # Invalid: must be > 0 'activity_statement_period_id': 1, 'statement_period_id': 1, 'payment_name': 'test custom payment', } res = fixture_client.post('/payment/custom', json=test_data) assert res.status_code == 400 assert 'message' in res.json assert 'json' in res.json['message'] assert 'amount' in res.json['message']['json'] def test_create_worksheet_payment_custom_handler_missing_required_fields( fixture_client, fresh_db, mock_accounts, mock_contracts, mock_statement_periods ): """Test create_worksheet_payment_custom handler with missing required fields.""" test_data = { 'account_id': 1, # Missing other required fields } res = fixture_client.post('/payment/custom', json=test_data) assert res.status_code == 400 @patch('payment.logic.worksheet_payment_custom.WorksheetPaymentCustom') def test_create_worksheet_payment_custom_handler_with_positive_wht( mock_model, fixture_client, fresh_db, mock_accounts, mock_contracts, mock_statement_periods, ): """Test create_worksheet_payment_custom handler with positive WHT (correction).""" test_data = { 'account_id': 1, 'contract_id': 1, 'currency_code': 'USD', 'amount': '100.00', 'withholding_tax_amount': '10.00', # Positive for correction 'vat_amount': '20.00', 'withholding_tax_rate': '5.00', 'vat_rate': '10.00', 'amount_after_withholding_and_vat': '130.00', 'activity_statement_period_id': 1, 'statement_period_id': 1, 'payment_name': 'test correction payment', } worksheet = WorksheetPaymentCustomFactory.create( withholding_tax_amount=Decimal('10.00'), amount_after_withholding_and_vat=Decimal('130.00'), ) mock_model.create.return_value = worksheet res = fixture_client.post('/payment/custom', json=test_data) assert res.status_code == 201 call_kwargs = mock_model.create.call_args[1] assert call_kwargs['withholding_tax_amount'] == Decimal('10.00') @patch('payment.logic.worksheet_payment_custom.WorksheetPaymentCustom') def test_create_worksheet_payment_custom_handler_with_negative_vat( mock_model, fixture_client, fresh_db, mock_accounts, mock_contracts, mock_statement_periods, ): """Test create_worksheet_payment_custom handler with negative VAT (correction).""" test_data = { 'account_id': 1, 'contract_id': 1, 'currency_code': 'USD', 'amount': '100.00', 'withholding_tax_amount': '-10.00', 'vat_amount': '-20.00', # Negative for correction 'withholding_tax_rate': '5.00', 'vat_rate': '10.00', 'amount_after_withholding_and_vat': '70.00', 'activity_statement_period_id': 1, 'statement_period_id': 1, 'payment_name': 'test correction payment', } worksheet = WorksheetPaymentCustomFactory.create( vat_amount=Decimal('-20.00'), amount_after_withholding_and_vat=Decimal('70.00'), ) mock_model.create.return_value = worksheet res = fixture_client.post('/payment/custom', json=test_data) assert res.status_code == 201 call_kwargs = mock_model.create.call_args[1] assert call_kwargs['vat_amount'] == Decimal('-20.00') @patch('payment.utils.authorization.flask_request') def test_create_worksheet_payment_custom_handler_authorization_check( mock_flask_request, fixture_client, fresh_db, mock_accounts, mock_contracts, mock_statement_periods, ): """Test create_worksheet_payment_custom handler authorization check.""" mock_flask_request.verify_rules_access_standalone.return_value = False test_data = { 'account_id': 1, 'contract_id': 1, 'currency_code': 'USD', 'amount': '100.00', 'activity_statement_period_id': 1, 'statement_period_id': 1, 'payment_name': 'test custom payment', } res = fixture_client.post('/payment/custom', json=test_data) assert res.status_code == 403 assert res.json == {'code': 'authorization_error', 'message': 'Unauthorized'} mock_flask_request.verify_rules_access_standalone.assert_called_once()