-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #15 from snuhcs-course/feat/test-backend
User related API unit tests
- Loading branch information
Showing
3 changed files
with
164 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ pycparser==2.21 | |
pytz==2023.3.post1 | ||
six==1.16.0 | ||
sqlparse==0.4.4 | ||
PyJWT==2.8.0 | ||
PyJWT==2.8.0 | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,151 @@ | ||
from unittest import TestCase | ||
|
||
|
||
class TestUserSignUpView(TestCase): | ||
def test_post(self): | ||
self.assertEqual(1, 1) | ||
|
||
# class TestSignUpEmailVerifySendView(TestCase): | ||
# def test_post(self): | ||
# self.fail() | ||
# | ||
# | ||
# class TestPasswordEmailVerifySendView(TestCase): | ||
# def test_post(self): | ||
# self.fail() | ||
# | ||
# | ||
# class TestSignUpEmailVerifyAcceptView(TestCase): | ||
# def test_post(self): | ||
# self.fail() | ||
# | ||
# | ||
# class TestPasswordEmailVerifyAcceptView(TestCase): | ||
# def test_post(self): | ||
# self.fail() | ||
# | ||
# | ||
# class TestUserLoginView(TestCase): | ||
# def test_post(self): | ||
# self.fail() | ||
# | ||
# | ||
# class TestUserLogoutView(TestCase): | ||
# def test_post(self): | ||
# self.fail() | ||
# | ||
# | ||
# class TestUserProfileView(TestCase): | ||
# def test_get(self): | ||
# self.fail() | ||
# | ||
# | ||
# class TestPasswordUpdateView(TestCase): | ||
# def test_patch(self): | ||
# self.fail() | ||
# | ||
# | ||
# class TestNicknameUpdateView(TestCase): | ||
# def test_patch(self): | ||
# self.fail() | ||
# | ||
# | ||
# class TestUserWithdrawView(TestCase): | ||
# def test_post(self): | ||
# self.fail() | ||
from django.test import TestCase | ||
from rest_framework import status | ||
from rest_framework.utils import json | ||
|
||
from user.models import User, EmailVerification | ||
|
||
|
||
class UserTest(TestCase): | ||
|
||
def setUp(self): | ||
self.user = User.objects.create_user( | ||
email='[email protected]', | ||
password='test_password', | ||
nickname='test_nickname' | ||
) | ||
self.tokens = self.login_and_get_tokens() | ||
self.access_token = self.tokens.get('access') | ||
self.refresh_token = self.tokens.get('refresh') | ||
|
||
def login_and_get_tokens(self): | ||
data = { | ||
'email': '[email protected]', | ||
'password': 'test_password', | ||
} | ||
response = self.client.post('/user/login/', data) | ||
if response.status_code == status.HTTP_200_OK: | ||
tokens = response.json() | ||
return tokens | ||
return None | ||
|
||
def test_signup_success(self): | ||
data = { | ||
'email': '[email protected]', | ||
'password': 'this_is_test', | ||
'nickname': 'this_is_test' | ||
} | ||
response = self.client.post('/user/signup/', data) | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
def test_login_success(self): | ||
data = { | ||
'email': '[email protected]', | ||
'password': 'test_password', | ||
} | ||
response = self.client.post('/user/login/', data) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_get_profile_success(self): | ||
headers = { | ||
'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' | ||
} | ||
response = self.client.get('/user/profile/', **headers) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
data = response.json()['user'] | ||
self.assertEqual(self.user.email, data['email']) | ||
self.assertEqual(self.user.nickname, data['nickname']) | ||
|
||
def test_logout_success(self): | ||
headers = { | ||
'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' | ||
} | ||
data = { | ||
'refresh': self.refresh_token | ||
} | ||
response = self.client.post('/user/logout/', data, **headers) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_refresh_success(self): | ||
data = { | ||
'refresh': self.refresh_token | ||
} | ||
response = self.client.post('/user/refresh/', data) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_withdraw_success(self): | ||
headers = { | ||
'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' | ||
} | ||
data = { | ||
'refresh': self.refresh_token | ||
} | ||
response = self.client.post('/user/withdraw/', data, **headers) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_change_nickname_success(self): | ||
headers = { | ||
'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' | ||
} | ||
data = { | ||
'nickname': 'changed_name' | ||
} | ||
response = self.client.patch('/user/profile/nickname/', data, content_type='application/json', **headers) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
# Call get-profile API to check whether the value change is applied | ||
response = self.client.get('/user/profile/', **headers) | ||
self.assertEqual(data['nickname'], response.json()['user']['nickname']) | ||
|
||
def test_change_password_success(self): | ||
headers = { | ||
'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' | ||
} | ||
data = { | ||
'password': 'changed_pw' | ||
} | ||
self.client.patch('/user/profile/password/', data, content_type='application/json', **headers) | ||
|
||
new_data = { | ||
'email': '[email protected]', | ||
'password': data['password'], | ||
} | ||
response = self.client.post('/user/login/', new_data) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_email_verification_signup_success(self): | ||
data = { | ||
'email': '[email protected]' | ||
} | ||
# Sending | ||
response = self.client.post('/user/validateemail/signup/send/', data) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
# Accepting | ||
email_veri = EmailVerification.objects.get(email=data['email']) | ||
verify_data = { | ||
'email': '[email protected]', | ||
'code': email_veri.code | ||
} | ||
response = self.client.post('/user/validateemail/signup/accept/', verify_data) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_email_verification_pw_success(self): | ||
data = { | ||
'email': '[email protected]' | ||
} | ||
# Sending | ||
response = self.client.post('/user/validateemail/pw/send/', data) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
# Accepting | ||
email_veri = EmailVerification.objects.get(email=data['email']) | ||
verify_data = { | ||
'email': '[email protected]', | ||
'code': email_veri.code | ||
} | ||
response = self.client.post('/user/validateemail/pw/accept/', verify_data) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
|
||
|
||
|