"""Functional tests for contract template endpoints.""" from unittest.mock import patch from abacus_contract.constants.error import ERROR_MISSING_FIELD from tests.utils.factories import ContractTemplateFactory from tests.utils.factories import ReferenceSapProfitCenterFactory from tests.utils.factories import ReferenceSigningEntityFactory @patch('abacus_contract.logic.contract.emit_contract_event') def test_create_contract_from_template_id_error( mock_emit_contract_kafka_event, fixture_client, create_mock_account, create_mock_run_controller ): """POST /contract-template//contract for missing field.""" mock_reference_signing_entity = ReferenceSigningEntityFactory.create( company_code=4914 ) ReferenceSapProfitCenterFactory.create( company_code=mock_reference_signing_entity.company_code ) contract_terms = [{ 'attachments': ['1234'], 'conditions': [{ 'name': 'WW', 'stores': [], 'priority': 1, 'countries': [], 'term_rate': '80.00', 'transaction_types': [] }] }] contract_template = ContractTemplateFactory.create( contract_terms=contract_terms, reference_signing_entity=mock_reference_signing_entity ) contract_template_id = contract_template.contract_template_id account_name = 'AWAL GDA' result = fixture_client.post( f'/contract-template/{contract_template_id}/contract', json={ 'account_id': 1, 'account_name': account_name } ) res = result.json assert result.status_code == 400 assert res['message']['Invalid_contract_terms']['error'] == \ ERROR_MISSING_FIELD.format(field="'term_type'") @patch('abacus_contract.logic.contract.emit_contract_event') def test_create_contract_from_template_invalid_term_rate( mock_emit_contract_kafka_event, fixture_client, create_mock_account, create_mock_run_controller ): """POST /contract-template//contract for invalid term_rate field.""" # noqa: E501 mock_reference_signing_entity = ReferenceSigningEntityFactory.create( company_code=4914 ) ReferenceSapProfitCenterFactory.create( company_code=mock_reference_signing_entity.company_code ) contract_terms = [{ 'attachments': ['1234'], 'term_type': 'label', 'conditions': [{ 'name': 'WW', 'stores': [], 'priority': 1, 'countries': [], 'term_rate': '129.00', 'transaction_types': [] }] }] contract_template = ContractTemplateFactory.create( contract_terms=contract_terms, reference_signing_entity=mock_reference_signing_entity ) contract_template_id = contract_template.contract_template_id account_name = 'AWAL GDA' result = fixture_client.post( f'/contract-template/{contract_template_id}/contract', json={ 'account_id': 1, 'account_name': account_name } ) assert 'Must be greater than or equal to 0.0 and less than or equal to 100.0.' \ in result.json['invalid_contract_terms'][0]['error'] @patch('abacus_contract.logic.contract.emit_contract_event') def test_create_contract_from_template_id( mock_emit_contract_kafka_event, fixture_client, create_mock_account, create_mock_run_controller ): """POST /contract-template//contract.""" # noqa: E501 mock_reference_signing_entity = ReferenceSigningEntityFactory.create( company_code=4914 ) ReferenceSapProfitCenterFactory.create( company_code=mock_reference_signing_entity.company_code ) contract_template = ContractTemplateFactory.create( reference_signing_entity=mock_reference_signing_entity ) contract_template_id = contract_template.contract_template_id reference_signing_entity_id = contract_template.reference_signing_entity_id account_name = 'AWAL GDA' result = fixture_client.post( f'/contract-template/{contract_template_id}/contract', json={ 'account_id': 1, 'account_name': account_name } ) res = result.json assert result.status_code == 201 assert res['contract_name'] == account_name assert res['contract_terms'][0]['term_type'] == \ contract_template.contract_terms[0]['term_type'] assert res['reference_signing_entity_id'] == reference_signing_entity_id assert res['initial_start_date'] is not None