Skip to content

Commit f284442

Browse files
group details api (#111)
* Added group detail API and tests * Added group detail tests * Renamed Tests * Tests docstring added * Rectified docstring * Modified id for respective tests
1 parent 995f3c2 commit f284442

File tree

4 files changed

+230
-11
lines changed

4 files changed

+230
-11
lines changed

org/responses.py

+44-4
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
"message": "Already a member of organization"
2424
}
2525

26-
user_unauthorized_401 = {
27-
"detail": "Authentication credentials were not provided."
28-
}
29-
3026
user_already_present_409 = {
3127
"message": "Already a member of organization"
3228
}
3329

30+
user_not_present_401 = {
31+
"detail":"You are not a member of this organisation"
32+
}
33+
3434
update_org_200 = {
3535
"message": "Organization details updated successfully"
3636
}
@@ -60,3 +60,43 @@
6060
"memberCount": 1
6161
}
6262
]
63+
64+
org_not_present_404 = {
65+
"message":"This organisation does not exist"
66+
}
67+
68+
group_not_present_400 = {
69+
"message":"This group does not exist"
70+
}
71+
72+
user_unauthorized_403 = {
73+
"message":"You do not have the required permissions."
74+
}
75+
76+
group_details_200 = {
77+
"id": 1,
78+
"name": "head coordinator",
79+
"role": "Managing the sub-ordinates. Supervising and inteeractiong the respective domains.",
80+
"permissions": {
81+
"Is Admin":{
82+
'value': False,
83+
'perm_int': 1,
84+
},
85+
"Is Staff":{
86+
'value': True,
87+
'perm_int': 4,
88+
},
89+
"Can create tasks":{
90+
'value': True,
91+
'perm_int': 3,
92+
},
93+
"Can create groups":{
94+
'value': True,
95+
'perm_int': 6,
96+
},
97+
"Can reply to queries":{
98+
'value': False,
99+
'perm_int': 5,
100+
}
101+
}
102+
}

org/urls.py

+5
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
path('<int:org_id>/', EditOrg, name='edit_org'),
88
path('<int:org_id>/volunteer/', AddVolunteer, name='add_volunteer'),
99
path('<int:org_id>/group/', GetGroup, name='get_group'),
10+
path(
11+
'<int:org_id>/group/<int:group_id>/',
12+
GroupDetailsView.as_view(),
13+
name='get_group_details'
14+
),
1015
]

org/views.py

+98-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
from django.shortcuts import render
22
from rest_framework import status
33
from rest_framework.response import Response
4-
from rest_framework import status
54
from rest_framework.views import APIView
65
from rest_framework.parsers import MultiPartParser
76
from rest_framework.permissions import IsAuthenticated
8-
from .serializers import *
9-
from .models import *
10-
from . import responses
117
from rest_framework.decorators import api_view, permission_classes
128
from drf_yasg.utils import swagger_auto_schema
139
from drf_yasg import openapi
1410
from utils.swagger import set_example
1511
from org.custom_model_field import Permissions
12+
from .serializers import *
13+
from .models import *
14+
from . import responses
1615

1716

1817
class OrgView(APIView):
@@ -29,11 +28,11 @@ class OrgView(APIView):
2928
)
3029
def post(self, request):
3130
"""
32-
1. when a Organisation is created Admin and Volunteer
31+
1. when a Organisation is created Admin and Volunteer
3332
groups are also automatically created for that org.
3433
2. Admin group has all the permissions available.
35-
3. Volunteer group has no permissions but when a user
36-
joins that org without invite link he/she will be
34+
3. Volunteer group has no permissions but when a user
35+
joins that org without invite link he/she will be
3736
put into volunteer group.
3837
4. The creator of the org will be automatically put into Admin group.
3938
"""
@@ -173,3 +172,95 @@ def GetGroup(request, org_id):
173172
return Response(response_object, status.HTTP_200_OK)
174173
else:
175174
return Response({"detail": "You are not authorised to view this."}, status.HTTP_403_FORBIDDEN)
175+
176+
177+
class GroupDetailsView(APIView):
178+
'''
179+
This is to provide details of a particular
180+
group of an organisation to authorised
181+
members.
182+
'''
183+
184+
permission_classes = [IsAuthenticated]
185+
186+
@swagger_auto_schema(
187+
operation_id='group_details',
188+
operation_description="Authenticated and permitted users receive\
189+
desired group details here",
190+
responses={
191+
'200': set_example(responses.group_details_200),
192+
'404': set_example(responses.org_not_present_404),
193+
'401': set_example(responses.user_not_present_401),
194+
'403': set_example(responses.user_unauthorized_403),
195+
'400': set_example(responses.group_not_present_400),
196+
},
197+
)
198+
199+
def get(self, request, org_id, group_id):
200+
201+
try:
202+
org = Org.objects.get(id=org_id)
203+
except Org.DoesNotExist:
204+
return Response(
205+
{"message":"This organisation does not exist"},
206+
status.HTTP_404_NOT_FOUND
207+
)
208+
209+
try:
210+
group = Group.objects.get(
211+
id=group_id,
212+
org=org
213+
)
214+
except Group.DoesNotExist:
215+
return Response(
216+
{"message":"This group does not exist"},
217+
status.HTTP_400_BAD_REQUEST
218+
)
219+
220+
try:
221+
member = Member.objects.get(
222+
user=request.user,
223+
org=org
224+
)
225+
except Member.DoesNotExist:
226+
return Response(
227+
{"detail":"You are not a member of this organisation"},
228+
status.HTTP_401_UNAUTHORIZED
229+
)
230+
231+
if member.group.perm_obj.permissions[Permissions.IS_STAFF]:
232+
return Response(
233+
{
234+
"id" : group_id,
235+
"name" : group.name,
236+
"role" : group.role,
237+
"permissions" : {
238+
239+
"Is Admin":{
240+
'value': group.perm_obj.permissions[Permissions.IS_ADMIN],
241+
'perm_int': Permissions.IS_ADMIN,
242+
},
243+
"Is Staff":{
244+
'value': group.perm_obj.permissions[Permissions.IS_STAFF],
245+
'perm_int': Permissions.IS_STAFF,
246+
},
247+
"Can create tasks":{
248+
'value': group.perm_obj.permissions[Permissions.CAN_CREATE_TASKS],
249+
'perm_int': Permissions.CAN_CREATE_TASKS,
250+
},
251+
"Can reply to queries":{
252+
'value': group.perm_obj.permissions[Permissions.CAN_REPLY_TO_QUERIES],
253+
'perm_int': Permissions.CAN_REPLY_TO_QUERIES,
254+
},
255+
"Can review proofs":{
256+
'value': group.perm_obj.permissions[Permissions.CAN_CREATE_TASKS],
257+
'perm_int': Permissions.CAN_REVIEW_PROOFS,
258+
}
259+
}
260+
},
261+
status.HTTP_200_OK
262+
)
263+
return Response(
264+
{"message":"You do not have the required permissions."},
265+
status.HTTP_403_FORBIDDEN
266+
)

tests/org/tests_view_group_api.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'''This module is to test group_details operation of the org app'''
2+
from tests.AuthAPITestCase import AuthAPITestCase
3+
from rest_framework.test import APIClient
4+
from rest_framework import status
5+
from users.models import User
6+
from org.models import *
7+
from org.serializers import CreateOrgSerializer
8+
from org.custom_model_field import Permissions
9+
10+
class ViewGroupAPITestCase(AuthAPITestCase):
11+
"""
12+
This class is to test the API [get] /api/org/(org-id)/group/(group-id)/
13+
present in the class based view Org.views.GroupDetailsView
14+
"""
15+
16+
def setUp(self):
17+
"""
18+
Create an organization in the test database
19+
"""
20+
#Inheriting the base class functionality
21+
super(ViewGroupAPITestCase, self).setUp()
22+
# Create the org using serializer
23+
data_org = {
24+
"name":'test',
25+
"tagline":'test'
26+
}
27+
serializer = CreateOrgSerializer(data=data_org)
28+
if serializer.is_valid():
29+
self.org = serializer.save()[0]
30+
31+
def test_fail_without_auth_header(self):
32+
group_detail_api = "/api/org/1/group/1/"
33+
un_auth_client = APIClient()
34+
response = un_auth_client.get(group_detail_api)
35+
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
36+
37+
def test_fail_invalid_org(self):
38+
group_detail_api = "/api/org/12345/group/1/"
39+
auth_client = self.create_auth_client()
40+
response = auth_client.get(group_detail_api)
41+
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
42+
43+
def test_fail_invalid_group(self):
44+
group_detail_api = "/api/org/1/group/12345/"
45+
auth_client = self.create_auth_client()
46+
response = auth_client.get(group_detail_api)
47+
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
48+
49+
def test_fail_not_a_member(self):
50+
group_detail_api = "/api/org/1/group/1/"
51+
auth_client = self.create_auth_client()
52+
response = auth_client.get(group_detail_api)
53+
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
54+
55+
def test_fail_unauthorised_member(self):
56+
group_detail_api = "/api/org/1/group/1/"
57+
auth_client = self.create_auth_client()
58+
volunteer_group = Group.objects.get(
59+
name='Volunteer',
60+
org=self.org,
61+
)
62+
member = Member.objects.create(
63+
user=self.auth_user,
64+
org=self.org,
65+
group=volunteer_group
66+
)
67+
response = auth_client.get(group_detail_api)
68+
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
69+
70+
def test_success_authorized_user(self):
71+
group_detail_api = "/api/org/1/group/1/"
72+
auth_client = self.create_auth_client()
73+
admin_group = Group.objects.get(
74+
name='Admin',
75+
org=self.org,
76+
)
77+
member = Member.objects.create(
78+
user=self.auth_user,
79+
org=self.org,
80+
group=admin_group,
81+
)
82+
response = auth_client.get(group_detail_api)
83+
self.assertEqual(response.status_code, status.HTTP_200_OK)

0 commit comments

Comments
 (0)