Skip to content

Commit b535c47

Browse files
committed
Closes Develop tests and document
Fix #169 Changes to be committed: modified: authentication/apis.py modified: authentication/services.py modified: tests/fixtures/test_data.json modified: tests/test_views/test_api_auth_add.py
1 parent ab0044d commit b535c47

File tree

4 files changed

+67
-60
lines changed

4 files changed

+67
-60
lines changed

authentication/apis.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ class AddAuthenticationApi(APIView):
123123
@swagger_auto_schema(
124124
request_body=schema,
125125
responses={
126-
200: "Add authentication is successful.",
127-
201: "Authentication credentials were created and added.",
126+
200: "New authentication credentials added to existing object.",
127+
201: "Authentication object created and added to account.",
128128
400: "Bad request.",
129+
403: "Authentication credentials were not provided.",
129130
409: "That object already exists for this account.",
130131
},
131132
tags=["Authentication"],
@@ -140,7 +141,6 @@ def post(self, request):
140141
return Response(status=status.HTTP_400_BAD_REQUEST, data=result)
141142
try:
142143
auth_object = Authentication.objects.get(username=request.user.username)
143-
144144
if request.data in auth_object.auth_service:
145145
return Response(
146146
status=status.HTTP_409_CONFLICT,
@@ -150,7 +150,7 @@ def post(self, request):
150150
auth_object.save()
151151
return Response(
152152
status=status.HTTP_200_OK,
153-
data={"message": "Authentication added to existing object"}
153+
data={"message": "New authentication credentials added to existing object"}
154154
)
155155

156156
except Authentication.DoesNotExist:
@@ -161,7 +161,7 @@ def post(self, request):
161161
print('status=status.HTTP_201_CREATED')
162162
return Response(
163163
status=status.HTTP_201_CREATED,
164-
data={"message": "Authentication object added to account"}
164+
data={"message": "Authentication object created and added to account"}
165165
)
166166

167167
except Exception as err:

authentication/services.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ def validate_auth_service(value):
7272
"required": ["iss", "sub"],
7373
"additionalProperties": False,
7474
"properties": {
75-
"iss": {"type": "string", "description": "The 'iss' (issuer) claim identifies the principal that issued the JWT."},
76-
"sub": {"type": "string", "description": "The 'sub' (subject) claim identifies the principal that is the subject of the JWT."}
75+
"iss": {
76+
"type": "string",
77+
"description": "The 'iss' (issuer) claim identifies the principal that issued the JWT."
78+
},
79+
"sub": {
80+
"type": "string",
81+
"description": "The 'sub' (subject) claim identifies the principal that is the subject of the JWT."
82+
}
7783
}
7884
}
7985
try:

tests/fixtures/test_data.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@
207207
"change_message": "[]"
208208
}
209209
},
210+
{
211+
"model": "authentication.authentication",
212+
"pk": 1,
213+
"fields": {
214+
"username": "bco_api_user",
215+
"auth_service": [
216+
{
217+
"iss": "Reeya1",
218+
"sub": "ReeyaGupta1"
219+
}
220+
]
221+
}
222+
},
210223
{
211224
"model": "auth.permission",
212225
"pk": 1,
Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,60 @@
1-
##test for api/auth/add
2-
##07801a1a4cdbf1945e22ac8439f1db27fe813f7a
1+
#!/usr/bin/env python3
2+
3+
"""Add Authentication
4+
Tests for 'New authentication credentials added to existing object' (200),
5+
'Authentication credentials were created and added' (201), 'Bad request' (400),
6+
'That object already exists for this account' (409)
7+
"""
8+
39
from django.test import TestCase, Client
410
from rest_framework.test import APIClient
511
from rest_framework.authtoken.models import Token
612
from django.contrib.auth.models import User
13+
from authentication.models import Authentication
714

815
class AuthenticationTestCase(TestCase):
16+
fixtures = ['tests/fixtures/test_data']
17+
918
def setUp(self):
1019
self.client = APIClient()
1120

21+
def test_credentials_created_response(self):
22+
"""Add authentication is successful (200)
23+
"""
1224

13-
# Creating a user for authentication
14-
self.user = User.objects.create(username='testuser')
15-
16-
# Checking if user already has token, if not then creating one
17-
if not Token.objects.filter(user=self.user).exists():
18-
self.token = Token.objects.create(user=self.user)
19-
else:
20-
self.token = Token.objects.get(user=self.user)
21-
def test_success_response(self):
22-
# successfull request
23-
data = {
24-
"iss": "Reeya1",
25-
"sub": "ReeyaGupta1"
26-
}
25+
token = Token.objects.get(user=User.objects.get(username='test50')).key
26+
data = {"iss": "Reeya1","sub": "ReeyaGupta1"}
2727

28-
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
28+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
2929
response = self.client.post('/api/auth/add/', data=data)
30-
'''fails with "AssertionError: 201 != 200":
31-
This test case is expecting a status code of 200 (OK) when making a successful request,
32-
but it receives a status code of 201 (Created) instead. It seems like the endpoint I am
33-
testing is returning a 201 status code instead of the expected 200. To fix this, I
34-
updated the test case to expect a status code of 201.
35-
'''
3630
self.assertEqual(response.status_code, 201)
3731

38-
def test_credentials_created_response(self):
39-
# Simulate a request where authentication credentials were created and added
40-
data = {
41-
"iss": "Reeya1",
42-
"sub": "ReeyaGupta1"
43-
}
44-
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
45-
response = self.client.post('/api/auth/add/', data=data)
46-
self.assertEqual(response.status_code, 201)
32+
def test_credentials_added(self):
33+
"""New authentication credentials added to existing object (200)
34+
"""
35+
36+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
37+
data = {"iss": "new","sub": "new One"}
38+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
39+
response = self.client.post('/api/auth/add/', data=data, format='json')
40+
self.assertEqual(response.status_code, 200)
4741

4842
def test_bad_request_response(self):
49-
# bad request
50-
data = {
51-
# Missing required fields
52-
}
53-
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
54-
response = self.client.post('/api/auth/add/', data=data)
43+
"""Bad request (400)
44+
"""
45+
46+
token = Token.objects.get(user=User.objects.get(username='test50')).key
47+
data = {"Missing required fields"}
48+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
49+
response = self.client.post('/api/auth/add/', data=data, format='json')
5550
self.assertEqual(response.status_code, 400)
5651

5752
def test_object_already_exists_response(self):
58-
# an object that already exists for this account
59-
data = {
60-
"iss": "Reeya",
61-
"sub": "ReeyaGupta"
62-
}
63-
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
64-
response = self.client.post('/api/auth/add/', data=data)
65-
'''fails with "AssertionError: 201 != 409"
66-
Similarly, this test case is expecting 409 (Conflict) when trying to
67-
create an object that already exists, but it receives 201 (Created).
68-
This endpoint is not handling the object's existence as expected.
69-
I updated the test case to expect a status code of 201 when creating an object
70-
that already exists.
71-
'''
72-
self.assertEqual(response.status_code, 201)
53+
"""That object already exists for this account (409)
54+
"""
55+
56+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
57+
data = {"iss": "Reeya1","sub": "ReeyaGupta1"}
58+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
59+
response = self.client.post('/api/auth/add/', data=data, format='json')
60+
self.assertEqual(response.status_code, 409)

0 commit comments

Comments
 (0)