<filename>users/tests.py<gh_stars>0
from django.contrib.auth import get_user_model
from django.test import TestCase

User = get_user_model()
from .models import UserProfile

# Create your tests here.
class UsersManagersTests(TestCase):
    def test_create_user(self):
        user = User.objects.create_user(email="<EMAIL>", password="<PASSWORD>")
        self.assertEqual(user.email, "<EMAIL>")
        self.assertTrue(user.is_active)
        self.assertFalse(user.is_staff)
        self.assertFalse(user.is_superuser)
        try:
            # username is None for the AbstractUser option
            # username does not exist for the AbstractBaseUser option
            self.assertIsNone(user.username)
        except AttributeError:
            pass
        with self.assertRaises(TypeError):
            User.objects.create_user()
        with self.assertRaises(TypeError):
            User.objects.create_user(email="")
        with self.assertRaises(ValueError):
            User.objects.create_user(email="", password="<PASSWORD>")

    def test_create_superuser(self):
        admin_user = User.objects.create_superuser(
            email="<EMAIL>", password="<PASSWORD>"
        )
        self.assertEqual(admin_user.email, "<EMAIL>")
        self.assertTrue(admin_user.is_active)
        self.assertTrue(admin_user.is_staff)
        self.assertTrue(admin_user.is_superuser)
        try:
            # username is None for the AbstractUser option
            # username does not exist for the AbstractBaseUser option
            self.assertIsNone(admin_user.username)
        except AttributeError:
            pass
        with self.assertRaises(ValueError):
            User.objects.create_superuser(
                email="<EMAIL>", password="<PASSWORD>", is_superuser=False
            )

    def test_custom_user_profile_uuid_matches(self):
        user = User.objects.create_user(email="<EMAIL>", password="<PASSWORD>")
        self.assertTrue(hasattr(user, "userprofile"))
        self.assertEqual(user.id, user.userprofile.id)

    def test_user_no_has_password_none(self):
        user = User.objects.create_user(email="<EMAIL>", password=None)
        self.assertFalse(user.has_usable_password())

    def test_user_no_has_password_blank(self):
        """fails, this test should not pass"""
        user = User.objects.create_user(email="<EMAIL>", password="")
        self.assertFalse(user.has_usable_password())

    def test_user_has_password(self):
        """expand to include validating for good paswword"""
        user = User.objects.create_user(email="<EMAIL>", password="password")
        self.assertTrue(user.has_usable_password())

    def test_user_has_matching_profile_id(self):
        user = User.objects.create_user(
            email="<EMAIL>", password="<PASSWORD>"
        )
        profile = UserProfile.objects.filter(id=user.id).first()
        self.assertEqual(user.id, profile.id)
