Skip to content

Commit

Permalink
Merge fafcb11 into 585d7a2
Browse files Browse the repository at this point in the history
  • Loading branch information
ongebo committed Dec 6, 2018
2 parents 585d7a2 + fafcb11 commit 900499d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
30 changes: 30 additions & 0 deletions authors/apps/authentication/tests/test_list_users_functionality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from rest_framework.test import APITestCase
from rest_framework import status
import json


class ListUserFunctionalityTests(APITestCase):
def test_api_can_list_all_registered_users(self):
user1 = {'user': {
'username': 'Jack Sparrow',
'email': 'sparrow@gmail.com',
'password': 'SeaC4ptain'
}}
user2 = {'user': {
'username': 'Thanos',
'email': 'thanos@yahoo.com',
'password': 'T1tanKing'
}}
self.client.post('/api/users/', data=user1, format='json')
resp = self.client.post('/api/users/', data=user2, format='json')
body = json.loads(resp.content)
# specify authorization header
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + body['user']['token'])
resp = self.client.get('/api/authors/')
body = json.loads(resp.content)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertEqual(body['authors'][0]['username'], 'Jack Sparrow')
self.assertEqual(body['authors'][0]['email'], 'sparrow@gmail.com')
self.assertEqual(body['authors'][1]['username'], 'Thanos')
self.assertEqual(body['authors'][1]['email'], 'thanos@yahoo.com')

4 changes: 2 additions & 2 deletions authors/apps/authentication/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework_swagger.views import get_swagger_view

from .views import (
LoginAPIView, RegistrationAPIView, UserRetrieveUpdateAPIView
LoginAPIView, RegistrationAPIView, UserRetrieveUpdateAPIView, UsersRetrieveAPIView
)

schema_view = get_swagger_view(title='Authors Haven')
Expand All @@ -12,5 +12,5 @@
path('users/', RegistrationAPIView.as_view()),
path('users/login/', LoginAPIView.as_view()),
path('swagger/', schema_view),

path('authors/', UsersRetrieveAPIView.as_view())
]
18 changes: 16 additions & 2 deletions authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.reverse import reverse

from .renderers import UserJSONRenderer
from .serializers import (
LoginSerializer, RegistrationSerializer, UserSerializer
)
from .models import User
from django.http import JsonResponse



Expand All @@ -30,8 +33,6 @@ def post(self, request):

return Response(serializer.data, status=status.HTTP_201_CREATED)




class LoginAPIView(APIView):
permission_classes = (AllowAny,)
Expand Down Expand Up @@ -77,3 +78,16 @@ def update(self, request, *args, **kwargs):

return Response(serializer.data, status=status.HTTP_200_OK)


class UsersRetrieveAPIView(APIView):
permission_classes = (IsAuthenticated,)
serializer_class = UserSerializer

def get(self, request):
users = User.objects.all()
serializer = self.serializer_class(users, many=True)
response = {'authors': serializer.data}
for author in response['authors']:
del author['token']
return JsonResponse(response, status=200, safe=False)

0 comments on commit 900499d

Please sign in to comment.