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 ebff588 commit aa89df6
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 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)
7 changes: 7 additions & 0 deletions authors/apps/profiles/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path
from .views import RetrieveProfileAPIView, RetrieveAuthorsAPIView

urlpatterns = [
path('profiles/<username>/', RetrieveProfileAPIView.as_view(), name='profile'),
path('authors/', RetrieveAuthorsAPIView.as_view(), name='authors')
]
33 changes: 33 additions & 0 deletions authors/apps/profiles/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.shortcuts import render
from rest_framework import status
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.generics import RetrieveAPIView, ListAPIView
from .models import Profile
from .serializers import ProfileSerializer
from .exceptions import ProfileNotFound

class RetrieveProfileAPIView(RetrieveAPIView):
"""retrieve users profile"""
permission_classes = (IsAuthenticated,)
serializer_class = ProfileSerializer

def retrieve(self, request, username, *args, **kwargs):
"""retrieve existing user profiles"""
try:
profile = Profile.objects.select_related('user').get(
user__username=username
)
except Profile.DoesNotExist:
raise ProfileNotFound
serializer = self.serializer_class(profile)
return Response({
'profile': serializer.data
}, status = status.HTTP_200_OK)


class RetrieveAuthorsAPIView(ListAPIView):
"""retrieve all authors"""
permission_classes = (IsAuthenticated,)
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
41 changes: 41 additions & 0 deletions authors/base_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.urls import reverse
from rest_framework.test import APIClient
from django.test import TestCase


class BaseTestCase(TestCase):
"""Base test file to be used by other test files in the project"""
def setUp(self):
"""Basic configurations of the tests and data for mockups"""
self.register_data = {'user': {
"username": "Jacob",
"email": "jake@jake.jake",
"password": "Ajakejake12#"
}}

self.login_data = {
"user": {
"email": "jake@jake.jake",
"password": "Ajakejake12#"
}
}
self.login_no_email = {
"user": {
"email": "",
"password": "jakejake"
}
}
self.login_invalid_pass = {
"user": {
"email": "jake@jake.jake",
"password": "jakejake"
}
}

self.client = APIClient()
self.register_url = reverse("authentication:user-signup")
self.login_url = reverse("authentication:user-login")
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 aa89df6

Please sign in to comment.