fixed tests, added to deploy

This commit is contained in:
alyssadev 2023-09-17 07:19:04 +10:00
parent 02854265c8
commit 04547edfe7
2 changed files with 47 additions and 24 deletions

View file

@ -6,8 +6,7 @@ on:
jobs: jobs:
deploy: deploy:
runs-on: self-hosted runs-on: ubuntu-latest
# runs-on: ubuntu-latest
name: Deploy name: Deploy
environment: Cloudflare Worker environment: Cloudflare Worker
steps: steps:
@ -20,3 +19,23 @@ jobs:
AUTH_KEY AUTH_KEY
env: env:
AUTH_KEY: ${{ secrets.AUTH_KEY }} AUTH_KEY: ${{ secrets.AUTH_KEY }}
test:
runs-on: ubuntu-latest
name: Test
environment: Cloudflare Worker
steps:
- uses: actions/checkout@master
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests
- name: Test
env:
HOST: ${{ vars.DEPLOY_HOST }}
AUTH_KEY: ${{ secrets.AUTH_KEY }}
run:
python test.py

48
test.py
View file

@ -1,18 +1,22 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from requests import get,put,post,delete,patch,request from requests import Session, request
from os import environ from os import environ
host = environ.get("HOST", "https://linkie.username.workers.dev") host = environ.get("HOST", "https://linkie.username.workers.dev")
auth = {"Authorization": environ.get("AUTH_KEY", "")} auth = {"Authorization": environ.get("AUTH_KEY", "")}
path = environ.get("TEST_PATH", "/devtestpath") path = environ.get("TEST_PATH", "/devtestpath")
print(f"Running tests on {host}")
unauth_session = Session()
unauth_session.trust_env = False
auth_session = Session()
try: try:
# unauth get / # unauth get /
req = get(host, allow_redirects=False) req = unauth_session.get(host, allow_redirects=False)
assert req.status_code == 301 and "http" in req.headers["location"] # unauth get / assert req.status_code == 301 and "http" in req.headers["location"] # unauth get /
# auth get / # auth get /
req = get(host,headers=auth) req = auth_session.get(host,headers=auth)
assert req.status_code == 200 # auth get / assert req.status_code == 200 # auth get /
# unsupported method # unsupported method
@ -20,57 +24,57 @@ try:
assert req.status_code == 405 # unsupported method assert req.status_code == 405 # unsupported method
# unauth requests to auth methods # unauth requests to auth methods
reqs = put(host),post(host),delete(host) reqs = unauth_session.put(host),unauth_session.post(host),unauth_session.delete(host)
assert all(req.status_code == 403 for req in reqs) # unauth requests to auth methods assert all(req.status_code == 403 for req in reqs) # unauth requests to auth methods
# auth put wo data # auth put wo data
req = put(host + path,data={},headers=auth) req = auth_session.put(host + path,data={},headers=auth)
assert req.status_code == 400 # auth put wo data assert req.status_code == 400 # auth put wo data
req = post(host + path,data={},headers=auth) req = auth_session.post(host + path,data={},headers=auth)
assert req.status_code == 400 # auth post wo data assert req.status_code == 400 # auth post wo data
req = patch(host + path,data={},headers=auth) req = auth_session.patch(host + path,data={},headers=auth)
assert req.status_code == 400 # auth patch wo data assert req.status_code == 400 # auth patch wo data
# auth put invalid url # auth put invalid url
req = put(host + path,data={"u": "golf sale"},headers=auth) req = auth_session.put(host + path,data={"u": "golf sale"},headers=auth)
assert req.status_code == 400 # auth put invalid url assert req.status_code == 400 # auth put invalid url
req = post(host + path,data={"u": "golf sale"},headers=auth) req = auth_session.post(host + path,data={"u": "golf sale"},headers=auth)
assert req.status_code == 400 # auth post invalid url assert req.status_code == 400 # auth post invalid url
req = patch(host + path,data={"u": "golf sale"},headers=auth) req = auth_session.patch(host + path,data={"u": "golf sale"},headers=auth)
assert req.status_code == 400 # auth patch invalid url assert req.status_code == 400 # auth patch invalid url
# auth put wo path # auth put wo path
req = put(host,data={"u": "http://www.example.com"},headers=auth) req = auth_session.put(host,data={"u": "http://www.example.com"},headers=auth)
assert req.status_code == 400 # auth put wo path assert req.status_code == 400 # auth put wo path
req = post(host,data={"u": "http://www.example.com"},headers=auth) req = auth_session.post(host,data={"u": "http://www.example.com"},headers=auth)
assert req.status_code == 400 # auth post wo path assert req.status_code == 400 # auth post wo path
req = post(host,data={"u": "http://www.example.com"},headers=auth) req = auth_session.post(host,data={"u": "http://www.example.com"},headers=auth)
assert req.status_code == 400 # auth patch wo path assert req.status_code == 400 # auth patch wo path
# auth put valid # auth put valid
req = put(host + path,data={"u": "http://www.example.com/?put"},headers=auth) req = auth_session.put(host + path,data={"u": "http://www.example.com/?put"},headers=auth)
assert req.status_code == 201 # auth put valid assert req.status_code == 201 # auth put valid
req = get(host + path, allow_redirects=False) req = unauth_session.get(host + path, allow_redirects=False)
assert req.status_code == 302 and req.headers["location"] == "http://www.example.com/?put" assert req.status_code == 302 and req.headers["location"] == "http://www.example.com/?put"
req = post(host + path + "post",data={"u": "http://www.example.com/?post"},headers=auth) req = auth_session.post(host + path + "post",data={"u": "http://www.example.com/?post"},headers=auth)
assert req.status_code == 201 # auth post valid assert req.status_code == 201 # auth post valid
req = get(host + path + "post", allow_redirects=False) req = unauth_session.get(host + path + "post", allow_redirects=False)
assert req.status_code == 302 and req.headers["location"] == "http://www.example.com/?post" assert req.status_code == 302 and req.headers["location"] == "http://www.example.com/?post"
req = patch(host + path + "patch",data={"u": "http://www.example.com/?patch"},headers=auth) req = auth_session.patch(host + path + "patch",data={"u": "http://www.example.com/?patch"},headers=auth)
assert req.status_code == 201 # auth patch valid assert req.status_code == 201 # auth patch valid
req = get(host + path + "patch", allow_redirects=False) req = unauth_session.get(host + path + "patch", allow_redirects=False)
assert req.status_code == 302 and req.headers["location"] == "http://www.example.com/?patch" assert req.status_code == 302 and req.headers["location"] == "http://www.example.com/?patch"
# auth delete wo path # auth delete wo path
req = delete(host,headers=auth) req = auth_session.delete(host,headers=auth)
assert req.status_code == 400 # auth delete wo path assert req.status_code == 400 # auth delete wo path
# auth delete valid # auth delete valid
req = delete(host + path,headers=auth) req = auth_session.delete(host + path,headers=auth)
assert req.status_code == 200 # auth delete valid assert req.status_code == 200 # auth delete valid
# req = get(host + path, allow_redirects=False) # req = unauth_session.get(host + path, allow_redirects=False)
# assert req.status_code == 404 # assert req.status_code == 404
except AssertionError: except AssertionError:
print(req,req.headers,req.text) print(req,req.headers,req.text)