Skip to content

Commit

Permalink
feat(profile) add check for getting profile if it exists
Browse files Browse the repository at this point in the history
This commit adds a check if the profile one is trying to get exists. Adds ProfileDoesNotExist class to raise exception if it does not exist. It also removes update function from ProfileRetrieveView because it was not being used anywhere. Also adds test for this check

[Delivers #162163267]
  • Loading branch information
SokoPaulSokool committed Dec 8, 2018
1 parent 014d6b0 commit e727e0d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ omit =
*/venv/*
*/.vscode/*
*__init__*
*/test/*

[run]
branch = true
Expand Down
6 changes: 6 additions & 0 deletions authors/apps/profiles/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from rest_framework.exceptions import APIException


class ProfileDoesNotExist(APIException):
status_code = 400
default_detail = 'Profile does not exist.'
8 changes: 8 additions & 0 deletions authors/apps/profiles/test/test_create_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def test_get_profile(self):
"soko"
)

def test_get_non_existing_profile(self):
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.create_testing_user())
response = self.client.get('/api/profiles/sokop')
self.assertEqual(
json.loads(response.content)['profile']['detail'],
"Profile does not exist."
)

def test_update_profile(self):
data = {
"user":{
Expand Down
13 changes: 3 additions & 10 deletions authors/apps/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .serializers import ProfileSerializer
from .models import Profile
from .renderers import ProfileJSONRenderer
from .exceptions import ProfileDoesNotExist


class ProfileRetrieveView(RetrieveAPIView):
Expand All @@ -19,23 +20,15 @@ class ProfileRetrieveView(RetrieveAPIView):

def retrieve(self, request, *args, **kwargs):
username = self.kwargs['slug']

try:
profile = Profile.objects.select_related('user').get(
user__username=username
)
except Profile.DoesNotExist:
raise
raise ProfileDoesNotExist

serializer = self.serializer_class(profile)

return Response(serializer.data, status=status.HTTP_200_OK)

def update(self, request, *args, **kwargs):
serializer_data = request.data.get('data', {})
serializer = self.serializer_class(
request.data, data=serializer_data, partial=True
)
serializer.is_valid(raise_exception=True)
serializer.save()

return Response(serializer.data, status=status.HTTP_200_OK)

0 comments on commit e727e0d

Please sign in to comment.