Repository: cdriehuys/django-simple-email-auth

Issue:
Add utility method to send verification from EmailAddress instance
It would be nice to have a `send_verification` method on `EmailAddress` instances that would create a new `EmailVerification` instance and send the verification email.

Discussion:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index c9b45ba..bb73d7c 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -2,6 +2,17 @@
 Changelog
 #########
 
+**************
+In Development
+**************
+
+Features
+========
+
+* #11: Added utility method ``EmailAddress.send_verification_email`` to
+  encapsulate the process of creating an ``EmailVerification`` instance and
+  sending an email for it.
+
 ******
 v0.1.0
 ******
diff --git a/email_auth/models.py b/email_auth/models.py
index de612f3..0a59d6e 100644
--- a/email_auth/models.py
+++ b/email_auth/models.py
@@ -144,6 +144,19 @@ class EmailAddress(models.Model):
         """
         self.normalized_address = self.address.lower()
 
+    def send_verification_email(self):
+        """
+        Create and send a verification email to the address associated
+        with the instance.
+
+        Returns:
+            The created :py:class:`EmailVerification` instance.
+        """
+        verification = EmailVerification.objects.create(email=self)
+        verification.send_email()
+
+        return verification
+
     def verify(self):
         """
         Mark the email address instance as verified.


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 8f8bce5..9cbda52 100644
--- a/email_auth/test/models/test_email_address_model.py
+++ b/email_auth/test/models/test_email_address_model.py
@@ -65,6 +65,20 @@ def test_repr():
     assert repr(email) == expected
 
 
+@mock.patch("email_auth.models.EmailVerification.save", autospec=True)
+@mock.patch("email_auth.models.EmailVerification.send_email", autospec=True)
+def test_send_verification_email(*_):
+    """
+    This method should create a new verification token for the address
+    and send it to the user.
+    """
+    email = models.EmailAddress(address="test@example.com")
+    verification = email.send_verification_email()
+
+    assert verification.save.call_count == 1
+    assert verification.send_email.call_count == 1
+
+
 def test_str():
     """
     Converting an email address to a string should return the address