From e80eae214bed35ec4375dcaa3f763bfeeca5dacb Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sun, 1 Aug 2021 13:06:19 +1200 Subject: [PATCH] Move to test folder --- app/test/__init__.py | 3 ++ app/test/test_auth.py | 73 +++++++++++++++++++++++++++++++++++++++++++ app/test/test_pack.py | 10 ++++++ 3 files changed, 86 insertions(+) create mode 100644 app/test/__init__.py create mode 100644 app/test/test_auth.py create mode 100644 app/test/test_pack.py diff --git a/app/test/__init__.py b/app/test/__init__.py new file mode 100644 index 0000000..10d9009 --- /dev/null +++ b/app/test/__init__.py @@ -0,0 +1,3 @@ +import sys +import os +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) \ No newline at end of file diff --git a/app/test/test_auth.py b/app/test/test_auth.py new file mode 100644 index 0000000..bbdf916 --- /dev/null +++ b/app/test/test_auth.py @@ -0,0 +1,73 @@ +from fastapi import FastAPI, Request, Depends + +from fastapi.testclient import TestClient +import hmac + +from starlette.routing import request_response +from app.main import app +from app.auth import auth_hook, auth_web, check_ref +from os import environ +import json + +environ['WEBHOOK_SECRET'] = "dfsgdsjghhgdaehlsdfjhjkdh" +environ["BRANCH"] = "master" +environ["TOKEN"] = "assdcvfgvh" +secret_key = environ.get('WEBHOOK_SECRET') + +client = TestClient(app) + +@app.post("/test_auth", dependencies=[Depends(auth_hook)]) +async def auth_test_handler(request: Request): + return 200 + +@app.post("/test_ref", dependencies=[Depends(check_ref)]) +async def auth_test_handler(request: Request): + return 200 + +@app.get("/test_web", dependencies=[Depends(auth_web)]) +async def web_test_hnadler(request: Request): + return 200 + +def test_auth(): + payload = {"Hello":"World"} + msg = json.dumps(payload).encode() + mac = hmac.new(secret_key.encode(), msg=msg, digestmod='sha1').hexdigest() + + response = client.post("/test_auth", json= payload, headers={"X-Hub-Signature": "sha1="+mac}) + assert response.status_code == 200 + + response = client.post("/test_auth", headers={"X-Hub-Signature": "sha1="+mac}) + assert response.status_code == 204 + assert response.text == '{"detail":"Missing or bad content"}' + + response = client.post("/test_auth", json= payload, headers={"X-Hub-Signature": "sha="+mac}) + assert response.status_code == 400 + assert response.text == '{"detail":"Invalid signature"}' + + response = client.post("/test_auth", json=payload) + assert response.status_code == 400 + assert response.text == '{"detail":"Missing signature"}' + + response = client.post("/test_auth", json= payload, headers={"X-Hub-Signature": "sha1="+mac+"a"}) + assert response.status_code == 403 + assert response.text == '{"detail":"Unauthorized"}' + + +def test_branch(): + payload = {"ref": "refs/heads/master"} + response = client.post("/test_ref", json= payload) + assert response.status_code == 200 + + payload = {"ref": "refs/heads/test"} + response = client.post("/test_ref", json= payload) + assert response.status_code == 403 + +def test_web(): + response = client.get('/test_web?token={}'.format(environ.get("TOKEN"))) + assert response.status_code == 200 + + response = client.get('/test_web') + assert response.status_code == 400 + + response = client.get('/test_web?token=a') + assert response.status_code == 403 diff --git a/app/test/test_pack.py b/app/test/test_pack.py new file mode 100644 index 0000000..bbb1a61 --- /dev/null +++ b/app/test/test_pack.py @@ -0,0 +1,10 @@ +import pytest +from app.pack import Pack + +def test_pack(): + pack = Pack() + pack.clone() + pack.collate() + pack.compress() + pack.hash() +