From 62815d9278a8fd67c2cc3b56c1e28ea51b6d2f3a Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sat, 23 Jan 2021 02:18:44 +0100 Subject: [PATCH] [System][Tests]: Added infrastructure for tests, added first unit test --- setup.py | 2 +- tests/conftest.py | 28 ++++++++++++++++++++++++++++ tests/test_auth.py | 16 ++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/conftest.py create mode 100644 tests/test_auth.py diff --git a/setup.py b/setup.py index d97177b..97d3100 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ setup( # Needed for python < 3.7 "backports-datetime-fromisoformat", ], - extras_require={"ldap": ["flask_ldapconn", "ldap3"]}, + extras_require={"ldap": ["flask_ldapconn", "ldap3"], "tests": ["pytest"]}, entry_points={ "flaschengeist.plugin": [ # Authentication providers diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..0c0931b --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,28 @@ +import os +import tempfile +import pytest + +from flaschengeist.app import create_app, install_all + + +@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}, + } + ) + with app.app_context(): + install_all() + yield app + os.close(db_fd) + os.unlink(db_path) + + +@pytest.fixture +def client(app): + return app.test_client() diff --git a/tests/test_auth.py b/tests/test_auth.py new file mode 100644 index 0000000..6dcf468 --- /dev/null +++ b/tests/test_auth.py @@ -0,0 +1,16 @@ +import pytest + +# with app.app_context(): +# engine = database.db.engine +# with engine.connect() as connection: +# connection.execute("") + + +def test_login_decorator(client): + """Testing the login_required decorator""" + # No header at all + assert client.get("/auth").status_code == 401 + # Invalid header + assert client.get("/auth", headers={"Authorization": "INVALID"}).status_code == 401 + # Invalid Token + assert client.get("/auth", headers={"Authorization": "Bearer INVALID"}).status_code == 401