diff --git a/app/core/models.py b/app/core/models.py index 06f621e..8d56a93 100644 --- a/app/core/models.py +++ b/app/core/models.py @@ -13,6 +13,8 @@ class UserManager(BaseUserManager): """Manager for users.""" def create_user(self, email, password=None, **extra_fields): + if not email: + raise ValueError('User must have an email address.') user = self.model(email=self.normalize_email(email), **extra_fields) user.set_password(password) user.save(using=self._db) diff --git a/app/core/tests/test_models.py b/app/core/tests/test_models.py index 57245e2..75d6ad1 100644 --- a/app/core/tests/test_models.py +++ b/app/core/tests/test_models.py @@ -25,3 +25,7 @@ def test_new_user_email_normalized(self): for email, expected in sample_emails: user = get_user_model().objects.create_user(email, 'sample123') self.assertEqual(user.email, expected) + + def test_new_user_without_email_raises_error(self): + with self.assertRaises(ValueError): + get_user_model().objects.create_user('', 'sample123')