Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ppl-fasilkom-ui
2020
PPL-C
PPTI-Mobile Apps Monitoring Wabah Tuberkolosis
Neza-Backend
Commits
4af8c74b
Commit
4af8c74b
authored
Apr 07, 2020
by
Jonathan Christopher Jakub
Browse files
Implement Profile Endpoint
parent
78571aa8
Changes
2
Hide whitespace changes
Inline
Side-by-side
apps/accounts/tests/test_units/test_accounts.py
View file @
4af8c74b
...
...
@@ -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
)
apps/accounts/views.py
View file @
4af8c74b
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
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment