Compare commits
No commits in common. "92849d8396fbb23077416b7b55bea9acd0fba05f" and "fc58a9149084616c0c837f60b9787207e5153c76" have entirely different histories.
92849d8396
...
fc58a91490
|
@ -13,7 +13,7 @@ from amanuensis.errors import ArgumentError
|
||||||
RE_ALPHANUM_DASH_UNDER = re.compile(r'^[A-Za-z0-9-_]*$')
|
RE_ALPHANUM_DASH_UNDER = re.compile(r'^[A-Za-z0-9-_]*$')
|
||||||
|
|
||||||
|
|
||||||
def create(
|
def create_lexicon(
|
||||||
db: DbContext,
|
db: DbContext,
|
||||||
name: str,
|
name: str,
|
||||||
title: str,
|
title: str,
|
||||||
|
|
|
@ -15,7 +15,7 @@ RE_NO_LETTERS = re.compile(r'^[0-9-_]*$')
|
||||||
RE_ALPHANUM_DASH_UNDER = re.compile(r'^[A-Za-z0-9-_]*$')
|
RE_ALPHANUM_DASH_UNDER = re.compile(r'^[A-Za-z0-9-_]*$')
|
||||||
|
|
||||||
|
|
||||||
def create(
|
def create_user(
|
||||||
db: DbContext,
|
db: DbContext,
|
||||||
username: str,
|
username: str,
|
||||||
password: str,
|
password: str,
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
|
import datetime
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
|
|
||||||
from amanuensis.db import *
|
from amanuensis.db import *
|
||||||
|
import amanuensis.backend.lexicon as lexiq
|
||||||
|
import amanuensis.backend.user as userq
|
||||||
|
from amanuensis.errors import ArgumentError
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -22,3 +27,77 @@ def test_create(db):
|
||||||
assert db.session.query(func.count(ArticleIndexRule.id)).scalar() == 0
|
assert db.session.query(func.count(ArticleIndexRule.id)).scalar() == 0
|
||||||
assert db.session.query(func.count(ArticleContentRule.id)).scalar() == 0
|
assert db.session.query(func.count(ArticleContentRule.id)).scalar() == 0
|
||||||
assert db.session.query(func.count(Post.id)).scalar() == 0
|
assert db.session.query(func.count(Post.id)).scalar() == 0
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
||||||
|
userq.create_user(db, **{**kwargs, 'username': 'me'})
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
userq.create_user(db, **{**kwargs, 'username': 'the right honorable user-name, esquire'})
|
||||||
|
# Test allowed characters
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
userq.create_user(db, **{**kwargs, 'username': 'user name'})
|
||||||
|
# No password
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
userq.create_user(db, **{**kwargs, 'password': None})
|
||||||
|
|
||||||
|
# Valid creation works and populates fields
|
||||||
|
new_user = userq.create_user(db, **kwargs)
|
||||||
|
assert new_user
|
||||||
|
assert new_user.id is not None
|
||||||
|
assert new_user.created is not None
|
||||||
|
|
||||||
|
# No duplicate usernames
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
duplicate = userq.create_user(db, **kwargs)
|
||||||
|
|
||||||
|
# Missing display name populates with username
|
||||||
|
user2_kw = {**kwargs, 'username': 'user2', 'display_name': None}
|
||||||
|
user2 = userq.create_user(db, **user2_kw)
|
||||||
|
assert user2.display_name is not None
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_lexicon(db):
|
||||||
|
"""Test new game creation."""
|
||||||
|
kwargs = {
|
||||||
|
'name': 'Test',
|
||||||
|
'title': None,
|
||||||
|
'prompt': 'A test Lexicon game'
|
||||||
|
}
|
||||||
|
# Test name constraints
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
lexiq.create_lexicon(db, **{**kwargs, 'name': None})
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
lexiq.create_lexicon(db, **{**kwargs, 'name': ''})
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
lexiq.create_lexicon(db, **{**kwargs, 'name': ' '})
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
lexiq.create_lexicon(db, **{**kwargs, 'name': '..'})
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
lexiq.create_lexicon(db, **{**kwargs, 'name': '\x00'})
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
lexiq.create_lexicon(db, **{**kwargs, 'name': 'space in name'})
|
||||||
|
|
||||||
|
# Validate that creation populates fields, including timestamps
|
||||||
|
before = datetime.datetime.utcnow() - datetime.timedelta(seconds=1)
|
||||||
|
new_lexicon = lexiq.create_lexicon(db, **kwargs)
|
||||||
|
after = datetime.datetime.utcnow() + datetime.timedelta(seconds=1)
|
||||||
|
assert new_lexicon
|
||||||
|
assert new_lexicon.id is not None
|
||||||
|
assert new_lexicon.created is not None
|
||||||
|
assert before < new_lexicon.created
|
||||||
|
assert new_lexicon.created < after
|
||||||
|
|
||||||
|
# No duplicate lexicon names
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
duplicate = lexiq.create_lexicon(db, **kwargs)
|
||||||
|
|
|
@ -1,45 +0,0 @@
|
||||||
import datetime
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from amanuensis.db import *
|
|
||||||
import amanuensis.backend.lexicon as lexiq
|
|
||||||
from amanuensis.errors import ArgumentError
|
|
||||||
|
|
||||||
from .test_db import db
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_lexicon(db):
|
|
||||||
"""Test new game creation."""
|
|
||||||
kwargs = {
|
|
||||||
'name': 'Test',
|
|
||||||
'title': None,
|
|
||||||
'prompt': 'A test Lexicon game'
|
|
||||||
}
|
|
||||||
# Test name constraints
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
lexiq.create(db, **{**kwargs, 'name': None})
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
lexiq.create(db, **{**kwargs, 'name': ''})
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
lexiq.create(db, **{**kwargs, 'name': ' '})
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
lexiq.create(db, **{**kwargs, 'name': '..'})
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
lexiq.create(db, **{**kwargs, 'name': '\x00'})
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
lexiq.create(db, **{**kwargs, 'name': 'space in name'})
|
|
||||||
|
|
||||||
# Validate that creation populates fields, including timestamps
|
|
||||||
before = datetime.datetime.utcnow() - datetime.timedelta(seconds=1)
|
|
||||||
new_lexicon = lexiq.create(db, **kwargs)
|
|
||||||
after = datetime.datetime.utcnow() + datetime.timedelta(seconds=1)
|
|
||||||
assert new_lexicon
|
|
||||||
assert new_lexicon.id is not None
|
|
||||||
assert new_lexicon.created is not None
|
|
||||||
assert before < new_lexicon.created
|
|
||||||
assert new_lexicon.created < after
|
|
||||||
|
|
||||||
# No duplicate lexicon names
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
duplicate = lexiq.create(db, **kwargs)
|
|
|
@ -1,53 +0,0 @@
|
||||||
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):
|
|
||||||
userq.create(db, **{**kwargs, 'username': 'me'})
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
userq.create(db, **{**kwargs, 'username': 'the right honorable user-name, esquire'})
|
|
||||||
# Test allowed characters
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
userq.create(db, **{**kwargs, 'username': 'user name'})
|
|
||||||
# No password
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
userq.create(db, **{**kwargs, 'password': None})
|
|
||||||
|
|
||||||
# Valid creation works and populates fields
|
|
||||||
new_user = userq.create(db, **kwargs)
|
|
||||||
assert new_user
|
|
||||||
assert new_user.id is not None
|
|
||||||
assert new_user.created is not None
|
|
||||||
|
|
||||||
# No duplicate usernames
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
duplicate = userq.create(db, **kwargs)
|
|
||||||
|
|
||||||
# Missing display name populates with username
|
|
||||||
user2_kw = {**kwargs, 'username': 'user2', 'display_name': None}
|
|
||||||
user2 = userq.create(db, **user2_kw)
|
|
||||||
assert user2.display_name is not None
|
|
Loading…
Reference in New Issue