-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user profile): upadate user profile
- 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
Showing
5 changed files
with
63 additions
and
19 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
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
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 |
---|---|---|
@@ -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) | ||
|
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
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 |
---|---|---|
@@ -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) |