"""Tests for accounting period handlers.""" from unittest.mock import MagicMock, patch import pytest from abacus_common_logic.test_utils.helpers import get_json_body, get_message from flask import testing as flask_testing from owsresponse import response from royalties.constants import error from royalties.constants.constants import ACCOUNTING_PERIOD_STATUSES, CONTRACT_TYPES from royalties.tests.utils import factories @patch('royalties.blueprints.accounting_period.logic') class TestAccountingPeriodHandlers: """Tests for accounting period handlers.""" def test_create_accounting_period_success(self, mock_logic, fixture_client): """Successful accounting period creation response.""" contract_type = CONTRACT_TYPES.DISTRIBUTION default_status = ACCOUNTING_PERIOD_STATUSES.OPEN json_body = { 'accounting_period_name': 'Holland, 1945', 'contract_type': contract_type, 'statement_period_id': 256, } res_body = { 'accounting_period_name': 'Holland, 1945', 'accounting_period_status': default_status, 'closed_date': None, 'contract_type': contract_type, 'statement_period_id': 256, } mock_response = response.Response(message=res_body, status=201) mock_logic.create_accounting_period.return_value = mock_response res = fixture_client.post('/accounting-period', json=json_body) assert res.status_code == 201 assert get_json_body(res) == res_body mock_logic.create_accounting_period.assert_called_once_with(**json_body) def test_create_accounting_period_fails_with_missing_fields( self, _, fixture_client ): """Failed accounting period creation due to missing/null fields.""" json_body = {'data': 'invalid'} res = fixture_client.post('/accounting-period', json=json_body) assert res.status_code == 400 message = get_message(res) assert error.ERROR_FIELD_MISSING in message['accounting_period_name'] def test_get_accounting_period_by_accounting_run_id( self, mock_logic, fixture_client ): """Successfully get accounting_period by accounting_run_id.""" res_body = { 'closed_date': None, 'statement_period_id': 282, 'accounting_period_id': 1, 'accounting_period_name': 'test', 'accounting_period_status': 'open', 'contract_type': 'neighbouring_rights', } mock_response = response.Response(message=res_body, status=200) mock_logic.get_accounting_period_by_accounting_run.return_value = mock_response accounting_run_id = 1 res = fixture_client.get( f'/accounting-run/{accounting_run_id}/accounting-period' ) assert res.status_code == 200 mock_logic.get_accounting_period_by_accounting_run.assert_called_once_with( accounting_run_id ) @pytest.mark.parametrize( ['standalone_check_result', 'pdp_check_result', 'expected_status_code'], [ pytest.param(True, None, 200, id='standalone check passed'), pytest.param(False, False, 403, id='pdp check failed'), pytest.param(False, True, 200, id='pdp check passed'), ], ) @patch('royalties.blueprints.accounting_period.authorization') @patch('royalties.blueprints.accounting_period.flask_request') def test_get_accounting_period_by_id_authorization( flask_request_mock: MagicMock, authorization_mock: MagicMock, standalone_check_result: bool, pdp_check_result: bool | None, expected_status_code: int, fixture_client: flask_testing.FlaskClient, ) -> None: """Test authorization for endpoint to get accounting period by id.""" accounting_period_id = 1 factories.AccountingPeriodFactory.create(accounting_period_id=accounting_period_id) flask_request_mock.verify_rules_access_standalone.return_value = ( standalone_check_result ) authorization_mock.pdp_authorize_resource.return_value = pdp_check_result res = fixture_client.get(f'/accounting-period/{accounting_period_id}') assert res.status_code == expected_status_code flask_request_mock.verify_rules_access_standalone.assert_called_once() if not standalone_check_result: authorization_mock.pdp_authorize_resource.assert_called_once_with( resource_id=0, resource_type='accounting_period', )