-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ICMSLST-2169 - Email Template model and admin screens
- Loading branch information
Showing
17 changed files
with
236 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from uuid import UUID | ||
|
||
from django.conf import settings | ||
from notifications_python_client import NotificationsAPIClient | ||
from notifications_python_client.errors import HTTPError | ||
|
||
|
||
def get_gov_notify_client() -> NotificationsAPIClient: | ||
return NotificationsAPIClient(settings.GOV_NOTIFY_API_KEY) | ||
|
||
|
||
def get_template_by_id(template_id: UUID) -> dict: | ||
client = get_gov_notify_client() | ||
try: | ||
return client.get_template(template_id) | ||
except HTTPError as e: | ||
return e.response.json() | ||
|
||
|
||
def is_valid_template_id(template_id: UUID) -> bool: | ||
response = get_template_by_id(template_id) | ||
gov_notify_template_id = response.get("id", "") | ||
try: | ||
gov_notify_template_id = UUID(gov_notify_template_id) | ||
except ValueError: | ||
return False | ||
return gov_notify_template_id == template_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from web.types import TypedTextChoices | ||
|
||
|
||
class EmailTypes(TypedTextChoices): | ||
ACCESS_REQUEST = ("ACCESS_REQUEST", "Access Request") | ||
CASE_COMPLETE = ("CASE_COMPLETE", "Case Complete") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.db import models | ||
|
||
from .constants import EmailTypes | ||
|
||
|
||
class EmailTemplate(models.Model): | ||
name = models.CharField(max_length=255, unique=True, choices=EmailTypes.choices) | ||
gov_notify_template_id = models.UUIDField() | ||
|
||
def __str__(self) -> str: | ||
return self.get_name_display() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from web.mail.constants import EmailTypes | ||
from web.models import EmailTemplate | ||
|
||
templates = [ | ||
(EmailTypes.ACCESS_REQUEST, "d8905fee-1f7d-48dc-bc11-aee71c130b3e"), | ||
(EmailTypes.CASE_COMPLETE, "2e03bc8e-1d57-404d-ba53-0fbf00316a4d"), # /PS-IGNORE | ||
] | ||
|
||
|
||
def add_email_gov_notify_templates(): | ||
EmailTemplate.objects.bulk_create( | ||
[ | ||
EmailTemplate(name=name, gov_notify_template_id=gov_notify_template_id) | ||
for name, gov_notify_template_id in templates | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from unittest import mock | ||
from uuid import UUID | ||
|
||
import pytest | ||
|
||
from web.admin import EmailTemplateForm | ||
from web.mail.constants import EmailTypes | ||
from web.models import EmailTemplate | ||
|
||
FAKE_TEMPLATE_UUID = UUID("646bea34-20ef-437c-b001-ea557f3ba1e6") | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_email_template_form_uuid_error(): | ||
case_complete = EmailTemplate.objects.get(name=EmailTypes.CASE_COMPLETE) | ||
form = EmailTemplateForm( | ||
instance=case_complete, | ||
data={"gov_notify_template_id": "hello", "name": EmailTypes.CASE_COMPLETE}, | ||
) | ||
assert form.is_valid() is False | ||
assert form.errors == {"gov_notify_template_id": ["Enter a valid UUID."]} | ||
|
||
|
||
@pytest.mark.django_db | ||
@mock.patch("web.admin.is_valid_template_id") | ||
def test_email_template_id_is_invalid(mock_is_valid_template_id): | ||
mock_is_valid_template_id.return_value = False | ||
case_complete = EmailTemplate.objects.get(name=EmailTypes.CASE_COMPLETE) | ||
form = EmailTemplateForm( | ||
instance=case_complete, | ||
data={ | ||
"gov_notify_template_id": FAKE_TEMPLATE_UUID, | ||
"name": EmailTypes.CASE_COMPLETE, | ||
}, | ||
) | ||
assert form.is_valid() is False | ||
assert form.errors == {"gov_notify_template_id": ["GOV Notify template not found"]} | ||
mock_is_valid_template_id.assert_called_once_with(FAKE_TEMPLATE_UUID) | ||
|
||
|
||
@pytest.mark.django_db | ||
@mock.patch("web.admin.is_valid_template_id") | ||
def test_email_template_id_is_valid(mock_is_valid_template_id): | ||
mock_is_valid_template_id.return_value = True | ||
case_complete = EmailTemplate.objects.get(name=EmailTypes.CASE_COMPLETE) | ||
form = EmailTemplateForm( | ||
instance=case_complete, | ||
data={ | ||
"gov_notify_template_id": FAKE_TEMPLATE_UUID, | ||
"name": EmailTypes.CASE_COMPLETE, | ||
}, | ||
) | ||
assert form.is_valid() is True, form.errors | ||
instance = form.save() | ||
mock_is_valid_template_id.assert_called_once_with(FAKE_TEMPLATE_UUID) | ||
assert str(instance) == EmailTypes.CASE_COMPLETE.label | ||
assert instance.gov_notify_template_id == FAKE_TEMPLATE_UUID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from unittest import mock | ||
from uuid import UUID | ||
|
||
import pytest | ||
from notifications_python_client.errors import HTTPError | ||
|
||
from web.mail import api | ||
|
||
HTTP_404_NOT_FOUND_ERROR = { | ||
"errors": [{"error": "NoResultFound", "message": "No result found"}], | ||
"status_code": 404, | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"template_id,expected_result,mock_get_template_id_response", | ||
[ | ||
(UUID("4adda435-af30-4f24-98a4-1f07e222369e"), False, HTTP_404_NOT_FOUND_ERROR), | ||
( | ||
UUID("646bea34-20ef-437c-b001-ea557f3ba1e6"), | ||
True, | ||
{"id": "646bea34-20ef-437c-b001-ea557f3ba1e6"}, | ||
), | ||
(UUID("646bea34-20ef-437c-b001-ea557f3ba1e6"), False, {}), | ||
( | ||
UUID("646bea34-20ef-437c-b001-ea557f3ba1e6"), | ||
False, | ||
{"id": "fb9a1023-3901-44e8-a7d3-a0e309e93951"}, | ||
), | ||
], | ||
) | ||
def test_is_valid_template_by_id(template_id, expected_result, mock_get_template_id_response): | ||
with mock.patch("web.mail.api.get_template_by_id") as mock_get_template_id: | ||
mock_get_template_id.return_value = mock_get_template_id_response | ||
assert api.is_valid_template_id(template_id) == expected_result | ||
|
||
|
||
@mock.patch("notifications_python_client.NotificationsAPIClient.get_template") | ||
def test_get_template_by_id(mock_gov_notify_get_template): | ||
fake_response = {"id": "fb9a1023-3901-44e8-a7d3-a0e309e93951"} | ||
mock_gov_notify_get_template.return_value = fake_response | ||
assert api.get_template_by_id(UUID("fb9a1023-3901-44e8-a7d3-a0e309e93951")) == fake_response | ||
|
||
|
||
@mock.patch("notifications_python_client.NotificationsAPIClient.get_template") | ||
def test_get_template_by_id_error(mock_gov_notify_get_template): | ||
fake_response = mock.Mock(status_code=404, json=lambda: HTTP_404_NOT_FOUND_ERROR) | ||
fake_error = mock.Mock(response=fake_response) | ||
mock_gov_notify_get_template.side_effect = HTTPError.create(fake_error) | ||
assert ( | ||
api.get_template_by_id(UUID("fb9a1023-3901-44e8-a7d3-a0e309e93951")) | ||
== HTTP_404_NOT_FOUND_ERROR | ||
) |