2023-03-10 11:05:22 -05:00
|
|
|
import pytest
|
2023-03-10 18:17:03 -05:00
|
|
|
import yaml
|
2023-03-10 13:57:18 -05:00
|
|
|
from sachet.server.users import manage
|
2023-03-10 11:05:22 -05:00
|
|
|
from click.testing import CliRunner
|
|
|
|
from sachet.server import app, db
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-03-10 13:57:18 -05:00
|
|
|
def client():
|
2023-03-10 11:05:22 -05:00
|
|
|
"""Flask application with DB already set up and ready."""
|
|
|
|
with app.test_client() as client:
|
|
|
|
with app.app_context():
|
|
|
|
db.drop_all()
|
|
|
|
db.create_all()
|
|
|
|
db.session.commit()
|
2023-03-10 13:57:18 -05:00
|
|
|
yield client
|
2023-03-10 11:05:22 -05:00
|
|
|
db.session.remove()
|
|
|
|
db.drop_all()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def flask_app_bare():
|
|
|
|
"""Flask application with empty DB."""
|
|
|
|
with app.test_client() as client:
|
|
|
|
with app.app_context():
|
|
|
|
yield client
|
|
|
|
db.drop_all()
|
|
|
|
|
|
|
|
|
2023-03-10 13:57:18 -05:00
|
|
|
@pytest.fixture
|
|
|
|
def users(client):
|
|
|
|
"""Creates all the test users.
|
|
|
|
|
|
|
|
Returns a dictionary with all the info for each user.
|
|
|
|
"""
|
|
|
|
userinfo = {
|
|
|
|
"jeff": {
|
|
|
|
"password": "1234",
|
|
|
|
"admin": False
|
|
|
|
},
|
|
|
|
"administrator": {
|
|
|
|
"password": "4321",
|
|
|
|
"admin": True
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for user, info in userinfo.items():
|
|
|
|
info["username"] = user
|
|
|
|
manage.create_user(
|
|
|
|
info["admin"],
|
|
|
|
info["username"],
|
|
|
|
info["password"]
|
|
|
|
)
|
|
|
|
|
|
|
|
return userinfo
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def validate_info(users):
|
|
|
|
"""Given a dictionary, validate the information against a given user's info."""
|
|
|
|
def _validate(user, info):
|
|
|
|
for k, v in info.items():
|
|
|
|
assert users[user][k] == v
|
|
|
|
|
|
|
|
return _validate
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def tokens(client, users):
|
|
|
|
"""Logs in all test users.
|
|
|
|
|
|
|
|
Returns a dictionary of auth tokens for all test users.
|
|
|
|
"""
|
|
|
|
|
|
|
|
toks = {}
|
|
|
|
|
|
|
|
for user, creds in users.items():
|
|
|
|
resp = client.post("/users/login", json={
|
|
|
|
"username": creds["username"],
|
|
|
|
"password": creds["password"]
|
|
|
|
})
|
|
|
|
resp_json = resp.get_json()
|
|
|
|
token = resp_json.get("auth_token")
|
|
|
|
assert token is not None and token != ""
|
|
|
|
toks[creds["username"]] = token
|
|
|
|
|
|
|
|
return toks
|
|
|
|
|
|
|
|
|
2023-03-10 11:05:22 -05:00
|
|
|
@pytest.fixture
|
|
|
|
def cli():
|
|
|
|
"""click's testing fixture"""
|
|
|
|
return CliRunner()
|