"""Neo4j integration tests for get_subscribed_profiles query.""" from typing import Any import pytest from notifications.models.subscriptions import get_subscribed_profiles from notifications.validation.relationship import ( RELATIONSHIP_FROM_IDENTITY, RELATIONSHIP_FROM_PROFILE, ) from .data import ( BASE_DATA_FIXTURE, FANOUT_DEDUPLICATION, SINGLE_IDENTITY_AUTO_FOLLOWED_TEST, SINGLE_IDENTITY_AUTO_FOLLOWED_VENDOR_TEST, SINGLE_IDENTITY_NO_ACCESS_AUTO_FOLLOWED_TEST, SINGLE_IDENTITY_NO_ACCESS_AUTO_FOLLOWED_VENDOR_TEST, SINGLE_IDENTITY_PROFILE_FOLLOWED_TEST, SINGLE_IDENTITY_PROFILE_VENDOR_FOLLOWED_TEST, TWO_IDENTITIES_AUTO_FOLLOWED_TEST, TWO_IDENTITIES_AUTO_FOLLOWED_VENDOR_TEST, TWO_IDENTITIES_PROFILE_FOLLOWED_TEST, TWO_IDENTITIES_PROFILE_VENDOR_FOLLOWED_TEST, ) from .utils import Neo4JDataFixture as F def _sort_profiles[T: dict[str, Any]](data: list[T]) -> list[T]: """Sort profiles by type and id.""" return sorted(data, key=lambda x: (x['profile_type'], x['profile_id'])) @pytest.mark.parametrize( ('neo4j_data_fixture', 'expected_profiles'), ( # direct access pytest.param( ( F(BASE_DATA_FIXTURE), F(SINGLE_IDENTITY_PROFILE_FOLLOWED_TEST, {'user_active': 'Y'}), ), [{'profile_id': 100, 'profile_type': 'LabelProfile'}], id='single_identity_profile_followed_test_user_active', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(SINGLE_IDENTITY_PROFILE_FOLLOWED_TEST, {'user_active': 'N'}), ), [], id='single_identity_profile_followed_test_user_inactive', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(TWO_IDENTITIES_PROFILE_FOLLOWED_TEST, {'user_active': 'Y'}), ), [ {'profile_id': 100, 'profile_type': 'LabelProfile'}, {'profile_id': 200, 'profile_type': 'LabelProfile'}, ], id='two_identities_profile_followed_test_user_active', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(TWO_IDENTITIES_PROFILE_FOLLOWED_TEST, {'user_active': 'N'}), ), [], id='two_identities_profile_followed_test_user_inactive', ), # indirect access pytest.param( ( F(BASE_DATA_FIXTURE), F(SINGLE_IDENTITY_PROFILE_VENDOR_FOLLOWED_TEST, {'user_active': 'Y'}), ), [{'profile_id': 100, 'profile_type': 'LabelProfile'}], id='single_identity_profile_vendor_followed_test_user_active', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(SINGLE_IDENTITY_PROFILE_VENDOR_FOLLOWED_TEST, {'user_active': 'N'}), ), [], id='single_identity_profile_vendor_followed_test_user_inactive', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(TWO_IDENTITIES_PROFILE_VENDOR_FOLLOWED_TEST, {'user_active': 'Y'}), ), [ {'profile_id': 100, 'profile_type': 'LabelProfile'}, {'profile_id': 200, 'profile_type': 'LabelProfile'}, ], id='two_identities_profile_vendor_followed_test_user_active', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(TWO_IDENTITIES_PROFILE_VENDOR_FOLLOWED_TEST, {'user_active': 'N'}), ), [], id='two_identities_profile_vendor_followed_test_user_inactive', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(FANOUT_DEDUPLICATION, {'user_active': 'Y'}), ), [{'profile_id': 100, 'profile_type': 'LabelProfile'}], id='fanout_deduplication_user_active', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(FANOUT_DEDUPLICATION, {'user_active': 'N'}), ), [], id='fanout_deduplication_user_inactive', ), ), indirect=['neo4j_data_fixture'], ) def test_get_subscribed_profiles_from_profile(neo4j_data_fixture, expected_profiles): """Test get_subscribed_profiles with profile level subscription.""" result = get_subscribed_profiles( profile_types=['LabelProfile'], entity_type='Subaccount', entity_id='10001', relationship='HAS_FOLLOWED_TEST', relationship_from=RELATIONSHIP_FROM_PROFILE, ) assert _sort_profiles(result.message) == _sort_profiles(expected_profiles) @pytest.mark.parametrize( ('neo4j_data_fixture', 'expected_profiles'), ( # direct access pytest.param( ( F(BASE_DATA_FIXTURE), F(SINGLE_IDENTITY_AUTO_FOLLOWED_TEST, {'user_active': 'Y'}), ), [{'profile_id': 100, 'profile_type': 'LabelProfile'}], id='single_identity_auto_followed_test_user_active', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(SINGLE_IDENTITY_AUTO_FOLLOWED_TEST, {'user_active': 'N'}), ), [], id='single_identity_auto_followed_test_user_inactive', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(SINGLE_IDENTITY_NO_ACCESS_AUTO_FOLLOWED_TEST), ), [], id='single_identity_no_access_auto_followed_test', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(TWO_IDENTITIES_AUTO_FOLLOWED_TEST, {'user_active': 'Y'}), ), [ {'profile_id': 100, 'profile_type': 'LabelProfile'}, {'profile_id': 200, 'profile_type': 'LabelProfile'}, ], id='two_identities_auto_followed_test_user_active', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(TWO_IDENTITIES_AUTO_FOLLOWED_TEST, {'user_active': 'N'}), ), [], id='two_identities_auto_followed_test_user_inactive', ), # indirect access pytest.param( ( F(BASE_DATA_FIXTURE), F(SINGLE_IDENTITY_AUTO_FOLLOWED_VENDOR_TEST, {'user_active': 'Y'}), ), [{'profile_id': 100, 'profile_type': 'LabelProfile'}], id='single_identity_auto_followed_vendor_test_user_active', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(SINGLE_IDENTITY_AUTO_FOLLOWED_VENDOR_TEST, {'user_active': 'N'}), ), [], id='single_identity_auto_followed_vendor_test_user_inactive', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(SINGLE_IDENTITY_NO_ACCESS_AUTO_FOLLOWED_VENDOR_TEST), ), [], id='single_identity_no_access_auto_followed_vendor_test', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(TWO_IDENTITIES_AUTO_FOLLOWED_VENDOR_TEST, {'user_active': 'Y'}), ), [ {'profile_id': 100, 'profile_type': 'LabelProfile'}, {'profile_id': 200, 'profile_type': 'LabelProfile'}, ], id='two_identities_auto_followed_vendor_test_user_active', ), pytest.param( ( F(BASE_DATA_FIXTURE), F(TWO_IDENTITIES_AUTO_FOLLOWED_VENDOR_TEST, {'user_active': 'N'}), ), [], id='two_identities_auto_followed_vendor_test_user_inactive', ), ), indirect=['neo4j_data_fixture'], ) def test_get_subscribed_profiles_from_identity(neo4j_data_fixture, expected_profiles): """Test get_subscribed_profiles with identity level subscription.""" result = get_subscribed_profiles( profile_types=['LabelProfile'], entity_type='Subaccount', entity_id='10001', relationship='HAS_AUTO_FOLLOWED', relationship_from=RELATIONSHIP_FROM_IDENTITY, subscription_name='TEST', ) assert _sort_profiles(result.message) == _sort_profiles(expected_profiles)