authentik.stages.authenticator_static.tests

Test Static API

 1"""Test Static API"""
 2
 3from django.test.utils import override_settings
 4from django.urls import reverse
 5from rest_framework.test import APITestCase
 6
 7from authentik.core.models import User
 8from authentik.core.tests.utils import create_test_admin_user
 9from authentik.lib.generators import generate_id
10from authentik.stages.authenticator.tests import TestCase, ThrottlingTestMixin
11from authentik.stages.authenticator_static.models import StaticDevice
12
13
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)
26
27
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
47@override_settings(
48    OTP_STATIC_THROTTLE_FACTOR=1,
49)
50class ThrottlingTestCase(ThrottlingTestMixin, TestCase):
51    """Test static device throttling"""
52
53    def setUp(self):
54        user = create_test_admin_user("alice")
55        self.device = user.staticdevice_set.create()
56        self.device.token_set.create(token=generate_id(length=16))
57        self.device.token_set.create(token=generate_id(length=16))
58        self.device.token_set.create(token=generate_id(length=16))
59
60    def valid_token(self):
61        return self.device.token_set.first().token
62
63    def invalid_token(self):
64        return "bogus"
class AuthenticatorStaticStageTests(rest_framework.test.APITestCase):
15class AuthenticatorStaticStageTests(APITestCase):
16    """Test Static API"""
17
18    def test_api_delete(self):
19        """Test api delete"""
20        user = User.objects.create(username="foo")
21        self.client.force_login(user)
22        dev = StaticDevice.objects.create(user=user)
23        response = self.client.delete(
24            reverse("authentik_api:staticdevice-detail", kwargs={"pk": dev.pk})
25        )
26        self.assertEqual(response.status_code, 204)

Test Static API

def test_api_delete(self):
18    def test_api_delete(self):
19        """Test api delete"""
20        user = User.objects.create(username="foo")
21        self.client.force_login(user)
22        dev = StaticDevice.objects.create(user=user)
23        response = self.client.delete(
24            reverse("authentik_api:staticdevice-detail", kwargs={"pk": dev.pk})
25        )
26        self.assertEqual(response.status_code, 204)

Test api delete

class DeviceTest(django.test.testcases.TestCase):
29class DeviceTest(TestCase):
30    """A few generic tests to get us started."""
31
32    def setUp(self):
33        self.user = create_test_admin_user("alice")
34
35    def test_str(self):
36        """Test __str__ of model"""
37        device = StaticDevice.objects.create(user=self.user, name="Device")
38
39        str(device)
40
41    def test_str_unpopulated(self):
42        """Test __str__ of model"""
43        device = StaticDevice()
44
45        str(device)

A few generic tests to get us started.

def setUp(self):
32    def setUp(self):
33        self.user = create_test_admin_user("alice")

Hook method for setting up the test fixture before exercising it.

def test_str(self):
35    def test_str(self):
36        """Test __str__ of model"""
37        device = StaticDevice.objects.create(user=self.user, name="Device")
38
39        str(device)

Test __str__ of model

def test_str_unpopulated(self):
41    def test_str_unpopulated(self):
42        """Test __str__ of model"""
43        device = StaticDevice()
44
45        str(device)

Test __str__ of model

@override_settings(OTP_STATIC_THROTTLE_FACTOR=1)
class ThrottlingTestCase(authentik.stages.authenticator.tests.ThrottlingTestMixin, django.test.testcases.TestCase):
48@override_settings(
49    OTP_STATIC_THROTTLE_FACTOR=1,
50)
51class ThrottlingTestCase(ThrottlingTestMixin, TestCase):
52    """Test static device throttling"""
53
54    def setUp(self):
55        user = create_test_admin_user("alice")
56        self.device = user.staticdevice_set.create()
57        self.device.token_set.create(token=generate_id(length=16))
58        self.device.token_set.create(token=generate_id(length=16))
59        self.device.token_set.create(token=generate_id(length=16))
60
61    def valid_token(self):
62        return self.device.token_set.first().token
63
64    def invalid_token(self):
65        return "bogus"

Test static device throttling

def setUp(self):
54    def setUp(self):
55        user = create_test_admin_user("alice")
56        self.device = user.staticdevice_set.create()
57        self.device.token_set.create(token=generate_id(length=16))
58        self.device.token_set.create(token=generate_id(length=16))
59        self.device.token_set.create(token=generate_id(length=16))

Hook method for setting up the test fixture before exercising it.

def valid_token(self):
61    def valid_token(self):
62        return self.device.token_set.first().token

Returns a valid token to pass to our device under test.

def invalid_token(self):
64    def invalid_token(self):
65        return "bogus"

Returns an invalid token to pass to our device under test.