amanuensis/tests/test_user.py

53 lines
1.5 KiB
Python
Raw Normal View History

2021-06-03 04:31:42 +00:00
from amanuensis.db.models import User
2021-05-28 01:56:29 +00:00
import pytest
2021-05-31 17:52:37 +00:00
from amanuensis.db import DbContext
2021-05-28 01:56:29 +00:00
import amanuensis.backend.user as userq
from amanuensis.errors import ArgumentError
2021-05-31 17:52:37 +00:00
def test_create_user(db: DbContext):
2021-05-28 01:56:29 +00:00
"""Test new user creation."""
2021-06-03 04:31:42 +00:00
defaults: dict = {
"db": db,
2021-06-03 03:10:34 +00:00
"username": "username",
"password": "password",
"display_name": "User Name",
"email": "user@example.com",
"is_site_admin": False,
2021-05-28 01:56:29 +00:00
}
2021-06-03 04:31:42 +00:00
kwargs: dict
2021-05-28 01:56:29 +00:00
# Test length constraints
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "username": "me"}
userq.create(**kwargs)
2021-05-28 01:56:29 +00:00
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "username": "the right honorable user-name, esquire"}
userq.create(**kwargs)
2021-05-28 01:56:29 +00:00
# Test allowed characters
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "username": "user name"}
userq.create(**kwargs)
2021-05-28 01:56:29 +00:00
# No password
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "password": None}
userq.create(**kwargs)
2021-05-28 01:56:29 +00:00
# Valid creation works and populates fields
2021-06-03 04:31:42 +00:00
new_user = userq.create(**defaults)
2021-05-28 01:56:29 +00:00
assert new_user
assert new_user.id is not None
assert new_user.created is not None
# No duplicate usernames
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
userq.create(**defaults)
2021-05-28 01:56:29 +00:00
# Missing display name populates with username
2021-06-03 04:31:42 +00:00
user2_kw: dict = {**defaults, "username": "user2", "display_name": None}
user2: User = userq.create(**user2_kw)
2021-05-28 01:56:29 +00:00
assert user2.display_name is not None