Skip to content

Commit

Permalink
🔀 Merge pull request #15 from snuhcs-course/feat/test-backend
Browse files Browse the repository at this point in the history
User related API unit tests
  • Loading branch information
sukchan-0811 authored Oct 30, 2023
2 parents 1c9890a + 808d6d9 commit a8642f4
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 67 deletions.
22 changes: 11 additions & 11 deletions backend/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ def get_secret(setting, secrets=secrets):
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

# Local DB settings
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'sqlite3_for_dev'),
# }
# }

# Remote DB settings
# Add your own confidential.py
if not ("GITHUB_ACTIONS" in os.environ):
if "GITHUB_ACTIONS" in os.environ:
# Local DB settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'sqlite3_for_dev'),
}
}
else:
# Remote DB settings
# Add your own confidential.py
import confidential
DATABASES = confidential.DATABASES

Expand Down
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
206 changes: 151 additions & 55 deletions backend/user/test_views.py
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)




0 comments on commit a8642f4

Please sign in to comment.