from rest_framework.test import APITestCase
from LibraryApp.authentication.models import User


class TestModel(APITestCase):

    def test_creates_user(self):
        user = User.objects.create_user(
            'milly', '<EMAIL>', '<PASSWORD>!@')
        self.assertIsInstance(user, User)
        self.assertFalse(user.is_staff)
        self.assertEqual(user.email, '<EMAIL>')

    def test_raises_error_when_no_username_is_supplied(self):
        self.assertRaises(ValueError, User.objects.create_user, username="",
                          email='<EMAIL>', password='<PASSWORD>!@')

    def test_raises_error_with_message_when_no_username_is_supplied(self):
        with self.assertRaisesMessage(ValueError, 'The given username must be set'):
            User.objects.create_user(
                username='', email='<EMAIL>', password='<PASSWORD>!@')

    def test_raises_error_when_no_email_is_supplied(self):
        self.assertRaises(ValueError, User.objects.create_user,
                          username="username", email='', password='<PASSWORD>!@')

    def test_raises_error_with_message_when_no_email_is_supplied(self):
        with self.assertRaisesMessage(ValueError, 'The given email must be set'):
            User.objects.create_user(
                username='username', email='', password='<PASSWORD>!@')

    def test_cant_create_super_user_with_no_is_staff_status(self):
        with self.assertRaisesMessage(ValueError, 'Superuser must have is_staff=True.'):
            User.objects.create_superuser(
                username='username', email='<EMAIL>', password='<PASSWORD>!@', is_staff=False)

    def test_cant_create_super_user_with_no_super_user_status(self):
        with self.assertRaisesMessage(ValueError, 'Superuser must have is_superuser=True.'):
            User.objects.create_superuser(
                username='username', email='<EMAIL>', password='<PASSWORD>!@', is_superuser=False)

    def test_creates_super_user(self):
        user = User.objects.create_superuser(
            'milly', '<EMAIL>', '<PASSWORD>!@')
        self.assertIsInstance(user, User)
        self.assertTrue(user.is_staff)
        self.assertEqual(user.email, '<EMAIL>')
