diff --git a/app/event_controller.py b/app/event_controller.py new file mode 100644 index 0000000..cdf86c6 --- /dev/null +++ b/app/event_controller.py @@ -0,0 +1,8 @@ + +class Event_Controller: + + def __init__(self): + pass + + def handle_model_created_event(self, model): + pass \ No newline at end of file diff --git a/app/main.py b/app/main.py index 263bcf3..e93efba 100644 --- a/app/main.py +++ b/app/main.py @@ -2,55 +2,67 @@ import logging import structlog from unicodedata import name from urllib.error import HTTPError -from fastapi import FastAPI, Request +from fastapi import Depends, FastAPI, Request from pydantic import BaseModel from app.authentik.api import Authentik from app.authentik.models import User +from app.event_controller import Event_Controller from .wekan.api import Wekan import json logging = structlog.get_logger() -class Hook_Model(BaseModel): + +class Authentik_Hook_Model(BaseModel): pk: str app: str name: str model_name: str + class Http_request(BaseModel): args: dict path: str method: str + app = FastAPI() + @app.get("/") async def root(): return {'message': 'Hello World'} @app.post("/authentik/hook/") -async def hook(model: Hook_Model, http_request: Http_request): +async def hook(model: Authentik_Hook_Model, + http_request: Http_request, + event_controller: Event_Controller = Depends(Event_Controller)): # print(await request.body()) logging.info(model) logging.info(http_request) + if http_request.path == "/api/v3/core/users/": + event_controller.handle_model_created_event(model) return 200 # model_created = json.loads(r['body'].split("model_created: ")[1])["model"] # hook wekan.create_user(model_created["pk"]) + @app.get("/authentik/create_hook/") async def hook(request: Request): a = Authentik(base="http://localhost:9000/", token="foobar123") - res = a.create_web_hook() + res = a.create_web_hook() logging.info(res) + @app.get("/authentik/users/create_demo_user/") async def create_demo_user(request: Request): a = Authentik(base="http://localhost:9000/", token="foobar123") try: - user = a.create_user(User(username="demo", name="dmeo", email="foo@example.org")) - except Exception as e: # TODO + user = a.create_user( + User(username="demo", name="dmeo", email="foo@example.org")) + except Exception as e: # TODO return e logging.info(user) - return user.dict \ No newline at end of file + return user.dict diff --git a/app/settings.py b/app/settings.py index 849c773..1d67e33 100644 --- a/app/settings.py +++ b/app/settings.py @@ -8,4 +8,3 @@ class Settings(BaseSettings): class Config: env_file = '.env' -nc \ No newline at end of file diff --git a/app/test_main.py b/app/test_main.py index 6205423..f77fb66 100644 --- a/app/test_main.py +++ b/app/test_main.py @@ -2,7 +2,7 @@ from fastapi.testclient import TestClient from app.wekan.api import Wekan -from .main import app +from .main import Authentik_Hook_Model, app client = TestClient(app) @@ -24,16 +24,23 @@ def test_hook(): assert response.status_code == 422 +# mock = mocker.patch('app.wekan.api.Wekan.create_user', +# return_value='fake user') +# print(mock.mock_calls) +# + def test_hook_model_created(mocker): - mock = mocker.patch('app.wekan.api.Wekan.create_user', - return_value='fake user') - print(mock.mock_calls) + mock = mocker.patch("app.event_controller.Event_Controller.handle_model_created_event") d = """ {"model": {"pk": 5, "app": "authentik_core", "name": "asd", "model_name": "user"}, "http_request": {"args": {}, "path": "/api/v3/core/users/", "method": "POST"}} """ response = client.post("/authentik/hook/", data=d, ) assert response.status_code == 200 - # assert len(mock.mock_calls) > 0 - # kall = mock.call_args - # assert kall.args[0] == "18" - # assert str(response.text) == 'fake user' + assert len(mock.mock_calls) > 0 + kall = mock.call_args + assert type(kall.args[0]) == Authentik_Hook_Model + model: Authentik_Hook_Model = kall.args[0] + assert model.pk == "5" + assert model.app == "authentik_core" + assert model.name == "asd" + assert model.model_name == "user"