diff --git a/authentik/core/tests/test_token_api.py b/authentik/core/tests/test_token_api.py index a22982ce3..5588bc527 100644 --- a/authentik/core/tests/test_token_api.py +++ b/authentik/core/tests/test_token_api.py @@ -13,7 +13,7 @@ class TestTokenAPI(APITestCase): self.user = User.objects.get(username="akadmin") self.client.force_login(self.user) - def test_test_call(self): + def test_token_create(self): """Test token creation endpoint""" response = self.client.post( reverse("authentik_api:token-list"), {"identifier": "test-token"} diff --git a/authentik/policies/tests/test_bindings_api.py b/authentik/policies/tests/test_bindings_api.py new file mode 100644 index 000000000..4d3a0d3f9 --- /dev/null +++ b/authentik/policies/tests/test_bindings_api.py @@ -0,0 +1,56 @@ +"""Test bindings API""" +from django.urls import reverse +from rest_framework.test import APITestCase + +from authentik.core.models import Group, User +from authentik.policies.models import PolicyBinding, PolicyBindingModel + + +class TestBindingsAPI(APITestCase): + """Test bindings API""" + + def setUp(self) -> None: + super().setUp() + self.pbm = PolicyBindingModel.objects.create() + self.group = Group.objects.first() + self.user = User.objects.get(username="akadmin") + self.client.force_login(self.user) + + def test_valid_binding(self): + """Test valid binding""" + response = self.client.post( + reverse("authentik_api:policybinding-list"), + data={"target": self.pbm.pk, "user": self.user.pk, "order": 0}, + ) + self.assertEqual(response.status_code, 201) + + def test_invalid_too_much(self): + """Test invalid binding (too much)""" + response = self.client.post( + reverse("authentik_api:policybinding-list"), + data={ + "target": self.pbm.pk, + "user": self.user.pk, + "group": self.group.pk, + "order": 0, + }, + ) + self.assertJSONEqual( + response.content.decode(), + { + "non_field_errors": [ + "Only one of 'policy', 'group' or 'user' can be set." + ] + }, + ) + + def test_invalid_too_little(self): + """Test invvalid binding (too little)""" + response = self.client.post( + reverse("authentik_api:policybinding-list"), + data={"target": self.pbm.pk, "order": 0}, + ) + self.assertJSONEqual( + response.content.decode(), + {"non_field_errors": ["One of 'policy', 'group' or 'user' must be set."]}, + ) diff --git a/authentik/policies/tests/test_api.py b/authentik/policies/tests/test_policies_api.py similarity index 100% rename from authentik/policies/tests/test_api.py rename to authentik/policies/tests/test_policies_api.py