Skip to content

Commit

Permalink
feat(user profile): upadate user profile
Browse files Browse the repository at this point in the history
- edit user profile without the user entering the user_id
- Write tests for editing user profile

[Finishes]
  • Loading branch information
mulondo moses authored and Innocent Asiimwe committed Dec 11, 2018
1 parent 181af3c commit 9a40b3b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rest_framework.test import APITestCase, APIRequestFactory
from rest_framework.test import APITestCase, APIRequestFactory, APIClient
from rest_framework import status
import json
from rest_framework.reverse import reverse
Expand All @@ -25,6 +25,7 @@ def setUp(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
user =User.objects.get()
self.assertTrue(user.is_verified)
# self.client =

def account_verification(self, token, uid):
request = APIRequestFactory().get(
Expand All @@ -43,4 +44,5 @@ def test_api_can_list_all_registered_users(self):
self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertEqual(body['authors'][0]['username'], 'Jack Sparrow')
self.assertEqual(body['authors'][0]['email'], 'sparrow@gmail.com')


2 changes: 1 addition & 1 deletion authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def get(self, request):
serializer = self.serializer_class(users, many=True)
response = {'authors': serializer.data}
for author, obj in zip(response['authors'], User.objects.all()):
author['profile'] = reverse('profile', args=[obj.id], request=request)
author['profile'] = reverse('profile', request=request)
del author['token']
return JsonResponse(response, status=200, safe=False)

59 changes: 44 additions & 15 deletions authors/apps/profiles/test_profile.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,58 @@
from rest_framework.test import APITestCase
from rest_framework import status
from django.urls import path, reverse
from rest_framework.test import APITestCase, APIRequestFactory, APIClient
from ..authentication.views import RegistrationAPIView, AccountVerified
from ..authentication.models import User
from ..authentication.backends import JWTAuthentication


class TestUserProfile(APITestCase):

def setUp(self):
self.reg_user=0
self.user_data = {"user": { "username":"minime", "email": "alexkayabula@gmail.com", "password":"W123456/78"}}
self.url = reverse("registration")
self.client = APIClient()
self.client.post(self.url, self.user_data, format='json')
self.request = APIRequestFactory().post(
reverse("registration")
)
user = User.objects.get()
request = self.request
token, uid = RegistrationAPIView.generate_token(user, request)
response = self.account_verification(token, uid)
self.assertEqual(response.status_code, status.HTTP_200_OK)
user = User.objects.get()
self.assertTrue(user.is_verified)

response = self.client.post('/api/users/login/',self.user_data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.login_token = response.data['token']
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.login_token)



def account_verification(self, token, uid):
request = APIRequestFactory().get(
reverse("verify_account", kwargs={"token": token, "uid":uid})
)
verify = AccountVerified.as_view()
response = verify(request, token=token, uid=uid)
return response

def test_get_a_list_of_userprofiles(self):


response = self.client.get('/api/users/profiles', format='json')

def test_get_a_list_of_userprofiles(self):
response = self.client.get('/api/users/profiles',format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)



def test_update_profile(self):
data = {"user": {"username": "peter", "email": "peter@gmail.com", "password": "Peter1234.@"}}
update_data = {
"photo": "",
"bio": "I have been a programmer since 1995",
"fun_fact": "eating"
update_data = {"bio":"I have been doing programming since 1995",
"fun_fact":"I love dancing"
}

self.client.post('/api/users/', data, format='json')
usr = self.client.get('/api/users/profiles').json()
for u in usr:
self.reg_user = u['user']

response = self.client.put('/api/users/profiles/{}'.format(self.reg_user) + '/', update_data, format ='json')
response = self.client.put('/api/users/profiles/', update_data, format ='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)

3 changes: 2 additions & 1 deletion authors/apps/profiles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

urlpatterns = [
path('users/profiles', UserProfiles.as_view()),
path('users/profiles/<int:user_id>/', Updateprofile.as_view(), name='profile')
path('users/profiles/', Updateprofile.as_view(),name="profile")

]
14 changes: 13 additions & 1 deletion authors/apps/profiles/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
from django.shortcuts import render
from .serializers import GetUserProfileSerializer, UpdateProfileSerializer
from rest_framework import generics, viewsets
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework import status
from .models import UserProfile


class UserProfiles(generics.ListAPIView):
permission_classes = (IsAuthenticated,)
queryset = UserProfile.objects.all()
serializer_class = GetUserProfileSerializer


class Updateprofile(generics.RetrieveUpdateAPIView):
permission_classes = (IsAuthenticated,)
queryset = UserProfile.objects.all()
serializer_class = UpdateProfileSerializer
lookup_field = 'user_id'

def update(self,request):
serializer =self.serializer_class(request.user.userprofile, data=request.data)
serializer.is_valid(raise_exception=True)
serializer.update(request.user.userprofile, request.data)
return Response(serializer.data)

0 comments on commit 9a40b3b

Please sign in to comment.