"""Tests for the subaccount logic module.""" import uuid from unittest.mock import MagicMock, patch import pytest from permissions.logic import subaccount as subaccount_logic from permissions.types import Subaccount @pytest.mark.parametrize( 'user_profile, neo4j_subaccounts, expected_result', [ pytest.param(None, [], [], id='profile_not_found'), pytest.param( { 'profile_id': 12345, 'profile_type': 'MoneyProfile', 'roles': ['accounting'], 'uuid': 'profile-uuid-123', }, [ {'subaccount_uuid': 'subaccount-uuid-123', 'subaccount_id': 101}, {'subaccount_uuid': 'subaccount-uuid-456', 'subaccount_id': 102}, ], [ {'subaccount_uuid': 'subaccount-uuid-123', 'subaccount_id': 101}, {'subaccount_uuid': 'subaccount-uuid-456', 'subaccount_id': 102}, ], id='regular_access', ), ], ) @patch('permissions.models.identity.get_profile_by_identity_id_and_profile_id_and_type') @patch('permissions.models.neo4j_subaccount.get_directly_accessible_subaccounts_by_profile') def test_get_directly_accessible_subaccounts_by_profile( mock_neo4j_get_subaccounts: MagicMock, mock_get_profile: MagicMock, user_profile: dict | None, neo4j_subaccounts: list[dict], expected_result: list[dict], ) -> None: """Test get_directly_accessible_subaccounts_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_subaccount.get_directly_accessible_subaccounts_by_profile mock_subaccounts = [] for subaccount_data in neo4j_subaccounts: mock_subaccount = Subaccount( subaccount_uuid=subaccount_data['subaccount_uuid'], subaccount_id=subaccount_data['subaccount_id'], ) mock_subaccounts.append(mock_subaccount) mock_neo4j_get_subaccounts.return_value = mock_subaccounts result = subaccount_logic.get_directly_accessible_subaccounts_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_subaccounts.assert_not_called() else: for i, subaccount in enumerate(result): assert isinstance(subaccount, Subaccount) assert subaccount.subaccount_uuid == expected_result[i]['subaccount_uuid'] assert subaccount.subaccount_id == expected_result[i]['subaccount_id'] mock_get_profile.assert_called_once_with( identity_id=identity_id, profile_id=profile_id, profile_type=profile_type ) mock_neo4j_get_subaccounts.assert_called_once_with( identity_id=identity_id, profile_id=profile_id, profile_type=profile_type )