mirror of
https://github.com/enpaul/keyosk.git
synced 2024-11-25 07:57:24 +00:00
Add tests for unique field assurances on domain and account models
This commit is contained in:
parent
822287ad3c
commit
1fa5747647
@ -1,4 +1,8 @@
|
|||||||
|
import copy
|
||||||
|
|
||||||
|
import passlib
|
||||||
import peewee
|
import peewee
|
||||||
|
import pytest
|
||||||
from fixtures import demo_database
|
from fixtures import demo_database
|
||||||
|
|
||||||
from keyosk import database
|
from keyosk import database
|
||||||
@ -60,3 +64,40 @@ def test_crypto(demo_database):
|
|||||||
database.Account.username == "jack.oneill@airforce.gov"
|
database.Account.username == "jack.oneill@airforce.gov"
|
||||||
)
|
)
|
||||||
assert account.verify_server_set_secret(new_autopass)
|
assert account.verify_server_set_secret(new_autopass)
|
||||||
|
|
||||||
|
|
||||||
|
def test_unique(demo_database):
|
||||||
|
new_base = database.Account(
|
||||||
|
username="garbage",
|
||||||
|
encrypted_client_set_secret=passlib.hash.pbkdf2_sha512.hash("garbage"),
|
||||||
|
encrypted_server_set_secret=passlib.hash.pbkdf2_sha512.hash("garbage"),
|
||||||
|
enabled=True,
|
||||||
|
extras={"gar": "bage"},
|
||||||
|
)
|
||||||
|
|
||||||
|
vader = database.Account.get(database.Account.username == "dvader")
|
||||||
|
|
||||||
|
unique = ["username"]
|
||||||
|
nonunique = ["extras"]
|
||||||
|
|
||||||
|
for item in unique:
|
||||||
|
new = copy.deepcopy(new_base)
|
||||||
|
setattr(new, item, getattr(vader, item))
|
||||||
|
# Using bulk create as a hacky work around for a weird issue. When using numeric
|
||||||
|
# TIDs peewee apparently raises an integrity error when calling ``save()``,
|
||||||
|
# however when using UUID TIDs it just returns 0 (for the number of edited rows)
|
||||||
|
# The second is the behavior as documented, but I want the integrity error. I
|
||||||
|
# don't care enough to figure out why its behaving differently here, and bulk
|
||||||
|
# create gives me that integrity error I'm after
|
||||||
|
with pytest.raises(peewee.IntegrityError):
|
||||||
|
with database.interface.atomic():
|
||||||
|
database.Account.bulk_create([new])
|
||||||
|
|
||||||
|
for item in nonunique:
|
||||||
|
new = copy.deepcopy(new_base)
|
||||||
|
setattr(new, item, getattr(vader, item))
|
||||||
|
with database.interface.atomic():
|
||||||
|
database.Account.bulk_create([new])
|
||||||
|
|
||||||
|
with database.interface.atomic():
|
||||||
|
new.delete_instance()
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
|
import copy
|
||||||
|
import datetime
|
||||||
|
|
||||||
import peewee
|
import peewee
|
||||||
|
import pytest
|
||||||
from fixtures import demo_database
|
from fixtures import demo_database
|
||||||
|
|
||||||
from keyosk import database
|
from keyosk import database
|
||||||
|
|
||||||
|
|
||||||
def test_meta():
|
def test_meta():
|
||||||
|
|
||||||
models = [database.Domain, database.DomainAccessList, database.DomainPermission]
|
models = [database.Domain, database.DomainAccessList, database.DomainPermission]
|
||||||
|
|
||||||
for model in models:
|
for model in models:
|
||||||
for key in model.dict_keys():
|
for key in model.dict_keys():
|
||||||
assert hasattr(model, key)
|
assert hasattr(model, key)
|
||||||
@ -37,3 +39,60 @@ def test_formatting(demo_database):
|
|||||||
for access_list in database.DomainAccessList.select():
|
for access_list in database.DomainAccessList.select():
|
||||||
assert list(dict(access_list).keys()) == database.DomainAccessList.dict_keys()
|
assert list(dict(access_list).keys()) == database.DomainAccessList.dict_keys()
|
||||||
assert str(access_list.uuid) not in str(access_list)
|
assert str(access_list.uuid) not in str(access_list)
|
||||||
|
|
||||||
|
|
||||||
|
def test_unique(demo_database):
|
||||||
|
new_base = database.Domain(
|
||||||
|
name="garbage",
|
||||||
|
audience="garbage",
|
||||||
|
title="garbage",
|
||||||
|
description="garbage",
|
||||||
|
contact="garbage",
|
||||||
|
enabled=True,
|
||||||
|
enable_client_set_auth=True,
|
||||||
|
enable_server_set_auth=True,
|
||||||
|
enable_refresh=True,
|
||||||
|
lifespan_access=datetime.timedelta(minutes=30),
|
||||||
|
lifespan_refresh=datetime.timedelta(days=30),
|
||||||
|
)
|
||||||
|
|
||||||
|
starwars = database.Domain.get(database.Domain.name == "star-wars")
|
||||||
|
|
||||||
|
unique = ["name", "audience"]
|
||||||
|
nonunique = ["title", "description", "contact"]
|
||||||
|
|
||||||
|
for item in unique:
|
||||||
|
new = copy.deepcopy(new_base)
|
||||||
|
setattr(new, item, getattr(starwars, item))
|
||||||
|
with pytest.raises(peewee.IntegrityError):
|
||||||
|
with database.interface.atomic():
|
||||||
|
database.Domain.bulk_create([new])
|
||||||
|
|
||||||
|
for item in nonunique:
|
||||||
|
new = copy.deepcopy(new_base)
|
||||||
|
setattr(new, item, getattr(starwars, item))
|
||||||
|
with database.interface.atomic():
|
||||||
|
database.Domain.bulk_create([new])
|
||||||
|
|
||||||
|
with database.interface.atomic():
|
||||||
|
new.delete_instance()
|
||||||
|
|
||||||
|
|
||||||
|
def test_unique_access_lists(demo_database):
|
||||||
|
new_base = database.DomainAccessList(
|
||||||
|
name="imperial-star-destroyer",
|
||||||
|
domain=database.Domain.get(database.Domain.name == "star-wars"),
|
||||||
|
)
|
||||||
|
|
||||||
|
isd = database.DomainAccessList.get(
|
||||||
|
database.DomainAccessList.name == "imperial-star-destroyer"
|
||||||
|
)
|
||||||
|
|
||||||
|
unique = ["name"]
|
||||||
|
|
||||||
|
for item in unique:
|
||||||
|
new = copy.deepcopy(new_base)
|
||||||
|
setattr(new, item, getattr(isd, item))
|
||||||
|
with pytest.raises(peewee.IntegrityError):
|
||||||
|
with database.interface.atomic():
|
||||||
|
database.DomainAccessList.bulk_create([new])
|
||||||
|
Loading…
Reference in New Issue
Block a user