From f570f917f503c61b2b4ae4e95a95cb6051db960f Mon Sep 17 00:00:00 2001 From: DGK Date: Sun, 3 Jul 2022 10:46:05 +0900 Subject: [PATCH 1/2] test : DRF-simplejwt SignUp test code --- DRF_simplejwt/settings.py | 6 ++++ users/tests.py | 68 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/DRF_simplejwt/settings.py b/DRF_simplejwt/settings.py index 1cac7fe..89e4b92 100644 --- a/DRF_simplejwt/settings.py +++ b/DRF_simplejwt/settings.py @@ -175,6 +175,12 @@ 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication', ), + 'DEFAULT_PARSER_CLASSES': [ + 'rest_framework.parsers.JSONParser', + 'rest_framework.parsers.FormParser', + 'rest_framework.parsers.MultiPartParser' + ], + 'TEST_REQUEST_DEFAULT_FORMAT': 'json', } PASSWORD_HASHERS = [ diff --git a/users/tests.py b/users/tests.py index 7ce503c..06a02da 100644 --- a/users/tests.py +++ b/users/tests.py @@ -1,3 +1,67 @@ -from django.test import TestCase +import json -# Create your tests here. +from rest_framework.test import APITestCase +from rest_framework_simplejwt.serializers import TokenObtainPairSerializer + +from users.models import User + +class UserSignUpTest(APITestCase): + + def test_success_user_signup(self): + data = { + 'email' : 'DGK-test-01@gmail.com', + 'nickname' : 'DGK-01', + 'password' : 'DGK12345678' + } + + response = self.client.post('/users/signup', data=json.dumps(data), content_type='application/json') + + self.assertEqual(response.status_code, 201) + self.assertEqual( + response.data, + { + 'email' : 'DGK-test-01@gmail.com', + 'nickname' : 'DGK-01' + } + ) + + +class UserSignInTest(APITestCase): + def setUp(self): + User.objects.create( + id = 1, + email = 'DGK-test@gmail.com', + nickname = 'DGK01', + password = 'DGK12345678' + ) + + def tearDown(self): + User.objects.all().delete() + + def test_success_user_signin(self): + data = { + 'email' : 'DGK-test@gmail.com', + 'nickname' : 'DGK01', + 'password' : 'DGK12345678' + } + + # print(User.objects.values()) + user = User.objects.get(email='DGK-test@gmail.com') + # print(user.id, user.nickname, user.email, user.password) + token = TokenObtainPairSerializer.get_token(user) + + refresh_token = str(token) + access_token = str(token.access_token) + + # signup_res = self.client.post('/users/signup', data=json.dumps(data), content_type='application/json') + signin_res = self.client.post('/users/signin', data=json.dumps(data), content_type='application/json') + + + # self.assertEqual(response.status_code, 200) + self.assertEqual( + signin_res.json(), + { + 'refresh' : refresh_token, + 'access' : access_token + } + ) \ No newline at end of file From a4cde9de5f8fcf1bedb60b1d0241c7574a713c2a Mon Sep 17 00:00:00 2001 From: DGK Date: Sun, 3 Jul 2022 16:11:49 +0900 Subject: [PATCH 2/2] test : DRF-simplejwt test code --- users/serializers.py | 4 ---- users/tests.py | 45 +++++++++++++++++--------------------------- users/urls.py | 2 +- users/views.py | 1 - 4 files changed, 18 insertions(+), 34 deletions(-) diff --git a/users/serializers.py b/users/serializers.py index 08eddd4..77bbdb5 100644 --- a/users/serializers.py +++ b/users/serializers.py @@ -5,7 +5,6 @@ from rest_framework import serializers from rest_framework.serializers import ModelSerializer from rest_framework_simplejwt.serializers import TokenObtainPairSerializer -from rest_framework_simplejwt.tokens import OutstandingToken, BlacklistedToken from users.models import User @@ -49,9 +48,6 @@ def validate(self, data): if not check_password(password, user.password): raise serializers.ValidationError('detail : invalid password') - - for token in OutstandingToken.objects.filter(user=user): - BlacklistedToken.objects.get_or_create(token=token) token = super().get_token(user) refresh_token = str(token) diff --git a/users/tests.py b/users/tests.py index 06a02da..f80bc8a 100644 --- a/users/tests.py +++ b/users/tests.py @@ -1,12 +1,14 @@ import json -from rest_framework.test import APITestCase -from rest_framework_simplejwt.serializers import TokenObtainPairSerializer +from rest_framework.test import APITestCase +from rest_framework_simplejwt.tokens import OutstandingToken from users.models import User class UserSignUpTest(APITestCase): + maxDiff = None + def test_success_user_signup(self): data = { 'email' : 'DGK-test-01@gmail.com', @@ -27,41 +29,28 @@ def test_success_user_signup(self): class UserSignInTest(APITestCase): - def setUp(self): - User.objects.create( - id = 1, - email = 'DGK-test@gmail.com', + + maxDiff = None + + @classmethod + def setUpTestData(cls): + User.objects.create_user( + email = 'DGK-test@gmail.com', nickname = 'DGK01', password = 'DGK12345678' ) - - def tearDown(self): - User.objects.all().delete() def test_success_user_signin(self): data = { - 'email' : 'DGK-test@gmail.com', + 'email' : 'DGK-test@gmail.com', 'nickname' : 'DGK01', 'password' : 'DGK12345678' } - # print(User.objects.values()) - user = User.objects.get(email='DGK-test@gmail.com') - # print(user.id, user.nickname, user.email, user.password) - token = TokenObtainPairSerializer.get_token(user) - - refresh_token = str(token) - access_token = str(token.access_token) - - # signup_res = self.client.post('/users/signup', data=json.dumps(data), content_type='application/json') - signin_res = self.client.post('/users/signin', data=json.dumps(data), content_type='application/json') + response = self.client.post('/users/signin', data=json.dumps(data), content_type='application/json') + user = User.objects.get(email='DGK-test@gmail.com') + token = OutstandingToken.objects.get(user=user).token - # self.assertEqual(response.status_code, 200) - self.assertEqual( - signin_res.json(), - { - 'refresh' : refresh_token, - 'access' : access_token - } - ) \ No newline at end of file + self.assertEqual(response.status_code, 200) + self.assertEqual(token, response.json()['refresh']) \ No newline at end of file diff --git a/users/urls.py b/users/urls.py index 4bcd07e..db08f5d 100644 --- a/users/urls.py +++ b/users/urls.py @@ -7,6 +7,6 @@ path('/signup', UserSignUpView.as_view()), path('/signin', UserSignInView.as_view()), path('/signout', UserSignOutView.as_view()), - path('/auth', UserinfoView.as_view()), + path('/auth-info', UserinfoView.as_view()), path('/token/refresh', TokenRefreshView.as_view()), ] diff --git a/users/views.py b/users/views.py index 037cf7d..fea3041 100644 --- a/users/views.py +++ b/users/views.py @@ -6,7 +6,6 @@ from drf_yasg import openapi from drf_yasg.utils import swagger_auto_schema -from users.models import User from users.serializers import SignUpSerializer, SignInSerializer class UserSignUpView(APIView):