Skip to content

Commit 4ff42c6

Browse files
committed
Add test file
Fix #183 Changes to be committed: new file: tests/test_api_groups _modify.py
1 parent 5c9451c commit 4ff42c6

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

tests/test_api_groups _modify.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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
14+
15+
16+
class GroupInfoAPITestCase(TestCase):
17+
fixtures = ['tests/fixtures/test_data']
18+
19+
def setUp(self):
20+
self.client = APIClient()
21+
22+
# Checking if the user 'bco_api_user' already exists
23+
try:
24+
self.user = User.objects.get(username='bco_api_user')
25+
except User.DoesNotExist:
26+
self.user = User.objects.create_user(username='bco_api_user')
27+
28+
# Checking if user already has token, if not then creating one
29+
if not Token.objects.filter(user=self.user).exists():
30+
self.token = Token.objects.create(user=self.user)
31+
else:
32+
self.token = Token.objects.get(user=self.user)
33+
34+
def test_success_response(self):
35+
# Successful request with authentication data
36+
data = {
37+
"POST_api_groups_info": {
38+
"names": ["anon", "wheel"]
39+
}
40+
}
41+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
42+
response = self.client.post('/api/groups/group_info/', data=data, format='json')
43+
self.assertEqual(response.status_code, 200)
44+
45+
def test_bad_request_response(self):
46+
# Bad request: Authorization is not provided in the request headers
47+
#Gives 403 instead of 400
48+
49+
data = {
50+
"POST_api_groups_info": {
51+
"names": ["anon", "wheel"]
52+
}
53+
}
54+
#self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
55+
response = self.client.post('/api/groups/group_info/', data=data, format='json')
56+
self.assertEqual(response.status_code, 403)
57+
58+
def test_unauthorized_response(self):
59+
# Unauthorized: Authentication credentials were not valid
60+
#Gives 403 instead of 401
61+
62+
data = {
63+
"POST_api_groups_info": {
64+
"names": ["anon", "wheel"]
65+
}
66+
}
67+
# Use an invalid token or no token to simulate an unauthorized request
68+
self.client.credentials(HTTP_AUTHORIZATION='Token InvalidToken')
69+
response = self.client.post('/api/groups/group_info/', data=data, format='json')
70+
self.assertEqual(response.status_code, 403)

0 commit comments

Comments
 (0)