amanuensis/tests/test_user.py

46 lines
1.4 KiB
Python
Raw Normal View History

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."""
kwargs = {
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
}
# Test length constraints
with pytest.raises(ArgumentError):
2021-06-03 03:10:34 +00:00
userq.create(db, **{**kwargs, "username": "me"})
2021-05-28 01:56:29 +00:00
with pytest.raises(ArgumentError):
2021-06-03 03:05:33 +00:00
userq.create(
2021-06-03 03:10:34 +00:00
db, **{**kwargs, "username": "the right honorable user-name, esquire"}
2021-06-03 03:05:33 +00:00
)
2021-05-28 01:56:29 +00:00
# Test allowed characters
with pytest.raises(ArgumentError):
2021-06-03 03:10:34 +00:00
userq.create(db, **{**kwargs, "username": "user name"})
2021-05-28 01:56:29 +00:00
# No password
with pytest.raises(ArgumentError):
2021-06-03 03:10:34 +00:00
userq.create(db, **{**kwargs, "password": None})
2021-05-28 01:56:29 +00:00
# Valid creation works and populates fields
2021-05-29 23:53:27 +00:00
new_user = userq.create(db, **kwargs)
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-05-29 23:53:27 +00:00
duplicate = userq.create(db, **kwargs)
2021-05-28 01:56:29 +00:00
# Missing display name populates with username
2021-06-03 03:10:34 +00:00
user2_kw = {**kwargs, "username": "user2", "display_name": None}
2021-05-29 23:53:27 +00:00
user2 = userq.create(db, **user2_kw)
2021-05-28 01:56:29 +00:00
assert user2.display_name is not None