<filename>user/tests/test_user_api.py
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient

User = get_user_model()


class UserApiTestCase(TestCase):

    def setUp(self):
        self.client = APIClient()

    def test_create_valid_user(self):
        user_data = {
            'username': 'username',
            'email': '<EMAIL>',
            'password': 'password',
        }
        r = self.client.post(reverse('user-list'), user_data)
        self.assertEqual(r.status_code, status.HTTP_201_CREATED)
        self.assertEqual(r.data['username'], user_data['username'])
        self.assertEqual(r.data['email'], user_data['email'])
        self.assertNotIn('password', r.data)
        pk = r.data.get(User._meta.pk.name)
        user = User.objects.get(pk=pk)
        self.assertTrue(user.check_password(user_data['password']))

    def test_create_duplicated_user(self):
        user_data = {
            'username': 'username',
            'email': '<EMAIL>',
            'password': 'password',
        }
        r1 = self.client.post(reverse('user-list'), user_data)
        self.assertEqual(r1.status_code, status.HTTP_201_CREATED)
        r2 = self.client.post(reverse('user-list'), user_data)
        self.assertEqual(r2.status_code, status.HTTP_400_BAD_REQUEST)

    def test_create_password_too_short_user(self):
        user_data = {
            'username': 'username',
            'email': '<EMAIL>',
            'password': '<PASSWORD>',
        }
        r = self.client.post(reverse('user-list'), user_data)
        self.assertEqual(r.status_code, status.HTTP_400_BAD_REQUEST)
        with self.assertRaises(User.DoesNotExist):
            User.objects.get(username=user_data['username'])

    def test_list_users_without_auth(self):
        r = self.client.get(reverse('user-list'))
        self.assertEqual(r.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_list_users_can_see_self_only(self):
        user_data = {
            'username': 'username',
            'email': '<EMAIL>',
            'password': 'password',
        }
        user = User.objects.create(**user_data)
        self.client.force_authenticate(user=user)
        r = self.client.get(reverse('user-list'))
        self.assertEqual(r.status_code, status.HTTP_200_OK)
        self.assertEqual(len(r.data), 1)
        self.assertEqual(r.data[0]['username'], user_data['username'])

    def test_me(self):
        user_data = {
            'username': 'username',
            'email': '<EMAIL>',
            'password': 'password',
        }
        user = User.objects.create(**user_data)
        self.client.force_authenticate(user=user)
        r = self.client.get(reverse('user-me'))
        self.assertEqual(r.status_code, status.HTTP_200_OK)
        self.assertEqual(r.data['username'], user_data['username'])
        self.assertEqual(r.data['email'], user_data['email'])
        self.assertNotIn('password', r.data)

<filename>user/tests/test_user_api.py
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient

User = get_user_model()


class UserApiTestCase(TestCase):

    def setUp(self):
        self.client = APIClient()

    def test_create_valid_user(self):
        user_data = {
            'username': 'username',
            'email': '<EMAIL>',
            'password': 'password',
        }
        r = self.client.post(reverse('user-list'), user_data)
        self.assertEqual(r.status_code, status.HTTP_201_CREATED)
        self.assertEqual(r.data['username'], user_data['username'])
        self.assertEqual(r.data['email'], user_data['email'])
        self.assertNotIn('password', r.data)
        pk = r.data.get(User._meta.pk.name)
        user = User.objects.get(pk=pk)
        self.assertTrue(user.check_password(user_data['password']))

    def test_create_duplicated_user(self):
        user_data = {
            'username': 'username',
            'email': '<EMAIL>',
            'password': 'password',
        }
        r1 = self.client.post(reverse('user-list'), user_data)
        self.assertEqual(r1.status_code, status.HTTP_201_CREATED)
        r2 = self.client.post(reverse('user-list'), user_data)
        self.assertEqual(r2.status_code, status.HTTP_400_BAD_REQUEST)

    def test_create_password_too_short_user(self):
        user_data = {
            'username': 'username',
            'email': '<EMAIL>',
            'password': '<PASSWORD>',
        }
        r = self.client.post(reverse('user-list'), user_data)
        self.assertEqual(r.status_code, status.HTTP_400_BAD_REQUEST)
        with self.assertRaises(User.DoesNotExist):
            User.objects.get(username=user_data['username'])

    def test_list_users_without_auth(self):
        r = self.client.get(reverse('user-list'))
        self.assertEqual(r.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_list_users_can_see_self_only(self):
        user_data = {
            'username': 'username',
            'email': '<EMAIL>',
            'password': 'password',
        }
        user = User.objects.create(**user_data)
        self.client.force_authenticate(user=user)
        r = self.client.get(reverse('user-list'))
        self.assertEqual(r.status_code, status.HTTP_200_OK)
        self.assertEqual(len(r.data), 1)
        self.assertEqual(r.data[0]['username'], user_data['username'])

    def test_me(self):
        user_data = {
            'username': 'username',
            'email': '<EMAIL>',
            'password': 'password',
        }
        user = User.objects.create(**user_data)
        self.client.force_authenticate(user=user)
        r = self.client.get(reverse('user-me'))
        self.assertEqual(r.status_code, status.HTTP_200_OK)
        self.assertEqual(r.data['username'], user_data['username'])
        self.assertEqual(r.data['email'], user_data['email'])
        self.assertNotIn('password', r.data)
