from rest_framework.test import APITestCase
from django.urls import reverse
from rest_framework import status
from user.models import User
# Create your tests here.
class UserRegistrationAPIViewTestCase(APITestCase):
def test_registration(self):
url = reverse("sign_up_view")
user_data = {
"username":"testuser",
"account":"tester",
"email":"[email protected]",
"password":"popk1214",
}
response = self.client.post(url, user_data)
self.assertEqual(response.status_code, 201)
# def test_token_login(self):
# url = reverse("token_obtain_pair")
# user_data = {
# "username":"testuser",
# "account":"tester",
# "email":"[email protected]",
# "password":"popk1214",
# }
# response = self.client.post(url, user_data)
# print(response.data)
# self.assertEqual(response.status_code, 200)
class LoginUserTest(APITestCase):
def setUp(self):
self.data = {'email':'[email protected]','account':'tester','username':'testuser','password':'popk1214',}
self.user = User.objects.create_user('[email protected]','tester', 'testuser','popk1214')
def test_login(self):
response = self.client.post(reverse('token_obtain_pair'), self.data)
self.assertEqual(response.status_code, 200)
def test_get_user_data(self):
access_token = self.client.post(reverse('token_obtain_pair'), self.data).data['access']
response = self.client.get(
path=reverse("user_view"),
HTTP_AUTHORIZATION=f"Bearer {access_token}"
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['account'], self.data['account'])
# self.data는 34번째줄에서 셋업 데이터를 의미함.