Skip to content

Commit

Permalink
feat(list users): user should be able to see the the list and profile…
Browse files Browse the repository at this point in the history
…s of existing authors

- a logged in user should  be able to see the  existing users profiles

[Delivers #161966619]
  • Loading branch information
salma-nyagaka committed Dec 6, 2018
1 parent 154f7ab commit 6957106
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
34 changes: 34 additions & 0 deletions authors/apps/profiles/tests/test_view_all_profiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from authors.base_file import BaseTestCase
from django.urls import reverse
from rest_framework import status
from ..models import Profile

class TestProfile(BaseTestCase):
"""test the user profile"""

def login_user(self):
"""function to login the user"""
response = self.client.post(self.login_url, self.login_data, format='json')
return response.data['token']

def register_user(self):
"""function to register a new user"""
response = self.client.post(self.register_url, self.register_data, format='json')
return response

def test_get_all_authors(self):
"""test getting all authors"""
self.register_user()
token = self.login_user()
response = self.client.get(self.user_author, format='json', HTTP_AUTHORIZATION='Token ' +token)
self.assertEqual(response.status_code, status.HTTP_200_OK)


def test_get_specific_authors_profile(self):
"""test viewing a specific author's profile"""
self.register_user()
token = self.login_user()
response = self.client.get(reverse("profiles:profile", kwargs={
'username':self.register_data['user']['username'],
}), format='json', HTTP_AUTHORIZATION='Token ' +token)
self.assertEqual(response.status_code, status.HTTP_200_OK)
5 changes: 3 additions & 2 deletions authors/apps/profiles/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.urls import path
from .views import RetrieveProfileAPIView
from .views import RetrieveProfileAPIView, RetrieveAuthorsAPIView

urlpatterns = [
path('profiles/<username>/', RetrieveProfileAPIView.as_view(), name='profile')
path('profiles/<username>/', RetrieveProfileAPIView.as_view(), name='profile'),
path('authors/', RetrieveAuthorsAPIView.as_view(), name='authors')
]
11 changes: 9 additions & 2 deletions authors/apps/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework import status
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.generics import RetrieveAPIView
from rest_framework.generics import RetrieveAPIView, ListAPIView
from .models import Profile
from .serializers import ProfileSerializer
from .exceptions import ProfileNotFound
Expand All @@ -23,4 +23,11 @@ def retrieve(self, request, username, *args, **kwargs):
serializer = self.serializer_class(profile)
return Response({
'profile': serializer.data
}, status = status.HTTP_200_OK)
}, status = status.HTTP_200_OK)


class RetrieveAuthorsAPIView(ListAPIView):
"""retrieve all authors"""
permission_classes = (IsAuthenticated,)
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
1 change: 1 addition & 0 deletions authors/base_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ def setUp(self):
response = self.client.post(self.login_url, self.login_data, format='json')
self.token = "dummytokenhere"
self.user_url = reverse('authentication:user-retrieve-profile')
self.user_author = reverse('profiles:authors')

0 comments on commit 6957106

Please sign in to comment.