authentik.lib.tests.test_sync_status
Sync status shared by providers and directory sources
1"""Sync status shared by providers and directory sources""" 2 3from datetime import timedelta 4 5from django.urls import reverse 6from django.utils.timezone import now 7from django_dramatiq_postgres.models import TaskState 8from rest_framework.fields import DateTimeField 9from rest_framework.test import APITestCase 10 11from authentik.core.tests.utils import create_test_admin_user 12from authentik.lib.generators import generate_id 13from authentik.sources.kerberos.tasks import kerberos_sync 14from authentik.sources.ldap.models import LDAPSource 15from authentik.sources.ldap.tasks import ldap_sync 16from authentik.tasks.models import Task, TaskStatus 17from authentik.tasks.schedules.common import ScheduleSpec 18 19 20class SyncStatusTests(APITestCase): 21 """The status action reports one actor's schedule, not every schedule on the object.""" 22 23 def setUp(self) -> None: 24 self.source = LDAPSource.objects.create( 25 name=generate_id(), 26 slug=generate_id(), 27 server_uri="ldaps://ldap.example.com", 28 base_dn="dc=example,dc=com", 29 ) 30 self.client.force_login(create_test_admin_user()) 31 32 def test_ignores_schedule_for_a_different_actor(self) -> None: 33 """A schedule for another actor does not count as this sync.""" 34 other = ScheduleSpec( 35 actor=kerberos_sync, 36 crontab="0 * * * *", 37 identifier=generate_id(), 38 rel_obj=self.source, 39 ).update_or_create() 40 Task.objects.create( 41 actor_name=other.actor_name, state=TaskState.DONE, mtime=now(), rel_obj=other 42 ) 43 44 response = self.client.get( 45 reverse("authentik_api:ldapsource-sync-status", kwargs={"slug": self.source.slug}) 46 ) 47 48 self.assertEqual(response.status_code, 200) 49 self.assertJSONEqual(response.content, {"is_running": False}) 50 51 def test_reports_latest_finished_task_and_latest_success(self) -> None: 52 """The newest finished task and the newest successful task are separate.""" 53 current = now() 54 other = ScheduleSpec( 55 actor=kerberos_sync, 56 crontab="0 * * * *", 57 identifier=generate_id(), 58 rel_obj=self.source, 59 ).update_or_create() 60 matching = ScheduleSpec( 61 actor=ldap_sync, 62 crontab="0 * * * *", 63 identifier=generate_id(), 64 rel_obj=self.source, 65 ).update_or_create() 66 Task.objects.create( 67 actor_name=other.actor_name, state=TaskState.DONE, mtime=current, rel_obj=other 68 ) 69 successful = Task.objects.create( 70 actor_name=matching.actor_name, 71 state=TaskState.DONE, 72 mtime=current - timedelta(hours=2), 73 rel_obj=matching, 74 ) 75 Task.objects.create( 76 actor_name=matching.actor_name, 77 state=TaskState.REJECTED, 78 mtime=current - timedelta(hours=1), 79 rel_obj=matching, 80 ) 81 Task.objects.create( 82 actor_name=matching.actor_name, 83 state=TaskState.QUEUED, 84 mtime=current, 85 rel_obj=matching, 86 ) 87 88 response = self.client.get( 89 reverse("authentik_api:ldapsource-sync-status", kwargs={"slug": self.source.slug}) 90 ) 91 92 successful.refresh_from_db() 93 self.assertEqual(response.status_code, 200) 94 self.assertJSONEqual( 95 response.content, 96 { 97 "is_running": False, 98 "last_sync_status": TaskStatus.REJECTED, 99 "last_successful_sync": DateTimeField().to_representation(successful.mtime), 100 }, 101 )
class
SyncStatusTests(rest_framework.test.APITestCase):
21class SyncStatusTests(APITestCase): 22 """The status action reports one actor's schedule, not every schedule on the object.""" 23 24 def setUp(self) -> None: 25 self.source = LDAPSource.objects.create( 26 name=generate_id(), 27 slug=generate_id(), 28 server_uri="ldaps://ldap.example.com", 29 base_dn="dc=example,dc=com", 30 ) 31 self.client.force_login(create_test_admin_user()) 32 33 def test_ignores_schedule_for_a_different_actor(self) -> None: 34 """A schedule for another actor does not count as this sync.""" 35 other = ScheduleSpec( 36 actor=kerberos_sync, 37 crontab="0 * * * *", 38 identifier=generate_id(), 39 rel_obj=self.source, 40 ).update_or_create() 41 Task.objects.create( 42 actor_name=other.actor_name, state=TaskState.DONE, mtime=now(), rel_obj=other 43 ) 44 45 response = self.client.get( 46 reverse("authentik_api:ldapsource-sync-status", kwargs={"slug": self.source.slug}) 47 ) 48 49 self.assertEqual(response.status_code, 200) 50 self.assertJSONEqual(response.content, {"is_running": False}) 51 52 def test_reports_latest_finished_task_and_latest_success(self) -> None: 53 """The newest finished task and the newest successful task are separate.""" 54 current = now() 55 other = ScheduleSpec( 56 actor=kerberos_sync, 57 crontab="0 * * * *", 58 identifier=generate_id(), 59 rel_obj=self.source, 60 ).update_or_create() 61 matching = ScheduleSpec( 62 actor=ldap_sync, 63 crontab="0 * * * *", 64 identifier=generate_id(), 65 rel_obj=self.source, 66 ).update_or_create() 67 Task.objects.create( 68 actor_name=other.actor_name, state=TaskState.DONE, mtime=current, rel_obj=other 69 ) 70 successful = Task.objects.create( 71 actor_name=matching.actor_name, 72 state=TaskState.DONE, 73 mtime=current - timedelta(hours=2), 74 rel_obj=matching, 75 ) 76 Task.objects.create( 77 actor_name=matching.actor_name, 78 state=TaskState.REJECTED, 79 mtime=current - timedelta(hours=1), 80 rel_obj=matching, 81 ) 82 Task.objects.create( 83 actor_name=matching.actor_name, 84 state=TaskState.QUEUED, 85 mtime=current, 86 rel_obj=matching, 87 ) 88 89 response = self.client.get( 90 reverse("authentik_api:ldapsource-sync-status", kwargs={"slug": self.source.slug}) 91 ) 92 93 successful.refresh_from_db() 94 self.assertEqual(response.status_code, 200) 95 self.assertJSONEqual( 96 response.content, 97 { 98 "is_running": False, 99 "last_sync_status": TaskStatus.REJECTED, 100 "last_successful_sync": DateTimeField().to_representation(successful.mtime), 101 }, 102 )
The status action reports one actor's schedule, not every schedule on the object.
def
setUp(self) -> None:
24 def setUp(self) -> None: 25 self.source = LDAPSource.objects.create( 26 name=generate_id(), 27 slug=generate_id(), 28 server_uri="ldaps://ldap.example.com", 29 base_dn="dc=example,dc=com", 30 ) 31 self.client.force_login(create_test_admin_user())
Hook method for setting up the test fixture before exercising it.
def
test_ignores_schedule_for_a_different_actor(self) -> None:
33 def test_ignores_schedule_for_a_different_actor(self) -> None: 34 """A schedule for another actor does not count as this sync.""" 35 other = ScheduleSpec( 36 actor=kerberos_sync, 37 crontab="0 * * * *", 38 identifier=generate_id(), 39 rel_obj=self.source, 40 ).update_or_create() 41 Task.objects.create( 42 actor_name=other.actor_name, state=TaskState.DONE, mtime=now(), rel_obj=other 43 ) 44 45 response = self.client.get( 46 reverse("authentik_api:ldapsource-sync-status", kwargs={"slug": self.source.slug}) 47 ) 48 49 self.assertEqual(response.status_code, 200) 50 self.assertJSONEqual(response.content, {"is_running": False})
A schedule for another actor does not count as this sync.
def
test_reports_latest_finished_task_and_latest_success(self) -> None:
52 def test_reports_latest_finished_task_and_latest_success(self) -> None: 53 """The newest finished task and the newest successful task are separate.""" 54 current = now() 55 other = ScheduleSpec( 56 actor=kerberos_sync, 57 crontab="0 * * * *", 58 identifier=generate_id(), 59 rel_obj=self.source, 60 ).update_or_create() 61 matching = ScheduleSpec( 62 actor=ldap_sync, 63 crontab="0 * * * *", 64 identifier=generate_id(), 65 rel_obj=self.source, 66 ).update_or_create() 67 Task.objects.create( 68 actor_name=other.actor_name, state=TaskState.DONE, mtime=current, rel_obj=other 69 ) 70 successful = Task.objects.create( 71 actor_name=matching.actor_name, 72 state=TaskState.DONE, 73 mtime=current - timedelta(hours=2), 74 rel_obj=matching, 75 ) 76 Task.objects.create( 77 actor_name=matching.actor_name, 78 state=TaskState.REJECTED, 79 mtime=current - timedelta(hours=1), 80 rel_obj=matching, 81 ) 82 Task.objects.create( 83 actor_name=matching.actor_name, 84 state=TaskState.QUEUED, 85 mtime=current, 86 rel_obj=matching, 87 ) 88 89 response = self.client.get( 90 reverse("authentik_api:ldapsource-sync-status", kwargs={"slug": self.source.slug}) 91 ) 92 93 successful.refresh_from_db() 94 self.assertEqual(response.status_code, 200) 95 self.assertJSONEqual( 96 response.content, 97 { 98 "is_running": False, 99 "last_sync_status": TaskStatus.REJECTED, 100 "last_successful_sync": DateTimeField().to_representation(successful.mtime), 101 }, 102 )
The newest finished task and the newest successful task are separate.