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 10from authentik.providers.rac.models import Protocols, RACConnectionOverride 11 12 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 ) 52 53 def test_create(self): 54 """Devices can be created by hand, for machines which are not enrolled 55 through a connector""" 56 user = create_test_admin_user() 57 self.client.force_login(user) 58 name = generate_id() 59 res = self.client.post( 60 reverse("authentik_api:endpoint_device-list"), 61 data={ 62 "name": name, 63 "rac": {"host": "host.example.com", "protocol": Protocols.SSH}, 64 }, 65 content_type="application/json", 66 ) 67 self.assertEqual(res.status_code, 201) 68 device = Device.objects.get(name=name) 69 self.assertTrue(device.identifier.startswith("manual://")) 70 self.assertFalse(device.expiring) 71 # A device which is added by hand has to say how it is reached 72 self.assertEqual(device.rac_override.host, "host.example.com") 73 self.assertEqual(device.rac_override.protocol, Protocols.SSH) 74 75 def test_create_without_connection(self): 76 """A device which is added by hand cannot report how it is reached, so it has 77 to be told""" 78 user = create_test_admin_user() 79 self.client.force_login(user) 80 res = self.client.post( 81 reverse("authentik_api:endpoint_device-list"), 82 data={"name": generate_id()}, 83 ) 84 self.assertEqual(res.status_code, 400) 85 self.assertEqual(set(res.json().keys()), {"rac"}) 86 87 def test_list_without_connection(self): 88 """A device which is enrolled by a connector has no connection settings""" 89 user = create_test_admin_user() 90 self.client.force_login(user) 91 device = Device.objects.create(identifier=generate_id(), name=generate_id()) 92 res = self.client.get( 93 reverse("authentik_api:endpoint_device-detail", kwargs={"pk": device.device_uuid}) 94 ) 95 self.assertEqual(res.status_code, 200) 96 self.assertIsNone(res.json()["rac"]) 97 98 def test_update(self): 99 """A device which is enrolled reports how it is reached, so updating one does 100 not require it""" 101 user = create_test_admin_user() 102 self.client.force_login(user) 103 device = Device.objects.create(identifier=generate_id(), name=generate_id()) 104 name = generate_id() 105 res = self.client.patch( 106 reverse("authentik_api:endpoint_device-detail", kwargs={"pk": device.device_uuid}), 107 data={"name": name}, 108 content_type="application/json", 109 ) 110 self.assertEqual(res.status_code, 200) 111 device.refresh_from_db() 112 self.assertEqual(device.name, name) 113 114 def test_update_connection(self): 115 """How a device is reached can be changed after it was added""" 116 user = create_test_admin_user() 117 self.client.force_login(user) 118 device = Device.objects.create(identifier=generate_id(), name=generate_id()) 119 RACConnectionOverride.objects.create( 120 device=device, host="host.example.com", protocol=Protocols.SSH 121 ) 122 res = self.client.patch( 123 reverse("authentik_api:endpoint_device-detail", kwargs={"pk": device.device_uuid}), 124 data={"rac": {"host": "other.example.com", "protocol": Protocols.RDP}}, 125 content_type="application/json", 126 ) 127 self.assertEqual(res.status_code, 200) 128 device.rac_override.refresh_from_db() 129 self.assertEqual(device.rac_override.host, "other.example.com") 130 self.assertEqual(device.rac_override.protocol, Protocols.RDP)
14class TestDevicesAPI(APITestCase): 15 def create_device_with_snapshot(self, t: datetime): 16 device = Device.objects.create( 17 identifier=generate_id(), 18 name=generate_id(), 19 ) 20 connector = Connector.objects.create(name=generate_id()) 21 connection = DeviceConnection.objects.create( 22 device=device, 23 connector=connector, 24 ) 25 snap = connection.create_snapshot({"vendor": {"goauthentik.io/testing": {"foo": "bar"}}}) 26 snap.created = t 27 snap.save() 28 29 def test_summary(self): 30 user = create_test_admin_user() 31 self.client.force_login(user) 32 self.create_device_with_snapshot(now()) 33 self.create_device_with_snapshot(now()) 34 res = self.client.get(reverse("authentik_api:endpoint_device-summary")) 35 self.assertEqual(res.status_code, 200) 36 self.assertJSONEqual( 37 res.content, {"outdated_agent_count": 0, "total_count": 2, "unreachable_count": 0} 38 ) 39 40 def test_summary_unreachable(self): 41 user = create_test_admin_user() 42 self.client.force_login(user) 43 self.create_device_with_snapshot(now()) 44 self.create_device_with_snapshot(now()) 45 self.create_device_with_snapshot(now() - timedelta(hours=26)) 46 self.create_device_with_snapshot(now() - timedelta(hours=26)) 47 self.create_device_with_snapshot(now() - timedelta(hours=26)) 48 res = self.client.get(reverse("authentik_api:endpoint_device-summary")) 49 self.assertEqual(res.status_code, 200) 50 self.assertJSONEqual( 51 res.content, {"outdated_agent_count": 0, "total_count": 5, "unreachable_count": 3} 52 ) 53 54 def test_create(self): 55 """Devices can be created by hand, for machines which are not enrolled 56 through a connector""" 57 user = create_test_admin_user() 58 self.client.force_login(user) 59 name = generate_id() 60 res = self.client.post( 61 reverse("authentik_api:endpoint_device-list"), 62 data={ 63 "name": name, 64 "rac": {"host": "host.example.com", "protocol": Protocols.SSH}, 65 }, 66 content_type="application/json", 67 ) 68 self.assertEqual(res.status_code, 201) 69 device = Device.objects.get(name=name) 70 self.assertTrue(device.identifier.startswith("manual://")) 71 self.assertFalse(device.expiring) 72 # A device which is added by hand has to say how it is reached 73 self.assertEqual(device.rac_override.host, "host.example.com") 74 self.assertEqual(device.rac_override.protocol, Protocols.SSH) 75 76 def test_create_without_connection(self): 77 """A device which is added by hand cannot report how it is reached, so it has 78 to be told""" 79 user = create_test_admin_user() 80 self.client.force_login(user) 81 res = self.client.post( 82 reverse("authentik_api:endpoint_device-list"), 83 data={"name": generate_id()}, 84 ) 85 self.assertEqual(res.status_code, 400) 86 self.assertEqual(set(res.json().keys()), {"rac"}) 87 88 def test_list_without_connection(self): 89 """A device which is enrolled by a connector has no connection settings""" 90 user = create_test_admin_user() 91 self.client.force_login(user) 92 device = Device.objects.create(identifier=generate_id(), name=generate_id()) 93 res = self.client.get( 94 reverse("authentik_api:endpoint_device-detail", kwargs={"pk": device.device_uuid}) 95 ) 96 self.assertEqual(res.status_code, 200) 97 self.assertIsNone(res.json()["rac"]) 98 99 def test_update(self): 100 """A device which is enrolled reports how it is reached, so updating one does 101 not require it""" 102 user = create_test_admin_user() 103 self.client.force_login(user) 104 device = Device.objects.create(identifier=generate_id(), name=generate_id()) 105 name = generate_id() 106 res = self.client.patch( 107 reverse("authentik_api:endpoint_device-detail", kwargs={"pk": device.device_uuid}), 108 data={"name": name}, 109 content_type="application/json", 110 ) 111 self.assertEqual(res.status_code, 200) 112 device.refresh_from_db() 113 self.assertEqual(device.name, name) 114 115 def test_update_connection(self): 116 """How a device is reached can be changed after it was added""" 117 user = create_test_admin_user() 118 self.client.force_login(user) 119 device = Device.objects.create(identifier=generate_id(), name=generate_id()) 120 RACConnectionOverride.objects.create( 121 device=device, host="host.example.com", protocol=Protocols.SSH 122 ) 123 res = self.client.patch( 124 reverse("authentik_api:endpoint_device-detail", kwargs={"pk": device.device_uuid}), 125 data={"rac": {"host": "other.example.com", "protocol": Protocols.RDP}}, 126 content_type="application/json", 127 ) 128 self.assertEqual(res.status_code, 200) 129 device.rac_override.refresh_from_db() 130 self.assertEqual(device.rac_override.host, "other.example.com") 131 self.assertEqual(device.rac_override.protocol, Protocols.RDP)
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.
15 def create_device_with_snapshot(self, t: datetime): 16 device = Device.objects.create( 17 identifier=generate_id(), 18 name=generate_id(), 19 ) 20 connector = Connector.objects.create(name=generate_id()) 21 connection = DeviceConnection.objects.create( 22 device=device, 23 connector=connector, 24 ) 25 snap = connection.create_snapshot({"vendor": {"goauthentik.io/testing": {"foo": "bar"}}}) 26 snap.created = t 27 snap.save()
29 def test_summary(self): 30 user = create_test_admin_user() 31 self.client.force_login(user) 32 self.create_device_with_snapshot(now()) 33 self.create_device_with_snapshot(now()) 34 res = self.client.get(reverse("authentik_api:endpoint_device-summary")) 35 self.assertEqual(res.status_code, 200) 36 self.assertJSONEqual( 37 res.content, {"outdated_agent_count": 0, "total_count": 2, "unreachable_count": 0} 38 )
40 def test_summary_unreachable(self): 41 user = create_test_admin_user() 42 self.client.force_login(user) 43 self.create_device_with_snapshot(now()) 44 self.create_device_with_snapshot(now()) 45 self.create_device_with_snapshot(now() - timedelta(hours=26)) 46 self.create_device_with_snapshot(now() - timedelta(hours=26)) 47 self.create_device_with_snapshot(now() - timedelta(hours=26)) 48 res = self.client.get(reverse("authentik_api:endpoint_device-summary")) 49 self.assertEqual(res.status_code, 200) 50 self.assertJSONEqual( 51 res.content, {"outdated_agent_count": 0, "total_count": 5, "unreachable_count": 3} 52 )
54 def test_create(self): 55 """Devices can be created by hand, for machines which are not enrolled 56 through a connector""" 57 user = create_test_admin_user() 58 self.client.force_login(user) 59 name = generate_id() 60 res = self.client.post( 61 reverse("authentik_api:endpoint_device-list"), 62 data={ 63 "name": name, 64 "rac": {"host": "host.example.com", "protocol": Protocols.SSH}, 65 }, 66 content_type="application/json", 67 ) 68 self.assertEqual(res.status_code, 201) 69 device = Device.objects.get(name=name) 70 self.assertTrue(device.identifier.startswith("manual://")) 71 self.assertFalse(device.expiring) 72 # A device which is added by hand has to say how it is reached 73 self.assertEqual(device.rac_override.host, "host.example.com") 74 self.assertEqual(device.rac_override.protocol, Protocols.SSH)
Devices can be created by hand, for machines which are not enrolled through a connector
76 def test_create_without_connection(self): 77 """A device which is added by hand cannot report how it is reached, so it has 78 to be told""" 79 user = create_test_admin_user() 80 self.client.force_login(user) 81 res = self.client.post( 82 reverse("authentik_api:endpoint_device-list"), 83 data={"name": generate_id()}, 84 ) 85 self.assertEqual(res.status_code, 400) 86 self.assertEqual(set(res.json().keys()), {"rac"})
A device which is added by hand cannot report how it is reached, so it has to be told
88 def test_list_without_connection(self): 89 """A device which is enrolled by a connector has no connection settings""" 90 user = create_test_admin_user() 91 self.client.force_login(user) 92 device = Device.objects.create(identifier=generate_id(), name=generate_id()) 93 res = self.client.get( 94 reverse("authentik_api:endpoint_device-detail", kwargs={"pk": device.device_uuid}) 95 ) 96 self.assertEqual(res.status_code, 200) 97 self.assertIsNone(res.json()["rac"])
A device which is enrolled by a connector has no connection settings
99 def test_update(self): 100 """A device which is enrolled reports how it is reached, so updating one does 101 not require it""" 102 user = create_test_admin_user() 103 self.client.force_login(user) 104 device = Device.objects.create(identifier=generate_id(), name=generate_id()) 105 name = generate_id() 106 res = self.client.patch( 107 reverse("authentik_api:endpoint_device-detail", kwargs={"pk": device.device_uuid}), 108 data={"name": name}, 109 content_type="application/json", 110 ) 111 self.assertEqual(res.status_code, 200) 112 device.refresh_from_db() 113 self.assertEqual(device.name, name)
A device which is enrolled reports how it is reached, so updating one does not require it
115 def test_update_connection(self): 116 """How a device is reached can be changed after it was added""" 117 user = create_test_admin_user() 118 self.client.force_login(user) 119 device = Device.objects.create(identifier=generate_id(), name=generate_id()) 120 RACConnectionOverride.objects.create( 121 device=device, host="host.example.com", protocol=Protocols.SSH 122 ) 123 res = self.client.patch( 124 reverse("authentik_api:endpoint_device-detail", kwargs={"pk": device.device_uuid}), 125 data={"rac": {"host": "other.example.com", "protocol": Protocols.RDP}}, 126 content_type="application/json", 127 ) 128 self.assertEqual(res.status_code, 200) 129 device.rac_override.refresh_from_db() 130 self.assertEqual(device.rac_override.host, "other.example.com") 131 self.assertEqual(device.rac_override.protocol, Protocols.RDP)
How a device is reached can be changed after it was added