From 3ff18008694faf0417ba7a69d525edacf791ff28 Mon Sep 17 00:00:00 2001
From: Jimmy <git@jimmy.nz>
Date: Wed, 28 Jul 2021 21:51:48 +1200
Subject: [PATCH] Restructure

---
 app/__init__.py                    |  0
 src/auth.py => app/dependencies.py | 17 ++++++-----------
 app/main.py                        | 14 ++++++++++++++
 app/test/__init__.py               |  3 +++
 {src => app/test}/test.py          | 14 ++++++--------
 src/main.py                        | 25 -------------------------
 6 files changed, 29 insertions(+), 44 deletions(-)
 create mode 100644 app/__init__.py
 rename src/auth.py => app/dependencies.py (77%)
 create mode 100644 app/main.py
 create mode 100644 app/test/__init__.py
 rename {src => app/test}/test.py (87%)
 delete mode 100644 src/main.py

diff --git a/app/__init__.py b/app/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/src/auth.py b/app/dependencies.py
similarity index 77%
rename from src/auth.py
rename to app/dependencies.py
index dbf6b46..18b52ca 100644
--- a/src/auth.py
+++ b/app/dependencies.py
@@ -1,17 +1,13 @@
-import os
+from os import getenv
 import hmac
 from fastapi import Request
 from fastapi.exceptions import HTTPException
-from fastapi.param_functions import Header
-from dotenv import load_dotenv
-
-load_dotenv()
 
 async def check_ref(request: Request):
     json = await request.json()  
-    if json["ref"] and json["ref"] == f"refs/heads/{os.environ.get('BRANCH')}":
+    if json["ref"] and json["ref"] == f"refs/heads/{getenv('BRANCH')}":
         return
-    raise HTTPException(status_code=403, detail="Invalid branch")
+    raise HTTPException(status_code=202, detail="Invalid branch")
 
 async def auth_hook(request: Request):
     try:
@@ -29,7 +25,7 @@ async def auth_hook(request: Request):
     if sha_name != 'sha1':
         raise HTTPException(status_code=400, detail="Invalid signature")
 
-    secret_key = os.environ.get('WEBHOOK_SECRET')
+    secret_key = getenv('WEBHOOK_SECRET')
     if secret_key is None:
         raise HTTPException(status_code=503, detail="Missing WEBHOOK_SECRET")
 
@@ -42,9 +38,8 @@ async def auth_hook(request: Request):
 
 async def auth_web(request: Request):
     token = request._query_params.get("token")
-    if token is None:
+    if token is None or token is "":
         raise HTTPException(status_code=400, detail="Missing token")
-    print(token, os.environ.get("TOKEN"))
-    if token == os.environ.get("TOKEN"):
+    if token == getenv("TOKEN"):
         return
     raise HTTPException(status_code=403, detail="Invalid token")
\ No newline at end of file
diff --git a/app/main.py b/app/main.py
new file mode 100644
index 0000000..39fea3e
--- /dev/null
+++ b/app/main.py
@@ -0,0 +1,14 @@
+from fastapi import FastAPI, Request, Depends
+from fastapi_responses import custom_openapi
+from app.dependencies import auth_hook, auth_web, check_ref
+
+app = FastAPI()
+
+app.openapi = custom_openapi(app)
+
+@app.get("/", dependencies=[Depends(auth_web)])
+@app.post("/", dependencies=[Depends(auth_hook), Depends(check_ref)])
+async def hook(req: Request):
+    return "Update"
+
+
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/src/test.py b/app/test/test.py
similarity index 87%
rename from src/test.py
rename to app/test/test.py
index d83681d..8ec842b 100644
--- a/src/test.py
+++ b/app/test/test.py
@@ -2,17 +2,15 @@ from fastapi import FastAPI, Request, Depends
 
 from fastapi.testclient import TestClient
 import hmac
-
-from starlette.routing import request_response
-from main import app
-from auth import auth_hook, auth_web, check_ref
-from os import environ
+from app.main import app
+from app.dependencies import auth_hook, auth_web, check_ref
+from os import environ, getenv
 import json
 
 environ['WEBHOOK_SECRET'] = "dfsgdsjghhgdaehlsdfjhjkdh"
 environ["BRANCH"] = "master"
 environ["TOKEN"] = "assdcvfgvh"
-secret_key = environ.get('WEBHOOK_SECRET')
+secret_key = getenv('WEBHOOK_SECRET')
 
 client = TestClient(app)
 
@@ -56,14 +54,14 @@ def test_auth():
 def test_branch():
     payload = {"ref": "refs/heads/master"}
     response = client.post("/test_ref", json= payload)
-    assert response.status_code == 200
+    assert response.status_code == 202
 
     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")))
+    response = client.get('/test_web?token={}'.format(getenv("TOKEN")))
     assert response.status_code == 200
 
     response = client.get('/test_web')
diff --git a/src/main.py b/src/main.py
deleted file mode 100644
index 0ba8661..0000000
--- a/src/main.py
+++ /dev/null
@@ -1,25 +0,0 @@
-from os import environ
-import os
-from fastapi import FastAPI, Body, Request, Depends
-import json
-from fastapi.exceptions import HTTPException
-
-from fastapi.param_functions import Header
-from fastapi_responses import custom_openapi
-from auth import auth_hook, auth_web, check_ref
-
-
-if not os.environ.get("DOCKER"):
-    from dotenv import load_dotenv
-    load_dotenv
-    
-app = FastAPI()
-
-app.openapi = custom_openapi(app)
-
-@app.get("/", dependencies=[Depends(auth_web)])
-@app.post("/", dependencies=[Depends(auth_hook), Depends(check_ref)])
-async def hook(req: Request):
-    return "Update"
-
-