authentik.endpoints.tests.test_devices_api

 1from datetime import datetime, timedelta
 2
 3from django.urls import reverse
 4from django.utils.timezone import now
 5from rest_framework.test import APITestCase
 6
 7from authentik.core.tests.utils import create_test_admin_user
 8from authentik.endpoints.models import Connector, Device, DeviceConnection
 9from authentik.lib.generators import generate_id
10
11
12class TestDevicesAPI(APITestCase):
13    def create_device_with_snapshot(self, t: datetime):
14        device = Device.objects.create(
15            identifier=generate_id(),
16            name=generate_id(),
17        )
18        connector = Connector.objects.create(name=generate_id())
19        connection = DeviceConnection.objects.create(
20            device=device,
21            connector=connector,
22        )
23        snap = connection.create_snapshot({"vendor": {"goauthentik.io/testing": {"foo": "bar"}}})
24        snap.created = t
25        snap.save()
26
27    def test_summary(self):
28        user = create_test_admin_user()
29        self.client.force_login(user)
30        self.create_device_with_snapshot(now())
31        self.create_device_with_snapshot(now())
32        res = self.client.get(reverse("authentik_api:endpoint_device-summary"))
33        self.assertEqual(res.status_code, 200)
34        self.assertJSONEqual(
35            res.content, {"outdated_agent_count": 0, "total_count": 2, "unreachable_count": 0}
36        )
37
38    def test_summary_unreachable(self):
39        user = create_test_admin_user()
40        self.client.force_login(user)
41        self.create_device_with_snapshot(now())
42        self.create_device_with_snapshot(now())
43        self.create_device_with_snapshot(now() - timedelta(hours=26))
44        self.create_device_with_snapshot(now() - timedelta(hours=26))
45        self.create_device_with_snapshot(now() - timedelta(hours=26))
46        res = self.client.get(reverse("authentik_api:endpoint_device-summary"))
47        self.assertEqual(res.status_code, 200)
48        self.assertJSONEqual(
49            res.content, {"outdated_agent_count": 0, "total_count": 5, "unreachable_count": 3}
50        )
class TestDevicesAPI(rest_framework.test.APITestCase):
13class TestDevicesAPI(APITestCase):
14    def create_device_with_snapshot(self, t: datetime):
15        device = Device.objects.create(
16            identifier=generate_id(),
17            name=generate_id(),
18        )
19        connector = Connector.objects.create(name=generate_id())
20        connection = DeviceConnection.objects.create(
21            device=device,
22            connector=connector,
23        )
24        snap = connection.create_snapshot({"vendor": {"goauthentik.io/testing": {"foo": "bar"}}})
25        snap.created = t
26        snap.save()
27
28    def test_summary(self):
29        user = create_test_admin_user()
30        self.client.force_login(user)
31        self.create_device_with_snapshot(now())
32        self.create_device_with_snapshot(now())
33        res = self.client.get(reverse("authentik_api:endpoint_device-summary"))
34        self.assertEqual(res.status_code, 200)
35        self.assertJSONEqual(
36            res.content, {"outdated_agent_count": 0, "total_count": 2, "unreachable_count": 0}
37        )
38
39    def test_summary_unreachable(self):
40        user = create_test_admin_user()
41        self.client.force_login(user)
42        self.create_device_with_snapshot(now())
43        self.create_device_with_snapshot(now())
44        self.create_device_with_snapshot(now() - timedelta(hours=26))
45        self.create_device_with_snapshot(now() - timedelta(hours=26))
46        self.create_device_with_snapshot(now() - timedelta(hours=26))
47        res = self.client.get(reverse("authentik_api:endpoint_device-summary"))
48        self.assertEqual(res.status_code, 200)
49        self.assertJSONEqual(
50            res.content, {"outdated_agent_count": 0, "total_count": 5, "unreachable_count": 3}
51        )

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 create_device_with_snapshot(self, t: datetime.datetime):
14    def create_device_with_snapshot(self, t: datetime):
15        device = Device.objects.create(
16            identifier=generate_id(),
17            name=generate_id(),
18        )
19        connector = Connector.objects.create(name=generate_id())
20        connection = DeviceConnection.objects.create(
21            device=device,
22            connector=connector,
23        )
24        snap = connection.create_snapshot({"vendor": {"goauthentik.io/testing": {"foo": "bar"}}})
25        snap.created = t
26        snap.save()
def test_summary(self):
28    def test_summary(self):
29        user = create_test_admin_user()
30        self.client.force_login(user)
31        self.create_device_with_snapshot(now())
32        self.create_device_with_snapshot(now())
33        res = self.client.get(reverse("authentik_api:endpoint_device-summary"))
34        self.assertEqual(res.status_code, 200)
35        self.assertJSONEqual(
36            res.content, {"outdated_agent_count": 0, "total_count": 2, "unreachable_count": 0}
37        )
def test_summary_unreachable(self):
39    def test_summary_unreachable(self):
40        user = create_test_admin_user()
41        self.client.force_login(user)
42        self.create_device_with_snapshot(now())
43        self.create_device_with_snapshot(now())
44        self.create_device_with_snapshot(now() - timedelta(hours=26))
45        self.create_device_with_snapshot(now() - timedelta(hours=26))
46        self.create_device_with_snapshot(now() - timedelta(hours=26))
47        res = self.client.get(reverse("authentik_api:endpoint_device-summary"))
48        self.assertEqual(res.status_code, 200)
49        self.assertJSONEqual(
50            res.content, {"outdated_agent_count": 0, "total_count": 5, "unreachable_count": 3}
51        )