authentik.policies.event_matcher.tests

event_matcher tests

  1"""event_matcher tests"""
  2
  3from django.test import TestCase
  4from django.urls import reverse
  5from guardian.shortcuts import get_anonymous_user
  6from rest_framework.test import APITestCase
  7
  8from authentik.core.tests.utils import create_test_admin_user
  9from authentik.events.models import Event, EventAction
 10from authentik.lib.generators import generate_id
 11from authentik.policies.event_matcher.models import EventMatcherPolicy
 12from authentik.policies.types import PolicyRequest
 13
 14
 15class TestEventMatcherPolicy(TestCase):
 16    """EventMatcherPolicy tests"""
 17
 18    def test_match_action(self):
 19        """Test match action"""
 20        event = Event.new(EventAction.LOGIN)
 21        request = PolicyRequest(get_anonymous_user())
 22        request.context["event"] = event
 23        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(action=EventAction.LOGIN)
 24        response = policy.passes(request)
 25        self.assertTrue(response.passing)
 26        self.assertTupleEqual(response.messages, ("Action matched.",))
 27
 28    def test_match_client_ip(self):
 29        """Test match client_ip"""
 30        event = Event.new(EventAction.LOGIN)
 31        event.client_ip = "1.2.3.4"
 32        request = PolicyRequest(get_anonymous_user())
 33        request.context["event"] = event
 34        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4")
 35        response = policy.passes(request)
 36        self.assertTrue(response.passing)
 37        self.assertTupleEqual(response.messages, ("Client IP matched.",))
 38
 39    def test_match_app(self):
 40        """Test match app"""
 41        event = Event.new(EventAction.LOGIN)
 42        event.app = "foo"
 43        request = PolicyRequest(get_anonymous_user())
 44        request.context["event"] = event
 45        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(app="foo")
 46        response = policy.passes(request)
 47        self.assertTrue(response.passing)
 48        self.assertTupleEqual(response.messages, ("App matched.",))
 49
 50    def test_match_model(self):
 51        """Test match model"""
 52        event = Event.new(EventAction.LOGIN)
 53        event.context = {
 54            "model": {
 55                "app": "foo",
 56                "model_name": "bar",
 57            }
 58        }
 59        request = PolicyRequest(get_anonymous_user())
 60        request.context["event"] = event
 61        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(model="foo.bar")
 62        response = policy.passes(request)
 63        self.assertTrue(response.passing)
 64        self.assertTupleEqual(response.messages, ("Model matched.",))
 65
 66    def test_drop(self):
 67        """Test drop event"""
 68        event = Event.new(EventAction.LOGIN)
 69        event.client_ip = "1.2.3.4"
 70        request = PolicyRequest(get_anonymous_user())
 71        request.context["event"] = event
 72        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.5")
 73        response = policy.passes(request)
 74        self.assertFalse(response.passing)
 75        self.assertEqual(response.messages, ())
 76        self.assertEqual(response.source_results[0].messages, ())
 77
 78    def test_drop_multiple(self):
 79        """Test drop event"""
 80        event = Event.new(EventAction.LOGIN)
 81        event.app = "foo"
 82        event.client_ip = "1.2.3.4"
 83        request = PolicyRequest(get_anonymous_user())
 84        request.context["event"] = event
 85        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(
 86            client_ip="1.2.3.5", app="foo"
 87        )
 88        response = policy.passes(request)
 89        self.assertFalse(response.passing)
 90
 91    def test_multiple(self):
 92        """Test multiple"""
 93        event = Event.new(EventAction.LOGIN)
 94        event.app = "foo"
 95        event.client_ip = "1.2.3.4"
 96        request = PolicyRequest(get_anonymous_user())
 97        request.context["event"] = event
 98        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(
 99            client_ip="1.2.3.4", app="foo"
100        )
101        response = policy.passes(request)
102        self.assertTrue(response.passing)
103
104    def test_invalid(self):
105        """Test passing event"""
106        request = PolicyRequest(get_anonymous_user())
107        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4")
108        response = policy.passes(request)
109        self.assertFalse(response.passing)
110
111    def test_match_query(self):
112        """Test match query"""
113        event = Event.new(EventAction.LOGIN)
114        event.save()
115        request = PolicyRequest(get_anonymous_user())
116        request.context["event"] = event
117        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(query='action = "login"')
118        response = policy.passes(request)
119        self.assertTrue(response.passing)
120        self.assertTupleEqual(response.messages, ("Query matched.",))
121
122
123class TestEventMatcherPolicyAPI(APITestCase):
124    """EventMatcherPolicy API tests"""
125
126    def setUp(self):
127        self.client.force_login(create_test_admin_user())
128
129    def test_validate_criteria(self):
130        """Omitted criteria count as unset, and partial updates keep stored criteria"""
131        res = self.client.post(
132            reverse("authentik_api:eventmatcherpolicy-list"),
133            data={"name": generate_id()},
134        )
135        self.assertEqual(res.status_code, 400)
136        self.assertJSONEqual(
137            res.content, {"non_field_errors": ["At least one criteria must be set."]}
138        )
139        policy = EventMatcherPolicy.objects.create(name=generate_id(), action=EventAction.LOGIN)
140        res = self.client.patch(
141            reverse("authentik_api:eventmatcherpolicy-detail", kwargs={"pk": policy.pk}),
142            data={"name": generate_id()},
143        )
144        self.assertEqual(res.status_code, 200)
145        res = self.client.patch(
146            reverse("authentik_api:eventmatcherpolicy-detail", kwargs={"pk": policy.pk}),
147            data={"action": None},
148        )
149        self.assertEqual(res.status_code, 400)
class TestEventMatcherPolicy(django.test.testcases.TestCase):
 16class TestEventMatcherPolicy(TestCase):
 17    """EventMatcherPolicy tests"""
 18
 19    def test_match_action(self):
 20        """Test match action"""
 21        event = Event.new(EventAction.LOGIN)
 22        request = PolicyRequest(get_anonymous_user())
 23        request.context["event"] = event
 24        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(action=EventAction.LOGIN)
 25        response = policy.passes(request)
 26        self.assertTrue(response.passing)
 27        self.assertTupleEqual(response.messages, ("Action matched.",))
 28
 29    def test_match_client_ip(self):
 30        """Test match client_ip"""
 31        event = Event.new(EventAction.LOGIN)
 32        event.client_ip = "1.2.3.4"
 33        request = PolicyRequest(get_anonymous_user())
 34        request.context["event"] = event
 35        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4")
 36        response = policy.passes(request)
 37        self.assertTrue(response.passing)
 38        self.assertTupleEqual(response.messages, ("Client IP matched.",))
 39
 40    def test_match_app(self):
 41        """Test match app"""
 42        event = Event.new(EventAction.LOGIN)
 43        event.app = "foo"
 44        request = PolicyRequest(get_anonymous_user())
 45        request.context["event"] = event
 46        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(app="foo")
 47        response = policy.passes(request)
 48        self.assertTrue(response.passing)
 49        self.assertTupleEqual(response.messages, ("App matched.",))
 50
 51    def test_match_model(self):
 52        """Test match model"""
 53        event = Event.new(EventAction.LOGIN)
 54        event.context = {
 55            "model": {
 56                "app": "foo",
 57                "model_name": "bar",
 58            }
 59        }
 60        request = PolicyRequest(get_anonymous_user())
 61        request.context["event"] = event
 62        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(model="foo.bar")
 63        response = policy.passes(request)
 64        self.assertTrue(response.passing)
 65        self.assertTupleEqual(response.messages, ("Model matched.",))
 66
 67    def test_drop(self):
 68        """Test drop event"""
 69        event = Event.new(EventAction.LOGIN)
 70        event.client_ip = "1.2.3.4"
 71        request = PolicyRequest(get_anonymous_user())
 72        request.context["event"] = event
 73        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.5")
 74        response = policy.passes(request)
 75        self.assertFalse(response.passing)
 76        self.assertEqual(response.messages, ())
 77        self.assertEqual(response.source_results[0].messages, ())
 78
 79    def test_drop_multiple(self):
 80        """Test drop event"""
 81        event = Event.new(EventAction.LOGIN)
 82        event.app = "foo"
 83        event.client_ip = "1.2.3.4"
 84        request = PolicyRequest(get_anonymous_user())
 85        request.context["event"] = event
 86        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(
 87            client_ip="1.2.3.5", app="foo"
 88        )
 89        response = policy.passes(request)
 90        self.assertFalse(response.passing)
 91
 92    def test_multiple(self):
 93        """Test multiple"""
 94        event = Event.new(EventAction.LOGIN)
 95        event.app = "foo"
 96        event.client_ip = "1.2.3.4"
 97        request = PolicyRequest(get_anonymous_user())
 98        request.context["event"] = event
 99        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(
100            client_ip="1.2.3.4", app="foo"
101        )
102        response = policy.passes(request)
103        self.assertTrue(response.passing)
104
105    def test_invalid(self):
106        """Test passing event"""
107        request = PolicyRequest(get_anonymous_user())
108        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4")
109        response = policy.passes(request)
110        self.assertFalse(response.passing)
111
112    def test_match_query(self):
113        """Test match query"""
114        event = Event.new(EventAction.LOGIN)
115        event.save()
116        request = PolicyRequest(get_anonymous_user())
117        request.context["event"] = event
118        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(query='action = "login"')
119        response = policy.passes(request)
120        self.assertTrue(response.passing)
121        self.assertTupleEqual(response.messages, ("Query matched.",))

EventMatcherPolicy tests

def test_match_action(self):
19    def test_match_action(self):
20        """Test match action"""
21        event = Event.new(EventAction.LOGIN)
22        request = PolicyRequest(get_anonymous_user())
23        request.context["event"] = event
24        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(action=EventAction.LOGIN)
25        response = policy.passes(request)
26        self.assertTrue(response.passing)
27        self.assertTupleEqual(response.messages, ("Action matched.",))

Test match action

def test_match_client_ip(self):
29    def test_match_client_ip(self):
30        """Test match client_ip"""
31        event = Event.new(EventAction.LOGIN)
32        event.client_ip = "1.2.3.4"
33        request = PolicyRequest(get_anonymous_user())
34        request.context["event"] = event
35        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4")
36        response = policy.passes(request)
37        self.assertTrue(response.passing)
38        self.assertTupleEqual(response.messages, ("Client IP matched.",))

Test match client_ip

def test_match_app(self):
40    def test_match_app(self):
41        """Test match app"""
42        event = Event.new(EventAction.LOGIN)
43        event.app = "foo"
44        request = PolicyRequest(get_anonymous_user())
45        request.context["event"] = event
46        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(app="foo")
47        response = policy.passes(request)
48        self.assertTrue(response.passing)
49        self.assertTupleEqual(response.messages, ("App matched.",))

Test match app

def test_match_model(self):
51    def test_match_model(self):
52        """Test match model"""
53        event = Event.new(EventAction.LOGIN)
54        event.context = {
55            "model": {
56                "app": "foo",
57                "model_name": "bar",
58            }
59        }
60        request = PolicyRequest(get_anonymous_user())
61        request.context["event"] = event
62        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(model="foo.bar")
63        response = policy.passes(request)
64        self.assertTrue(response.passing)
65        self.assertTupleEqual(response.messages, ("Model matched.",))

Test match model

def test_drop(self):
67    def test_drop(self):
68        """Test drop event"""
69        event = Event.new(EventAction.LOGIN)
70        event.client_ip = "1.2.3.4"
71        request = PolicyRequest(get_anonymous_user())
72        request.context["event"] = event
73        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.5")
74        response = policy.passes(request)
75        self.assertFalse(response.passing)
76        self.assertEqual(response.messages, ())
77        self.assertEqual(response.source_results[0].messages, ())

Test drop event

def test_drop_multiple(self):
79    def test_drop_multiple(self):
80        """Test drop event"""
81        event = Event.new(EventAction.LOGIN)
82        event.app = "foo"
83        event.client_ip = "1.2.3.4"
84        request = PolicyRequest(get_anonymous_user())
85        request.context["event"] = event
86        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(
87            client_ip="1.2.3.5", app="foo"
88        )
89        response = policy.passes(request)
90        self.assertFalse(response.passing)

Test drop event

def test_multiple(self):
 92    def test_multiple(self):
 93        """Test multiple"""
 94        event = Event.new(EventAction.LOGIN)
 95        event.app = "foo"
 96        event.client_ip = "1.2.3.4"
 97        request = PolicyRequest(get_anonymous_user())
 98        request.context["event"] = event
 99        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(
100            client_ip="1.2.3.4", app="foo"
101        )
102        response = policy.passes(request)
103        self.assertTrue(response.passing)

Test multiple

def test_invalid(self):
105    def test_invalid(self):
106        """Test passing event"""
107        request = PolicyRequest(get_anonymous_user())
108        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4")
109        response = policy.passes(request)
110        self.assertFalse(response.passing)

Test passing event

def test_match_query(self):
112    def test_match_query(self):
113        """Test match query"""
114        event = Event.new(EventAction.LOGIN)
115        event.save()
116        request = PolicyRequest(get_anonymous_user())
117        request.context["event"] = event
118        policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(query='action = "login"')
119        response = policy.passes(request)
120        self.assertTrue(response.passing)
121        self.assertTupleEqual(response.messages, ("Query matched.",))

Test match query

class TestEventMatcherPolicyAPI(rest_framework.test.APITestCase):
124class TestEventMatcherPolicyAPI(APITestCase):
125    """EventMatcherPolicy API tests"""
126
127    def setUp(self):
128        self.client.force_login(create_test_admin_user())
129
130    def test_validate_criteria(self):
131        """Omitted criteria count as unset, and partial updates keep stored criteria"""
132        res = self.client.post(
133            reverse("authentik_api:eventmatcherpolicy-list"),
134            data={"name": generate_id()},
135        )
136        self.assertEqual(res.status_code, 400)
137        self.assertJSONEqual(
138            res.content, {"non_field_errors": ["At least one criteria must be set."]}
139        )
140        policy = EventMatcherPolicy.objects.create(name=generate_id(), action=EventAction.LOGIN)
141        res = self.client.patch(
142            reverse("authentik_api:eventmatcherpolicy-detail", kwargs={"pk": policy.pk}),
143            data={"name": generate_id()},
144        )
145        self.assertEqual(res.status_code, 200)
146        res = self.client.patch(
147            reverse("authentik_api:eventmatcherpolicy-detail", kwargs={"pk": policy.pk}),
148            data={"action": None},
149        )
150        self.assertEqual(res.status_code, 400)

EventMatcherPolicy API tests

def setUp(self):
127    def setUp(self):
128        self.client.force_login(create_test_admin_user())

Hook method for setting up the test fixture before exercising it.

def test_validate_criteria(self):
130    def test_validate_criteria(self):
131        """Omitted criteria count as unset, and partial updates keep stored criteria"""
132        res = self.client.post(
133            reverse("authentik_api:eventmatcherpolicy-list"),
134            data={"name": generate_id()},
135        )
136        self.assertEqual(res.status_code, 400)
137        self.assertJSONEqual(
138            res.content, {"non_field_errors": ["At least one criteria must be set."]}
139        )
140        policy = EventMatcherPolicy.objects.create(name=generate_id(), action=EventAction.LOGIN)
141        res = self.client.patch(
142            reverse("authentik_api:eventmatcherpolicy-detail", kwargs={"pk": policy.pk}),
143            data={"name": generate_id()},
144        )
145        self.assertEqual(res.status_code, 200)
146        res = self.client.patch(
147            reverse("authentik_api:eventmatcherpolicy-detail", kwargs={"pk": policy.pk}),
148            data={"action": None},
149        )
150        self.assertEqual(res.status_code, 400)

Omitted criteria count as unset, and partial updates keep stored criteria