tests/__init__.py
tests/__init__.py
This is a binary file and will not be displayed.
+81
tests/test_client.py
+81
tests/test_client.py
···
1
+
import pytest
2
+
import tempfile
3
+
import os
4
+
from flask_migrate import upgrade as db_upgrade
5
+
from io import BytesIO
6
+
7
+
from fhost import app, db, url_for, File, URL
8
+
9
+
@pytest.fixture
10
+
def client():
11
+
with tempfile.TemporaryDirectory() as tmpdir:
12
+
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{tmpdir}/db.sqlite"
13
+
app.config["FHOST_STORAGE_PATH"] = os.path.join(tmpdir, "up")
14
+
app.config["TESTING"] = True
15
+
16
+
with app.test_client() as client:
17
+
with app.app_context():
18
+
db_upgrade()
19
+
yield client
20
+
21
+
def test_client(client):
22
+
payloads = [
23
+
({ "file" : (BytesIO(b"hello"), "hello.txt") }, 200, b"https://localhost/E.txt\n"),
24
+
({ "file" : (BytesIO(b"hello"), "hello.ignorethis") }, 200, b"https://localhost/E.txt\n"),
25
+
({ "file" : (BytesIO(b"bye"), "bye.truncatethis") }, 200, b"https://localhost/Q.truncate\n"),
26
+
({ "file" : (BytesIO(b"hi"), "hi.tar.gz") }, 200, b"https://localhost/h.tar.gz\n"),
27
+
({ "file" : (BytesIO(b"lea!"), "lea!") }, 200, b"https://localhost/d.txt\n"),
28
+
({ "file" : (BytesIO(b"why?"), "balls", "application/x-dosexec") }, 415, None),
29
+
({ "shorten" : "https://0x0.st" }, 200, b"https://localhost/E\n"),
30
+
({ "shorten" : "https://localhost" }, 400, None),
31
+
({}, 400, None),
32
+
]
33
+
34
+
for p, s, r in payloads:
35
+
rv = client.post("/", buffered=True,
36
+
content_type="multipart/form-data",
37
+
data=p)
38
+
assert rv.status_code == s
39
+
if r:
40
+
assert rv.data == r
41
+
42
+
f = File.query.get(2)
43
+
f.removed = True
44
+
db.session.add(f)
45
+
db.session.commit()
46
+
47
+
rq = [
48
+
(200, [
49
+
"/",
50
+
"robots.txt",
51
+
"E.txt",
52
+
"E.txt/test",
53
+
"E.txt/test.py",
54
+
"d.txt",
55
+
"h.tar.gz",
56
+
]),
57
+
(302, [
58
+
"E",
59
+
"E/test",
60
+
"E/test.bin",
61
+
]),
62
+
(404, [
63
+
"test.bin",
64
+
"test.bin/test",
65
+
"test.bin/test.py",
66
+
"test",
67
+
"test/test",
68
+
"test.bin/test.py",
69
+
"E.bin",
70
+
]),
71
+
(451, [
72
+
"Q.truncate",
73
+
]),
74
+
]
75
+
76
+
for code, paths in rq:
77
+
for p in paths:
78
+
app.logger.info(f"GET {p}")
79
+
rv = client.get(p)
80
+
assert rv.status_code == code
81
+