"""Test project_transfer_term handlers.""" from decimal import Decimal from unittest.mock import patch from owsresponse import response _PATCH_BODY = {'destination_contract_term_id': 137451} _CREATE_BODY = { 'project_transfer_term_id': 7, 'contract_id': 100, 'term_type': 'product', 'attachments': ['196589123456'], 'conditions': [{'conditions': {}, 'term_rate': '80.00', 'priority': 1}], 'attachment_relations': {'label_ids': ['20']}, 'name': 'Product Term', } @patch('royalties.blueprints.project_transfer_term.logic') @patch('royalties.blueprints.project_transfer_term.flask_request') def test_patch_transfer_job_term(mock_flask_request, mock_logic, fixture_client): """Test PATCH /transfer-job//terms/.""" mock_flask_request.verify_rules_access_standalone.return_value = True mock_logic.set_destination_term_id.return_value = response.Response( message={'project_transfer_term_id': 7}, status=200 ) res = fixture_client.patch('/transfer-job/42/terms/7', json=_PATCH_BODY) assert res.status_code == 200 mock_logic.set_destination_term_id.assert_called_once_with( term_id=7, destination_contract_term_id=137451, ) @patch('royalties.blueprints.project_transfer_term.logic') @patch('royalties.blueprints.project_transfer_term.flask_request') def test_patch_transfer_job_term_invalid_body_returns_400( mock_flask_request, mock_logic, fixture_client ): """A body missing destination_contract_term_id fails validation.""" mock_flask_request.verify_rules_access_standalone.return_value = True res = fixture_client.patch('/transfer-job/42/terms/7', json={'other': 1}) assert res.status_code == 400 mock_logic.set_destination_term_id.assert_not_called() @patch('royalties.blueprints.project_transfer_term.logic') @patch('royalties.blueprints.project_transfer_term.flask_request') def test_patch_transfer_job_term_unauthorized( mock_flask_request, mock_logic, fixture_client ): """A failed rules check returns 401 without touching logic.""" mock_flask_request.verify_rules_access_standalone.return_value = False res = fixture_client.patch('/transfer-job/42/terms/7', json=_PATCH_BODY) assert res.status_code == 401 mock_logic.set_destination_term_id.assert_not_called() @patch('royalties.blueprints.project_transfer_term.logic') @patch('royalties.blueprints.project_transfer_term.flask_request') def test_create_transfer_term(mock_flask_request, mock_logic, fixture_client): """Test POST /account//contract-terms/transfer-create.""" mock_flask_request.verify_rules_access_standalone.return_value = True mock_logic.create_destination_term.return_value = response.Response( message={'contract_term_id': 9001}, status=201 ) res = fixture_client.post( '/account/20/contract-terms/transfer-create', json=_CREATE_BODY ) assert res.status_code == 201 mock_logic.create_destination_term.assert_called_once_with( project_transfer_term_id=7, contract_id=100, term_type='product', attachments=['196589123456'], conditions=[{'conditions': {}, 'term_rate': Decimal('80.00'), 'priority': 1}], attachment_relations={'label_ids': ['20']}, name='Product Term', ) @patch('royalties.blueprints.project_transfer_term.logic') @patch('royalties.blueprints.project_transfer_term.flask_request') def test_create_transfer_term_missing_idempotency_key_returns_400( mock_flask_request, mock_logic, fixture_client ): """A body missing project_transfer_term_id fails validation.""" mock_flask_request.verify_rules_access_standalone.return_value = True body = {k: v for k, v in _CREATE_BODY.items() if k != 'project_transfer_term_id'} res = fixture_client.post('/account/20/contract-terms/transfer-create', json=body) assert res.status_code == 400 mock_logic.create_destination_term.assert_not_called() @patch('royalties.blueprints.project_transfer_term.logic') @patch('royalties.blueprints.project_transfer_term.flask_request') def test_create_transfer_term_unauthorized( mock_flask_request, mock_logic, fixture_client ): """A failed rules check returns 401 without touching logic.""" mock_flask_request.verify_rules_access_standalone.return_value = False res = fixture_client.post( '/account/20/contract-terms/transfer-create', json=_CREATE_BODY ) assert res.status_code == 401 mock_logic.create_destination_term.assert_not_called()