authentik.stages.authenticator_static.tests
Test Static API
1"""Test Static API""" 2 3from django.urls import reverse 4from rest_framework.test import APITestCase 5 6from authentik.core.models import User 7from authentik.core.tests.utils import create_test_admin_user 8from authentik.lib.generators import generate_id 9from authentik.stages.authenticator.tests import TestCase, ThrottlingTestMixin 10from authentik.stages.authenticator_static.models import StaticDevice 11 12 13class AuthenticatorStaticStageTests(APITestCase): 14 """Test Static API""" 15 16 def test_api_delete(self): 17 """Test api delete""" 18 user = User.objects.create(username="foo") 19 self.client.force_login(user) 20 dev = StaticDevice.objects.create(user=user) 21 response = self.client.delete( 22 reverse("authentik_api:staticdevice-detail", kwargs={"pk": dev.pk}) 23 ) 24 self.assertEqual(response.status_code, 204) 25 26 27class DeviceTest(TestCase): 28 """A few generic tests to get us started.""" 29 30 def setUp(self): 31 self.user = create_test_admin_user("alice") 32 33 def test_str(self): 34 """Test __str__ of model""" 35 device = StaticDevice.objects.create(user=self.user, name="Device") 36 37 str(device) 38 39 def test_str_unpopulated(self): 40 """Test __str__ of model""" 41 device = StaticDevice() 42 43 str(device) 44 45 def test_verify_token_separators(self): 46 """Test tokens entered with the displayed hyphens, or with spaces, are accepted""" 47 device = StaticDevice.objects.create(user=self.user, name="Device") 48 device.token_set.create(token="abcdEFGH1234") 49 device.token_set.create(token="wxyzWXYZ5678") 50 51 self.assertTrue(device.verify_token("abcd-EFGH-1234")) 52 self.assertTrue(device.verify_token(" wxyz WXYZ 5678 ")) 53 self.assertFalse(device.token_set.exists()) 54 55 def test_verify_token_separators_wrong_token(self): 56 """Test separators don't make an incorrect token valid""" 57 device = StaticDevice.objects.create(user=self.user, name="Device") 58 device.token_set.create(token="abcdEFGH1234") 59 60 self.assertFalse(device.verify_token("abcd-efgh-1234")) 61 self.assertFalse(device.verify_token("-")) 62 self.assertTrue(device.token_set.exists()) 63 64 65class ThrottlingTestCase(ThrottlingTestMixin, TestCase): 66 """Test static device throttling""" 67 68 def setUp(self): 69 user = create_test_admin_user("alice") 70 self.device = user.staticdevice_set.create() 71 self.device.token_set.create(token=generate_id(length=16)) 72 self.device.token_set.create(token=generate_id(length=16)) 73 self.device.token_set.create(token=generate_id(length=16)) 74 75 def valid_token(self): 76 return self.device.token_set.first().token 77 78 def invalid_token(self): 79 return "bogus"
class
AuthenticatorStaticStageTests(rest_framework.test.APITestCase):
14class AuthenticatorStaticStageTests(APITestCase): 15 """Test Static API""" 16 17 def test_api_delete(self): 18 """Test api delete""" 19 user = User.objects.create(username="foo") 20 self.client.force_login(user) 21 dev = StaticDevice.objects.create(user=user) 22 response = self.client.delete( 23 reverse("authentik_api:staticdevice-detail", kwargs={"pk": dev.pk}) 24 ) 25 self.assertEqual(response.status_code, 204)
Test Static API
def
test_api_delete(self):
17 def test_api_delete(self): 18 """Test api delete""" 19 user = User.objects.create(username="foo") 20 self.client.force_login(user) 21 dev = StaticDevice.objects.create(user=user) 22 response = self.client.delete( 23 reverse("authentik_api:staticdevice-detail", kwargs={"pk": dev.pk}) 24 ) 25 self.assertEqual(response.status_code, 204)
Test api delete
class
DeviceTest(django.test.testcases.TestCase):
28class DeviceTest(TestCase): 29 """A few generic tests to get us started.""" 30 31 def setUp(self): 32 self.user = create_test_admin_user("alice") 33 34 def test_str(self): 35 """Test __str__ of model""" 36 device = StaticDevice.objects.create(user=self.user, name="Device") 37 38 str(device) 39 40 def test_str_unpopulated(self): 41 """Test __str__ of model""" 42 device = StaticDevice() 43 44 str(device) 45 46 def test_verify_token_separators(self): 47 """Test tokens entered with the displayed hyphens, or with spaces, are accepted""" 48 device = StaticDevice.objects.create(user=self.user, name="Device") 49 device.token_set.create(token="abcdEFGH1234") 50 device.token_set.create(token="wxyzWXYZ5678") 51 52 self.assertTrue(device.verify_token("abcd-EFGH-1234")) 53 self.assertTrue(device.verify_token(" wxyz WXYZ 5678 ")) 54 self.assertFalse(device.token_set.exists()) 55 56 def test_verify_token_separators_wrong_token(self): 57 """Test separators don't make an incorrect token valid""" 58 device = StaticDevice.objects.create(user=self.user, name="Device") 59 device.token_set.create(token="abcdEFGH1234") 60 61 self.assertFalse(device.verify_token("abcd-efgh-1234")) 62 self.assertFalse(device.verify_token("-")) 63 self.assertTrue(device.token_set.exists())
A few generic tests to get us started.
def
test_str(self):
34 def test_str(self): 35 """Test __str__ of model""" 36 device = StaticDevice.objects.create(user=self.user, name="Device") 37 38 str(device)
Test __str__ of model
def
test_str_unpopulated(self):
40 def test_str_unpopulated(self): 41 """Test __str__ of model""" 42 device = StaticDevice() 43 44 str(device)
Test __str__ of model
def
test_verify_token_separators(self):
46 def test_verify_token_separators(self): 47 """Test tokens entered with the displayed hyphens, or with spaces, are accepted""" 48 device = StaticDevice.objects.create(user=self.user, name="Device") 49 device.token_set.create(token="abcdEFGH1234") 50 device.token_set.create(token="wxyzWXYZ5678") 51 52 self.assertTrue(device.verify_token("abcd-EFGH-1234")) 53 self.assertTrue(device.verify_token(" wxyz WXYZ 5678 ")) 54 self.assertFalse(device.token_set.exists())
Test tokens entered with the displayed hyphens, or with spaces, are accepted
def
test_verify_token_separators_wrong_token(self):
56 def test_verify_token_separators_wrong_token(self): 57 """Test separators don't make an incorrect token valid""" 58 device = StaticDevice.objects.create(user=self.user, name="Device") 59 device.token_set.create(token="abcdEFGH1234") 60 61 self.assertFalse(device.verify_token("abcd-efgh-1234")) 62 self.assertFalse(device.verify_token("-")) 63 self.assertTrue(device.token_set.exists())
Test separators don't make an incorrect token valid
class
ThrottlingTestCase(authentik.stages.authenticator.tests.ThrottlingTestMixin, django.test.testcases.TestCase):
66class ThrottlingTestCase(ThrottlingTestMixin, TestCase): 67 """Test static device throttling""" 68 69 def setUp(self): 70 user = create_test_admin_user("alice") 71 self.device = user.staticdevice_set.create() 72 self.device.token_set.create(token=generate_id(length=16)) 73 self.device.token_set.create(token=generate_id(length=16)) 74 self.device.token_set.create(token=generate_id(length=16)) 75 76 def valid_token(self): 77 return self.device.token_set.first().token 78 79 def invalid_token(self): 80 return "bogus"
Test static device throttling
def
setUp(self):
69 def setUp(self): 70 user = create_test_admin_user("alice") 71 self.device = user.staticdevice_set.create() 72 self.device.token_set.create(token=generate_id(length=16)) 73 self.device.token_set.create(token=generate_id(length=16)) 74 self.device.token_set.create(token=generate_id(length=16))
Hook method for setting up the test fixture before exercising it.