From a91f820b7f5509e3cf11fa2e75a8e96d470d8915 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Mon, 25 Jan 2021 13:12:04 +0100 Subject: [PATCH] [Test] Reading default database from file. * Providing default database with dummy data for testing * Added test for login and decorator --- tests/conftest.py | 30 +++++++++++++++++++++--------- tests/data.sql | 4 ++++ tests/test_auth.py | 24 ++++++++++++++++++++---- 3 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 tests/data.sql diff --git a/tests/conftest.py b/tests/conftest.py index 0c0931b..b99d8d6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,25 +2,37 @@ import os import tempfile import pytest +from flaschengeist import database 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 def app(): db_fd, db_path = tempfile.mkstemp() - app = create_app( - { - "TESTING": True, - "DATABASE": {"file_path": f"/{db_path}"}, - "FLASCHENGEIST": {"auth": "auth_plain"}, - "auth_plain": {"enabled": True}, - } - ) + app = create_app({"TESTING": True, "DATABASE": {"file_path": f"/{db_path}"}, "LOGGING": {"level": "DEBUG"}}) with app.app_context(): install_all() + engine = database.db.engine + with engine.connect() as connection: + for statement in _data_sql: + connection.execute(statement) yield app os.close(db_fd) - os.unlink(db_path) + # os.unlink(db_path) + print(db_path) @pytest.fixture diff --git a/tests/data.sql b/tests/data.sql new file mode 100644 index 0000000..6c6a989 --- /dev/null +++ b/tests/data.sql @@ -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); \ No newline at end of file diff --git a/tests/test_auth.py b/tests/test_auth.py index 6dcf468..5e7cadb 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -1,9 +1,23 @@ import pytest -# with app.app_context(): -# engine = database.db.engine -# with engine.connect() as connection: -# connection.execute("") + +VALID_TOKEN = "f4ecbe14be3527ca998143a49200e294" +USERID = "user" +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): @@ -14,3 +28,5 @@ def test_login_decorator(client): assert client.get("/auth", headers={"Authorization": "INVALID"}).status_code == 401 # Invalid Token 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