authentik.enterprise.endpoints.connectors.agent.tests.test_apple
1from json import loads 2from urllib.parse import quote 3 4from django.test import TestCase 5from django.urls import reverse 6 7from authentik.blueprints.tests import reconcile_app 8from authentik.core.tests.utils import create_test_user 9from authentik.endpoints.connectors.agent.models import ( 10 AgentConnector, 11 AgentDeviceConnection, 12 AppleNonce, 13 DeviceToken, 14 EnrollmentToken, 15) 16from authentik.endpoints.models import Device 17from authentik.lib.generators import generate_id 18 19 20class TestAppleViews(TestCase): 21 22 def setUp(self): 23 self.connector = AgentConnector.objects.create(name=generate_id()) 24 self.token = EnrollmentToken.objects.create(name=generate_id(), connector=self.connector) 25 self.device = Device.objects.create( 26 name=generate_id(), 27 identifier=generate_id(), 28 ) 29 self.connection = AgentDeviceConnection.objects.create( 30 device=self.device, 31 connector=self.connector, 32 ) 33 self.user = create_test_user() 34 35 def test_apple_site_association(self): 36 res = self.client.get(reverse("authentik_enterprise_endpoints_connectors_agent_root:asa")) 37 self.assertEqual(res.status_code, 200) 38 39 @reconcile_app("authentik_crypto") 40 def test_apple_jwks(self): 41 res = self.client.get(reverse("authentik_enterprise_endpoints_connectors_agent:psso-jwks")) 42 self.assertEqual(res.status_code, 200) 43 44 def test_apple_nonce(self): 45 device_token = DeviceToken.objects.create(device=self.connection) 46 res = self.client.post( 47 reverse("authentik_enterprise_endpoints_connectors_agent:psso-nonce"), 48 data={"x-ak-device-token": quote(device_token.key)}, 49 ) 50 self.assertEqual(res.status_code, 200) 51 nonce = loads(res.content.decode()).get("Nonce") 52 self.assertIsNotNone(nonce) 53 db_nonce = AppleNonce.objects.filter(nonce=nonce).first() 54 self.assertIsNotNone(db_nonce) 55 self.assertFalse(db_nonce.is_expired)
class
TestAppleViews(django.test.testcases.TestCase):
21class TestAppleViews(TestCase): 22 23 def setUp(self): 24 self.connector = AgentConnector.objects.create(name=generate_id()) 25 self.token = EnrollmentToken.objects.create(name=generate_id(), connector=self.connector) 26 self.device = Device.objects.create( 27 name=generate_id(), 28 identifier=generate_id(), 29 ) 30 self.connection = AgentDeviceConnection.objects.create( 31 device=self.device, 32 connector=self.connector, 33 ) 34 self.user = create_test_user() 35 36 def test_apple_site_association(self): 37 res = self.client.get(reverse("authentik_enterprise_endpoints_connectors_agent_root:asa")) 38 self.assertEqual(res.status_code, 200) 39 40 @reconcile_app("authentik_crypto") 41 def test_apple_jwks(self): 42 res = self.client.get(reverse("authentik_enterprise_endpoints_connectors_agent:psso-jwks")) 43 self.assertEqual(res.status_code, 200) 44 45 def test_apple_nonce(self): 46 device_token = DeviceToken.objects.create(device=self.connection) 47 res = self.client.post( 48 reverse("authentik_enterprise_endpoints_connectors_agent:psso-nonce"), 49 data={"x-ak-device-token": quote(device_token.key)}, 50 ) 51 self.assertEqual(res.status_code, 200) 52 nonce = loads(res.content.decode()).get("Nonce") 53 self.assertIsNotNone(nonce) 54 db_nonce = AppleNonce.objects.filter(nonce=nonce).first() 55 self.assertIsNotNone(db_nonce) 56 self.assertFalse(db_nonce.is_expired)
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
setUp(self):
23 def setUp(self): 24 self.connector = AgentConnector.objects.create(name=generate_id()) 25 self.token = EnrollmentToken.objects.create(name=generate_id(), connector=self.connector) 26 self.device = Device.objects.create( 27 name=generate_id(), 28 identifier=generate_id(), 29 ) 30 self.connection = AgentDeviceConnection.objects.create( 31 device=self.device, 32 connector=self.connector, 33 ) 34 self.user = create_test_user()
Hook method for setting up the test fixture before exercising it.
def
test_apple_nonce(self):
45 def test_apple_nonce(self): 46 device_token = DeviceToken.objects.create(device=self.connection) 47 res = self.client.post( 48 reverse("authentik_enterprise_endpoints_connectors_agent:psso-nonce"), 49 data={"x-ak-device-token": quote(device_token.key)}, 50 ) 51 self.assertEqual(res.status_code, 200) 52 nonce = loads(res.content.decode()).get("Nonce") 53 self.assertIsNotNone(nonce) 54 db_nonce = AppleNonce.objects.filter(nonce=nonce).first() 55 self.assertIsNotNone(db_nonce) 56 self.assertFalse(db_nonce.is_expired)