summaryrefslogtreecommitdiffstats
path: root/api/tests/conftest.py
blob: 5290d7734266a2d68fe233b3c4e67a824f7fed33 (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
83
84
85
import os
import sys
import pytest
import random
import string
import uuid
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 commands.db_datas import make_db_seed  # noqa: E402
from models import user_datastore  # noqa: E402


@pytest.fixture(scope="session")
def app():
    app = create_app(config_filename="config.testing.Config")
    ctx = app.app_context()
    ctx.push()

    yield app

    ctx.pop()


@pytest.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.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