Fakultas Ilmu Komputer UI

Commit 2a2fb16f authored by Jonathan Christopher Jakub's avatar Jonathan Christopher Jakub
Browse files

Merge branch 'profile-endpoint' into 'staging'

Implement Profile Endpoint

See merge request !24
parents 78571aa8 4af8c74b
Pipeline #39176 passed with stages
in 25 minutes and 25 seconds
......@@ -5,8 +5,8 @@ from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase, APIClient
from apps.accounts.models import Account
from apps.accounts.tests.factories.accounts import AccountFactory, UserFactory
from apps.accounts.models import Account
from apps.constants import (
HEADER_PREFIX,
ACTIVITY_TYPE_CREATE,
......@@ -19,6 +19,7 @@ class AccountViewTest(APITestCase):
@classmethod
def setUpTestData(self):
self.BASE_URL = "/accounts/"
self.PROFILE_URL = self.BASE_URL + "me/"
self.user_1 = UserFactory(username="user_1", password="justpass")
self.user_2 = UserFactory(username="user_2", password="justpass")
......@@ -50,7 +51,6 @@ class AccountViewTest(APITestCase):
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"detail":"Invalid page."', response_string)
def test_list_all_accounts_filter_success(self):
......@@ -60,7 +60,6 @@ class AccountViewTest(APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":1', response_string)
def test_list_all_accounts_filter_failed(self):
......@@ -70,7 +69,6 @@ class AccountViewTest(APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":0', response_string)
def test_retrieve_account_success(self):
......@@ -220,3 +218,29 @@ class AccountViewTest(APITestCase):
self.assertIn(
'"action_type":"{}"'.format(ACTIVITY_TYPE_DELETE), response_string
)
def test_retrieve_current_profile_success(self):
url = self.PROFILE_URL
response = self.client.get(url)
data = {
"id": str(self.admin.id),
"name": self.admin.name,
"username": self.admin.user.username,
"email": self.admin.email,
"phone_number": self.admin.phone_number,
"area": self.admin.area,
"is_admin": True,
"is_verified": False,
"is_active": True,
}
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertJSONEqual(json.dumps(response.data), data)
def test_retrieve_current_profile_fails_without_login(self):
url = self.PROFILE_URL
self.client = APIClient()
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status, viewsets
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.pagination import PageNumberPagination
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.response import Response
from apps.accounts.filters import AccountFilter
from apps.accounts.models import Account
from apps.accounts.serializers import (
AccountSerializer,
AccountRegisterSerializer,
)
from apps.accounts.filters import AccountFilter
from apps.commons.permissions import (
IsSelfOrAdministrator,
CreateOnly,
)
from apps.logs.models import Log
from apps.constants import (
MODEL_NAME_ACCOUNT,
ACTIVITY_TYPE_CREATE,
ACTIVITY_TYPE_EDIT,
ACTIVITY_TYPE_DELETE,
)
from apps.logs.models import Log
class AccountViewSet(viewsets.ViewSet):
......@@ -36,12 +37,14 @@ class AccountViewSet(viewsets.ViewSet):
filtered_set = AccountFilter(request.GET, queryset=self.queryset).qs
context = paginator.paginate_queryset(filtered_set, request)
serializer = AccountSerializer(context, many=True)
return paginator.get_paginated_response(serializer.data)
def retrieve(self, request, pk=None):
instance = get_object_or_404(self.queryset, pk=pk)
self.check_object_permissions(request, instance)
serializer = AccountSerializer(instance)
return Response(serializer.data, status=status.HTTP_200_OK)
def create(self, request):
......@@ -52,7 +55,6 @@ class AccountViewSet(viewsets.ViewSet):
password = serializer.validated_data.pop("password")
user = User.objects.create_user(username=username, password=password)
account = Account.objects.create(user=user, **serializer.validated_data)
# Add account creation log
......@@ -96,6 +98,15 @@ class AccountViewSet(viewsets.ViewSet):
action_type=ACTIVITY_TYPE_DELETE,
author=request.user.account,
)
serializer = AccountSerializer(instance=instance)
return Response(serializer.data, status=status.HTTP_200_OK)
@action(detail=False, methods=["get"], url_path="me")
def profile(self, request):
user = request.user
instance = Account.objects.get(user=user)
self.check_object_permissions(request, instance)
serializer = AccountSerializer(instance)
return Response(serializer.data, status=status.HTTP_200_OK)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment