Raise message

This commit is contained in:
zandercymatics 2023-12-15 10:34:02 -07:00
parent 9f7f176472
commit eec7c577d7
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 10 additions and 53 deletions

View file

@ -6,7 +6,7 @@ from django.test import Client, TestCase
from django.urls import reverse from django.urls import reverse
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from .common import MockEppLib, completed_application, create_user # type: ignore from .common import MockEppLib, completed_application, create_user # type: ignore
from django.contrib.messages import get_messages
from django_webtest import WebTest # type: ignore from django_webtest import WebTest # type: ignore
import boto3_mocking # type: ignore import boto3_mocking # type: ignore
@ -1459,6 +1459,7 @@ class TestDomainManagers(TestDomainOverview):
# Check that the requesters first/last name do not exist # Check that the requesters first/last name do not exist
self.assertNotIn("First", email_content) self.assertNotIn("First", email_content)
self.assertNotIn("Last", email_content) self.assertNotIn("Last", email_content)
self.assertNotIn("First Last", email_content)
@boto3_mocking.patching @boto3_mocking.patching
def test_domain_invitation_email_has_email_as_requester(self): def test_domain_invitation_email_has_email_as_requester(self):
@ -1496,55 +1497,7 @@ class TestDomainManagers(TestDomainOverview):
# Check that the requesters first/last name do not exist # Check that the requesters first/last name do not exist
self.assertNotIn("First", email_content) self.assertNotIn("First", email_content)
self.assertNotIn("Last", email_content) self.assertNotIn("Last", email_content)
self.assertNotIn("First Last", email_content)
@boto3_mocking.patching
def test_domain_invitation_email_throws_exception_non_existent(self):
"""Inviting a non existent user sends them an email, with email as the name."""
# make sure there is no user with this email
email_address = "mayor@igorville.gov"
User.objects.filter(email=email_address).delete()
# Give the user who is sending the email an invalid email address
requester = User.objects.filter(email="info@example.com").get()
requester.email = ""
requester.save()
self.domain_information, _ = DomainInformation.objects.get_or_create(creator=self.user, domain=self.domain)
mock_client = MagicMock()
with boto3_mocking.clients.handler_for("sesv2", mock_client):
with self.assertRaisesMessage(ValueError, "Can't send email. No email exists for the requester"):
add_page = self.app.get(reverse("domain-users-add", kwargs={"pk": self.domain.id}))
session_id = self.app.cookies[settings.SESSION_COOKIE_NAME]
add_page.form["email"] = email_address
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
add_page.form.submit()
@boto3_mocking.patching
def test_domain_invitation_email_throws_exception(self):
"""Inviting a user sends them an email, with email as the name."""
# make sure there is no user with this email
# Create a fake user object
email_address = "mayor@igorville.gov"
User.objects.get_or_create(email=email_address, username="fakeuser@fakeymail.com")
# Give the user who is sending the email an invalid email address
requester = User.objects.filter(email="info@example.com").get()
requester.email = ""
requester.save()
self.domain_information, _ = DomainInformation.objects.get_or_create(creator=self.user, domain=self.domain)
mock_client = MagicMock()
with boto3_mocking.clients.handler_for("sesv2", mock_client):
with self.assertRaisesMessage(ValueError, "Can't send email. No email exists for the requester"):
add_page = self.app.get(reverse("domain-users-add", kwargs={"pk": self.domain.id}))
session_id = self.app.cookies[settings.SESSION_COOKIE_NAME]
add_page.form["email"] = email_address
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
add_page.form.submit()
def test_domain_invitation_cancel(self): def test_domain_invitation_cancel(self):
"""Posting to the delete view deletes an invitation.""" """Posting to the delete view deletes an invitation."""

View file

@ -654,9 +654,13 @@ class DomainAddUserView(DomainFormBaseView):
if requester.email is not None and requester.email.strip() != "": if requester.email is not None and requester.email.strip() != "":
requester_email = requester.email requester_email = requester.email
else: else:
# This edgecase would be unusual if encountered. We don't want to handle this here. Rather, we would messages.error(self.request, "Can't send invitation email. No email is associated with your account.")
# want to fix this upstream where it is happening. logger.error(
raise ValueError("Can't send email. No email exists for the requester.") f"Can't send email to '{email}' on domain '{self.object}'."
f"No email exists for the requester '{requester.username}'.",
exc_info=True,
)
return None
try: try:
send_templated_email( send_templated_email(