From c16ea36fc5f1dddbdcc26a2050094efca909e16d Mon Sep 17 00:00:00 2001 From: Bruno Barreto Freitas Date: Fri, 26 Apr 2019 17:36:17 -0300 Subject: [PATCH] Resend Account Verification Email - View, URL, Serializer rename - Test Implementation --- rest_auth/registration/serializers.py | 2 +- rest_auth/registration/urls.py | 5 +-- rest_auth/registration/views.py | 6 +-- rest_auth/tests/test_api.py | 65 +++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 7 deletions(-) diff --git a/rest_auth/registration/serializers.py b/rest_auth/registration/serializers.py index 6b98541..5033177 100644 --- a/rest_auth/registration/serializers.py +++ b/rest_auth/registration/serializers.py @@ -216,5 +216,5 @@ class RegisterSerializer(serializers.Serializer): class VerifyEmailSerializer(serializers.Serializer): key = serializers.CharField() -class ResendEmailVerificationSerializer(serializers.Serializer): +class ResendVerificationEmailSerializer(serializers.Serializer): email = serializers.EmailField() \ No newline at end of file diff --git a/rest_auth/registration/urls.py b/rest_auth/registration/urls.py index ffc3da7..57d7e49 100644 --- a/rest_auth/registration/urls.py +++ b/rest_auth/registration/urls.py @@ -4,14 +4,13 @@ from django.conf.urls import url from .views import ( RegisterView, VerifyEmailView, - ResendEmailVerification + ResendVerificationEmail ) urlpatterns = [ url(r'^$', RegisterView.as_view(), name='rest_register'), - url(r'^resend-verification-email/$', ResendEmailVerification.as_view(), name='rest_resend_verification_email'), + url(r'^resend-verification-email/$', ResendVerificationEmail.as_view(), name='rest_resend_verification_email'), url(r'^verify-email/$', VerifyEmailView.as_view(), name='rest_verify_email'), - # This url is used by django-allauth and empty TemplateView is # defined just to allow reverse() call inside app, for example when email # with verification link is being sent, then it's required to render email diff --git a/rest_auth/registration/views.py b/rest_auth/registration/views.py index 7823198..54e9d45 100644 --- a/rest_auth/registration/views.py +++ b/rest_auth/registration/views.py @@ -25,7 +25,7 @@ from rest_auth.app_settings import (TokenSerializer, create_token) from rest_auth.models import TokenModel from rest_auth.registration.serializers import (VerifyEmailSerializer, - ResendEmailVerificationSerializer, + ResendVerificationEmailSerializer, SocialLoginSerializer, SocialAccountSerializer, SocialConnectSerializer) @@ -100,8 +100,8 @@ class VerifyEmailView(APIView, ConfirmEmailView): return Response({'detail': _('ok')}, status=status.HTTP_200_OK) -class ResendEmailVerification(GenericAPIView): - serializer_class = ResendEmailVerificationSerializer +class ResendVerificationEmail(GenericAPIView): + serializer_class = ResendVerificationEmailSerializer permission_classes = (AllowAny,) allowed_methods = ('POST', 'OPTIONS', 'HEAD') diff --git a/rest_auth/tests/test_api.py b/rest_auth/tests/test_api.py index 9c5fd9e..590b8b1 100644 --- a/rest_auth/tests/test_api.py +++ b/rest_auth/tests/test_api.py @@ -11,6 +11,8 @@ from rest_framework.test import APIRequestFactory from rest_auth.registration.views import RegisterView from rest_auth.registration.app_settings import register_permission_classes +from allauth.account.models import EmailAddress + from .mixins import TestsMixin, CustomPermissionClass try: @@ -516,3 +518,66 @@ class APIBasicTests(TestsMixin, TestCase): self.post(self.login_url, data=payload, status_code=status.HTTP_200_OK) self.get(self.logout_url, status_code=status.HTTP_405_METHOD_NOT_ALLOWED) + + @override_settings(ACCOUNT_EMAIL_VERIFICATION='mandatory') + def test_resend_account_verification_email(self): + result = self.post( + self.register_url, + data=self.REGISTRATION_DATA_WITH_EMAIL, + status_code=status.HTTP_201_CREATED + ) + + self.assertEqual(EmailAddress.objects.count(), 1) + self.assertEqual(EmailAddress.objects.first().email, self.EMAIL) + self.assertEqual(EmailAddress.objects.first().verified, False) + + self.post( + reverse('rest_resend_verification_email'), + data={ + 'email': self.EMAIL + }, + status_code=status.HTTP_200_OK + ) + + self.assertEqual(len(mail.outbox), 2) + + @override_settings(ACCOUNT_EMAIL_VERIFICATION='mandatory') + def test_resend_not_registered_account_verification_email(self): + self.assertEqual(EmailAddress.objects.count(), 0) + + self.post( + reverse('rest_resend_verification_email'), + data={ + 'email': self.EMAIL + }, + status_code=status.HTTP_200_OK + ) + + self.assertEqual(len(mail.outbox), 0) + + @override_settings(ACCOUNT_EMAIL_VERIFICATION='mandatory') + def test_resend_already_verified_account_verification_email(self): + result = self.post( + self.register_url, + data=self.REGISTRATION_DATA_WITH_EMAIL, + status_code=status.HTTP_201_CREATED + ) + + self.assertEqual(EmailAddress.objects.count(), 1) + self.assertEqual(EmailAddress.objects.first().email, self.EMAIL) + self.assertEqual(EmailAddress.objects.first().verified, False) + self.assertEqual(len(mail.outbox), 1) + + email_address = EmailAddress.objects.first() + email_address.verified = True + email_address.save() + + self.post( + reverse('rest_resend_verification_email'), + data={ + 'email': self.EMAIL + }, + status_code=status.HTTP_200_OK + ) + + self.assertEqual(len(mail.outbox), 1)