2021-05-28 01:56:29 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from amanuensis.db import *
|
|
|
|
import amanuensis.backend.lexicon as lexiq
|
|
|
|
import amanuensis.backend.user as userq
|
|
|
|
from amanuensis.errors import ArgumentError
|
|
|
|
|
|
|
|
from .test_db import db
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def db():
|
|
|
|
db = DbContext('sqlite:///:memory:', debug=True)
|
|
|
|
db.create_all()
|
|
|
|
return db
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_user(db):
|
|
|
|
"""Test new user creation."""
|
|
|
|
kwargs = {
|
|
|
|
'username': 'username',
|
|
|
|
'password': 'password',
|
|
|
|
'display_name': 'User Name',
|
|
|
|
'email': 'user@example.com',
|
|
|
|
'is_site_admin': False
|
|
|
|
}
|
|
|
|
|
|
|
|
# Test length constraints
|
|
|
|
with pytest.raises(ArgumentError):
|
2021-05-29 23:53:27 +00:00
|
|
|
userq.create(db, **{**kwargs, 'username': 'me'})
|
2021-05-28 01:56:29 +00:00
|
|
|
with pytest.raises(ArgumentError):
|
2021-05-29 23:53:27 +00:00
|
|
|
userq.create(db, **{**kwargs, 'username': 'the right honorable user-name, esquire'})
|
2021-05-28 01:56:29 +00:00
|
|
|
# Test allowed characters
|
|
|
|
with pytest.raises(ArgumentError):
|
2021-05-29 23:53:27 +00:00
|
|
|
userq.create(db, **{**kwargs, 'username': 'user name'})
|
2021-05-28 01:56:29 +00:00
|
|
|
# No password
|
|
|
|
with pytest.raises(ArgumentError):
|
2021-05-29 23:53:27 +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
|
|
|
|
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
|