From 1fa5747647f955237b64a72d68700cfae79cb06a Mon Sep 17 00:00:00 2001 From: Ethan Paul Date: Mon, 24 Feb 2020 23:54:28 -0500 Subject: [PATCH] Add tests for unique field assurances on domain and account models --- tests/test_database_account.py | 41 ++++++++++++++++++++++ tests/test_database_domain.py | 63 ++++++++++++++++++++++++++++++++-- 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/tests/test_database_account.py b/tests/test_database_account.py index bf219c6..cad0220 100644 --- a/tests/test_database_account.py +++ b/tests/test_database_account.py @@ -1,4 +1,8 @@ +import copy + +import passlib import peewee +import pytest from fixtures import demo_database from keyosk import database @@ -60,3 +64,40 @@ def test_crypto(demo_database): database.Account.username == "jack.oneill@airforce.gov" ) 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() diff --git a/tests/test_database_domain.py b/tests/test_database_domain.py index 864dc32..16e1902 100644 --- a/tests/test_database_domain.py +++ b/tests/test_database_domain.py @@ -1,13 +1,15 @@ +import copy +import datetime + import peewee +import pytest 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) @@ -37,3 +39,60 @@ def test_formatting(demo_database): 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) + + +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])