from __future__ import annotations import unittest from govoplan_core.core.identity import IdentitySearchProvider from govoplan_core.db.base import Base from govoplan_core.db.session import configure_database, reset_database from govoplan_identity.backend.db.models import Identity, IdentityAccountLink from govoplan_identity.backend.directory import SqlIdentityDirectory class IdentityDirectoryTests(unittest.TestCase): def setUp(self) -> None: self.database = configure_database("sqlite:///:memory:") Base.metadata.create_all( self.database.engine, tables=[Identity.__table__, IdentityAccountLink.__table__], ) with self.database.session() as session: active = Identity( id="identity-active", display_name="Ada Example", external_subject="subject-active", source="local", is_active=True, settings={}, ) inactive = Identity( id="identity-inactive", display_name="Inactive Person", external_subject="subject-inactive", source="local", is_active=False, settings={}, ) session.add_all([active, inactive]) session.flush() session.add_all( [ IdentityAccountLink( id="link-active", identity_id=active.id, account_id="account-ada", is_primary=True, source="local", ), IdentityAccountLink( id="link-inactive", identity_id=inactive.id, account_id="account-inactive", is_primary=True, source="local", ), ] ) session.commit() def tearDown(self) -> None: reset_database(dispose=True) def test_search_capability_filters_inactive_identities_and_searches_account_ids(self) -> None: directory = SqlIdentityDirectory() self.assertIsInstance(directory, IdentitySearchProvider) self.assertEqual( ("identity-active",), tuple(identity.id for identity in directory.search_identities()), ) matches = directory.search_identities("account-ada") self.assertEqual(("identity-active",), tuple(identity.id for identity in matches)) self.assertEqual(("account-ada",), matches[0].account_ids) self.assertEqual("account-ada", matches[0].primary_account_id) def test_search_capability_can_include_inactive_identities(self) -> None: directory = SqlIdentityDirectory() matches = directory.search_identities("inactive", include_inactive=True) self.assertEqual(("identity-inactive",), tuple(identity.id for identity in matches)) if __name__ == "__main__": unittest.main()