authentik.policies.event_matcher.tests
event_matcher tests
1"""event_matcher tests""" 2 3from django.test import TestCase 4from guardian.shortcuts import get_anonymous_user 5 6from authentik.events.models import Event, EventAction 7from authentik.policies.event_matcher.models import EventMatcherPolicy 8from authentik.policies.types import PolicyRequest 9 10 11class TestEventMatcherPolicy(TestCase): 12 """EventMatcherPolicy tests""" 13 14 def test_match_action(self): 15 """Test match action""" 16 event = Event.new(EventAction.LOGIN) 17 request = PolicyRequest(get_anonymous_user()) 18 request.context["event"] = event 19 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(action=EventAction.LOGIN) 20 response = policy.passes(request) 21 self.assertTrue(response.passing) 22 self.assertTupleEqual(response.messages, ("Action matched.",)) 23 24 def test_match_client_ip(self): 25 """Test match client_ip""" 26 event = Event.new(EventAction.LOGIN) 27 event.client_ip = "1.2.3.4" 28 request = PolicyRequest(get_anonymous_user()) 29 request.context["event"] = event 30 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4") 31 response = policy.passes(request) 32 self.assertTrue(response.passing) 33 self.assertTupleEqual(response.messages, ("Client IP matched.",)) 34 35 def test_match_app(self): 36 """Test match app""" 37 event = Event.new(EventAction.LOGIN) 38 event.app = "foo" 39 request = PolicyRequest(get_anonymous_user()) 40 request.context["event"] = event 41 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(app="foo") 42 response = policy.passes(request) 43 self.assertTrue(response.passing) 44 self.assertTupleEqual(response.messages, ("App matched.",)) 45 46 def test_match_model(self): 47 """Test match model""" 48 event = Event.new(EventAction.LOGIN) 49 event.context = { 50 "model": { 51 "app": "foo", 52 "model_name": "bar", 53 } 54 } 55 request = PolicyRequest(get_anonymous_user()) 56 request.context["event"] = event 57 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(model="foo.bar") 58 response = policy.passes(request) 59 self.assertTrue(response.passing) 60 self.assertTupleEqual(response.messages, ("Model matched.",)) 61 62 def test_drop(self): 63 """Test drop event""" 64 event = Event.new(EventAction.LOGIN) 65 event.client_ip = "1.2.3.4" 66 request = PolicyRequest(get_anonymous_user()) 67 request.context["event"] = event 68 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.5") 69 response = policy.passes(request) 70 self.assertFalse(response.passing) 71 72 def test_drop_multiple(self): 73 """Test drop event""" 74 event = Event.new(EventAction.LOGIN) 75 event.app = "foo" 76 event.client_ip = "1.2.3.4" 77 request = PolicyRequest(get_anonymous_user()) 78 request.context["event"] = event 79 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create( 80 client_ip="1.2.3.5", app="foo" 81 ) 82 response = policy.passes(request) 83 self.assertFalse(response.passing) 84 85 def test_multiple(self): 86 """Test multiple""" 87 event = Event.new(EventAction.LOGIN) 88 event.app = "foo" 89 event.client_ip = "1.2.3.4" 90 request = PolicyRequest(get_anonymous_user()) 91 request.context["event"] = event 92 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create( 93 client_ip="1.2.3.4", app="foo" 94 ) 95 response = policy.passes(request) 96 self.assertTrue(response.passing) 97 98 def test_invalid(self): 99 """Test passing event""" 100 request = PolicyRequest(get_anonymous_user()) 101 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4") 102 response = policy.passes(request) 103 self.assertFalse(response.passing)
class
TestEventMatcherPolicy(django.test.testcases.TestCase):
12class TestEventMatcherPolicy(TestCase): 13 """EventMatcherPolicy tests""" 14 15 def test_match_action(self): 16 """Test match action""" 17 event = Event.new(EventAction.LOGIN) 18 request = PolicyRequest(get_anonymous_user()) 19 request.context["event"] = event 20 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(action=EventAction.LOGIN) 21 response = policy.passes(request) 22 self.assertTrue(response.passing) 23 self.assertTupleEqual(response.messages, ("Action matched.",)) 24 25 def test_match_client_ip(self): 26 """Test match client_ip""" 27 event = Event.new(EventAction.LOGIN) 28 event.client_ip = "1.2.3.4" 29 request = PolicyRequest(get_anonymous_user()) 30 request.context["event"] = event 31 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4") 32 response = policy.passes(request) 33 self.assertTrue(response.passing) 34 self.assertTupleEqual(response.messages, ("Client IP matched.",)) 35 36 def test_match_app(self): 37 """Test match app""" 38 event = Event.new(EventAction.LOGIN) 39 event.app = "foo" 40 request = PolicyRequest(get_anonymous_user()) 41 request.context["event"] = event 42 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(app="foo") 43 response = policy.passes(request) 44 self.assertTrue(response.passing) 45 self.assertTupleEqual(response.messages, ("App matched.",)) 46 47 def test_match_model(self): 48 """Test match model""" 49 event = Event.new(EventAction.LOGIN) 50 event.context = { 51 "model": { 52 "app": "foo", 53 "model_name": "bar", 54 } 55 } 56 request = PolicyRequest(get_anonymous_user()) 57 request.context["event"] = event 58 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(model="foo.bar") 59 response = policy.passes(request) 60 self.assertTrue(response.passing) 61 self.assertTupleEqual(response.messages, ("Model matched.",)) 62 63 def test_drop(self): 64 """Test drop event""" 65 event = Event.new(EventAction.LOGIN) 66 event.client_ip = "1.2.3.4" 67 request = PolicyRequest(get_anonymous_user()) 68 request.context["event"] = event 69 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.5") 70 response = policy.passes(request) 71 self.assertFalse(response.passing) 72 73 def test_drop_multiple(self): 74 """Test drop event""" 75 event = Event.new(EventAction.LOGIN) 76 event.app = "foo" 77 event.client_ip = "1.2.3.4" 78 request = PolicyRequest(get_anonymous_user()) 79 request.context["event"] = event 80 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create( 81 client_ip="1.2.3.5", app="foo" 82 ) 83 response = policy.passes(request) 84 self.assertFalse(response.passing) 85 86 def test_multiple(self): 87 """Test multiple""" 88 event = Event.new(EventAction.LOGIN) 89 event.app = "foo" 90 event.client_ip = "1.2.3.4" 91 request = PolicyRequest(get_anonymous_user()) 92 request.context["event"] = event 93 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create( 94 client_ip="1.2.3.4", app="foo" 95 ) 96 response = policy.passes(request) 97 self.assertTrue(response.passing) 98 99 def test_invalid(self): 100 """Test passing event""" 101 request = PolicyRequest(get_anonymous_user()) 102 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4") 103 response = policy.passes(request) 104 self.assertFalse(response.passing)
EventMatcherPolicy tests
def
test_match_action(self):
15 def test_match_action(self): 16 """Test match action""" 17 event = Event.new(EventAction.LOGIN) 18 request = PolicyRequest(get_anonymous_user()) 19 request.context["event"] = event 20 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(action=EventAction.LOGIN) 21 response = policy.passes(request) 22 self.assertTrue(response.passing) 23 self.assertTupleEqual(response.messages, ("Action matched.",))
Test match action
def
test_match_client_ip(self):
25 def test_match_client_ip(self): 26 """Test match client_ip""" 27 event = Event.new(EventAction.LOGIN) 28 event.client_ip = "1.2.3.4" 29 request = PolicyRequest(get_anonymous_user()) 30 request.context["event"] = event 31 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4") 32 response = policy.passes(request) 33 self.assertTrue(response.passing) 34 self.assertTupleEqual(response.messages, ("Client IP matched.",))
Test match client_ip
def
test_match_app(self):
36 def test_match_app(self): 37 """Test match app""" 38 event = Event.new(EventAction.LOGIN) 39 event.app = "foo" 40 request = PolicyRequest(get_anonymous_user()) 41 request.context["event"] = event 42 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(app="foo") 43 response = policy.passes(request) 44 self.assertTrue(response.passing) 45 self.assertTupleEqual(response.messages, ("App matched.",))
Test match app
def
test_match_model(self):
47 def test_match_model(self): 48 """Test match model""" 49 event = Event.new(EventAction.LOGIN) 50 event.context = { 51 "model": { 52 "app": "foo", 53 "model_name": "bar", 54 } 55 } 56 request = PolicyRequest(get_anonymous_user()) 57 request.context["event"] = event 58 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(model="foo.bar") 59 response = policy.passes(request) 60 self.assertTrue(response.passing) 61 self.assertTupleEqual(response.messages, ("Model matched.",))
Test match model
def
test_drop(self):
63 def test_drop(self): 64 """Test drop event""" 65 event = Event.new(EventAction.LOGIN) 66 event.client_ip = "1.2.3.4" 67 request = PolicyRequest(get_anonymous_user()) 68 request.context["event"] = event 69 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.5") 70 response = policy.passes(request) 71 self.assertFalse(response.passing)
Test drop event
def
test_drop_multiple(self):
73 def test_drop_multiple(self): 74 """Test drop event""" 75 event = Event.new(EventAction.LOGIN) 76 event.app = "foo" 77 event.client_ip = "1.2.3.4" 78 request = PolicyRequest(get_anonymous_user()) 79 request.context["event"] = event 80 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create( 81 client_ip="1.2.3.5", app="foo" 82 ) 83 response = policy.passes(request) 84 self.assertFalse(response.passing)
Test drop event
def
test_multiple(self):
86 def test_multiple(self): 87 """Test multiple""" 88 event = Event.new(EventAction.LOGIN) 89 event.app = "foo" 90 event.client_ip = "1.2.3.4" 91 request = PolicyRequest(get_anonymous_user()) 92 request.context["event"] = event 93 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create( 94 client_ip="1.2.3.4", app="foo" 95 ) 96 response = policy.passes(request) 97 self.assertTrue(response.passing)
Test multiple
def
test_invalid(self):
99 def test_invalid(self): 100 """Test passing event""" 101 request = PolicyRequest(get_anonymous_user()) 102 policy: EventMatcherPolicy = EventMatcherPolicy.objects.create(client_ip="1.2.3.4") 103 response = policy.passes(request) 104 self.assertFalse(response.passing)
Test passing event