[Test] Reading default database from file.

* Providing default database with dummy data for testing
* Added test for login and decorator
This commit is contained in:
Ferdinand Thiessen 2021-01-25 13:12:04 +01:00
parent 5b57278721
commit a91f820b7f
3 changed files with 45 additions and 13 deletions

View File

@ -2,25 +2,37 @@ import os
import tempfile import tempfile
import pytest import pytest
from flaschengeist import database
from flaschengeist.app import create_app, install_all from flaschengeist.app import create_app, install_all
# read in SQL for populating test data
with open(os.path.join(os.path.dirname(__file__), "data.sql"), "r") as f:
_data_sql = []
__sql_command = ""
for line in f.readlines():
if not line.startswith("--"):
line = line.strip("\n")
__sql_command += line.strip("\n")
if __sql_command.endswith(";"):
_data_sql.append(__sql_command)
__sql_command = ""
@pytest.fixture @pytest.fixture
def app(): def app():
db_fd, db_path = tempfile.mkstemp() db_fd, db_path = tempfile.mkstemp()
app = create_app( app = create_app({"TESTING": True, "DATABASE": {"file_path": f"/{db_path}"}, "LOGGING": {"level": "DEBUG"}})
{
"TESTING": True,
"DATABASE": {"file_path": f"/{db_path}"},
"FLASCHENGEIST": {"auth": "auth_plain"},
"auth_plain": {"enabled": True},
}
)
with app.app_context(): with app.app_context():
install_all() install_all()
engine = database.db.engine
with engine.connect() as connection:
for statement in _data_sql:
connection.execute(statement)
yield app yield app
os.close(db_fd) os.close(db_fd)
os.unlink(db_path) # os.unlink(db_path)
print(db_path)
@pytest.fixture @pytest.fixture

4
tests/data.sql Normal file
View File

@ -0,0 +1,4 @@
INSERT INTO user ('userid', 'firstname', 'lastname', 'mail', 'id') VALUES ('user', 'Max', 'Mustermann', 'abc@def.gh', 1);
-- Password = 1234
INSERT INTO user_attribute VALUES(1,1,'password',X'800495c4000000000000008cc0373731346161336536623932613830366664353038656631323932623134393936393561386463353536623037363761323037623238346264623833313265323333373066376233663462643332666332653766303537333564366335393133366463366234356539633865613835643661643435343931376636626663343163653333643635646530386634396231323061316236386162613164373663663333306564306463303737303733336136353363393538396536343266393865942e');
INSERT INTO session ('expires', 'token', 'lifetime', 'id', 'user_id') VALUES ('2999-01-01 00:00:00', 'f4ecbe14be3527ca998143a49200e294', 600, 1, 1);

View File

@ -1,9 +1,23 @@
import pytest import pytest
# with app.app_context():
# engine = database.db.engine VALID_TOKEN = "f4ecbe14be3527ca998143a49200e294"
# with engine.connect() as connection: USERID = "user"
# connection.execute("") PASSWORD = "1234"
@pytest.mark.depends(on=["test_login_decorator"])
def test_login(client):
"""Testing login"""
result = client.post("/auth", json={"userid": USERID, "password": PASSWORD})
json = result.get_json()
# Login successful
assert result.status_code == 201
# User set correctly
assert json["user"]["userid"] == USERID
# Token works
assert client.get("/auth", headers={"Authorization": f"Bearer {json['session']['token']}"}).status_code == 200
def test_login_decorator(client): def test_login_decorator(client):
@ -14,3 +28,5 @@ def test_login_decorator(client):
assert client.get("/auth", headers={"Authorization": "INVALID"}).status_code == 401 assert client.get("/auth", headers={"Authorization": "INVALID"}).status_code == 401
# Invalid Token # Invalid Token
assert client.get("/auth", headers={"Authorization": "Bearer INVALID"}).status_code == 401 assert client.get("/auth", headers={"Authorization": "Bearer INVALID"}).status_code == 401
# Valid Token
assert client.get("/auth", headers={"Authorization": f"Bearer {VALID_TOKEN}"}).status_code == 200