linkie/test.py

82 lines
3.6 KiB
Python
Raw Normal View History

2023-09-16 18:50:55 +00:00
#!/usr/bin/env python3
2023-09-16 21:19:04 +00:00
from requests import Session, request
2023-09-16 18:50:55 +00:00
from os import environ
2023-09-16 20:47:09 +00:00
host = environ.get("HOST", "https://linkie.username.workers.dev")
auth = {"Authorization": environ.get("AUTH_KEY", "")}
2023-09-16 18:50:55 +00:00
path = environ.get("TEST_PATH", "/devtestpath")
2023-09-16 21:19:04 +00:00
print(f"Running tests on {host}")
unauth_session = Session()
unauth_session.trust_env = False
auth_session = Session()
2023-09-16 18:50:55 +00:00
try:
# unauth get /
2023-09-16 21:19:04 +00:00
req = unauth_session.get(host, allow_redirects=False)
2023-09-16 18:50:55 +00:00
assert req.status_code == 301 and "http" in req.headers["location"] # unauth get /
# auth get /
2023-09-16 21:19:04 +00:00
req = auth_session.get(host,headers=auth)
2023-09-16 18:50:55 +00: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 21:19:04 +00:00
reqs = unauth_session.put(host),unauth_session.post(host),unauth_session.delete(host)
2023-09-16 18:50:55 +00:00
assert all(req.status_code == 403 for req in reqs) # unauth requests to auth methods
# auth put wo data
2023-09-16 21:19:04 +00:00
req = auth_session.put(host + path,data={},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 400 # auth put wo data
2023-09-16 21:19:04 +00:00
req = auth_session.post(host + path,data={},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 400 # auth post wo data
2023-09-16 21:19:04 +00:00
req = auth_session.patch(host + path,data={},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 400 # auth patch wo data
# auth put invalid url
2023-09-16 21:19:04 +00:00
req = auth_session.put(host + path,data={"u": "golf sale"},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 400 # auth put invalid url
2023-09-16 21:19:04 +00:00
req = auth_session.post(host + path,data={"u": "golf sale"},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 400 # auth post invalid url
2023-09-16 21:19:04 +00:00
req = auth_session.patch(host + path,data={"u": "golf sale"},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 400 # auth patch invalid url
# auth put wo path
2023-09-16 21:19:04 +00:00
req = auth_session.put(host,data={"u": "http://www.example.com"},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 400 # auth put wo path
2023-09-16 21:19:04 +00:00
req = auth_session.post(host,data={"u": "http://www.example.com"},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 400 # auth post wo path
2023-09-16 21:19:04 +00:00
req = auth_session.post(host,data={"u": "http://www.example.com"},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 400 # auth patch wo path
# auth put valid
2023-09-16 21:19:04 +00:00
req = auth_session.put(host + path,data={"u": "http://www.example.com/?put"},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 201 # auth put valid
2023-09-16 21:19:04 +00:00
req = unauth_session.get(host + path, allow_redirects=False)
2023-09-16 18:50:55 +00:00
assert req.status_code == 302 and req.headers["location"] == "http://www.example.com/?put"
2023-09-16 21:19:04 +00:00
req = auth_session.post(host + path + "post",data={"u": "http://www.example.com/?post"},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 201 # auth post valid
2023-09-16 21:19:04 +00:00
req = unauth_session.get(host + path + "post", allow_redirects=False)
2023-09-16 18:50:55 +00:00
assert req.status_code == 302 and req.headers["location"] == "http://www.example.com/?post"
2023-09-16 21:19:04 +00:00
req = auth_session.patch(host + path + "patch",data={"u": "http://www.example.com/?patch"},headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 201 # auth patch valid
2023-09-16 21:19:04 +00:00
req = unauth_session.get(host + path + "patch", allow_redirects=False)
2023-09-16 18:50:55 +00:00
assert req.status_code == 302 and req.headers["location"] == "http://www.example.com/?patch"
# auth delete wo path
2023-09-16 21:19:04 +00:00
req = auth_session.delete(host,headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 400 # auth delete wo path
# auth delete valid
2023-09-16 21:19:04 +00:00
req = auth_session.delete(host + path,headers=auth)
2023-09-16 18:50:55 +00:00
assert req.status_code == 200 # auth delete valid
2023-09-16 21:19:04 +00:00
# req = unauth_session.get(host + path, allow_redirects=False)
2023-09-16 18:50:55 +00:00
# assert req.status_code == 404
except AssertionError:
print(req,req.headers,req.text)
raise