-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(list users): user should be able to see the the list and profile…
…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
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |