1
0
mirror of https://github.com/enpaul/keyosk.git synced 2024-11-24 23:47:49 +00:00

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
This commit is contained in:
Ethan Paul 2020-02-24 23:03:59 -05:00
parent e202152fc5
commit 54ac7c4141
2 changed files with 72 additions and 4 deletions

View File

@ -50,8 +50,28 @@ class Domain(KeyoskBaseModel):
enable_client_set_auth = peewee.BooleanField(null=False) enable_client_set_auth = peewee.BooleanField(null=False)
enable_server_set_auth = peewee.BooleanField(null=False) enable_server_set_auth = peewee.BooleanField(null=False)
enable_refresh = peewee.BooleanField(null=False) enable_refresh = peewee.BooleanField(null=False)
lifespan_access = peewee.IntegerField(null=False) _lifespan_access = peewee.IntegerField(null=False)
lifespan_refresh = 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 @staticmethod
def dict_keys() -> List[str]: def dict_keys() -> List[str]:
@ -65,8 +85,8 @@ class Domain(KeyoskBaseModel):
"description", "description",
"contact", "contact",
"enabled", "enabled",
"enable_password", "enable_client_set_auth",
"enable_autopassword", "enable_server_set_auth",
"enable_refresh", "enable_refresh",
"lifespan_access", "lifespan_access",
"lifespan_refresh", "lifespan_refresh",
@ -78,6 +98,9 @@ class Domain(KeyoskBaseModel):
def foreign_backref() -> List[str]: def foreign_backref() -> List[str]:
return ["access_lists", "permissions"] return ["access_lists", "permissions"]
def __str__(self) -> str:
return f"Domain '{self.name}' ({self.uuid})"
class DomainAccessList(KeyoskBaseModel): class DomainAccessList(KeyoskBaseModel):
"""Access list name model definition """Access list name model definition
@ -96,6 +119,9 @@ class DomainAccessList(KeyoskBaseModel):
def dict_keys() -> List[str]: def dict_keys() -> List[str]:
return ["name"] return ["name"]
def __str__(self) -> str:
return self.name
class DomainPermission(KeyoskBaseModel): class DomainPermission(KeyoskBaseModel):
"""Permission name model definition """Permission name model definition
@ -116,3 +142,6 @@ class DomainPermission(KeyoskBaseModel):
@staticmethod @staticmethod
def dict_keys() -> List[str]: def dict_keys() -> List[str]:
return ["name", "bitindex"] return ["name", "bitindex"]
def __str__(self) -> str:
return self.name

View File

@ -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)