from argparse import Namespace import asyncio from async_generator import asynccontextmanager import pytest from phi.ldap.async_model import ( get_class, recall, call_if_callable, inheritance, Entry, Hackers, Robots, Service, User, ) BASE_DN = "dc=test,dc=abbiamoundominio,dc=org" class MockClient: def __init__(self, base_dn): self._base_dn = base_dn @property def base_dn(self): return self._base_dn @asynccontextmanager async def connect(self, *args, **kwargs): conn = Namespace() async def _search(*args, **kwargs): await asyncio.sleep(0.01) return "Fake description" conn.search = _search yield conn @pytest.fixture def client_fixture(): return MockClient(BASE_DN) @pytest.fixture def lineage_fixture(): class Grand: @classmethod def name(cls): return cls.__name__ class Ma(Grand): pass class Child(Ma): pass grand = Grand() ma = Ma() child = Child() return Grand, Ma, Child, grand, ma, child def test_get_class(): c = MockClient(BASE_DN) assert get_class(c) is MockClient def test_recall(lineage_fixture): Grand, Ma, Child, grand, ma, child = lineage_fixture assert object is recall(grand) assert recall(grand) is recall(Grand) assert Grand is recall(ma) assert recall(ma) is recall(Ma) assert Ma is recall(child) assert recall(child) is recall(Child) def test_call_if_callable(): class Dummy: classattr = "classattr" @property def prop(self): return "prop" def func(self): return "func" @classmethod def clsmth(cls): return "clsmth" d = Dummy() assert "classattr" == call_if_callable(d, "classattr") assert "prop" == call_if_callable(d, "prop") assert "func" == call_if_callable(d, "func") assert "clsmth" == call_if_callable(d, "clsmth") def test_inheritance(lineage_fixture): Grand, Ma, Child, _, _, _ = lineage_fixture assert inheritance(Grand, "name") == "Grand" assert inheritance(Ma, "name") == "Ma,Grand" assert inheritance(Child, "name") == "Child,Ma,Grand" assert inheritance(Child, "name", Grand) == "Child,Ma" assert inheritance(Child, "name", Ma) == "Child" def test_Entry(client_fixture): e = Entry(client_fixture) assert e.base_dn == BASE_DN assert e.client is not None def test_Entry_name(client_fixture): e = Entry(client_fixture) assert e.name() == "Entry" def test_Entry_qualified_name(client_fixture): e = Entry(client_fixture) assert e.qualified_name() == "None=Entry" def test_Entry_dn(client_fixture): e = Entry(client_fixture) assert e.dn == "None=Entry,{}".format(BASE_DN) @pytest.mark.asyncio async def test_Entry_describe(client_fixture): e = Entry(client_fixture) assert "Fake description" == await e.describe() def test_Hackers(client_fixture): h = Hackers(client_fixture) assert h.kind == "ou" assert h.dn == "ou={},{}".format(h.name(), BASE_DN) def test_Robots(client_fixture): r = Robots(client_fixture) assert r.kind == "ou" assert r.dn == "ou={},{}".format(r.name(), BASE_DN) def test_User(client_fixture): c = User("conte_mascetti", client_fixture) assert c.kind == "uid" assert c.name == "conte_mascetti" assert c.dn == "uid=conte_mascetti,ou=Hackers,{}".format(BASE_DN) def test_Service(client_fixture): c = Service("phi", client_fixture) assert c.kind == "uid" assert c.name == "phi" assert c.dn == "uid=phi,ou=Services,{}".format(BASE_DN)