"""Test for logic application collaborator module.""" from unittest.mock import MagicMock, patch import pytest from account.constants.features import FEATURES from account.logic.application import collaborator as collaborator_app_logic from account.models import application as app_model, ows_collaborator, types @pytest.fixture() def collaborator_attributes_fixture() -> dict[str, types.CollaboratorAttributes]: """Fixture for collaborator attributes.""" return { 'e864c3be-a6ea-4509-b1fe-4437d5a67500': { 'uuid': 'e864c3be-a6ea-4509-b1fe-4437d5a67500', 'collaborator_id': 123, 'dp_enabled_date': '2023-01-15T10:30:00Z', 'vendor_attributes': { 'vendor_id': 9573, 'uuid': 'c2c3ea5d-f91e-4c86-8d57-3eacca69de47', 'company_brand': {'id': 2, 'name': 'theorchard'}, 'features': [FEATURES.COLLABORATORS_DIRECT_PAYMENTS], }, } } @patch('account.logic.application.collaborator.get_customer_accounting_app_for_collaborator') @patch('account.logic.application.collaborator.get_banking_tax_app_for_collaborator') def test_generate_collaborator_app_check_map( mock_get_banking_tax_app_for_collaborator, mock_get_customer_accounting_app_for_collaborator, collaborator_attributes_fixture, ): """Test generate_collaborator_app_check_map.""" collaborator_attributes = collaborator_attributes_fixture[ 'e864c3be-a6ea-4509-b1fe-4437d5a67500' ] result = collaborator_app_logic.generate_collaborator_app_check_map(collaborator_attributes) assert isinstance(result, dict) assert len(result) == 2 for app_id in [app_model.CUSTOMER_ACCOUNTING_APP]: assert app_id in result assert callable(result[app_id]) result[app_id]() for app_id in [app_model.BANKING_TAX_APP]: assert app_id in result assert callable(result[app_id]) result[app_id]() if app_id == app_model.CUSTOMER_ACCOUNTING_APP: mock_get_customer_accounting_app_for_collaborator.assert_called_with( collaborator_attributes ) if app_id == app_model.BANKING_TAX_APP: mock_get_banking_tax_app_for_collaborator.assert_called_with(collaborator_attributes) @patch('account.models.ows_collaborator.get_vendor_terms_and_conditions') def test_get_enabled_collaborator_applications_existing_collaborator( mock_get_vendor_terms_and_conditions: MagicMock, collaborator_attributes_fixture, ): """Test get_enabled_collaborator_applications for existing collaborator.""" # Mock terms and conditions to return None (no banking/tax app) mock_get_vendor_terms_and_conditions.return_value = None applications = collaborator_app_logic.get_enabled_collaborator_applications( collaborator_attributes_fixture['e864c3be-a6ea-4509-b1fe-4437d5a67500'] ) assert applications == [ { 'application_id': 'CUSTOMER_ACCOUNTING_APP', 'roles': ['CUSTOMER_ACCOUNTING_BASE_ROLE'], 'url': 'https://accounting.theorchard.com', } ] @pytest.mark.parametrize( 'collaborator_uuid, expected', [ pytest.param( 'e864c3be-a6ea-4509-b1fe-4437d5a67500', { 'application_id': 'CUSTOMER_ACCOUNTING_APP', 'roles': ['CUSTOMER_ACCOUNTING_BASE_ROLE'], 'url': 'https://accounting.theorchard.com', }, ) ], ) def test_get_customer_accounting_app_for_collaborator( collaborator_attributes_fixture: types.CollaboratorAttributes, collaborator_uuid: str, expected: types.TenantApplication | None, ): """Test get_customer_accounting_app_for_collaborator.""" collaborator_attributes = collaborator_attributes_fixture[collaborator_uuid] result = collaborator_app_logic.get_customer_accounting_app_for_collaborator( collaborator_attributes ) assert result == expected @pytest.mark.parametrize( 'features,dp_enabled_date,mock_terms_return_value,expected_result', [ pytest.param( [], '2023-01-15T10:30:00Z', None, None, id='no_direct_payments_feature', ), pytest.param( [FEATURES.COLLABORATORS_DIRECT_PAYMENTS], None, None, None, id='no_dp_enabled_date', ), pytest.param( [FEATURES.COLLABORATORS_DIRECT_PAYMENTS], '2023-01-15T10:30:00Z', None, None, id='no_agreement_data', ), pytest.param( [FEATURES.COLLABORATORS_DIRECT_PAYMENTS], '2023-01-15T10:30:00Z', None, None, id='vendor_terms_conditions_none_response', ), pytest.param( [FEATURES.COLLABORATORS_DIRECT_PAYMENTS], '2023-01-15T10:30:00Z', ows_collaborator.VendorTermsAndConditions( latest_version=3, latest_template='Template v3', agreed_version=2, agreed_date='2023-01-01T00:00:00Z', ), None, id='outdated_agreement', ), pytest.param( [FEATURES.COLLABORATORS_DIRECT_PAYMENTS], '2023-01-15T10:30:00Z', ows_collaborator.VendorTermsAndConditions( latest_version=2, latest_template='Template v2', agreed_version=2, agreed_date='2023-01-15T10:30:00Z', ), { 'application_id': 'BANKING_TAX_APP', 'roles': ['BANKING_TAX_BASE_ROLE'], 'url': 'https://documents.theorchard.com', }, id='valid_application', ), ], ) @patch('account.models.ows_collaborator.get_vendor_terms_and_conditions') def test_get_banking_tax_app_for_collaborator( mock_get_vendor_terms_and_conditions: MagicMock, collaborator_attributes_fixture: dict[str, types.CollaboratorAttributes], features: list, dp_enabled_date: str | None, mock_terms_return_value: ows_collaborator.VendorTermsAndConditions | None, expected_result: types.TenantApplication | None, ) -> None: """Test get_banking_tax_app_for_collaborator with various conditions.""" # Get base collaborator attributes collaborator_attributes = collaborator_attributes_fixture[ 'e864c3be-a6ea-4509-b1fe-4437d5a67500' ].copy() # Configure test scenario collaborator_attributes['vendor_attributes']['features'] = features collaborator_attributes['dp_enabled_date'] = dp_enabled_date mock_get_vendor_terms_and_conditions.return_value = mock_terms_return_value # Call the function result = collaborator_app_logic.get_banking_tax_app_for_collaborator(collaborator_attributes) # Assert the result assert result == expected_result