2020-03-09 04:32:25 +00:00
|
|
|
# pylint: disable=unused-argument,redefined-outer-name,unused-import
|
2020-02-25 04:54:28 +00:00
|
|
|
import copy
|
|
|
|
|
|
|
|
import passlib
|
2020-02-25 03:38:28 +00:00
|
|
|
import peewee
|
2020-02-25 04:54:28 +00:00
|
|
|
import pytest
|
2020-02-25 03:38:28 +00:00
|
|
|
from fixtures import demo_database
|
|
|
|
|
|
|
|
from keyosk import database
|
|
|
|
|
|
|
|
|
|
|
|
def test_formatting(demo_database):
|
2020-03-09 04:14:29 +00:00
|
|
|
for account in database.KeyoskAccount.select():
|
2020-02-25 03:38:28 +00:00
|
|
|
assert str(account.uuid) in str(account)
|
|
|
|
assert account.username in str(account)
|
|
|
|
|
|
|
|
|
|
|
|
def test_extras(demo_database):
|
2020-03-09 04:14:29 +00:00
|
|
|
account = database.KeyoskAccount.get(
|
|
|
|
database.KeyoskAccount.username == "lskywalker"
|
|
|
|
)
|
2020-02-25 03:38:28 +00:00
|
|
|
|
|
|
|
new_extras = {"foo": "bar", "fizz": "buzz", "baz": False, "blop": 1234.567}
|
|
|
|
|
|
|
|
account.extras = new_extras
|
|
|
|
with database.interface.atomic():
|
|
|
|
account.save()
|
|
|
|
|
2020-03-09 04:14:29 +00:00
|
|
|
account = database.KeyoskAccount.get(
|
|
|
|
database.KeyoskAccount.username == "lskywalker"
|
2020-02-25 03:38:28 +00:00
|
|
|
)
|
2020-03-09 04:14:29 +00:00
|
|
|
assert account.extras == new_extras
|
2020-02-25 04:54:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_unique(demo_database):
|
2020-03-09 04:14:29 +00:00
|
|
|
new_base = database.KeyoskAccount(
|
2020-02-25 04:54:28 +00:00
|
|
|
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"},
|
|
|
|
)
|
|
|
|
|
2020-03-09 04:14:29 +00:00
|
|
|
vader = database.KeyoskAccount.get(database.KeyoskAccount.username == "dvader")
|
2020-02-25 04:54:28 +00:00
|
|
|
|
|
|
|
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():
|
2020-03-09 04:14:29 +00:00
|
|
|
database.KeyoskAccount.bulk_create([new])
|
2020-02-25 04:54:28 +00:00
|
|
|
|
|
|
|
for item in nonunique:
|
|
|
|
new = copy.deepcopy(new_base)
|
|
|
|
setattr(new, item, getattr(vader, item))
|
|
|
|
with database.interface.atomic():
|
2020-03-09 04:14:29 +00:00
|
|
|
database.KeyoskAccount.bulk_create([new])
|
2020-02-25 04:54:28 +00:00
|
|
|
|
|
|
|
with database.interface.atomic():
|
|
|
|
new.delete_instance()
|