Skip to content

Commit

Permalink
Display a warning page when an issuer already exists with a given adr…
Browse files Browse the repository at this point in the history
…ess in DB
  • Loading branch information
christophehenry committed Mar 15, 2022
1 parent 2737ba6 commit b03117a
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 5 deletions.
9 changes: 9 additions & 0 deletions aidants_connect/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,12 @@ def getenv_bool(key: str, default: Optional[bool] = None) -> bool:
Je vous contacte car je ne reçois pas les emails de confirmation de mon adresse email.""",
)

EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_FROM = os.getenv(
"EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_FROM", SUPPORT_EMAIL
)

EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_SUBJECT = os.getenv(
"EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_SUBJECT",
"Rappel de votre profil demandeur",
)
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'layouts/main-habilitation.html' %}
{% load static form_extras %}

{% block title %}
Aidants Connect - Un compte demandeur existe déjà
{% endblock %}

{% block content %}
<div class="fr-container">
<p>
Un profil avec cette adresse email existe déjà. Nous venons d'envoyer un email à cette adresse contenant un lien
où vous pourrez retrouver toutes les demandes faites avec ce profil.
</p>
</div>
{% endblock %}
29 changes: 28 additions & 1 deletion aidants_connect_habilitation/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import timedelta
from unittest.mock import patch
from unittest.mock import ANY, Mock, patch
from uuid import UUID, uuid4

from django.forms import model_to_dict
Expand Down Expand Up @@ -97,6 +97,33 @@ def test_redirect_valid_post_creates_an_email_confirmation(self):
"Request should have created an instance of IssuerEmailConfirmation"
)

@patch("aidants_connect_habilitation.views.send_mail")
def test_send_email_when_issuer_already_exists(self, send_mail_mock: Mock):
issuer: Issuer = IssuerFactory()

data = utils.get_form(IssuerForm).clean()
data["email"] = issuer.email

self.client.post(reverse(self.pattern_name), data)

send_mail_mock.assert_called_with(
from_email=settings.EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_FROM,
recipient_list=[issuer.email],
subject=settings.EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_SUBJECT,
message=ANY,
html_message=ANY,
)

def test_render_warning_when_issuer_already_exists(self):
issuer: Issuer = IssuerFactory()

data = utils.get_form(IssuerForm).clean()
data["email"] = issuer.email

response = self.client.post(reverse(self.pattern_name), data)

self.assertTemplateUsed(response, "issuer_already_exists_warning.html")


@tag("habilitation")
class IssuerEmailConfirmationWaitingViewTests(TestCase):
Expand Down
39 changes: 35 additions & 4 deletions aidants_connect_habilitation/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.conf import settings
from django.shortcuts import get_object_or_404, redirect
from django.core.mail import send_mail
from django.shortcuts import get_object_or_404, redirect, render
from django.template import loader
from django.urls import reverse
from django.views.generic import FormView, RedirectView, TemplateView, View
from django.views.generic.base import ContextMixin
Expand Down Expand Up @@ -113,14 +115,23 @@ class NewHabilitationView(RedirectView):


class NewIssuerFormView(HabilitationStepMixin, FormView):
template_name = "issuer_form.html"
form_class = IssuerForm

@property
def step(self) -> HabilitationFormStep:
return HabilitationFormStep.ISSUER

template_name = "issuer_form.html"
form_class = IssuerForm
def form_invalid(self, form: IssuerForm):
# Gets the error code of all the errors for email, if any
# See https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.error_messages # noqa
error_codes = [error.code for error in form.errors["email"].as_data()]
if "unique" in error_codes:
self.send_mail(form)
return render(self.request, "issuer_already_exists_warning.html")
return super().form_invalid(form)

def form_valid(self, form):
def form_valid(self, form: IssuerForm):
self.saved_model: Issuer = form.save()
IssuerEmailConfirmation.for_issuer(self.saved_model).send(self.request)
return super().form_valid(form)
Expand All @@ -134,6 +145,26 @@ def get_success_url(self):
def get_context_data(self, **kwargs):
return {**super().get_context_data(**kwargs), "step": self.step}

def get_email_context_data(self, **kwargs):
return {**self.get_context_data(), **kwargs}

def send_mail(self, form: IssuerForm):
email = form.data["email"]
context = self.get_email_context_data(email=email)
text_message = loader.render_to_string(
"email/issuer_profile_reminder.txt", context
)
html_message = loader.render_to_string(
"email/issuer_profile_reminder.html", context
)
send_mail(
from_email=settings.EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_FROM,
recipient_list=[email],
subject=settings.EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_SUBJECT,
message=text_message,
html_message=html_message,
)


class IssuerEmailConfirmationWaitingView(
HabilitationStepMixin, CheckIssuerMixin, TemplateView
Expand Down

0 comments on commit b03117a

Please sign in to comment.