authentik.core.tests.test_object_attributes

Test object attributes API

  1"""Test object attributes API"""
  2
  3from django.urls import reverse
  4from rest_framework.exceptions import ValidationError
  5from rest_framework.test import APITestCase
  6
  7from authentik.core.api.object_attributes import ContentType
  8from authentik.core.models import ObjectAttribute, User
  9from authentik.core.tests.utils import create_test_admin_user, create_test_user
 10from authentik.lib.generators import generate_id
 11
 12
 13class TestObjectAttributes(APITestCase):
 14    """Test object attributes API"""
 15
 16    def setUp(self) -> None:
 17        super().setUp()
 18        self.user = create_test_admin_user()
 19        self.client.force_login(self.user)
 20
 21    def test_create(self):
 22        res = self.client.post(
 23            reverse("authentik_api:objectattribute-list"),
 24            data={
 25                "object_type": "authentik_core.user",
 26                "enabled": False,
 27                "key": "employeeNumber",
 28                "label": "Employee Number",
 29                "type": "text",
 30                "group": "Employee",
 31                "is_unique": False,
 32                "is_required": False,
 33            },
 34        )
 35        self.assertEqual(res.status_code, 201)
 36        attr = ObjectAttribute.objects.filter(key="employeeNumber").first()
 37        self.assertIsNotNone(attr)
 38
 39    def test_create_invalid(self):
 40        res = self.client.post(
 41            reverse("authentik_api:objectattribute-list"),
 42            data={
 43                "object_type": "authentik_core.objectattribute",
 44                "enabled": False,
 45                "key": "employeeNumber",
 46                "label": "Employee Number",
 47                "type": "text",
 48                "group": "Employee",
 49                "is_unique": False,
 50                "is_required": False,
 51            },
 52        )
 53        self.assertEqual(res.status_code, 400)
 54        self.assertJSONEqual(res.content, {"object_type": ["Invalid object type"]})
 55
 56    def test_update(self):
 57        attr = ObjectAttribute.objects.create(
 58            object_type=ContentType.objects.get_for_model(User),
 59            label="foo",
 60            key=generate_id(),
 61            type=ObjectAttribute.AttributeType.TEXT,
 62        )
 63        res = self.client.put(
 64            reverse("authentik_api:objectattribute-detail", kwargs={"pk": attr.pk}),
 65            data={
 66                "object_type": "authentik_core.user",
 67                "enabled": False,
 68                "key": attr.key,
 69                "label": "Employee Number",
 70                "type": "text",
 71                "group": "Employee",
 72                "is_unique": False,
 73                "is_required": False,
 74            },
 75        )
 76        self.assertEqual(res.status_code, 200)
 77        attr.refresh_from_db()
 78        self.assertEqual(attr.label, "Employee Number")
 79
 80    def test_user_attrib_validation_required(self):
 81        attr = ObjectAttribute.objects.create(
 82            object_type=ContentType.objects.get_for_model(User),
 83            label="foo",
 84            key=generate_id(),
 85            type=ObjectAttribute.AttributeType.TEXT,
 86            is_required=True,
 87        )
 88        res = self.client.patch(
 89            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
 90            data={
 91                "attributes": {},
 92            },
 93        )
 94        self.assertEqual(res.status_code, 400)
 95        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["This field is required"]})
 96
 97    def test_user_attrib_validation_no_required_blank(self):
 98        attr = ObjectAttribute.objects.create(
 99            object_type=ContentType.objects.get_for_model(User),
100            label="foo",
101            key=generate_id(),
102            type=ObjectAttribute.AttributeType.TEXT,
103            is_required=False,
104        )
105        res = self.client.post(
106            reverse("authentik_api:user-list"),
107            data={
108                "username": generate_id(),
109                "name": "test",
110                "attributes": {
111                    attr.key: "",
112                },
113            },
114        )
115        self.assertEqual(res.status_code, 201, res.content)
116
117    def test_user_attrib_validation_unique(self):
118        attr = ObjectAttribute.objects.create(
119            object_type=ContentType.objects.get_for_model(User),
120            label="foo",
121            key=generate_id(),
122            type=ObjectAttribute.AttributeType.TEXT,
123            is_unique=True,
124        )
125        other_user = create_test_user()
126        other_user.attributes[attr.key] = "foo"
127        other_user.save()
128        res = self.client.patch(
129            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
130            data={
131                "attributes": {attr.key: "foo"},
132            },
133        )
134        self.assertEqual(res.status_code, 400)
135        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["Value is not unique."]})
136
137    def test_user_attrib_validation_unique_same_user(self):
138        attr = ObjectAttribute.objects.create(
139            object_type=ContentType.objects.get_for_model(User),
140            label="foo",
141            key=generate_id(),
142            type=ObjectAttribute.AttributeType.TEXT,
143            is_unique=True,
144        )
145        self.user.attributes[attr.key] = "foo"
146        self.user.save()
147        res = self.client.patch(
148            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
149            data={
150                "name": "Renamed",
151                "attributes": {attr.key: "foo"},
152            },
153        )
154        self.assertEqual(res.status_code, 200, res.content)
155        self.user.refresh_from_db()
156        self.assertEqual(self.user.name, "Renamed")
157        self.assertEqual(self.user.attributes[attr.key], "foo")
158
159        other_user = create_test_user()
160        res = self.client.patch(
161            reverse("authentik_api:user-detail", kwargs={"pk": other_user.pk}),
162            data={
163                "attributes": {attr.key: "foo"},
164            },
165        )
166        self.assertEqual(res.status_code, 400)
167        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["Value is not unique."]})
168
169    def test_user_attrib_validation_regex(self):
170        attr = ObjectAttribute.objects.create(
171            object_type=ContentType.objects.get_for_model(User),
172            label="foo",
173            key=generate_id(),
174            type=ObjectAttribute.AttributeType.TEXT,
175            regex="bar",
176        )
177        res = self.client.patch(
178            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
179            data={
180                "attributes": {attr.key: "foo"},
181            },
182        )
183        self.assertEqual(res.status_code, 400)
184        self.assertJSONEqual(
185            res.content, {f"attributes_{attr.key}": ["Value does not match configured pattern."]}
186        )
187
188    def test_user_attrib_validation_array(self):
189        attr = ObjectAttribute.objects.create(
190            object_type=ContentType.objects.get_for_model(User),
191            label="foo",
192            key=generate_id(),
193            type=ObjectAttribute.AttributeType.TEXT,
194            is_array=True,
195        )
196        res = self.client.patch(
197            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
198            data={
199                "attributes": {attr.key: "foo"},
200            },
201        )
202        self.assertEqual(res.status_code, 400)
203        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["Value must be an array."]})
204
205        res = self.client.patch(
206            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
207            data={
208                "attributes": {attr.key: ["foo"]},
209            },
210        )
211        self.assertEqual(res.status_code, 200)
212
213    def test_user_attrib_validation_array_regex(self):
214        attr = ObjectAttribute.objects.create(
215            object_type=ContentType.objects.get_for_model(User),
216            label="foo",
217            key=generate_id(),
218            type=ObjectAttribute.AttributeType.TEXT,
219            is_array=True,
220            regex="bar",
221        )
222        res = self.client.patch(
223            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
224            data={
225                "attributes": {attr.key: ["foo"]},
226            },
227        )
228        self.assertEqual(res.status_code, 400)
229        self.assertJSONEqual(
230            res.content, {f"attributes_{attr.key}": ["Value does not match configured pattern."]}
231        )
232
233    def test_types_str(self):
234        attr = ObjectAttribute.objects.create(
235            object_type=ContentType.objects.get_for_model(User),
236            label="foo",
237            key=generate_id(),
238            type=ObjectAttribute.AttributeType.TEXT,
239        )
240        attr.run_validation("foo")
241        attr.run_validation(3)
242        with self.assertRaises(ValidationError):
243            attr.run_validation(False)
244
245    def test_types_number(self):
246        attr = ObjectAttribute.objects.create(
247            object_type=ContentType.objects.get_for_model(User),
248            label="foo",
249            key=generate_id(),
250            type=ObjectAttribute.AttributeType.NUMBER,
251        )
252        attr.run_validation(3)
253        with self.assertRaises(ValidationError):
254            attr.run_validation("foo")
255        with self.assertRaises(ValidationError):
256            attr.run_validation(False)
257
258    def test_types_bool(self):
259        attr = ObjectAttribute.objects.create(
260            object_type=ContentType.objects.get_for_model(User),
261            label="foo",
262            key=generate_id(),
263            type=ObjectAttribute.AttributeType.BOOLEAN,
264        )
265        attr.run_validation(False)
266        with self.assertRaises(ValidationError):
267            attr.run_validation("foo")
268        with self.assertRaises(ValidationError):
269            attr.run_validation(3)
class TestObjectAttributes(rest_framework.test.APITestCase):
 14class TestObjectAttributes(APITestCase):
 15    """Test object attributes API"""
 16
 17    def setUp(self) -> None:
 18        super().setUp()
 19        self.user = create_test_admin_user()
 20        self.client.force_login(self.user)
 21
 22    def test_create(self):
 23        res = self.client.post(
 24            reverse("authentik_api:objectattribute-list"),
 25            data={
 26                "object_type": "authentik_core.user",
 27                "enabled": False,
 28                "key": "employeeNumber",
 29                "label": "Employee Number",
 30                "type": "text",
 31                "group": "Employee",
 32                "is_unique": False,
 33                "is_required": False,
 34            },
 35        )
 36        self.assertEqual(res.status_code, 201)
 37        attr = ObjectAttribute.objects.filter(key="employeeNumber").first()
 38        self.assertIsNotNone(attr)
 39
 40    def test_create_invalid(self):
 41        res = self.client.post(
 42            reverse("authentik_api:objectattribute-list"),
 43            data={
 44                "object_type": "authentik_core.objectattribute",
 45                "enabled": False,
 46                "key": "employeeNumber",
 47                "label": "Employee Number",
 48                "type": "text",
 49                "group": "Employee",
 50                "is_unique": False,
 51                "is_required": False,
 52            },
 53        )
 54        self.assertEqual(res.status_code, 400)
 55        self.assertJSONEqual(res.content, {"object_type": ["Invalid object type"]})
 56
 57    def test_update(self):
 58        attr = ObjectAttribute.objects.create(
 59            object_type=ContentType.objects.get_for_model(User),
 60            label="foo",
 61            key=generate_id(),
 62            type=ObjectAttribute.AttributeType.TEXT,
 63        )
 64        res = self.client.put(
 65            reverse("authentik_api:objectattribute-detail", kwargs={"pk": attr.pk}),
 66            data={
 67                "object_type": "authentik_core.user",
 68                "enabled": False,
 69                "key": attr.key,
 70                "label": "Employee Number",
 71                "type": "text",
 72                "group": "Employee",
 73                "is_unique": False,
 74                "is_required": False,
 75            },
 76        )
 77        self.assertEqual(res.status_code, 200)
 78        attr.refresh_from_db()
 79        self.assertEqual(attr.label, "Employee Number")
 80
 81    def test_user_attrib_validation_required(self):
 82        attr = ObjectAttribute.objects.create(
 83            object_type=ContentType.objects.get_for_model(User),
 84            label="foo",
 85            key=generate_id(),
 86            type=ObjectAttribute.AttributeType.TEXT,
 87            is_required=True,
 88        )
 89        res = self.client.patch(
 90            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
 91            data={
 92                "attributes": {},
 93            },
 94        )
 95        self.assertEqual(res.status_code, 400)
 96        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["This field is required"]})
 97
 98    def test_user_attrib_validation_no_required_blank(self):
 99        attr = ObjectAttribute.objects.create(
100            object_type=ContentType.objects.get_for_model(User),
101            label="foo",
102            key=generate_id(),
103            type=ObjectAttribute.AttributeType.TEXT,
104            is_required=False,
105        )
106        res = self.client.post(
107            reverse("authentik_api:user-list"),
108            data={
109                "username": generate_id(),
110                "name": "test",
111                "attributes": {
112                    attr.key: "",
113                },
114            },
115        )
116        self.assertEqual(res.status_code, 201, res.content)
117
118    def test_user_attrib_validation_unique(self):
119        attr = ObjectAttribute.objects.create(
120            object_type=ContentType.objects.get_for_model(User),
121            label="foo",
122            key=generate_id(),
123            type=ObjectAttribute.AttributeType.TEXT,
124            is_unique=True,
125        )
126        other_user = create_test_user()
127        other_user.attributes[attr.key] = "foo"
128        other_user.save()
129        res = self.client.patch(
130            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
131            data={
132                "attributes": {attr.key: "foo"},
133            },
134        )
135        self.assertEqual(res.status_code, 400)
136        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["Value is not unique."]})
137
138    def test_user_attrib_validation_unique_same_user(self):
139        attr = ObjectAttribute.objects.create(
140            object_type=ContentType.objects.get_for_model(User),
141            label="foo",
142            key=generate_id(),
143            type=ObjectAttribute.AttributeType.TEXT,
144            is_unique=True,
145        )
146        self.user.attributes[attr.key] = "foo"
147        self.user.save()
148        res = self.client.patch(
149            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
150            data={
151                "name": "Renamed",
152                "attributes": {attr.key: "foo"},
153            },
154        )
155        self.assertEqual(res.status_code, 200, res.content)
156        self.user.refresh_from_db()
157        self.assertEqual(self.user.name, "Renamed")
158        self.assertEqual(self.user.attributes[attr.key], "foo")
159
160        other_user = create_test_user()
161        res = self.client.patch(
162            reverse("authentik_api:user-detail", kwargs={"pk": other_user.pk}),
163            data={
164                "attributes": {attr.key: "foo"},
165            },
166        )
167        self.assertEqual(res.status_code, 400)
168        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["Value is not unique."]})
169
170    def test_user_attrib_validation_regex(self):
171        attr = ObjectAttribute.objects.create(
172            object_type=ContentType.objects.get_for_model(User),
173            label="foo",
174            key=generate_id(),
175            type=ObjectAttribute.AttributeType.TEXT,
176            regex="bar",
177        )
178        res = self.client.patch(
179            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
180            data={
181                "attributes": {attr.key: "foo"},
182            },
183        )
184        self.assertEqual(res.status_code, 400)
185        self.assertJSONEqual(
186            res.content, {f"attributes_{attr.key}": ["Value does not match configured pattern."]}
187        )
188
189    def test_user_attrib_validation_array(self):
190        attr = ObjectAttribute.objects.create(
191            object_type=ContentType.objects.get_for_model(User),
192            label="foo",
193            key=generate_id(),
194            type=ObjectAttribute.AttributeType.TEXT,
195            is_array=True,
196        )
197        res = self.client.patch(
198            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
199            data={
200                "attributes": {attr.key: "foo"},
201            },
202        )
203        self.assertEqual(res.status_code, 400)
204        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["Value must be an array."]})
205
206        res = self.client.patch(
207            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
208            data={
209                "attributes": {attr.key: ["foo"]},
210            },
211        )
212        self.assertEqual(res.status_code, 200)
213
214    def test_user_attrib_validation_array_regex(self):
215        attr = ObjectAttribute.objects.create(
216            object_type=ContentType.objects.get_for_model(User),
217            label="foo",
218            key=generate_id(),
219            type=ObjectAttribute.AttributeType.TEXT,
220            is_array=True,
221            regex="bar",
222        )
223        res = self.client.patch(
224            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
225            data={
226                "attributes": {attr.key: ["foo"]},
227            },
228        )
229        self.assertEqual(res.status_code, 400)
230        self.assertJSONEqual(
231            res.content, {f"attributes_{attr.key}": ["Value does not match configured pattern."]}
232        )
233
234    def test_types_str(self):
235        attr = ObjectAttribute.objects.create(
236            object_type=ContentType.objects.get_for_model(User),
237            label="foo",
238            key=generate_id(),
239            type=ObjectAttribute.AttributeType.TEXT,
240        )
241        attr.run_validation("foo")
242        attr.run_validation(3)
243        with self.assertRaises(ValidationError):
244            attr.run_validation(False)
245
246    def test_types_number(self):
247        attr = ObjectAttribute.objects.create(
248            object_type=ContentType.objects.get_for_model(User),
249            label="foo",
250            key=generate_id(),
251            type=ObjectAttribute.AttributeType.NUMBER,
252        )
253        attr.run_validation(3)
254        with self.assertRaises(ValidationError):
255            attr.run_validation("foo")
256        with self.assertRaises(ValidationError):
257            attr.run_validation(False)
258
259    def test_types_bool(self):
260        attr = ObjectAttribute.objects.create(
261            object_type=ContentType.objects.get_for_model(User),
262            label="foo",
263            key=generate_id(),
264            type=ObjectAttribute.AttributeType.BOOLEAN,
265        )
266        attr.run_validation(False)
267        with self.assertRaises(ValidationError):
268            attr.run_validation("foo")
269        with self.assertRaises(ValidationError):
270            attr.run_validation(3)

Test object attributes API

def setUp(self) -> None:
17    def setUp(self) -> None:
18        super().setUp()
19        self.user = create_test_admin_user()
20        self.client.force_login(self.user)

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

def test_create(self):
22    def test_create(self):
23        res = self.client.post(
24            reverse("authentik_api:objectattribute-list"),
25            data={
26                "object_type": "authentik_core.user",
27                "enabled": False,
28                "key": "employeeNumber",
29                "label": "Employee Number",
30                "type": "text",
31                "group": "Employee",
32                "is_unique": False,
33                "is_required": False,
34            },
35        )
36        self.assertEqual(res.status_code, 201)
37        attr = ObjectAttribute.objects.filter(key="employeeNumber").first()
38        self.assertIsNotNone(attr)
def test_create_invalid(self):
40    def test_create_invalid(self):
41        res = self.client.post(
42            reverse("authentik_api:objectattribute-list"),
43            data={
44                "object_type": "authentik_core.objectattribute",
45                "enabled": False,
46                "key": "employeeNumber",
47                "label": "Employee Number",
48                "type": "text",
49                "group": "Employee",
50                "is_unique": False,
51                "is_required": False,
52            },
53        )
54        self.assertEqual(res.status_code, 400)
55        self.assertJSONEqual(res.content, {"object_type": ["Invalid object type"]})
def test_update(self):
57    def test_update(self):
58        attr = ObjectAttribute.objects.create(
59            object_type=ContentType.objects.get_for_model(User),
60            label="foo",
61            key=generate_id(),
62            type=ObjectAttribute.AttributeType.TEXT,
63        )
64        res = self.client.put(
65            reverse("authentik_api:objectattribute-detail", kwargs={"pk": attr.pk}),
66            data={
67                "object_type": "authentik_core.user",
68                "enabled": False,
69                "key": attr.key,
70                "label": "Employee Number",
71                "type": "text",
72                "group": "Employee",
73                "is_unique": False,
74                "is_required": False,
75            },
76        )
77        self.assertEqual(res.status_code, 200)
78        attr.refresh_from_db()
79        self.assertEqual(attr.label, "Employee Number")
def test_user_attrib_validation_required(self):
81    def test_user_attrib_validation_required(self):
82        attr = ObjectAttribute.objects.create(
83            object_type=ContentType.objects.get_for_model(User),
84            label="foo",
85            key=generate_id(),
86            type=ObjectAttribute.AttributeType.TEXT,
87            is_required=True,
88        )
89        res = self.client.patch(
90            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
91            data={
92                "attributes": {},
93            },
94        )
95        self.assertEqual(res.status_code, 400)
96        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["This field is required"]})
def test_user_attrib_validation_no_required_blank(self):
 98    def test_user_attrib_validation_no_required_blank(self):
 99        attr = ObjectAttribute.objects.create(
100            object_type=ContentType.objects.get_for_model(User),
101            label="foo",
102            key=generate_id(),
103            type=ObjectAttribute.AttributeType.TEXT,
104            is_required=False,
105        )
106        res = self.client.post(
107            reverse("authentik_api:user-list"),
108            data={
109                "username": generate_id(),
110                "name": "test",
111                "attributes": {
112                    attr.key: "",
113                },
114            },
115        )
116        self.assertEqual(res.status_code, 201, res.content)
def test_user_attrib_validation_unique(self):
118    def test_user_attrib_validation_unique(self):
119        attr = ObjectAttribute.objects.create(
120            object_type=ContentType.objects.get_for_model(User),
121            label="foo",
122            key=generate_id(),
123            type=ObjectAttribute.AttributeType.TEXT,
124            is_unique=True,
125        )
126        other_user = create_test_user()
127        other_user.attributes[attr.key] = "foo"
128        other_user.save()
129        res = self.client.patch(
130            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
131            data={
132                "attributes": {attr.key: "foo"},
133            },
134        )
135        self.assertEqual(res.status_code, 400)
136        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["Value is not unique."]})
def test_user_attrib_validation_unique_same_user(self):
138    def test_user_attrib_validation_unique_same_user(self):
139        attr = ObjectAttribute.objects.create(
140            object_type=ContentType.objects.get_for_model(User),
141            label="foo",
142            key=generate_id(),
143            type=ObjectAttribute.AttributeType.TEXT,
144            is_unique=True,
145        )
146        self.user.attributes[attr.key] = "foo"
147        self.user.save()
148        res = self.client.patch(
149            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
150            data={
151                "name": "Renamed",
152                "attributes": {attr.key: "foo"},
153            },
154        )
155        self.assertEqual(res.status_code, 200, res.content)
156        self.user.refresh_from_db()
157        self.assertEqual(self.user.name, "Renamed")
158        self.assertEqual(self.user.attributes[attr.key], "foo")
159
160        other_user = create_test_user()
161        res = self.client.patch(
162            reverse("authentik_api:user-detail", kwargs={"pk": other_user.pk}),
163            data={
164                "attributes": {attr.key: "foo"},
165            },
166        )
167        self.assertEqual(res.status_code, 400)
168        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["Value is not unique."]})
def test_user_attrib_validation_regex(self):
170    def test_user_attrib_validation_regex(self):
171        attr = ObjectAttribute.objects.create(
172            object_type=ContentType.objects.get_for_model(User),
173            label="foo",
174            key=generate_id(),
175            type=ObjectAttribute.AttributeType.TEXT,
176            regex="bar",
177        )
178        res = self.client.patch(
179            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
180            data={
181                "attributes": {attr.key: "foo"},
182            },
183        )
184        self.assertEqual(res.status_code, 400)
185        self.assertJSONEqual(
186            res.content, {f"attributes_{attr.key}": ["Value does not match configured pattern."]}
187        )
def test_user_attrib_validation_array(self):
189    def test_user_attrib_validation_array(self):
190        attr = ObjectAttribute.objects.create(
191            object_type=ContentType.objects.get_for_model(User),
192            label="foo",
193            key=generate_id(),
194            type=ObjectAttribute.AttributeType.TEXT,
195            is_array=True,
196        )
197        res = self.client.patch(
198            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
199            data={
200                "attributes": {attr.key: "foo"},
201            },
202        )
203        self.assertEqual(res.status_code, 400)
204        self.assertJSONEqual(res.content, {f"attributes_{attr.key}": ["Value must be an array."]})
205
206        res = self.client.patch(
207            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
208            data={
209                "attributes": {attr.key: ["foo"]},
210            },
211        )
212        self.assertEqual(res.status_code, 200)
def test_user_attrib_validation_array_regex(self):
214    def test_user_attrib_validation_array_regex(self):
215        attr = ObjectAttribute.objects.create(
216            object_type=ContentType.objects.get_for_model(User),
217            label="foo",
218            key=generate_id(),
219            type=ObjectAttribute.AttributeType.TEXT,
220            is_array=True,
221            regex="bar",
222        )
223        res = self.client.patch(
224            reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk}),
225            data={
226                "attributes": {attr.key: ["foo"]},
227            },
228        )
229        self.assertEqual(res.status_code, 400)
230        self.assertJSONEqual(
231            res.content, {f"attributes_{attr.key}": ["Value does not match configured pattern."]}
232        )
def test_types_str(self):
234    def test_types_str(self):
235        attr = ObjectAttribute.objects.create(
236            object_type=ContentType.objects.get_for_model(User),
237            label="foo",
238            key=generate_id(),
239            type=ObjectAttribute.AttributeType.TEXT,
240        )
241        attr.run_validation("foo")
242        attr.run_validation(3)
243        with self.assertRaises(ValidationError):
244            attr.run_validation(False)
def test_types_number(self):
246    def test_types_number(self):
247        attr = ObjectAttribute.objects.create(
248            object_type=ContentType.objects.get_for_model(User),
249            label="foo",
250            key=generate_id(),
251            type=ObjectAttribute.AttributeType.NUMBER,
252        )
253        attr.run_validation(3)
254        with self.assertRaises(ValidationError):
255            attr.run_validation("foo")
256        with self.assertRaises(ValidationError):
257            attr.run_validation(False)
def test_types_bool(self):
259    def test_types_bool(self):
260        attr = ObjectAttribute.objects.create(
261            object_type=ContentType.objects.get_for_model(User),
262            label="foo",
263            key=generate_id(),
264            type=ObjectAttribute.AttributeType.BOOLEAN,
265        )
266        attr.run_validation(False)
267        with self.assertRaises(ValidationError):
268            attr.run_validation("foo")
269        with self.assertRaises(ValidationError):
270            attr.run_validation(3)