Skip to content

Commit

Permalink
Merge fec87dc into 1295a27
Browse files Browse the repository at this point in the history
  • Loading branch information
a-braham committed May 5, 2019
2 parents 1295a27 + fec87dc commit e5ff691
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
7 changes: 6 additions & 1 deletion authors/apps/profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ class UserProfileSerializer(serializers.ModelSerializer):
username = serializers.ReadOnlyField(source='fetch_username')
img_url = serializers.ReadOnlyField(source='fetch_image')
created_at = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
updated_at = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")

class Meta:
model = Profile
fields = (
'username', 'first_name', 'last_name', 'bio', 'img_url', 'created_at'
'username', 'first_name', 'last_name', 'bio', 'img_url', 'created_at', 'updated_at'
)


class UpdateUserProfileSerializer(serializers.ModelSerializer):
"""
Serializer class for updating user profile
Expand All @@ -36,6 +39,8 @@ class FollowingSerializer(serializers.ModelSerializer):
class Meta:
model = Followers
fields = ('profile', 'followed')


class UserListSerializer(serializers.ModelSerializer):
"""
Serializer class for getting user profile
Expand Down
5 changes: 3 additions & 2 deletions authors/apps/profiles/tests/basetests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def setUp(self):
password="@Us3r.com"
)

self.user.is_verified=True
self.user.is_verified = True
self.user.save()

self.user1 = User.objects.create_user(
Expand All @@ -36,7 +36,7 @@ def setUp(self):
password="@Us3r.com"
)

self.user1.is_verified=True
self.user1.is_verified = True
self.user1.save()

def login_user(self, email="", password=""):
Expand Down Expand Up @@ -131,6 +131,7 @@ def update_user_profile_notexist(self, first_name="", last_name="", bio=""):
),
content_type="application/json"
)

def list_profiles(self):
"""
Method to get all profiles
Expand Down
20 changes: 12 additions & 8 deletions authors/apps/profiles/tests/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ def test_getting_profile_with_invalid_username(self):
self.is_authenticated("adam@gmail.com", "@Us3r.com")
response = self.get_profile_with_invalid_username()
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.data.get("error"), "User does not exist")
self.assertEqual(response.data.get("errors").get("user")[0], "User does not exist")

def test_update_profile_successfuly(self):
"""
Test method for successfully updtaing a user
"""
self.is_authenticated("adam@gmail.com", "@Us3r.com")
response = self.update_user_profile("Abraham", "Kamau", "I love history")
response = self.update_user_profile(
"Abraham", "Kamau", "I love history")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data.get("profile").get("last_name"), "Kamau")
self.assertEqual(response.data.get(
"profile").get("last_name"), "Kamau")

def test_update_profile_while_loggedout(self):
"""
Expand All @@ -42,7 +44,8 @@ def test_update_profile_while_loggedout(self):
response = self.update_user_profile(
"Abraham", "Kamau", "I love history")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(response.data.get("detail"), "Authentication credentials were not provided.")
self.assertEqual(response.data.get("detail"),
"Authentication credentials were not provided.")

def test_update_profile_while_not_owner(self):
"""
Expand All @@ -52,8 +55,9 @@ def test_update_profile_while_not_owner(self):
response = self.update_another_user_profile(
"Abraham", "Kamau", "I love history")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(response.data.get("error"), "You do not own this profile")

self.assertEqual(response.data.get("errors").get("user")[0],
"You do not own this profile")

def test_update_profile_while_not_exist(self):
"""
Test method for updating while user is not owner
Expand All @@ -62,7 +66,7 @@ def test_update_profile_while_not_exist(self):
response = self.update_user_profile_notexist(
"Abraham", "Kamau", "I love history")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.data.get("error"), "User does not exist")
self.assertEqual(response.data.get("errors").get("user")[0], "User does not exist")

def test_successfully_listing_profiles(self):
"""
Expand All @@ -80,4 +84,4 @@ def test_listing_profiles_with_unauthorised_user(self):
response = self.list_profiles()
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(response.data.get("detail"),
"Authentication credentials were not provided.")
"Authentication credentials were not provided.")
9 changes: 6 additions & 3 deletions authors/apps/profiles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from django.conf.urls import url
from .views import FollowAPI, FollowersAPI

from authors.apps.profiles.views import (UserProfileView, UpdateUserProfileView, UserListView)
from authors.apps.profiles.views import (
UserProfileView, UpdateUserProfileView, UserListView)

from authors.apps.profiles.views import (UserProfileView, UpdateUserProfileView)
from authors.apps.profiles.views import (
UserProfileView, UpdateUserProfileView)

app_name = 'profiles'

Expand All @@ -15,6 +17,7 @@
UpdateUserProfileView.as_view(), name='update_profile'),
path('profiles/<username>/followers/', FollowersAPI.as_view()),
path('profiles/<username>/follow/', FollowAPI.as_view()),
path('profiles/<str:username>/', UpdateUserProfileView.as_view(), name='update_profile'),
path('profiles/<str:username>/',
UpdateUserProfileView.as_view(), name='update_profile'),
path('profiles/', UserListView.as_view(), name='list_users'),
]
27 changes: 18 additions & 9 deletions authors/apps/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ def get(self, request, username):
try:
profile = Profile.objects.get(user__username=username)
except Exception:
return Response({
"error": "User does not exist"
}, status = status.HTTP_404_NOT_FOUND)
return Response({
'errors': {
'user': ['User does not exist']
}
}, status=status.HTTP_404_NOT_FOUND)

serializer = UserProfileSerializer(
profile, context={'request': request}
Expand All @@ -38,6 +40,7 @@ def get(self, request, username):
'profile': serializer.data
}, status=status.HTTP_200_OK)


class UpdateUserProfileView(GenericAPIView):
"""
A class for updating user profile
Expand All @@ -52,20 +55,24 @@ def patch(self, request, username):
try:
profile = Profile.objects.get(user__username=username)
except Exception:
return Response({
"error": "User does not exist"
}, status = status.HTTP_404_NOT_FOUND)
return Response({
'errors': {
'user': ['User does not exist']
}
}, status=status.HTTP_404_NOT_FOUND)
user_name = request.user.username
if user_name != username:
return Response({
'error': 'You do not own this profile'
'errors': {
'user': ['You do not own this profile']
}
}, status=status.HTTP_403_FORBIDDEN)

data = request.data

serializer = UpdateUserProfileSerializer(
instance=request.user.profile,
data=data,
data=data,
partial=True
)
serializer.is_valid()
Expand Down Expand Up @@ -189,6 +196,8 @@ def get(self, request, username):
response = {
'count': len(profiles), 'current_followers': profiles}
return Response(response, status=status.HTTP_200_OK)


class UserListView(GenericAPIView):
"""
A class for getting all user profiles
Expand All @@ -200,4 +209,4 @@ def get(self, request):
serializer = UserListSerializer(queryset, many=True)
return Response({
'profiles': serializer.data
}, status=status.HTTP_200_OK)
}, status=status.HTTP_200_OK)

0 comments on commit e5ff691

Please sign in to comment.