# Path: account/models.py
# class EmailAddress(models.Model):
# 
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#     email = models.EmailField(max_length=254, unique=settings.ACCOUNT_EMAIL_UNIQUE)
#     verified = models.BooleanField(_("verified"), default=False)
#     primary = models.BooleanField(_("primary"), default=False)
# 
#     objects = EmailAddressManager()
# 
#     class Meta:
#         verbose_name = _("email address")
#         verbose_name_plural = _("email addresses")
#         if not settings.ACCOUNT_EMAIL_UNIQUE:
#             unique_together = [("user", "email")]
# 
#     def __str__(self):
#         return "{0} ({1})".format(self.email, self.user)
# 
#     def set_as_primary(self, conditional=False):
#         old_primary = EmailAddress.objects.get_primary(self.user)
#         if old_primary:
#             if conditional:
#                 return False
#             old_primary.primary = False
#             old_primary.save()
#         self.primary = True
#         self.save()
#         self.user.email = self.email
#         self.user.save()
#         return True
# 
#     def send_confirmation(self, **kwargs):
#         confirmation = EmailConfirmation.create(self)
#         confirmation.send(**kwargs)
#         return confirmation
# 
#     def change(self, new_email, confirm=True):
#         """
#         Given a new email address, change self and re-confirm.
#         """
#         with transaction.atomic():
#             self.user.email = new_email
#             self.user.save()
#             self.email = new_email
#             self.verified = False
#             self.save()
#             if confirm:
#                 self.send_confirmation()
# 
#     def validate_unique(self, exclude=None):
#         super(EmailAddress, self).validate_unique(exclude=exclude)
# 
#         qs = EmailAddress.objects.filter(email__iexact=self.email)
# 
#         if qs.exists() and settings.ACCOUNT_EMAIL_UNIQUE:
#             raise forms.ValidationError({
#                 "email": _("A user is registered with this email address."),
#             })


from django.contrib.auth.models import User
from django.forms import ValidationError
from django.test import TestCase, override_settings
from account.models import EmailAddress



@override_settings(ACCOUNT_EMAIL_UNIQUE=True)
class UniqueEmailAddressTestCase(TestCase):
    def test_unique_email(self):
        user = User.objects.create_user("user1", email="user1@example.com", password="password")

        email_1 = EmailAddress(user=user, email="user2@example.com")
        email_1.full_clean()
        email_1.save()

        validation_error = False
        try:

# Path: account/models.py
# class EmailAddress(models.Model):
# 
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#     email = models.EmailField(max_length=254, unique=settings.ACCOUNT_EMAIL_UNIQUE)
#     verified = models.BooleanField(_("verified"), default=False)
#     primary = models.BooleanField(_("primary"), default=False)
# 
#     objects = EmailAddressManager()
# 
#     class Meta:
#         verbose_name = _("email address")
#         verbose_name_plural = _("email addresses")
#         if not settings.ACCOUNT_EMAIL_UNIQUE:
#             unique_together = [("user", "email")]
# 
#     def __str__(self):
#         return "{0} ({1})".format(self.email, self.user)
# 
#     def set_as_primary(self, conditional=False):
#         old_primary = EmailAddress.objects.get_primary(self.user)
#         if old_primary:
#             if conditional:
#                 return False
#             old_primary.primary = False
#             old_primary.save()
#         self.primary = True
#         self.save()
#         self.user.email = self.email
#         self.user.save()
#         return True
# 
#     def send_confirmation(self, **kwargs):
#         confirmation = EmailConfirmation.create(self)
#         confirmation.send(**kwargs)
#         return confirmation
# 
#     def change(self, new_email, confirm=True):
#         """
#         Given a new email address, change self and re-confirm.
#         """
#         with transaction.atomic():
#             self.user.email = new_email
#             self.user.save()
#             self.email = new_email
#             self.verified = False
#             self.save()
#             if confirm:
#                 self.send_confirmation()
# 
#     def validate_unique(self, exclude=None):
#         super(EmailAddress, self).validate_unique(exclude=exclude)
# 
#         qs = EmailAddress.objects.filter(email__iexact=self.email)
# 
#         if qs.exists() and settings.ACCOUNT_EMAIL_UNIQUE:
#             raise forms.ValidationError({
#                 "email": _("A user is registered with this email address."),
#             })


# Path: account/tests/test_email_address.py
from django.contrib.auth.models import User
from django.forms import ValidationError
from django.test import TestCase, override_settings
from account.models import EmailAddress




@override_settings(ACCOUNT_EMAIL_UNIQUE=True)
class UniqueEmailAddressTestCase(TestCase):
    def test_unique_email(self):
        user = User.objects.create_user("user1", email="user1@example.com", password="password")

        email_1 = EmailAddress(user=user, email="user2@example.com")
        email_1.full_clean()
        email_1.save()

        validation_error = False
        try:

            email_2 = EmailAddress(user=user, email="USER2@example.com")