Skip to content

Commit 23fb085

Browse files
committed
Add Tests for prefix_token
Fix #177 Changes to be committed: modified: api/views.py new file: tests/test_views/test_api_prefixes_token.py
1 parent 383366c commit 23fb085

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

api/views.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,10 +1529,11 @@ class ApiPrefixesToken(APIView):
15291529
15301530
--------------------
15311531
1532-
Get all available prefixes and their associated permissions for a given token.
1533-
The word 'Token' must be included in the header.
1532+
Get all available prefixes and their associated permissions for a given
1533+
token. The word 'Token' must be included in the header.
15341534
1535-
For example: 'Token 627626823549f787c3ec763ff687169206626149'.
1535+
For example: 'Token 627626823549f787c3ec763ff687169206626149'. Using that
1536+
token will return an empty list, as that is test user.
15361537
"""
15371538

15381539
auth = [
@@ -1547,18 +1548,20 @@ class ApiPrefixesToken(APIView):
15471548
@swagger_auto_schema(
15481549
manual_parameters=auth,
15491550
responses={
1550-
200: "The Authorization header was provided and available prefixes were returned.",
1551-
400: "The Authorization header was not provided.",
1551+
200: "The available prefixes were returned.",
1552+
401: "The authorization header was not provided.",
1553+
403: "Invalid token.",
15521554
},
15531555
tags=["Prefix Management"],
15541556
)
15551557
def post(self, request) -> Response:
15561558
if "Authorization" in request.headers:
1557-
# Pass the request to the handling function
1558-
# Source: https://stackoverflow.com/a/31813810
15591559
return post_api_prefixes_token_flat(request=request)
15601560
else:
1561-
return Response(status=status.HTTP_400_BAD_REQUEST)
1561+
return Response(
1562+
data={"detail": "The authorization header was not provided."},
1563+
status=status.HTTP_401_UNAUTHORIZED
1564+
)
15621565

15631566

15641567
class ApiPrefixesTokenFlat(APIView):
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
#!/usr/bin/env python3
3+
4+
"""Prefixes token
5+
Tests for 'Successful request' (200),
6+
'forbiddden' (403)
7+
"""
8+
9+
from django.test import TestCase
10+
from rest_framework.test import APIClient
11+
from rest_framework.authtoken.models import Token
12+
from django.contrib.auth.models import User
13+
from rest_framework.test import APITestCase
14+
15+
16+
class PrefixesTokenTestCase(APITestCase):
17+
fixtures = ['tests/fixtures/test_data']
18+
19+
def setUp(self):
20+
self.client = APIClient()
21+
22+
def test_success_response(self):
23+
"""The available prefixes were returned. (200)"""
24+
25+
token = Token.objects.get(
26+
user=User.objects.get(username='bco_api_user')
27+
).key
28+
29+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
30+
response = self.client.post('/api/prefixes/token/',data={})
31+
self.assertEqual(response.status_code, 200)
32+
33+
34+
# def test_unauthorized_response(self):
35+
# """The authorization header was not provided. (401)"""
36+
37+
# self.client.credentials()
38+
# response = self.client.post('/api/prefixes/token/')
39+
# self.assertEqual(response.status_code, 401)
40+
41+
def test_invalid_token(self):
42+
"""Invalid token(403)"""
43+
44+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + "token")
45+
response = self.client.post('/api/prefixes/token/')
46+
self.assertEqual(response.status_code, 403)

0 commit comments

Comments
 (0)