"""Tests for run controller 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.constants import CONTRACT_TYPES from royalties.constants.error import ERROR_FIELD_MISSING @patch('royalties.blueprints.run_controller.logic') class TestRunControllerHandlers: """Tests for run controller handlers.""" def test_create_run_controller_invalid_parameters(self, mock_logic, fixture_client): """Create a run controller with invalid parameters.""" res = fixture_client.post('/run-controller', json={'run_controller_name': ''}) mock_logic.create_run_controller.assert_not_called() assert res.status_code == 400 message = get_message(res) assert message == { 'run_controller_name': [ERROR_FIELD_MISSING], 'contract_type': [ERROR_FIELD_MISSING], } def test_create_run_controller(self, mock_logic, fixture_client): """Create a run controller with valid parameters.""" name = 'High Priority 1' mock_logic.create_run_controller.return_value = response.Response( { 'run_controller_id': 1, 'run_controller_name': name, 'contract_type': CONTRACT_TYPES.DISTRIBUTION, } ) res = fixture_client.post( '/run-controller', json={ 'run_controller_name': name, 'contract_type': CONTRACT_TYPES.DISTRIBUTION, }, ) mock_logic.create_run_controller.assert_called_once_with( run_controller_name=name, contract_type=CONTRACT_TYPES.DISTRIBUTION ) assert res.status_code == 200 message = get_json_body(res) assert message == { 'run_controller_id': 1, 'run_controller_name': name, 'contract_type': CONTRACT_TYPES.DISTRIBUTION, } @patch('royalties.blueprints.run_controller.RunController') def test_get_run_controllers_no_filter(mock_model, fixture_client): """Test getting list of all run controllers.""" mock_model.base_list_query.return_value.count.return_value = 1 mock_model.get_page.return_value = [ { 'contract_count': 0, 'contract_type': CONTRACT_TYPES.DISTRIBUTION, 'run_controller_id': 1, 'run_controller_name': 'test', } ] res = fixture_client.get('/run-controllers') assert res.status_code == 200 mock_model.base_list_query.assert_called_with(None, False) @patch('royalties.blueprints.run_controller.RunController') def test_get_run_controllers_by_contract_type(mock_model, fixture_client): """Test getting list of run controllers by contract_type.""" mock_model.base_list_query.return_value.count.return_value = 1 mock_model.get_page.return_value = [ { 'contract_count': 0, 'contract_type': CONTRACT_TYPES.DISTRIBUTION, 'run_controller_id': 1, 'run_controller_name': 'test', } ] res = fixture_client.get( f'/run-controllers?contract_type={CONTRACT_TYPES.DISTRIBUTION}' ) assert res.status_code == 200 mock_model.base_list_query.assert_called_with(CONTRACT_TYPES.DISTRIBUTION, False) @patch('royalties.blueprints.run_controller.RunController') def test_get_run_controllers_active_only(mock_model, fixture_client): """Test getting list of run controllers that are not soft deleted.""" mock_model.base_list_query.return_value.count.return_value = 1 mock_model.get_page.return_value = [ { 'contract_count': 0, 'contract_type': CONTRACT_TYPES.DISTRIBUTION, 'run_controller_id': 1, 'run_controller_name': 'test', } ] res = fixture_client.get('/run-controllers?active_only=TRUE') assert res.status_code == 200 mock_model.base_list_query.assert_called_with(None, True) @pytest.mark.parametrize( [ 'standalone_check_result', 'get_account_ids_result', 'pdp_check_result', 'expected_status', ], [ pytest.param(True, None, None, 200, id='standalone check pass'), pytest.param(False, [], None, 404, id='account id not found'), pytest.param(False, [1], False, 403, id='pdp check fail'), pytest.param(False, [1], True, 200, id='pdp check pass'), ], ) @patch('royalties.blueprints.run_controller.logic') @patch('royalties.blueprints.run_controller.authorization') @patch('royalties.blueprints.run_controller.contract_logic') @patch('royalties.blueprints.run_controller.flask_request') def test_get_run_controller_by_contract_authorization( mock_flask_request: MagicMock, mock_contract_logic: MagicMock, mock_authorization: MagicMock, mock_logic: MagicMock, standalone_check_result: bool, get_account_ids_result: int | None, pdp_check_result: bool | None, expected_status: int, fixture_client: flask_testing.FlaskClient, ) -> None: """Test getting run controller by contract.""" mock_flask_request.verify_rules_access_standalone.return_value = ( standalone_check_result ) mock_contract_logic.get_account_ids_by_contract_ids.return_value = ( get_account_ids_result ) mock_authorization.pdp_authorize_many_accounts.return_value = pdp_check_result mock_logic.get_contract_run_controller.return_value = {} res = fixture_client.get('/run-controller-by-contract/1') assert res.status_code == expected_status mock_flask_request.verify_rules_access_standalone.assert_called_once() if not standalone_check_result: mock_contract_logic.get_account_ids_by_contract_ids.assert_called_once_with([1]) if get_account_ids_result: mock_authorization.pdp_authorize_many_accounts.assert_called_once_with( get_account_ids_result, ) else: mock_authorization.pdp_authorize_many_accounts.assert_not_called()