summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMia Herkt <mia@0x0.st>2020-12-30 03:03:03 +0100
committerMia Herkt <mia@0x0.st>2021-05-23 19:13:51 +0200
commitb65209db7bd63084af3f85dbcd336fe62bf14d2c (patch)
treed7a86268b6abaed30a04ba482f473715b06fa7db
parent3bdbab96c10f5ad01cb46c2af3009564b7d27997 (diff)
Add tests
-rw-r--r--pyproject.toml2
-rw-r--r--tests/__init__.py0
-rw-r--r--tests/test_client.py81
3 files changed, 83 insertions, 0 deletions
diff --git a/pyproject.toml b/pyproject.toml
new file mode 100644
index 0000000..21f57cf
--- /dev/null
+++ b/pyproject.toml
@@ -0,0 +1,2 @@
+[tool.pytest.ini_options]
+log_level = "INFO"
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/__init__.py
diff --git a/tests/test_client.py b/tests/test_client.py
new file mode 100644
index 0000000..40041ce
--- /dev/null
+++ b/tests/test_client.py
@@ -0,0 +1,81 @@
+import pytest
+import tempfile
+import os
+from flask_migrate import upgrade as db_upgrade
+from io import BytesIO
+
+from fhost import app, db, url_for, File, URL
+
+@pytest.fixture
+def client():
+ with tempfile.TemporaryDirectory() as tmpdir:
+ app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{tmpdir}/db.sqlite"
+ app.config["FHOST_STORAGE_PATH"] = os.path.join(tmpdir, "up")
+ app.config["TESTING"] = True
+
+ with app.test_client() as client:
+ with app.app_context():
+ db_upgrade()
+ yield client
+
+def test_client(client):
+ payloads = [
+ ({ "file" : (BytesIO(b"hello"), "hello.txt") }, 200, b"https://localhost/E.txt\n"),
+ ({ "file" : (BytesIO(b"hello"), "hello.ignorethis") }, 200, b"https://localhost/E.txt\n"),
+ ({ "file" : (BytesIO(b"bye"), "bye.truncatethis") }, 200, b"https://localhost/Q.truncate\n"),
+ ({ "file" : (BytesIO(b"hi"), "hi.tar.gz") }, 200, b"https://localhost/h.tar.gz\n"),
+ ({ "file" : (BytesIO(b"lea!"), "lea!") }, 200, b"https://localhost/d.txt\n"),
+ ({ "file" : (BytesIO(b"why?"), "balls", "application/x-dosexec") }, 415, None),
+ ({ "shorten" : "https://0x0.st" }, 200, b"https://localhost/E\n"),
+ ({ "shorten" : "https://localhost" }, 400, None),
+ ({}, 400, None),
+ ]
+
+ for p, s, r in payloads:
+ rv = client.post("/", buffered=True,
+ content_type="multipart/form-data",
+ data=p)
+ assert rv.status_code == s
+ if r:
+ assert rv.data == r
+
+ f = File.query.get(2)
+ f.removed = True
+ db.session.add(f)
+ db.session.commit()
+
+ rq = [
+ (200, [
+ "/",
+ "robots.txt",
+ "E.txt",
+ "E.txt/test",
+ "E.txt/test.py",
+ "d.txt",
+ "h.tar.gz",
+ ]),
+ (302, [
+ "E",
+ "E/test",
+ "E/test.bin",
+ ]),
+ (404, [
+ "test.bin",
+ "test.bin/test",
+ "test.bin/test.py",
+ "test",
+ "test/test",
+ "test.bin/test.py",
+ "E.bin",
+ ]),
+ (451, [
+ "Q.truncate",
+ ]),
+ ]
+
+ for code, paths in rq:
+ for p in paths:
+ app.logger.info(f"GET {p}")
+ rv = client.get(p)
+ assert rv.status_code == code
+