This commit is contained in:
Philipp Rothmann 2022-04-29 15:50:33 +02:00
parent 73c5440454
commit 34d0a73923
4 changed files with 47 additions and 26 deletions

View file

@ -21,8 +21,9 @@ class WekanConsumer(Consumer):
self._api = api self._api = api
def create_user(self, user: BaseUser): def create_user(self, user: BaseUser):
# TODO if not self.wekan.get_user(user.name): if self._api.get_user(user.username) == None:
return self._api.create_user(username=user.username, email=user.email, password="") return self._api.create_user(username=user.username, email=user.email, password="")
raise Exception("[Wekan] User already exists")
def create_group(self, group: BaseGroup): def create_group(self, group: BaseGroup):
print("Create Wekan Group: ", group) print("Create Wekan Group: ", group)

View file

@ -48,7 +48,10 @@ class EventController:
def handle_model_created_event(self, model: Authentik_Hook_Model): def handle_model_created_event(self, model: Authentik_Hook_Model):
user: User = self._authentik.get_user_by_pk(model.pk) user: User = self._authentik.get_user_by_pk(model.pk)
for sink in self._sinks: for sink in self._sinks: # TODO this could run async
logging.info(f"Creating User {user.username} in {sink.__class__}") logging.info(f"Creating User {user.username} in {sink.__class__}")
sink.create_user(user) try:
sink.create_user(user)
except Exception as e:
logging.error("create user", exception=str(e), sink=sink, user=user)
return True return True

View file

@ -2,19 +2,20 @@ import logging
from app import dependencies from app import dependencies
from app.authentik.api import Authentik from app.authentik.api import Authentik
from app.event_controller import Authentik_Hook_Model, EventController, Http_request from app.event_controller import Authentik_Hook_Model, EventController, Http_request
from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends, BackgroundTasks
from app.authentik.settings import AuthentikSettings from app.authentik.settings import AuthentikSettings
router = APIRouter() router = APIRouter()
@router.post("/authentik/hook/") @router.post("/authentik/hook/")
async def hook(model: Authentik_Hook_Model, async def hook(model: Authentik_Hook_Model,
http_request: Http_request, http_request: Http_request,
): background_tasks: BackgroundTasks
):
logging.info(model) logging.info(model)
logging.info(http_request) logging.info(http_request)
ec = EventController(Authentik(AuthentikSettings())) ec = EventController(Authentik(AuthentikSettings()))
if http_request.path == "/api/v3/core/users/": if http_request.path == "/api/v3/core/users/":
ec.handle_model_created_event(model) background_tasks.add_task(ec.handle_model_created_event, model)
return 200 return 200

View file

@ -3,29 +3,34 @@ from time import sleep
import pytest import pytest
import requests import requests
from fastapi.testclient import TestClient
from .main import app
from app.authentik.api import Authentik from app.authentik.api import Authentik
from app.authentik.models import User from app.authentik.models import User
from app.authentik.settings import AuthentikSettings from app.authentik.settings import AuthentikSettings
from app.consumer.wekan.models import User as WekanUser from app.consumer.wekan.models import User as WekanUser
from app.consumer.wekan.api import WekanApi from app.consumer.wekan.api import WekanApi
from app.authentik.test_authentik import settings
from app.consumer.wekan.test_wekan import settings as wekan_settings
client = TestClient(app)
@pytest.fixture() @pytest.fixture()
def wekan_api(): def wekan(wekan_settings):
w = None w = None
try: try:
r = requests.post("http://localhost:3000/users/register", json={ r = requests.post("http://localhost:3000/users/register", json={
"username": "api", "password": "foobar123", "email": "foo@example.org"}) "username": "api", "password": "foobar123", "email": "foo@example.org"})
w = WekanApi("http://localhost:3000", "api", "foobar123") w = WekanApi(wekan_settings)
except Exception as e: except Exception as e:
logging.error(e) logging.error(e)
return w return w
@pytest.fixture() @pytest.fixture()
def authentik_api(settings: AuthentikSettings): def authentik(settings: AuthentikSettings):
a = Authentik(settings) a = Authentik(settings)
try: try:
r = a.create_web_hook( r = a.create_web_hook(
@ -35,18 +40,29 @@ def authentik_api(settings: AuthentikSettings):
return a return a
@pytest.mark.skip() @pytest.fixture()
def test_create_user(wekan_api: WekanApi, authentik_api: Authentik): def authentik_user(authentik):
user = authentik_api.create_user( user = authentik.create_user(User(username="foobar", name="Foo Bar", email="foo@bar.com"))
User(username="banane43", email="banane@example.org", name="Herr Banane")) yield user
print(user) authentik.delete_user(user)
sleep(5) print("DELETING USER")
authentik_api.delete_user(user)
# authentik username == wekan username
# user must be created from authentik user and noch api to trigger the notiftication rule?
logging.error( def test_create_user(mocker, authentik: Authentik, authentik_user: WekanUser, wekan: WekanApi):
"authentik notifcation rule doesn't work with api?? , plz create user in authentik") # Actually this should already trigger the hook, but in authentik it doesn't trigger when come from api
assert False # mock = mocker.patch("app.event_controller.EventController.handle_model_created_event")
authentik_message = {"model": {"pk": authentik_user.pk, "app": "authentik_core", "name": authentik_user.name,
"model_name": "user"}, "http_request": {"args": {}, "path": "/api/v3/core/users/", "method": "POST"}}
response = client.post("/authentik/hook/", json=authentik_message)
print(response.text)
assert response.status_code == 200
wu = wekan.get_user(authentik_user.username)
assert not wu == None
assert wu.username == authentik_user.username
assert authentik_user.email in [i.address for i in wu.emails]
wekan.delete_user(authentik_user.username) # TODO WTF THIS NOT WORK?
@pytest.mark.skip() @pytest.mark.skip()