Repository: cdriehuys/django-simple-email-auth

Issue:
Add method to email addresses to notify of a duplicate registration attempt
This method should send an email to the associated address notifying the owner that another attempt to register the email has been made. The email should indicate that the user can take no action, reset their password, or resend the verification email.

Discussion:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index e19656b..447c81f 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -19,6 +19,8 @@ Features
 * #11: Added utility method ``EmailAddress.send_verification_email`` to
   encapsulate the process of creating an ``EmailVerification`` instance and
   sending an email for it.
+* #14: Added method to send a notification that an email address had another
+  registration attempt.
 
 ******
 v0.1.0
diff --git a/email_auth/models.py b/email_auth/models.py
index 15c33c6..5f0e675 100644
--- a/email_auth/models.py
+++ b/email_auth/models.py
@@ -129,6 +129,22 @@ class EmailAddress(models.Model):
         """
         return self.address
 
+    def send_duplicate_notification(self):
+        """
+        Send an email notifying the user that this email address has
+        already been registered.
+        """
+        context = {"email": self}
+        template = "email_auth/emails/duplicate-email"
+
+        email_utils.send_email(
+            context=context,
+            from_email=settings.DEFAULT_FROM_EMAIL,
+            recipient_list=[self.address],
+            subject=_("Your Email Address has Already Been Registered"),
+            template_name=template,
+        )
+
     def send_verification_email(self):
         """
         Create and send a verification email to the address associated
diff --git a/email_auth/templates/email_auth/emails/duplicate-email.txt b/email_auth/templates/email_auth/emails/duplicate-email.txt
new file mode 100644
index 0000000..4f9cd37
--- /dev/null
+++ b/email_auth/templates/email_auth/emails/duplicate-email.txt
@@ -0,0 +1,9 @@
+{% load i18n %}{% blocktrans with user=email.user %}Hello {{ user }},
+
+Someone recently attempted to register this email address which you have already
+registered. If this was not you, you can safely ignore this message.
+
+If you have forgotten your password, please reset your password. If you have not
+verified this email address yet and your verification token is lost or expired,
+please resend the verification email.
+{% endblocktrans %}


Test patch:
diff --git a/email_auth/test/models/test_email_address_model.py b/email_auth/test/models/test_email_address_model.py
index 13c2ecb..0033e36 100644
--- a/email_auth/test/models/test_email_address_model.py
+++ b/email_auth/test/models/test_email_address_model.py
@@ -1,5 +1,6 @@
 from unittest import mock
 
+from django.conf import settings
 from django.contrib import auth
 from django.utils import timezone
 
@@ -50,6 +51,25 @@ def test_repr():
     assert repr(email) == expected
 
 
+@mock.patch("email_auth.models.email_utils.send_email", autospec=True)
+def test_send_duplicate_notification(mock_send_email):
+    """
+    This method should send a notification to the email address letting
+    the user know another attempt was made to register the email
+    address.
+    """
+    email = models.EmailAddress(address="test@example.com")
+    email.send_duplicate_notification()
+
+    assert mock_send_email.call_args[1] == {
+        "context": {"email": email},
+        "from_email": settings.DEFAULT_FROM_EMAIL,
+        "recipient_list": [email.address],
+        "subject": "Your Email Address has Already Been Registered",
+        "template_name": "email_auth/emails/duplicate-email",
+    }
+
+
 @mock.patch("email_auth.models.EmailVerification.save", autospec=True)
 @mock.patch("email_auth.models.EmailVerification.send_email", autospec=True)
 def test_send_verification_email(*_):