Skip to content

Commit

Permalink
165305752-ft(swagger):fix swagger description messages
Browse files Browse the repository at this point in the history
-add swagger description messages

[Finishes #165305752]
  • Loading branch information
Amoswachira committed May 5, 2019
1 parent fa4b3c8 commit 9f18f4a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
4 changes: 4 additions & 0 deletions authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


class RegistrationAPIView(GenericAPIView):
"""Endpoint to signup a user"""
# Allow any user (authenticated or not) to hit this endpoint.
permission_classes = (AllowAny,)
renderer_classes = (SignupUserJSONRenderer,)
Expand Down Expand Up @@ -58,11 +59,13 @@ def post(self, request):


class LoginAPIView(GenericAPIView):
"""Endpoint to login a user"""
permission_classes = (AllowAny,)
renderer_classes = (UserJSONRenderer,)
serializer_class = LoginSerializer

def post(self, request):
"""Login a User"""
user = request.data.get('user', {})

# Notice here that we do not call `serializer.save()` like we did for
Expand All @@ -75,6 +78,7 @@ def post(self, request):


class UserRetrieveUpdateAPIView(RetrieveUpdateAPIView):
"""Endpoint to Retrieve and update users"""
permission_classes = (IsAuthenticated,)
renderer_classes = (UserJSONRenderer,)
serializer_class = UserSerializer
Expand Down
60 changes: 58 additions & 2 deletions authors/apps/profiles/tests/test_follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_followed_Successfully(self):
'/api/profiles/Thanos/follow/')
self.assertEqual(res.status_code, status.HTTP_202_ACCEPTED)
self.assertEqual(json.loads(res.content), {'profile': {
'username': 'Thanos', 'first_name': '', 'last_name': '', 'bio': ''}})
'username': 'Thanos', 'first_name': '', 'last_name': '', 'bio': '', 'following': True}})

def test_cannot_followthemselves(self):
""" test users cannot follow themselves"""
Expand Down Expand Up @@ -71,7 +71,7 @@ def test_unfollow(self):
'/api/profiles/Thanos/follow/')
self.assertEqual(res.status_code, status.HTTP_202_ACCEPTED)
self.assertEqual(json.loads(res.content), {'profile': {
'username': 'Thanos', 'first_name': '', 'last_name': '', 'bio': ''}})
'username': 'Thanos', 'first_name': '', 'last_name': '', 'bio': '', 'following': False}})

def test_already_following(self):
"""test if user is already following a user"""
Expand Down Expand Up @@ -145,3 +145,59 @@ def test_get_all_followers(self):

res = self.client.get('/api/profiles/ironman/followers/')
self.assertEqual(res.status_code, status.HTTP_200_OK)

def test_user_not_found_follow(self):
"""test user not found when follow"""
correct_user = self.get_user_object()
jwt = correct_user.token()
self.client.credentials(
HTTP_AUTHORIZATION='Bearer ' + jwt)
res = self.client.post(
'/api/profiles/john/follow/')
self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(json.loads(res.content), {'profile': {
'detail': 'User with that username Not found'}})

def test_user_not_found_unfollow(self):
"""test user not found when unfollow"""
correct_user = self.get_user_object()
jwt = correct_user.token()
self.client.credentials(
HTTP_AUTHORIZATION='Bearer ' + jwt)
res = self.client.delete(
'/api/profiles/john/follow/')
self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(json.loads(res.content), {'profile': {
'detail': 'User with that username Not found'}})

def test_user_not_found_get_followers(self):
"""test user not found when getfollowers"""
correct_user = self.get_user_object()
jwt = correct_user.token()
self.client.credentials(
HTTP_AUTHORIZATION='Bearer ' + jwt)
follow = Followers()
follow.profile_id = correct_user.pk
follow.followed_id = self.jane.pk
follow.save()

res = self.client.get('/api/profiles/john/followers/')
self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(json.loads(res.content), {'profile': {
'detail': 'User with that username Not found'}})

def test_user_not_found_get_followings(self):
"""test user not found when getfollowings"""
correct_user = self.get_user_object()
jwt = correct_user.token()
self.client.credentials(
HTTP_AUTHORIZATION='Bearer ' + jwt)
follow = Followers()
follow.profile_id = correct_user.pk
follow.followed_id = self.flex.pk
follow.save()

res = self.client.get('/api/profiles/john/follow/')
self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(json.loads(res.content), {'profile': {
'detail': 'User with that username Not found'}})
45 changes: 37 additions & 8 deletions authors/apps/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from authors.apps.authentication.models import User
from django.shortcuts import get_object_or_404
from rest_framework.exceptions import APIException
import json


from .serializers import UserProfileSerializer, UpdateUserProfileSerializer, FollowingSerializer, UserListSerializer
from .models import Profile, Followers
Expand Down Expand Up @@ -83,13 +85,20 @@ def patch(self, request, username):
)


class FollowAPI(GenericAPIView):
class UserNotFound(APIException):
"""exception message"""
status_code = 404
default_detail = 'User with that username Not found'


class FollowAPI(GenericAPIView):
"""class for follow and unfollow users"""
permission_classes = (IsAuthenticated,)
renderer_classes = (FollowersJSONRenderer,)
serializer_class = FollowingSerializer

def is_following_user(self, profile, followed):
"""check if a user is following or not"""
if profile.id == followed.id:
return Response({
'error': 'You can not follow yourself.'
Expand All @@ -102,9 +111,12 @@ def is_following_user(self, profile, followed):
}, status=status.HTTP_400_BAD_REQUEST)

def post(self, request, username):

"""Follow a certain user"""
profile = User.objects.get(username=request.user.username)
followed = get_object_or_404(User, username=username)
try:
followed = User.objects.get(username=username)
except User.DoesNotExist:
raise UserNotFound

verify_follow = self.is_following_user(profile, followed)
if isinstance(verify_follow, Response):
Expand All @@ -121,14 +133,21 @@ def post(self, request, username):
"first_name": profile.first_name,
"last_name": profile.last_name,
"bio": profile.bio,
"following": True,


}
return Response(response, status=status.HTTP_202_ACCEPTED)

def delete(self, request, username):
"""Unfollow a certain user"""
profile = User.objects.get(username=request.user.username)
followed = get_object_or_404(User, username=username)

try:
followed = User.objects.get(username=username)
except User.DoesNotExist:
raise UserNotFound

follow = Followers.objects.filter(
profile_id=profile.id, followed_id=followed.profile.id).first()
if not follow:
Expand All @@ -142,13 +161,16 @@ def delete(self, request, username):
"first_name": profile.first_name,
"last_name": profile.last_name,
"bio": profile.bio,

"following": False,
}
return Response(response, status=status.HTTP_202_ACCEPTED)

def get(self, request, username):
"""get all users a user is following"""
user = get_object_or_404(User, username=username)
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
raise UserNotFound
follows = Followers.objects.filter(profile_id=user.id)
serializer = self.serializer_class(follows, many=True)
profiles = []
Expand All @@ -159,6 +181,7 @@ def get(self, request, username):
"first_name": profile.first_name,
"last_name": profile.last_name,
"bio": profile.bio,
"following": True,
})
if not profiles:
response = {'message': '{} has no followings yet'.format(username)}
Expand All @@ -169,14 +192,18 @@ def get(self, request, username):


class FollowersAPI(GenericAPIView):
"""class for get all followings"""
permission_classes = (IsAuthenticated,)
renderer_classes = (FollowersJSONRenderer,)
serializer_class = FollowingSerializer

def get(self, request, username):
""" Get all Users following a user """
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
raise UserNotFound

user = get_object_or_404(User, username=username)
followers = Followers.objects.filter(profile_id=user.id)
serializer = self.serializer_class(followers, many=True)
profiles = []
Expand All @@ -188,6 +215,8 @@ def get(self, request, username):
"first_name": profile.first_name,
"last_name": profile.last_name,
"bio": profile.bio,
"following": True,

})
if not profiles:
response = {
Expand Down

0 comments on commit 9f18f4a

Please sign in to comment.