Skip to content

Commit 6a70489

Browse files
committed
Test and document '/api/groups/modify/'
Fix #183 Changes to be committed: modified: api/model/groups.py modified: api/views.py deleted: tests/test_api_groups _modify.py new file: tests/test_views/test_api_groups_modify.py
1 parent 4ff42c6 commit 6a70489

File tree

4 files changed

+127
-87
lines changed

4 files changed

+127
-87
lines changed

api/model/groups.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from api.scripts.utilities.DbUtils import DbUtils
1414
from api.scripts.utilities.UserUtils import UserUtils
15+
from api.models import BCO
1516

1617
usr_utils = UserUtils()
1718
db_utils = DbUtils()
@@ -280,14 +281,20 @@ def post_api_groups_delete(request):
280281

281282

282283
def post_api_groups_modify(request):
283-
"""Instantiate any necessary imports."""
284-
285-
bulk_request = request.data["POST_api_groups_modify"]
284+
"""Instantiate any necessary imports.
285+
TODO: This needs a serious revamp... Permissions and specific groups need
286+
to be adjusted. IE no one should be able to change a group without GroupInfo.
287+
"""
288+
try:
289+
bulk_request = request.data["POST_api_groups_modify"]
290+
except:
291+
return Response(status=status.HTTP_400_BAD_REQUEST)
286292
requestor_info = usr_utils.user_from_request(request=request)
287293
groups = list(Group.objects.all().values_list("name", flat=True))
288294
return_data = []
289295
for modification_object in bulk_request:
290296
standardized = modification_object["name"].lower()
297+
291298
if standardized in groups:
292299
grouped = Group.objects.get(name=standardized)
293300
if (
@@ -319,6 +326,12 @@ def post_api_groups_modify(request):
319326
if action_set["rename"] not in groups:
320327
grouped.name = action_set["rename"]
321328
grouped.save()
329+
group_information.group = grouped
330+
group_information.save()
331+
bco_list = BCO.objects.filter(owner_group=standardized)
332+
for bco in bco_list:
333+
bco.owner_group = grouped
334+
bco.save()
322335

323336
# Change description of group if set in actions.
324337
if "redescribe" in action_set:

api/views.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -418,25 +418,32 @@ def post(self, request):
418418

419419

420420
class ApiGroupsModify(APIView):
421-
"""Modify group
421+
"""Bulk Modify groups
422422
423423
--------------------
424-
Modifies an already existing BCO group. An array of objects are taken where each of these objects
425-
represents the instructions to modify a specific group. Within each of these objects, along with the
426-
group name, the set of modifications to that group exists in a dictionary as defined below.
427-
428-
Example request body which encodes renaming a group named `myGroup1` to `myGroup2`:
424+
Modifies one or more existing BCO groups. An array of objects are taken
425+
where each of these objects represents the instructions to modify a
426+
specific group. Within each of these objects, along with the group name,
427+
the set of modifications to that group exists in a dictionary indecated by
428+
the following 'actions': 'rename', 'redescribe', 'add_users',
429+
'remove_users', and 'owner_user'.
430+
431+
Example request body which encodes renaming a group named `myGroup1` to
432+
`myGroup2`:
429433
```
430-
request_body = ['POST_api_groups_modify' : {
431-
'name': 'myGroup1',
432-
'actions': {
433-
'rename': 'myGroup2'
434-
}
435-
}
436-
]
434+
"POST_api_groups_modify": [
435+
{
436+
"name": "myGroup1",
437+
"actions": {
438+
"rename": "myGroup2"
439+
}
440+
}
441+
]
437442
```
438443
439-
More than one action can be included for a specific group name.
444+
More than one action can be included for a specific group name, and more
445+
than one group can be modified with a request. To modify multiple groups
446+
they must each have their own request object.
440447
"""
441448

442449
POST_api_groups_modify_schema = openapi.Schema(

tests/test_api_groups _modify.py

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
#!/usr/bin/env python3
3+
4+
"""Group info
5+
Tests for 'Authorization is successful. Group permissions returned' (200),
6+
Forbidden response (400)
7+
"""
8+
9+
10+
from django.test import TestCase
11+
from rest_framework.test import APIClient
12+
from rest_framework.authtoken.models import Token
13+
from django.contrib.auth.models import User, Group
14+
from api.models import BCO
15+
from api.model.groups import GroupInfo
16+
17+
18+
class GroupInfoAPITestCase(TestCase):
19+
fixtures = ['tests/fixtures/test_data']
20+
21+
def setUp(self):
22+
self.client = APIClient()
23+
24+
def test_success_response(self):
25+
"""Successful request with authentication data
26+
"""
27+
28+
old_name = "test_drafter"
29+
new_name = "new_name"
30+
old_bco_counts = len(BCO.objects.filter(owner_group=old_name))
31+
old_group_counts = len(Group.objects.filter(name=old_name))
32+
old_groupInfo_counts = len(GroupInfo.objects.filter(group=old_name))
33+
34+
token = Token.objects.get(user=User.objects.get(username='test50')).key
35+
36+
data = {
37+
"POST_api_groups_modify": [
38+
{
39+
"name": old_name,
40+
"actions": {
41+
"rename": new_name
42+
}
43+
}
44+
]
45+
}
46+
47+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
48+
response = self.client.post('/api/groups/modify/', data=data, format='json')
49+
new_bco_counts = len(BCO.objects.filter(owner_group=new_name))
50+
new_group_counts = len(Group.objects.filter(name=new_name))
51+
new_groupInfo_counts = len(GroupInfo.objects.filter(group=new_name))
52+
self.assertEqual(response.status_code, 200)
53+
self.assertEqual(new_bco_counts, old_bco_counts)
54+
self.assertEqual(new_group_counts, old_group_counts)
55+
self.assertEqual(new_groupInfo_counts, old_groupInfo_counts)
56+
57+
def test_bad_request_response(self):
58+
"""Bad request: Authorization is not provided in the request headers
59+
Gives 403 instead of 400
60+
"""
61+
62+
token = Token.objects.get(user=User.objects.get(username='test50')).key
63+
64+
data = {
65+
"POST_api_groups_info": {
66+
"names": ["anon", "wheel"]
67+
}
68+
}
69+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
70+
response = self.client.post('/api/groups/modify/', data=data, format='json')
71+
self.assertEqual(response.status_code, 400)
72+
73+
def test_unauthorized_response(self):
74+
# Unauthorized: Authentication credentials were not valid
75+
#Gives 403 instead of 401
76+
77+
data = {
78+
"POST_api_groups_modify": [
79+
{
80+
"name": "old_name",
81+
"actions": {
82+
"rename": "new_name"
83+
}
84+
}
85+
]
86+
}
87+
88+
self.client.credentials(HTTP_AUTHORIZATION='Token InvalidToken')
89+
response = self.client.post('/api/groups/group_info/', data=data, format='json')
90+
self.assertEqual(response.status_code, 403)

0 commit comments

Comments
 (0)