authentik.tasks.tests.test_worker_middleware
1from django.test import TestCase 2from dramatiq import actor, get_broker 3 4from authentik.tasks.middleware import CurrentTask, MetricsMiddleware 5from authentik.tasks.models import Task, TaskLog 6 7 8class TestWorkerMiddleware(TestCase): 9 10 def test_metrics_multiple_brokers(self): 11 """Booting metrics more than once (as e2e tests do per test) must not fail""" 12 for _ in range(2): 13 metrics = MetricsMiddleware(prefix="authentik") 14 metrics.before_worker_boot(get_broker(), None) 15 self.assertTrue(hasattr(metrics, "in_progress_messages")) 16 17 def test_task_log(self): 18 @actor 19 def test_task(): 20 self = CurrentTask.get_task() 21 self.info("foo") 22 23 test_task.send() 24 task = Task.objects.filter(actor_name=test_task.actor_name).first() 25 logs = list( 26 TaskLog.objects.filter(task=task).order_by("timestamp").values_list("event", flat=True) 27 ) 28 self.assertEqual( 29 logs, 30 [ 31 "Task has been queued", 32 "Task is being processed", 33 "foo", 34 "Task finished processing without errors", 35 ], 36 ) 37 broker = get_broker() 38 del broker.actors[test_task.actor_name] 39 40 def test_task_exceptions(self): 41 @actor(description="test-helper-exception") 42 def test_task(): 43 raise ValueError("foo") 44 45 test_task.send() 46 task = Task.objects.filter(actor_name=test_task.actor_name).first() 47 logs = list( 48 TaskLog.objects.filter(task=task).order_by("timestamp").values_list("event", flat=True) 49 ) 50 self.assertEqual( 51 logs, 52 [ 53 "Task has been queued", 54 "Task is being processed", 55 "Task has encountered an error: foo", 56 ], 57 ) 58 broker = get_broker() 59 del broker.actors[test_task.actor_name]
class
TestWorkerMiddleware(django.test.testcases.TestCase):
9class TestWorkerMiddleware(TestCase): 10 11 def test_metrics_multiple_brokers(self): 12 """Booting metrics more than once (as e2e tests do per test) must not fail""" 13 for _ in range(2): 14 metrics = MetricsMiddleware(prefix="authentik") 15 metrics.before_worker_boot(get_broker(), None) 16 self.assertTrue(hasattr(metrics, "in_progress_messages")) 17 18 def test_task_log(self): 19 @actor 20 def test_task(): 21 self = CurrentTask.get_task() 22 self.info("foo") 23 24 test_task.send() 25 task = Task.objects.filter(actor_name=test_task.actor_name).first() 26 logs = list( 27 TaskLog.objects.filter(task=task).order_by("timestamp").values_list("event", flat=True) 28 ) 29 self.assertEqual( 30 logs, 31 [ 32 "Task has been queued", 33 "Task is being processed", 34 "foo", 35 "Task finished processing without errors", 36 ], 37 ) 38 broker = get_broker() 39 del broker.actors[test_task.actor_name] 40 41 def test_task_exceptions(self): 42 @actor(description="test-helper-exception") 43 def test_task(): 44 raise ValueError("foo") 45 46 test_task.send() 47 task = Task.objects.filter(actor_name=test_task.actor_name).first() 48 logs = list( 49 TaskLog.objects.filter(task=task).order_by("timestamp").values_list("event", flat=True) 50 ) 51 self.assertEqual( 52 logs, 53 [ 54 "Task has been queued", 55 "Task is being processed", 56 "Task has encountered an error: foo", 57 ], 58 ) 59 broker = get_broker() 60 del broker.actors[test_task.actor_name]
Similar to TransactionTestCase, but use transaction.atomic() to achieve
test isolation.
In most situations, TestCase should be preferred to TransactionTestCase as it allows faster execution. However, there are some situations where using TransactionTestCase might be necessary (e.g. testing some transactional behavior).
On database backends with no transaction support, TestCase behaves as TransactionTestCase.
def
test_metrics_multiple_brokers(self):
11 def test_metrics_multiple_brokers(self): 12 """Booting metrics more than once (as e2e tests do per test) must not fail""" 13 for _ in range(2): 14 metrics = MetricsMiddleware(prefix="authentik") 15 metrics.before_worker_boot(get_broker(), None) 16 self.assertTrue(hasattr(metrics, "in_progress_messages"))
Booting metrics more than once (as e2e tests do per test) must not fail
def
test_task_log(self):
18 def test_task_log(self): 19 @actor 20 def test_task(): 21 self = CurrentTask.get_task() 22 self.info("foo") 23 24 test_task.send() 25 task = Task.objects.filter(actor_name=test_task.actor_name).first() 26 logs = list( 27 TaskLog.objects.filter(task=task).order_by("timestamp").values_list("event", flat=True) 28 ) 29 self.assertEqual( 30 logs, 31 [ 32 "Task has been queued", 33 "Task is being processed", 34 "foo", 35 "Task finished processing without errors", 36 ], 37 ) 38 broker = get_broker() 39 del broker.actors[test_task.actor_name]
def
test_task_exceptions(self):
41 def test_task_exceptions(self): 42 @actor(description="test-helper-exception") 43 def test_task(): 44 raise ValueError("foo") 45 46 test_task.send() 47 task = Task.objects.filter(actor_name=test_task.actor_name).first() 48 logs = list( 49 TaskLog.objects.filter(task=task).order_by("timestamp").values_list("event", flat=True) 50 ) 51 self.assertEqual( 52 logs, 53 [ 54 "Task has been queued", 55 "Task is being processed", 56 "Task has encountered an error: foo", 57 ], 58 ) 59 broker = get_broker() 60 del broker.actors[test_task.actor_name]