policies: add tests for bindings API

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-03-31 20:36:37 +02:00
parent 330d5047e7
commit 041b51a7f8
3 changed files with 57 additions and 1 deletions

View file

@ -13,7 +13,7 @@ class TestTokenAPI(APITestCase):
self.user = User.objects.get(username="akadmin") self.user = User.objects.get(username="akadmin")
self.client.force_login(self.user) self.client.force_login(self.user)
def test_test_call(self): def test_token_create(self):
"""Test token creation endpoint""" """Test token creation endpoint"""
response = self.client.post( response = self.client.post(
reverse("authentik_api:token-list"), {"identifier": "test-token"} reverse("authentik_api:token-list"), {"identifier": "test-token"}

View file

@ -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."]},
)