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:
parent
e202152fc5
commit
54ac7c4141
@ -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
|
||||
|
39
tests/test_database_domain.py
Normal file
39
tests/test_database_domain.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user