Skip to content

Commit

Permalink
Add resend email confirmation flow. (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahboyce authored Mar 31, 2024
1 parent 123929b commit eea7e73
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 19 deletions.
38 changes: 38 additions & 0 deletions accounts/tests/test_resend_confirmation_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from __future__ import annotations

from django.core import mail
from django.test import Client
from django.test import TestCase
from django.urls import reverse

from accounts.factories import UserFactory


class ResendConfirmationEmailViewTests(TestCase):
def setUp(self):
self.client = Client()

@classmethod
def setUpTestData(cls):
cls.user = UserFactory.create()
cls.resend_confirmation_email_url = reverse("resend_email_confirmation")

def test_must_be_authentication(self):
response = self.client.post(self.resend_confirmation_email_url, {}, follow=True)
self.assertRedirects(
response,
f"{reverse('login')}?next={self.resend_confirmation_email_url}",
)

def test_get_update_email_subscription_url(self):
self.client.force_login(self.user)
response = self.client.post(self.resend_confirmation_email_url, {}, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Profile Info")
self.assertContains(response, "A verification email has been sent")
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, "Djangonaut Space Email Confirmation")
self.assertIn(
"Thank you for signing up to Djangonaut Space! Click the link to verify your email:",
mail.outbox[0].body,
)
4 changes: 1 addition & 3 deletions accounts/tests/test_signup_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def test_signup_template_post_success(self, mock_captcha):
),
)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
mail.outbox[0].subject, "Djangonaut Space Registration Confirmation"
)
self.assertEqual(mail.outbox[0].subject, "Djangonaut Space Email Confirmation")
self.assertIn(
"Thank you for signing up to Djangonaut Space! Click the link to verify your email:",
mail.outbox[0].body,
Expand Down
6 changes: 6 additions & 0 deletions accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .views import ActivateAccountView
from .views import CustomPasswordResetView
from .views import profile
from .views import ResendConfirmationEmailView
from .views import SignUpView
from .views import UpdateEmailSubscriptionView
from .views import UpdateUserView
Expand All @@ -31,6 +32,11 @@
ActivateAccountView.as_view(),
name="activate_account",
),
path(
"resend_email_confirmation/",
ResendConfirmationEmailView.as_view(),
name="resend_email_confirmation",
),
path(
"email_subscriptions/",
UpdateEmailSubscriptionView.as_view(),
Expand Down
14 changes: 13 additions & 1 deletion accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def send_user_confirmation_email(request, user):
"unsubscribe_link": request.build_absolute_uri(unsubscribe_link),
}
send_mail(
"Djangonaut Space Registration Confirmation",
"Djangonaut Space Email Confirmation",
render_to_string("emails/email_confirmation.txt", email_dict),
settings.DEFAULT_FROM_EMAIL,
[user.email],
Expand Down Expand Up @@ -121,6 +121,18 @@ def profile(request):
return render(request, "registration/profile.html")


class ResendConfirmationEmailView(LoginRequiredMixin, View):
def post(self, request):
user = request.user
send_user_confirmation_email(request, user)
messages.add_message(
request,
messages.SUCCESS,
f"A verification email has been sent to {user.email}",
)
return redirect("profile")


class UpdateUserView(LoginRequiredMixin, UpdateView):
form_class = CustomUserChangeForm
template_name = "registration/update_user.html"
Expand Down
3 changes: 2 additions & 1 deletion home/templates/home/includes/email_confirmed_warning.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% if request.user.is_authenticated and not request.user.profile.email_confirmed %}
<div class="my-2 mx-2 bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded" role="alert">
<div class="my-2 bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded inline-block w-full" role="alert">
<strong class="font-bold">Your email is not confirmed!</strong>
<span class="block sm:inline">You may not be able to apply for sessions without <a href="{% url 'profile' %}" class="underline">confirming your email address.</a></span>
{% include 'includes/resend_confirmation_email_button.html' %}
</div>
{% endif %}
20 changes: 6 additions & 14 deletions indymeet/templates/includes/messages.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
{% if messages %}
{% for message in messages %}
<div class="messages alert-dismisible alert fade show text-center
{% if 'error' in message.tags %}
alert-danger
{% elif 'warning' in message.tags %}
alert-warning
{% elif 'success' in message.tags %}
alert-success
{% else %}
alert-info
{% endif %}
">
<div class="my-2 mx-2 px-4 py-3 rounded relative border
{% if 'error' in message.tags %}bg-red-100 border-red-400 text-red-700
{% elif 'warning' in message.tags %}bg-orange-100 border-orange-400 text-orange-700
{% elif 'success' in message.tags %}bg-green-100 border-green-500 text-green-700
{% else %}bg-blue-100 border-blue-500 text-blue-700{% endif %}"
role="alert">
<div class="{{ message.tags }}">{{ message}}</div>
<!-- <button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button> -->
</div>
{% endfor %}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% load i18n %}
<form action="{% url 'resend_email_confirmation' %}" method="POST" class="inline-block sm:ml-4 sm:float-right">
{% csrf_token %}
<button type="submit">{% translate "Resend Email" %}</button>
</form>
1 change: 1 addition & 0 deletions indymeet/templates/registration/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ <h2 class="text-3xl">{% translate "Profile Info" %}</h2>
<i class="fa-solid fa-check"></i> {% translate "Yes" %}
{% else %}
<i class="fa-solid fa-x"></i> {% translate "No" %}
{% include 'includes/resend_confirmation_email_button.html' %}
{% endif %}
</td>
</tr>
Expand Down

0 comments on commit eea7e73

Please sign in to comment.