ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • TIL.85 Unit_test_실습_2
    TIL 2021. 1. 1. 13:58
    728x90

    지난 실습 코드로 아래와 같은 테스트 코드를 작성해보았다.

    역시 처음이 어렵지 한번 하고 나니 어느정도 감이 잡힌다.

     

    추후 뷰 작성시에도 테스트 코드는 필수이므로 이번 프로젝트에서의 목표는 소셜 로그인과 테스트코드로 정했다!!

    import unittest, json, jwt, bcrypt
    from datetime    import datetime, timedelta
    from django.test import TestCase, Client
    from user.models import User, Gender, Country
    from my_settings import SECRET_KEY, JWT_ALGORITHM
    
    class SignUpTestCase(unittest.TestCase):
        def setUp(self):
             self.gender     = Gender.objects.create(name='male')
             self.country    = Country.objects.create(name='대한민국')
             hashed_password = bcrypt.hashpw('123456aA!'.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
    
             User.objects.create(
                     id            = 1,
                     korean_name   = '문타리',
                     english_name  = 'munshee',
                     email         = 'applee24@gmail.com',
                     password      = hashed_password,
                     date_of_birth = '1992-04-24',
                     phone_number  = '010-9999-1234',
                     gender_id     = self.gender.id,
                     country_id    = self.country.id
                     )
    
        def tearDown(self):
            Gender.objects.all().delete()
            Country.objects.all().delete()
            User.objects.all().delete()
            print('end')
    
        def test_user_post_signup_success(self):
            client = Client()
            user = {
                    'korean_name'   : '김애플',
                    'english_name'  : 'kimapple',
                    'email'         : 'apple92@gmail.com',
                    'password'      : '123456aA!',
                    'date_of_birth' : '1992-04-24',
                    'phone_number'  : '010-1234-1234',
                    'gender'        : 'male',
                    'country'       : '대한민국'
                    }
            response = client.post('/user/signup', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 201)
            self.assertEqual(response.json(),
                {
                    'MESSAGE':'SUCCESS'
                }
            )
    
        def test_user_post_signup_emailformat_validation(self):
            client = Client()
            user = {
                    'korean_name'   : '김애플',
                    'english_name'  : 'kimapple',
                    'email'         : 'apple92gmail.com',
                    'password'      : '123456aA!',
                    'date_of_birth' : '1992-04-24',
                    'phone_number'  : '010-1234-1234',
                    'gender'        : 'male',
                    'country'       : '대한민국'
                    }
            response = client.post('/user/signup', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 401)
            self.assertEqual(response.json(),
                {
                    'MESSAGE' : 'INCORRECT_EMAIL_FORMAT'
                }
            )
    
        def test_user_post_signup_password_validation(self):
            client = Client()
            user = {
                    'korean_name'   : '김애플',
                    'english_name'  : 'kimapple',
                    'email'         : 'apple92@gmail.com',
                    'password'      : '123456',
                    'date_of_birth' : '1992-04-24',
                    'phone_number'  : '010-1234-1234',
                    'gender'        : 'male',
                    'country'       : '대한민국'
                    }
            response = client.post('/user/signup', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 401)
            self.assertEqual(response.json(),
                {
                    'MESSAGE' : 'INCORRECT_PASSWORD_FORMAT'
                }
            )
    
        def test_user_post_signup_email_exists_validation(self):
            client = Client()
            user = {
                    'korean_name'   : '김애플',
                    'english_name'  : 'kimapple',
                    'email'         : 'applee24@gmail.com',
                    'password'      : '123456aA!',
                    'date_of_birth' : '1992-04-24',
                    'phone_number'  : '010-1234-1234',
                    'gender'        : 'male',
                    'country'       : '대한민국'
                    }
            response = client.post('/user/signup', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 401)
            self.assertEqual(response.json(),
                {
                    'MESSAGE' : 'ALREADY_EXISTS_EMAIL'
                }
            )
    
        def test_user_post_signup_phone_number_exists_validation(self):
            client = Client()
            user = {
                    'korean_name'   : '김애플',
                    'english_name'  : 'kimapple',
                    'email'         : 'applee99@gmail.com',
                    'password'      : '123456aA!',
                    'date_of_birth' : '1992-04-24',
                    'phone_number'  : '010-9999-1234',
                    'gender'        : 'male',
                    'country'       : '대한민국'
                    }
            response = client.post('/user/signup', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 401)
            self.assertEqual(response.json(),
                {
                    'MESSAGE' : 'ALREADY_EXISTS_PHONE_NUMBER'
                }
            )
    
        def test_user_post_signup_key_error(self):
            client = Client()
            user = {
                    'korean_name'   : '김애플',
                    'email'         : 'applee24@gmail.com',
                    'password'      : '123456aA!',
                    'date_of_birth' : '1992-04-24',
                    'phone_number'  : '010-1234-1234',
                    'gender'        : 'male',
                    'country'       : '대한민국'
                    }
            response = client.post('/user/signup', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 400)
            self.assertEqual(response.json(),
                {
                    'MESSAGE' : 'KEY_ERROR'
                }
            )
    
        def test_user_post_signup_gender_does_not_exist_error(self):
            client = Client()
            user = {
                    'korean_name'   : '김애플',
                    'english_name'  : 'pineapple',
                    'email'         : 'poewee@gmail.com',
                    'password'      : '123456aA!',
                    'date_of_birth' : '1992-04-24',
                    'phone_number'  : '010-1002-1234',
                    'gender'        : 'mmal',
                    'country'       : '대한민국'
                    }
            response = client.post('/user/signup', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 401)
            self.assertEqual(response.json(),
                {
                    'MESSAGE' : 'DOES_NOT_EXIST_GENDER'
                }
            )
    
        def test_user_post_signin_success(self):
            client = Client()
            user = {
                    'email'    : 'applee24@gmail.com',
                    'password' : '123456aA!'
                    }
    
            self.payload      = {'user-id' : 1, 'exp' : datetime.now() +timedelta(hours=1)}
            self.access_token = jwt.encode(self.payload, SECRET_KEY, algorithm=JWT_ALGORITHM)
            
            response = client.post('/user/signin', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.json(),
                {
                    'access_token' : self.access_token
                }
            )
    
        def test_user_post_signin_emailformat_validation(self):
            client = Client()
            user = {
                    'email'    : 'applee24@gmailcom',
                    'password' : '123456aA!'
                    }
    
            response = client.post('/user/signin', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 401)
            self.assertEqual(response.json(),
                {
                    'MESSAGE' : 'INCORRECT_EMAIL_FORMAT'
                }
            )
    
        def test_user_post_signin_user_email_validation(self):
            client = Client()
            user = {
                    'email'    : 'muntroo@gmail.com',
                    'password' : '122256aA!'
                    }
    
            response = client.post('/user/signin', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 401)
    
        def test_user_post_signin_user_password_validation(self):
            client = Client()
            user = {
                    'email'    : 'applee24@gmail.com',
                    'password' : '123444aA!'
                    }
    
            response = client.post('/user/signin', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 401)
    
        def test_user_post_signin_user_information_validation(self):
            client = Client()
            user = {
                    'email'    : 'muntrock@gmail.com',
                    'password' : '123444aA!'
                    }
    
            response = client.post('/user/signin', json.dumps(user), content_type='application/json')
            self.assertEqual(response.status_code, 401)
            self.assertEqual(response.json(),
                {
                    'MESSAGE' : 'DOES_NOT_EXIST_USER'
                }
            )
    728x90

    'TIL' 카테고리의 다른 글

    TIL.91 Javascript_Filter 함수  (0) 2021.01.07
    TIL.86 Mocking, Patching  (0) 2021.01.02
    TIL.84 Unit Test_실습  (0) 2020.12.31
    TIL.83 Unit Test  (0) 2020.12.30
    TIL.82 Git rebase  (0) 2020.12.30
Designed by Tistory.