blob: a609a7fa393c71dc6b5fdfaf2cc9035423068f1b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
import os
import sys
import pytest
import random
import string
from flask_security.utils import hash_password
mypath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, mypath + "/../")
from app import create_app # noqa: E402
from models import db as _db # noqa: E402
from models import Role, User # noqa: E402
from dbseed import make_db_seed # noqa: E402
from models import user_datastore # noqa: E402
@pytest.yield_fixture(scope="session")
def app():
cfg = os.getenv("CONFIGTEST", "tests/config_test.py")
app = create_app(cfg)
ctx = app.app_context()
ctx.push()
yield app
ctx.pop()
@pytest.yield_fixture(scope="session")
def db(app):
_db.drop_all()
_db.engine.connect().execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";')
_db.create_all()
make_db_seed(_db)
yield _db
_db.drop_all()
@pytest.fixture(scope="session")
def client(app):
return app.test_client()
@pytest.yield_fixture(scope="function")
def session(db):
connection = db.engine.connect()
transaction = connection.begin()
options = dict(bind=connection)
session = db.create_scoped_session(options=options)
db.session = session
yield session
transaction.rollback()
connection.close()
session.remove()
def test_user_slugs(client, session):
"""Mass test user slugs"""
role = Role.query.filter(Role.name == "user").first()
ids = []
for count in range(50):
suffix = "".join(random.choices(string.ascii_letters + string.digits, k=20))
username = f"test_slug_{count}_{suffix}"
u = user_datastore.create_user(
name=username, email=f"test_slug_{count}@localhost", password=hash_password(f"slug_{count}"), roles=[role]
)
session.commit()
assert u.id >= 0
ids.append(u.id)
# Check
for i in ids:
user = User.query.filter(User.id == i).first()
assert user.slug != ""
assert user.slug is not None
assert len(user.slug) >= 15
|