summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py53
-rw-r--r--tests/data.sql4
-rw-r--r--tests/test_auth.py40
-rw-r--r--tests/test_db.py30
-rw-r--r--tests/test_factory.py11
5 files changed, 138 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)
diff --git a/tests/data.sql b/tests/data.sql
new file mode 100644
index 0000000..4579284
--- /dev/null
+++ b/tests/data.sql
@@ -0,0 +1,4 @@
+insert into user (username, password)
+values
+ ('test', 'pbkdf2:sha256:50000$TCI4GzcX$0de171a4f4dac32e3364c7ddc7c14f3e2fa61f2d17574483f7ffbb431b4acb2f'),
+ ('other', 'pbkdf2:sha256:50000$kJPKsz6N$d2d4784f1b030a9761f5ccaeeaca413f27f2ecb76d6168407af962ddce849f79');
diff --git a/tests/test_auth.py b/tests/test_auth.py
new file mode 100644
index 0000000..3fb2493
--- /dev/null
+++ b/tests/test_auth.py
@@ -0,0 +1,40 @@
+import pytest
+
+from flask import g, session, json
+
+from sw.db import get_db
+
+
+def test_register(client, app):
+ response = client.post(
+ "/auth/register",
+ data=json.dumps({"username": "a", "password": "a",}),
+ content_type="application/json",
+ )
+
+ # TODO assert "/u/a" in response.headers["Location"]
+
+ with app.app_context():
+ db_result = (
+ get_db().execute("select * from user where username='a'",).fetchone()
+ )
+ assert db_result is not None
+
+
+@pytest.mark.parametrize(
+ ("username", "password", "message"),
+ (
+ (None, "", b"Username required"),
+ ("", "", b"Username must not be empty"),
+ ("a", None, b"Password required"),
+ ("a", "", b"Password must not be empty"),
+ ("test", "test", b"Username test is taken"),
+ ),
+)
+def test_register_validate_input(client, username, password, message):
+ response = client.post(
+ "/auth/register",
+ data=json.dumps({"username": username, "password": password,}),
+ content_type="application/json",
+ )
+ assert message in response.data
diff --git a/tests/test_db.py b/tests/test_db.py
new file mode 100644
index 0000000..7a35593
--- /dev/null
+++ b/tests/test_db.py
@@ -0,0 +1,30 @@
+import pytest
+
+import sqlite3
+
+from sw.db import get_db
+
+
+def test_get_close_db(app):
+ with app.app_context():
+ db = get_db()
+ assert db is get_db()
+
+ with pytest.raises(sqlite3.ProgrammingError) as e:
+ db.execute("select 1")
+
+ assert "closed" in str(e.value)
+
+
+def test_init_db_command(runner, monkeypatch):
+ class Recorder(object):
+ called = False
+
+ def fake_init_db():
+ Recorder.called = True
+
+ monkeypatch.setattr("sw.db.init_db", fake_init_db)
+ result = runner.invoke(args=["init-db"])
+
+ assert "Initialised" in result.output
+ assert Recorder.called
diff --git a/tests/test_factory.py b/tests/test_factory.py
new file mode 100644
index 0000000..bf2b321
--- /dev/null
+++ b/tests/test_factory.py
@@ -0,0 +1,11 @@
+from sw import create_app
+
+
+def test_config():
+ assert not create_app().testing
+ assert create_app({"TESTING": True}).testing
+
+
+def test_hello(client):
+ response = client.post("/auth/login")
+ assert response.status_code == 422