def test_send_email_with_verification_url( mock_now, mock_send_email, _, settings ): """ This method should send the email verification token to the associated email address and record the send time of the email. """ verification_url = "example.com/verify/{key}" settings.EMAIL_AUTH = {"EMAIL_VERIFICATION_URL": verification_url} user = get_user_model()() email = models.EmailAddress(address="*****@*****.**", user=user) verification = models.EmailVerification(email=email) verification.send_email() assert mock_send_email.call_args[1] == { "context": { "email": email, "user": user, "verification": verification, "verification_url": verification_url.format( key=verification.token ), }, "from_email": django_settings.DEFAULT_FROM_EMAIL, "recipient_list": [email.address], "subject": "Please Verify Your Email Address", "template_name": "email_auth/emails/verify-email", } assert verification.time_sent == mock_now.return_value assert verification.save.call_count == 1
def save(self, **kwargs) -> Optional[models.EmailVerification]: """ Send the appropriate email notification to the provided email address. If the email address exists in the database but has not been verified, a new verification token is sent. If the email is already verified, a notification informing the user their email is already verified is sent. If the email does not exist in the system, we advice the user to register or add the email address to their account first. Returns: The :py:class:`EmailVerification` instance that was created if one was created or else ``None``. """ try: email_inst = models.EmailAddress.objects.get( address__iexact=self.validated_data["email"]) except models.EmailAddress.DoesNotExist: self._send_missing_email_notification() return None if email_inst.is_verified: email_inst.send_already_verified() return None verification = models.EmailVerification(email=email_inst) verification.send_email() return verification
def test_init_all_fields(): """ Test creating an email verification with all allowed fields. """ verification = models.EmailVerification( email=models.EmailAddress(), time_sent=timezone.now() ) assert verification.pk
def test_str(): """ Converting a verification instance to a string should return a message stating which email address the verification is for. """ email = models.EmailAddress(address="*****@*****.**") verification = models.EmailVerification(email=email) expected = f"Verification for {email}" assert str(verification) == expected
def test_validate_valid_token(mock_email_verification_qs): """ If the token is valid, it should be returned. """ verification = models.EmailVerification() mock_email_verification_qs.get.return_value = verification serializer = serializers.EmailVerificationSerializer() token = serializer.validate_token(verification.token) assert token == verification.token assert mock_email_verification_qs.get.call_args[1] == { "token": verification.token }
def test_repr(): """ The string representation of an instance should contain all the information necessary to recreate it. """ email = models.EmailAddress( address="*****@*****.**", user=get_user_model()() ) verification = models.EmailVerification( email=email, time_sent=timezone.now() ) expected = test_utils.create_expected_repr( verification, ["email", "time_created", "time_sent", "time_updated", "token"], ) assert repr(verification) == expected
def test_verify(): """ Verifying an instance should mark the associated email as verified and delete the verification instance. """ email = models.EmailAddress() verification = models.EmailVerification(email=email) with mock.patch.object( email, "verify", autospec=True ) as mock_verify, mock.patch.object( verification, "delete", autospec=True ) as mock_delete: verification.verify() assert mock_verify.call_count == 1 assert mock_delete.call_count == 1
def test_save_valid_data(mock_verify, mock_email_verification_qs): """ Saving a serializer with a valid token should mark the email address associated with the token as verified and delete the token. """ verification = models.EmailVerification() mock_email_verification_qs.get.return_value = verification data = {"token": verification.token} serializer = serializers.EmailVerificationSerializer(data=data) assert serializer.is_valid() serializer.save() assert serializer.data == {} assert mock_verify.call_count == 1 assert mock_email_verification_qs.get.call_args[1] == { "token": verification.token }