Skip to content

Commit

Permalink
[feat-3] Shortcut 데코레이터 테스트 케이스 추가 (#14)
Browse files Browse the repository at this point in the history
* [feat-8] Unknown Exception에 대하여 Issue 링크 추가 (#11)

* 데코레이터 기반 테스트 케이스 추가

* 데코레이터 기반 sample view 추가

- 주석 영어로 작성하도록 수정
  • Loading branch information
iml1111 committed Jul 24, 2022
1 parent 7d1908b commit 02f0a0c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
32 changes: 26 additions & 6 deletions tests/sample/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from django_jwt_extended import get_jwt


# 로그인 및 토큰 발급하기
# Login and issue tokens
def login(request):
return JsonResponse({
"access_token": create_access_token("iml"),
'refresh_token': create_refresh_token('iml'),
})

# 토큰 리프레시
# Refresh tokens
@jwt_required(refresh=True)
def refresh(request):
identity = get_jwt_identity(request)
Expand All @@ -26,7 +26,7 @@ def refresh(request):
})


# 로그인 인증 테스트
# Authentication access token
@jwt_required()
def user(request):
identity = get_jwt_identity(request)
Expand All @@ -36,8 +36,28 @@ def user(request):
'raw_jwt': payload,
})

# Authentication access token with Decorator
def login_required(func):
@jwt_required()
def wrapper(request, **path):
identity = get_jwt_identity(request)
request.META['logined_identity'] = identity
return func(request, **path)
return wrapper


@login_required
def decorator_user(request):
identity = request.META['logined_identity']
payload = get_jwt(request)

return JsonResponse({
'id': identity,
'raw_jwt': payload,
})


# 옵셔널 로그인 인증 테스트
# Optional Login test
@jwt_required(optional=True)
def user_optional(request):
identity = get_jwt_identity(request)
Expand All @@ -48,7 +68,7 @@ def user_optional(request):
})


# Rest framework 테스트
# Rest framework test
class RestAPIView(APIView):

@jwt_required()
Expand Down Expand Up @@ -88,7 +108,7 @@ def delete(self, request):
})


# Rest framework Func 테스트
# Rest framework Func test
@api_view(['GET', 'POST', 'PUT', 'DELETE'])
@jwt_required()
def rest_user(request, hello: str):
Expand Down
13 changes: 12 additions & 1 deletion tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.test import RequestFactory
from django.apps import apps
from tests.sample.views import (
login, refresh,
login, decorator_user, refresh,
user, user_optional,
RestAPIView, rest_user
)
Expand Down Expand Up @@ -35,6 +35,17 @@ def test_auth_basic(self):
response = user(request)
self.assertEqual(response.status_code, 200)

def test_auth_decorator(self):
"""Run Authentication basic with Decorator"""
request = self.factory.get(
'/user',
HTTP_Authorization=(
"Bearer " + self.access_token
)
)
response = decorator_user(request)
self.assertEqual(response.status_code, 200)

def test_refresh(self):
"""Run Token Refresh & reauth"""
request = self.factory.get(
Expand Down

0 comments on commit 02f0a0c

Please sign in to comment.