linkie/test.py

64 lines
2.2 KiB
Python
Raw Permalink Normal View History

2023-09-16 14:50:55 -04:00
#!/usr/bin/env python3
2023-09-16 22:50:31 -04:00
from requests import request,get,put,post,patch,delete
2023-09-16 14:50:55 -04:00
from os import environ
2023-09-17 14:35:05 -04:00
from mimetypes import guess_type
2023-09-16 14:50:55 -04:00
2023-09-16 22:50:31 -04:00
host = environ.get("DEPLOY_HOST", "https://linkie.username.workers.dev")
2023-09-16 16:47:09 -04:00
auth = {"Authorization": environ.get("AUTH_KEY", "")}
2023-09-16 17:19:04 -04:00
print(f"Running tests on {host}")
2023-09-16 14:50:55 -04:00
try:
# unauth get /
2023-09-16 22:50:31 -04:00
req = get(host, allow_redirects=False)
2023-09-16 14:50:55 -04:00
assert req.status_code == 301 and "http" in req.headers["location"] # unauth get /
# auth get /
2023-09-16 22:50:31 -04:00
req = get(host,headers=auth)
2023-09-16 14:50:55 -04:00
assert req.status_code == 200 # auth get /
# unsupported method
req = request("OPTIONS", host)
assert req.status_code == 405 # unsupported method
# unauth requests to auth methods
2023-09-16 22:50:31 -04:00
reqs = put(host),post(host),delete(host)
assert all(req.status_code == 401 for req in reqs) # unauth requests to auth methods
2023-09-16 14:50:55 -04:00
# auth put wo data
2023-09-17 15:53:06 -04:00
req = put(host + "/devtestpath",data={},headers=auth)
2023-09-16 14:50:55 -04:00
assert req.status_code == 400 # auth put wo data
# auth put wo path
2023-09-16 22:50:31 -04:00
req = put(host,data={"u": "http://www.example.com"},headers=auth)
2023-09-16 14:50:55 -04:00
assert req.status_code == 400 # auth put wo path
# auth put valid
2023-09-17 15:53:06 -04:00
req = put(host + "/devtestpath",data={"u": "http://www.example.com/?put"},headers=auth)
2023-09-16 14:50:55 -04:00
assert req.status_code == 201 # auth put valid
2023-09-17 15:53:06 -04:00
req = get(host + "/devtestpath", allow_redirects=False)
2023-09-16 14:50:55 -04:00
assert req.status_code == 302 and req.headers["location"] == "http://www.example.com/?put"
# auth delete wo path
2023-09-16 22:50:31 -04:00
req = delete(host,headers=auth)
2023-09-16 14:50:55 -04:00
assert req.status_code == 400 # auth delete wo path
# auth delete valid
2023-09-17 15:53:06 -04:00
req = delete(host + "/devtestpath",headers=auth)
2023-09-16 14:50:55 -04:00
assert req.status_code == 200 # auth delete valid
2023-09-17 15:53:06 -04:00
# req = get(host + "/devtestpath", allow_redirects=False)
2023-09-16 14:50:55 -04:00
# assert req.status_code == 404
2023-09-17 14:35:05 -04:00
# auth put file valid
fn = "package.json"
mime = guess_type(fn)[0]
with open(fn, "rb") as f:
files = {'u': (fn, f, mime)}
2023-09-17 15:53:06 -04:00
req = put(host + "/devtestpatha",headers=auth,files=files)
2023-09-17 14:35:05 -04:00
assert req.status_code == 201 # auth put file valid
2023-09-17 15:53:06 -04:00
req = get(host + "/devtestpatha", allow_redirects=False)
2023-09-17 14:35:05 -04:00
assert req.status_code == 200 and req.headers["content-type"] == mime
2023-09-16 14:50:55 -04:00
except AssertionError:
print(req,req.headers,req.text)
raise