From 54ac7c41412eb638ea50afec6204e0316f614a8e Mon Sep 17 00:00:00 2001 From: Ethan Paul Date: Mon, 24 Feb 2020 23:03:59 -0500 Subject: [PATCH] Add tests for domain model and domain extras models Add string reprs to domain models Fix misnamed dict key names on domain model Fix improper timedelta/int conversions on domain model --- keyosk/database/domain.py | 37 +++++++++++++++++++++++++++++---- tests/test_database_domain.py | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 tests/test_database_domain.py diff --git a/keyosk/database/domain.py b/keyosk/database/domain.py index 0b5166b..92fcab4 100644 --- a/keyosk/database/domain.py +++ b/keyosk/database/domain.py @@ -50,8 +50,28 @@ class Domain(KeyoskBaseModel): enable_client_set_auth = peewee.BooleanField(null=False) enable_server_set_auth = peewee.BooleanField(null=False) enable_refresh = peewee.BooleanField(null=False) - lifespan_access = peewee.IntegerField(null=False) - lifespan_refresh = peewee.IntegerField(null=False) + _lifespan_access = peewee.IntegerField(null=False) + _lifespan_refresh = peewee.IntegerField(null=False) + + @property + def lifespan_access(self) -> datetime.timedelta: + """Return the access lifespan as a timedelta""" + return datetime.timedelta(seconds=self._lifespan_access) + + @lifespan_access.setter + def lifespan_access(self, value: datetime.timedelta): + """Set the access lifespan as an integer from a timedelta""" + self._lifespan_access = int(value.total_seconds()) + + @property + def lifespan_refresh(self) -> datetime.timedelta: + """Return the refresh lifespan as a timedelta""" + return datetime.timedelta(seconds=self._lifespan_refresh) + + @lifespan_refresh.setter + def lifespan_refresh(self, value: datetime.timedelta): + """Set the refresh lifespan as an integer from a timedelta""" + self._lifespan_refresh = int(value.total_seconds()) @staticmethod def dict_keys() -> List[str]: @@ -65,8 +85,8 @@ class Domain(KeyoskBaseModel): "description", "contact", "enabled", - "enable_password", - "enable_autopassword", + "enable_client_set_auth", + "enable_server_set_auth", "enable_refresh", "lifespan_access", "lifespan_refresh", @@ -78,6 +98,9 @@ class Domain(KeyoskBaseModel): def foreign_backref() -> List[str]: return ["access_lists", "permissions"] + def __str__(self) -> str: + return f"Domain '{self.name}' ({self.uuid})" + class DomainAccessList(KeyoskBaseModel): """Access list name model definition @@ -96,6 +119,9 @@ class DomainAccessList(KeyoskBaseModel): def dict_keys() -> List[str]: return ["name"] + def __str__(self) -> str: + return self.name + class DomainPermission(KeyoskBaseModel): """Permission name model definition @@ -116,3 +142,6 @@ class DomainPermission(KeyoskBaseModel): @staticmethod def dict_keys() -> List[str]: return ["name", "bitindex"] + + def __str__(self) -> str: + return self.name diff --git a/tests/test_database_domain.py b/tests/test_database_domain.py new file mode 100644 index 0000000..864dc32 --- /dev/null +++ b/tests/test_database_domain.py @@ -0,0 +1,39 @@ +import peewee +from fixtures import demo_database + +from keyosk import database + + +def test_meta(): + + models = [database.Domain, database.DomainAccessList, database.DomainPermission] + + for model in models: + for key in model.dict_keys(): + assert hasattr(model, key) + attr = getattr(model, key) + + if key in model.foreign_ref(): + assert isinstance(attr, peewee.ForeignKeyField) + else: + assert not isinstance(attr, peewee.ForeignKeyField) + + if key in model.foreign_backref(): + assert isinstance(attr, peewee.BackrefAccessor) + else: + assert not isinstance(attr, peewee.BackrefAccessor) + + +def test_formatting(demo_database): + for domain in database.Domain.select(): + assert list(dict(domain).keys()) == database.Domain.dict_keys() + assert str(domain.uuid) in str(domain) + assert domain.name in str(domain) + + for permission in database.DomainPermission.select(): + assert list(dict(permission).keys()) == database.DomainPermission.dict_keys() + assert str(permission.uuid) not in str(permission) + + for access_list in database.DomainAccessList.select(): + assert list(dict(access_list).keys()) == database.DomainAccessList.dict_keys() + assert str(access_list.uuid) not in str(access_list)