summaryrefslogtreecommitdiff
path: root/tests/conftest.py
diff options
context:
space:
mode:
authorTom Harley2021-07-03 00:18:13 +0000
committerTom Harley2021-07-03 00:18:13 +0000
commit7c5c6c52250ca57257ab4af2cc9cdf4425595ec0 (patch)
treed36019286125bf984e0c267645d53ff562533ebf /tests/conftest.py
downloadswap-7c5c6c52250ca57257ab4af2cc9cdf4425595ec0.tar.gz
swap-7c5c6c52250ca57257ab4af2cc9cdf4425595ec0.zip

Initial commit

Diffstat (limited to 'tests/conftest.py')
-rw-r--r--tests/conftest.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000..fb8c751
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,53 @@
+import os
+import tempfile
+
+import pytest
+from sw import create_app
+from sw.db import get_db, init_db
+
+with open(os.path.join(os.path.dirname(__file__), "data.sql"), "rb") as f:
+ _data_sql = f.read().decode("utf8")
+
+
+@pytest.fixture
+def app():
+ db_fd, db_path = tempfile.mkstemp()
+
+ app = create_app({"TESTING": True, "DATABASE": db_path,})
+
+ with app.app_context():
+ init_db()
+ get_db().executescript(_data_sql)
+
+ yield app
+
+ os.close(db_fd)
+ os.unlink(db_path)
+
+
+@pytest.fixture
+def client(app):
+ return app.test_client()
+
+
+@pytest.fixture
+def runner(app):
+ return app.test_cli_runner()
+
+
+class AuthActions(object):
+ def __init__(self, client):
+ self._client = client
+
+ def login(self, username="test", password="test"):
+ return self._client.post(
+ "/auth/login", data=jsonify({"username": username, "password": password,})
+ )
+
+ def logout(self):
+ return self._client.get("/auth/logout")
+
+
+@pytest.fixture
+def auth(client):
+ return AuthActions(client)