lib: add tests for ak_create_event (#5710)
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
parent
444deae637
commit
44341f0224
|
@ -7,7 +7,6 @@ from smtplib import SMTPException
|
||||||
from typing import TYPE_CHECKING, Optional
|
from typing import TYPE_CHECKING, Optional
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import Count, ExpressionWrapper, F
|
from django.db.models import Count, ExpressionWrapper, F
|
||||||
from django.db.models.fields import DurationField
|
from django.db.models.fields import DurationField
|
||||||
|
@ -207,9 +206,7 @@ class Event(SerializerModel, ExpiringModel):
|
||||||
self.user = get_user(user)
|
self.user = get_user(user)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def from_http(
|
def from_http(self, request: HttpRequest, user: Optional[User] = None) -> "Event":
|
||||||
self, request: HttpRequest, user: Optional[settings.AUTH_USER_MODEL] = None
|
|
||||||
) -> "Event":
|
|
||||||
"""Add data from a Django-HttpRequest, allowing the creation of
|
"""Add data from a Django-HttpRequest, allowing the creation of
|
||||||
Events independently from requests.
|
Events independently from requests.
|
||||||
`user` arguments optionally overrides user from requests."""
|
`user` arguments optionally overrides user from requests."""
|
||||||
|
|
|
@ -140,17 +140,19 @@ class BaseEvaluator:
|
||||||
def expr_event_create(self, action: str, **kwargs):
|
def expr_event_create(self, action: str, **kwargs):
|
||||||
"""Create event with supplied data and try to extract as much relevant data
|
"""Create event with supplied data and try to extract as much relevant data
|
||||||
from the context"""
|
from the context"""
|
||||||
|
context = self._context.copy()
|
||||||
# If the result was a complex variable, we don't want to re-use it
|
# If the result was a complex variable, we don't want to re-use it
|
||||||
self._context.pop("result", None)
|
context.pop("result", None)
|
||||||
self._context.pop("handler", None)
|
context.pop("handler", None)
|
||||||
kwargs["context"] = self._context
|
event_kwargs = context
|
||||||
|
event_kwargs.update(kwargs)
|
||||||
event = Event.new(
|
event = Event.new(
|
||||||
action,
|
action,
|
||||||
app=self._filename,
|
app=self._filename,
|
||||||
**kwargs,
|
**event_kwargs,
|
||||||
)
|
)
|
||||||
if "request" in self._context and isinstance(self._context["request"], PolicyRequest):
|
if "request" in context and isinstance(context["request"], PolicyRequest):
|
||||||
policy_request: PolicyRequest = self._context["request"]
|
policy_request: PolicyRequest = context["request"]
|
||||||
if policy_request.http_request:
|
if policy_request.http_request:
|
||||||
event.from_http(policy_request)
|
event.from_http(policy_request)
|
||||||
return
|
return
|
||||||
|
|
|
@ -2,28 +2,41 @@
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from authentik.core.tests.utils import create_test_admin_user
|
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.expression.evaluator import BaseEvaluator
|
||||||
|
from authentik.lib.generators import generate_id
|
||||||
|
|
||||||
|
|
||||||
class TestEvaluator(TestCase):
|
class TestEvaluator(TestCase):
|
||||||
"""Test Evaluator base functions"""
|
"""Test Evaluator base functions"""
|
||||||
|
|
||||||
def test_regex_match(self):
|
def test_expr_regex_match(self):
|
||||||
"""Test expr_regex_match"""
|
"""Test expr_regex_match"""
|
||||||
self.assertFalse(BaseEvaluator.expr_regex_match("foo", "bar"))
|
self.assertFalse(BaseEvaluator.expr_regex_match("foo", "bar"))
|
||||||
self.assertTrue(BaseEvaluator.expr_regex_match("foo", "foo"))
|
self.assertTrue(BaseEvaluator.expr_regex_match("foo", "foo"))
|
||||||
|
|
||||||
def test_regex_replace(self):
|
def test_expr_regex_replace(self):
|
||||||
"""Test expr_regex_replace"""
|
"""Test expr_regex_replace"""
|
||||||
self.assertEqual(BaseEvaluator.expr_regex_replace("foo", "o", "a"), "faa")
|
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"""
|
"""Test expr_user_by"""
|
||||||
user = create_test_admin_user()
|
user = create_test_admin_user()
|
||||||
self.assertIsNotNone(BaseEvaluator.expr_user_by(username=user.username))
|
self.assertIsNotNone(BaseEvaluator.expr_user_by(username=user.username))
|
||||||
self.assertIsNone(BaseEvaluator.expr_user_by(username="bar"))
|
self.assertIsNone(BaseEvaluator.expr_user_by(username="bar"))
|
||||||
self.assertIsNone(BaseEvaluator.expr_user_by(foo="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"""
|
"""Test expr_is_group_member"""
|
||||||
self.assertFalse(BaseEvaluator.expr_is_group_member(create_test_admin_user(), name="test"))
|
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"})
|
||||||
|
|
|
@ -8,7 +8,7 @@ msgid ""
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: PACKAGE VERSION\n"
|
"Project-Id-Version: PACKAGE VERSION\n"
|
||||||
"Report-Msgid-Bugs-To: \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"
|
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||||
|
@ -31,12 +31,16 @@ msgstr ""
|
||||||
msgid "Validation Error"
|
msgid "Validation Error"
|
||||||
msgstr ""
|
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
|
#, python-format
|
||||||
msgid "Failed to validate blueprint: %(logs)s"
|
msgid "Failed to validate blueprint: %(logs)s"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/blueprints/api.py:58
|
#: authentik/blueprints/api.py:59
|
||||||
msgid "Either path or content must be set."
|
msgid "Either path or content must be set."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
@ -321,105 +325,105 @@ msgstr ""
|
||||||
msgid "Certificate-Key Pairs"
|
msgid "Certificate-Key Pairs"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:293
|
#: authentik/events/models.py:290
|
||||||
msgid "Event"
|
msgid "Event"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:294
|
#: authentik/events/models.py:291
|
||||||
msgid "Events"
|
msgid "Events"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:300
|
#: authentik/events/models.py:297
|
||||||
msgid "authentik inbuilt notifications"
|
msgid "authentik inbuilt notifications"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:301
|
#: authentik/events/models.py:298
|
||||||
msgid "Generic Webhook"
|
msgid "Generic Webhook"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:302
|
#: authentik/events/models.py:299
|
||||||
msgid "Slack Webhook (Slack/Discord)"
|
msgid "Slack Webhook (Slack/Discord)"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:303
|
#: authentik/events/models.py:300
|
||||||
msgid "Email"
|
msgid "Email"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:321
|
#: authentik/events/models.py:318
|
||||||
msgid ""
|
msgid ""
|
||||||
"Only send notification once, for example when sending a webhook into a chat "
|
"Only send notification once, for example when sending a webhook into a chat "
|
||||||
"channel."
|
"channel."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:386
|
#: authentik/events/models.py:383
|
||||||
msgid "Severity"
|
msgid "Severity"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:391
|
#: authentik/events/models.py:388
|
||||||
msgid "Dispatched for user"
|
msgid "Dispatched for user"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:400
|
#: authentik/events/models.py:397
|
||||||
msgid "Event user"
|
msgid "Event user"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:487
|
#: authentik/events/models.py:484
|
||||||
msgid "Notification Transport"
|
msgid "Notification Transport"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:488
|
#: authentik/events/models.py:485
|
||||||
msgid "Notification Transports"
|
msgid "Notification Transports"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:494
|
#: authentik/events/models.py:491
|
||||||
msgid "Notice"
|
msgid "Notice"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:495
|
#: authentik/events/models.py:492
|
||||||
msgid "Warning"
|
msgid "Warning"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:496
|
#: authentik/events/models.py:493
|
||||||
msgid "Alert"
|
msgid "Alert"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:521
|
#: authentik/events/models.py:518
|
||||||
msgid "Notification"
|
msgid "Notification"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:522
|
#: authentik/events/models.py:519
|
||||||
msgid "Notifications"
|
msgid "Notifications"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:532
|
#: authentik/events/models.py:529
|
||||||
msgid ""
|
msgid ""
|
||||||
"Select which transports should be used to notify the user. If none are "
|
"Select which transports should be used to notify the user. If none are "
|
||||||
"selected, the notification will only be shown in the authentik UI."
|
"selected, the notification will only be shown in the authentik UI."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:540
|
#: authentik/events/models.py:537
|
||||||
msgid "Controls which severity level the created notifications will have."
|
msgid "Controls which severity level the created notifications will have."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:545
|
#: authentik/events/models.py:542
|
||||||
msgid ""
|
msgid ""
|
||||||
"Define which group of users this notification should be sent and shown to. "
|
"Define which group of users this notification should be sent and shown to. "
|
||||||
"If left empty, Notification won't ben sent."
|
"If left empty, Notification won't ben sent."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:563
|
#: authentik/events/models.py:560
|
||||||
msgid "Notification Rule"
|
msgid "Notification Rule"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:564
|
#: authentik/events/models.py:561
|
||||||
msgid "Notification Rules"
|
msgid "Notification Rules"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:584
|
#: authentik/events/models.py:581
|
||||||
msgid "Webhook Mapping"
|
msgid "Webhook Mapping"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: authentik/events/models.py:585
|
#: authentik/events/models.py:582
|
||||||
msgid "Webhook Mappings"
|
msgid "Webhook Mappings"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
|
Reference in New Issue