Commit 44ec0566 authored by daniel.bacher's avatar daniel.bacher
Browse files

add user_roles endpoint to project

parent 193c05e4
Pipeline #254356 failed
......@@ -39,6 +39,13 @@ class TestApiLogic(APITestCase):
self.test_user_2 = User.objects.create(
username=faker.user_name(), password=faker.password()
)
self.test_user_3 = User.objects.create(
username=faker.user_name(), password=faker.password()
)
self.project = Project.objects.create(name=faker.word())
self.project.managers.add(self.test_user)
self.project.members.add(self.test_user_2)
self.project.save()
def test_creator_to_manager(self):
self.client.force_login(self.test_user)
......@@ -78,6 +85,31 @@ class TestApiLogic(APITestCase):
project = Project.objects.get(id=response.data['id'])
self.assertEqual(project.roottopic.topic, slugify(project.name))
def test_user_role_endpoint_with_manager(self):
self.client.force_login(self.test_user)
response = self.client.get(
"{0}{1}/user_roles/".format(self._url, self.project.id)
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {'roles': ['manager']})
def test_user_role_endpoint_with_member(self):
self.client.force_login(self.test_user_2)
response = self.client.get(
"{0}{1}/user_roles/".format(self._url, self.project.id)
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {'roles': ['member']})
def test_user_role_endpoint_with_no_member(self):
self.client.force_login(self.test_user_3)
response = self.client.get(
"{0}{1}/user_roles/".format(self._url, self.project.id)
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {'roles': []})
class TestPermissions(APITestCase):
def setUp(self) -> None:
self._url = reverse("project-list")
......
......@@ -38,6 +38,20 @@ class ProjectViewSet(viewsets.ModelViewSet):
resp = Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
return resp
@action(detail=True)
def user_roles(self, request, pk):
roles = []
if request.user.is_authenticated:
if request.user.is_superuser:
roles += ['superuser']
if Project.objects.get(id=pk).managers.filter(id=request.user.id).exists():
roles += ['manager']
if Project.objects.get(id=pk).members.filter(id=request.user.id).exists():
roles += ['member']
else:
roles += ['not_authenticated']
return Response({"roles": roles})
class UserViewSet(viewsets.ReadOnlyModelViewSet):
"""viewset for the user model."""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment