lib: add tests for ak_create_event (#5710)

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
Jens L 2023-05-22 00:18:54 +02:00 committed by GitHub
parent 444deae637
commit 44341f0224
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 41 deletions

View File

@ -7,7 +7,6 @@ from smtplib import SMTPException
from typing import TYPE_CHECKING, Optional
from uuid import uuid4
from django.conf import settings
from django.db import models
from django.db.models import Count, ExpressionWrapper, F
from django.db.models.fields import DurationField
@ -207,9 +206,7 @@ class Event(SerializerModel, ExpiringModel):
self.user = get_user(user)
return self
def from_http(
self, request: HttpRequest, user: Optional[settings.AUTH_USER_MODEL] = None
) -> "Event":
def from_http(self, request: HttpRequest, user: Optional[User] = None) -> "Event":
"""Add data from a Django-HttpRequest, allowing the creation of
Events independently from requests.
`user` arguments optionally overrides user from requests."""

View File

@ -140,17 +140,19 @@ class BaseEvaluator:
def expr_event_create(self, action: str, **kwargs):
"""Create event with supplied data and try to extract as much relevant data
from the context"""
context = self._context.copy()
# If the result was a complex variable, we don't want to re-use it
self._context.pop("result", None)
self._context.pop("handler", None)
kwargs["context"] = self._context
context.pop("result", None)
context.pop("handler", None)
event_kwargs = context
event_kwargs.update(kwargs)
event = Event.new(
action,
app=self._filename,
**kwargs,
**event_kwargs,
)
if "request" in self._context and isinstance(self._context["request"], PolicyRequest):
policy_request: PolicyRequest = self._context["request"]
if "request" in context and isinstance(context["request"], PolicyRequest):
policy_request: PolicyRequest = context["request"]
if policy_request.http_request:
event.from_http(policy_request)
return

View File

@ -2,28 +2,41 @@
from django.test import TestCase
from authentik.core.tests.utils import create_test_admin_user
from authentik.events.models import Event
from authentik.lib.expression.evaluator import BaseEvaluator
from authentik.lib.generators import generate_id
class TestEvaluator(TestCase):
"""Test Evaluator base functions"""
def test_regex_match(self):
def test_expr_regex_match(self):
"""Test expr_regex_match"""
self.assertFalse(BaseEvaluator.expr_regex_match("foo", "bar"))
self.assertTrue(BaseEvaluator.expr_regex_match("foo", "foo"))
def test_regex_replace(self):
def test_expr_regex_replace(self):
"""Test expr_regex_replace"""
self.assertEqual(BaseEvaluator.expr_regex_replace("foo", "o", "a"), "faa")
def test_user_by(self):
def test_expr_user_by(self):
"""Test expr_user_by"""
user = create_test_admin_user()
self.assertIsNotNone(BaseEvaluator.expr_user_by(username=user.username))
self.assertIsNone(BaseEvaluator.expr_user_by(username="bar"))
self.assertIsNone(BaseEvaluator.expr_user_by(foo="bar"))
def test_is_group_member(self):
def test_expr_is_group_member(self):
"""Test expr_is_group_member"""
self.assertFalse(BaseEvaluator.expr_is_group_member(create_test_admin_user(), name="test"))
def test_expr_event_create(self):
"""Test expr_event_create"""
evaluator = BaseEvaluator(generate_id())
evaluator._context = {
"foo": "bar",
}
evaluator.evaluate("ak_create_event('foo', bar='baz')")
event = Event.objects.filter(action="custom_foo").first()
self.assertIsNotNone(event)
self.assertEqual(event.context, {"bar": "baz", "foo": "bar"})

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-05-19 22:00+0000\n"
"POT-Creation-Date: 2023-05-21 21:59+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -31,12 +31,16 @@ msgstr ""
msgid "Validation Error"
msgstr ""
#: authentik/blueprints/api.py:53
#: authentik/blueprints/api.py:43
msgid "Blueprint file does not exist"
msgstr ""
#: authentik/blueprints/api.py:54
#, python-format
msgid "Failed to validate blueprint: %(logs)s"
msgstr ""
#: authentik/blueprints/api.py:58
#: authentik/blueprints/api.py:59
msgid "Either path or content must be set."
msgstr ""
@ -321,105 +325,105 @@ msgstr ""
msgid "Certificate-Key Pairs"
msgstr ""
#: authentik/events/models.py:293
#: authentik/events/models.py:290
msgid "Event"
msgstr ""
#: authentik/events/models.py:294
#: authentik/events/models.py:291
msgid "Events"
msgstr ""
#: authentik/events/models.py:300
#: authentik/events/models.py:297
msgid "authentik inbuilt notifications"
msgstr ""
#: authentik/events/models.py:301
#: authentik/events/models.py:298
msgid "Generic Webhook"
msgstr ""
#: authentik/events/models.py:302
#: authentik/events/models.py:299
msgid "Slack Webhook (Slack/Discord)"
msgstr ""
#: authentik/events/models.py:303
#: authentik/events/models.py:300
msgid "Email"
msgstr ""
#: authentik/events/models.py:321
#: authentik/events/models.py:318
msgid ""
"Only send notification once, for example when sending a webhook into a chat "
"channel."
msgstr ""
#: authentik/events/models.py:386
#: authentik/events/models.py:383
msgid "Severity"
msgstr ""
#: authentik/events/models.py:391
#: authentik/events/models.py:388
msgid "Dispatched for user"
msgstr ""
#: authentik/events/models.py:400
#: authentik/events/models.py:397
msgid "Event user"
msgstr ""
#: authentik/events/models.py:487
#: authentik/events/models.py:484
msgid "Notification Transport"
msgstr ""
#: authentik/events/models.py:488
#: authentik/events/models.py:485
msgid "Notification Transports"
msgstr ""
#: authentik/events/models.py:494
#: authentik/events/models.py:491
msgid "Notice"
msgstr ""
#: authentik/events/models.py:495
#: authentik/events/models.py:492
msgid "Warning"
msgstr ""
#: authentik/events/models.py:496
#: authentik/events/models.py:493
msgid "Alert"
msgstr ""
#: authentik/events/models.py:521
#: authentik/events/models.py:518
msgid "Notification"
msgstr ""
#: authentik/events/models.py:522
#: authentik/events/models.py:519
msgid "Notifications"
msgstr ""
#: authentik/events/models.py:532
#: authentik/events/models.py:529
msgid ""
"Select which transports should be used to notify the user. If none are "
"selected, the notification will only be shown in the authentik UI."
msgstr ""
#: authentik/events/models.py:540
#: authentik/events/models.py:537
msgid "Controls which severity level the created notifications will have."
msgstr ""
#: authentik/events/models.py:545
#: authentik/events/models.py:542
msgid ""
"Define which group of users this notification should be sent and shown to. "
"If left empty, Notification won't ben sent."
msgstr ""
#: authentik/events/models.py:563
#: authentik/events/models.py:560
msgid "Notification Rule"
msgstr ""
#: authentik/events/models.py:564
#: authentik/events/models.py:561
msgid "Notification Rules"
msgstr ""
#: authentik/events/models.py:584
#: authentik/events/models.py:581
msgid "Webhook Mapping"
msgstr ""
#: authentik/events/models.py:585
#: authentik/events/models.py:582
msgid "Webhook Mappings"
msgstr ""