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
def create_user(self, user: BaseUser):
# TODO if not self.wekan.get_user(user.name):
return self._api.create_user(username=user.username, email=user.email, password="")
if self._api.get_user(user.username) == None:
return self._api.create_user(username=user.username, email=user.email, password="")
raise Exception("[Wekan] User already exists")
def create_group(self, group: BaseGroup):
print("Create Wekan Group: ", group)

View file

@ -48,7 +48,10 @@ class EventController:
def handle_model_created_event(self, model: Authentik_Hook_Model):
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__}")
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

View file

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

View file

@ -3,29 +3,34 @@ from time import sleep
import pytest
import requests
from fastapi.testclient import TestClient
from .main import app
from app.authentik.api import Authentik
from app.authentik.models import User
from app.authentik.settings import AuthentikSettings
from app.consumer.wekan.models import User as WekanUser
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()
def wekan_api():
def wekan(wekan_settings):
w = None
try:
r = requests.post("http://localhost:3000/users/register", json={
"username": "api", "password": "foobar123", "email": "foo@example.org"})
w = WekanApi("http://localhost:3000", "api", "foobar123")
w = WekanApi(wekan_settings)
except Exception as e:
logging.error(e)
return w
@pytest.fixture()
def authentik_api(settings: AuthentikSettings):
def authentik(settings: AuthentikSettings):
a = Authentik(settings)
try:
r = a.create_web_hook(
@ -35,18 +40,29 @@ def authentik_api(settings: AuthentikSettings):
return a
@pytest.mark.skip()
def test_create_user(wekan_api: WekanApi, authentik_api: Authentik):
user = authentik_api.create_user(
User(username="banane43", email="banane@example.org", name="Herr Banane"))
print(user)
sleep(5)
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(
"authentik notifcation rule doesn't work with api?? , plz create user in authentik")
assert False
@pytest.fixture()
def authentik_user(authentik):
user = authentik.create_user(User(username="foobar", name="Foo Bar", email="foo@bar.com"))
yield user
authentik.delete_user(user)
print("DELETING USER")
def test_create_user(mocker, authentik: Authentik, authentik_user: WekanUser, wekan: WekanApi):
# Actually this should already trigger the hook, but in authentik it doesn't trigger when come from api
# 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()