"""Tests for the vendor logic module.""" import uuid from unittest.mock import MagicMock, patch import pytest from permissions.constants import constants from permissions.logic import vendor as vendor_logic from permissions.types import Vendor @pytest.mark.parametrize( 'user_profile, neo4j_vendors, expected_result', [ pytest.param(None, [], [], id='profile_not_found'), pytest.param( { 'profile_id': 12345, 'profile_type': 'AbacusProfile', 'roles': ['administrator'], 'uuid': 'profile-uuid-123', }, [ {'vendor_uuid': constants.VENDOR_STAR_UUID, 'vendor_id': '*'}, {'vendor_uuid': 'vendor-uuid-123', 'vendor_id': 101}, ], [ {'vendor_uuid': constants.VENDOR_STAR_UUID, 'vendor_id': '*'}, {'vendor_uuid': 'vendor-uuid-123', 'vendor_id': 101}, ], id='has_vendor_star_access', ), pytest.param( { 'profile_id': 12345, 'profile_type': 'MoneyProfile', 'roles': ['accounting'], 'uuid': 'profile-uuid-123', }, [ {'vendor_uuid': 'vendor-uuid-123', 'vendor_id': 101}, {'vendor_uuid': 'vendor-uuid-456', 'vendor_id': 102}, ], [ {'vendor_uuid': 'vendor-uuid-123', 'vendor_id': 101}, {'vendor_uuid': 'vendor-uuid-456', 'vendor_id': 102}, ], id='regular_access', ), ], ) @patch('permissions.models.identity.get_profile_by_identity_id_and_profile_id_and_type') @patch('permissions.models.neo4j_vendor.get_directly_accessible_vendors_by_profile') def test_get_directly_accessible_vendors_by_profile( mock_neo4j_get_vendors: MagicMock, mock_get_profile: MagicMock, user_profile: dict | None, neo4j_vendors: list[dict], expected_result: list[dict], ) -> None: """Test get_directly_accessible_vendors_by_profile.""" # set up test data identity_id = uuid.uuid4() profile_id = 12345 profile_type = 'AbacusProfile' # set up the model mocks based on the test case mock_get_profile.return_value = user_profile # mock model neo4j_vendor.get_directly_accessible_vendors_by_profile mock_vendors = [] for vendor_data in neo4j_vendors: mock_vendor = Vendor( vendor_uuid=vendor_data['vendor_uuid'], vendor_id=vendor_data['vendor_id'] ) mock_vendors.append(mock_vendor) mock_neo4j_get_vendors.return_value = mock_vendors result = vendor_logic.get_directly_accessible_vendors_by_profile( identity_id=identity_id, profile_id=profile_id, profile_type=profile_type ) # assert result matches expected assert isinstance(result, list) assert len(result) == len(expected_result) # Verify results based on test case mock_get_profile.assert_called_once_with( identity_id=identity_id, profile_id=profile_id, profile_type=profile_type ) if user_profile is None: assert len(result) == 0 mock_neo4j_get_vendors.assert_not_called() else: for i, vendor in enumerate(result): assert isinstance(vendor, Vendor) assert vendor.vendor_uuid == expected_result[i]['vendor_uuid'] assert vendor.vendor_id == expected_result[i]['vendor_id'] mock_get_profile.assert_called_once_with( identity_id=identity_id, profile_id=profile_id, profile_type=profile_type ) mock_neo4j_get_vendors.assert_called_once_with( identity_id=identity_id, profile_id=profile_id, profile_type=profile_type )