forked from ethyca/fides
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add email_templates module (ethyca#1123)
* Add email_templates module * run isort * Add unit tests * Update ttl calculation * Add ttl minutes test * fix lint issues * fix pylint issue * fix pylint issue * fix isort * Update template constant * Update changelog * fix lints * Add jinja to requirements.txt * update templates directory * update unit test * Update imports * fix issue template path * Add templates to manifest Co-authored-by: Paul Sanders <[email protected]>
- Loading branch information
1 parent
5e937ad
commit b0eee6d
Showing
14 changed files
with
112 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .get_email_template import get_email_template |
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,29 @@ | ||
import logging | ||
import pathlib | ||
|
||
from jinja2 import Environment, FileSystemLoader, Template, select_autoescape | ||
|
||
from fidesops.ops.common_exceptions import EmailTemplateUnhandledActionType | ||
from fidesops.ops.email_templates.template_names import ( | ||
SUBJECT_IDENTITY_VERIFICATION_TEMPLATE, | ||
) | ||
from fidesops.ops.schemas.email.email import EmailActionType | ||
|
||
pathlib.Path(__file__).parent.resolve() | ||
logger = logging.getLogger(__name__) | ||
|
||
abs_path_to_current_file_dir = pathlib.Path(__file__).parent.resolve() | ||
template_env = Environment( | ||
loader=FileSystemLoader(f"{abs_path_to_current_file_dir}/templates"), | ||
autoescape=select_autoescape(), | ||
) | ||
|
||
|
||
def get_email_template(action_type: EmailActionType) -> Template: | ||
if action_type == EmailActionType.SUBJECT_IDENTITY_VERIFICATION: | ||
return template_env.get_template(SUBJECT_IDENTITY_VERIFICATION_TEMPLATE) | ||
|
||
logger.error(f"No corresponding template linked to the {action_type}") | ||
raise EmailTemplateUnhandledActionType( | ||
f"No corresponding template linked to the {action_type}" | ||
) |
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 @@ | ||
SUBJECT_IDENTITY_VERIFICATION_TEMPLATE = "subject_identity_verification.html" |
16 changes: 16 additions & 0 deletions
16
src/fidesops/ops/email_templates/templates/subject_identity_verification.html
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 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>ID Code</title> | ||
</head> | ||
<body> | ||
<main> | ||
<p> | ||
Your privacy request verification code is {{code}}. | ||
Please return to the Privacy Center and enter the code to | ||
continue. This code will expire in {{minutes}} minutes | ||
</p> | ||
</main> | ||
</body> | ||
</html> |
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,18 @@ | ||
import pytest | ||
from jinja2 import Template | ||
|
||
from fidesops.ops.common_exceptions import EmailTemplateUnhandledActionType | ||
from fidesops.ops.email_templates import get_email_template | ||
from fidesops.ops.schemas.email.email import EmailActionType | ||
|
||
|
||
def test_get_email_template_returns_template(): | ||
result = get_email_template(EmailActionType.SUBJECT_IDENTITY_VERIFICATION) | ||
assert type(result) == Template | ||
|
||
|
||
def test_get_email_template_exception(): | ||
fake_template = "templateThatDoesNotExist" | ||
with pytest.raises(EmailTemplateUnhandledActionType) as e: | ||
get_email_template(fake_template) | ||
assert e.value == f"No corresponding template linked to the {fake_template}" |
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 @@ | ||
import pytest | ||
|
||
from fidesops.ops.schemas.email.email import SubjectIdentityVerificationBodyParams | ||
|
||
|
||
@pytest.mark.parametrize("ttl, expected", [(600, 10), (155, 2), (33, 0)]) | ||
def test_get_verification_code_ttl_minutes_calc(ttl, expected): | ||
result = SubjectIdentityVerificationBodyParams( | ||
verification_code="123123", verification_code_ttl_seconds=ttl | ||
) | ||
assert result.get_verification_code_ttl_minutes() == expected |
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 |
---|---|---|
|
@@ -28,14 +28,16 @@ def test_email_dispatch_mailgun_success( | |
db=db, | ||
action_type=EmailActionType.SUBJECT_IDENTITY_VERIFICATION, | ||
to_email="[email protected]", | ||
email_body_params=SubjectIdentityVerificationBodyParams(access_code="2348"), | ||
email_body_params=SubjectIdentityVerificationBodyParams( | ||
verification_code="2348", verification_code_ttl_seconds=600 | ||
), | ||
) | ||
|
||
body = '<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <title>ID Code</title>\n</head>\n<body>\n<main>\n <p>\n Your privacy request verification code is 2348.\n Please return to the Privacy Center and enter the code to\n continue. This code will expire in 10 minutes\n </p>\n</main>\n</body>\n</html>' | ||
mock_mailgun_dispatcher.assert_called_with( | ||
email_config=email_config, | ||
email=EmailForActionType( | ||
subject="Your one-time code", | ||
body=f"<html>Your one-time code is 2348. Hurry! It expires in 10 minutes.</html>", | ||
body=body, | ||
), | ||
to_email="[email protected]", | ||
) | ||
|
@@ -51,7 +53,9 @@ def test_email_dispatch_mailgun_config_not_found( | |
db=db, | ||
action_type=EmailActionType.SUBJECT_IDENTITY_VERIFICATION, | ||
to_email="[email protected]", | ||
email_body_params=SubjectIdentityVerificationBodyParams(access_code="2348"), | ||
email_body_params=SubjectIdentityVerificationBodyParams( | ||
verification_code="2348", verification_code_ttl_seconds=600 | ||
), | ||
) | ||
assert exc.value.args[0] == "No email config found." | ||
|
||
|
@@ -80,7 +84,9 @@ def test_email_dispatch_mailgun_config_no_secrets( | |
db=db, | ||
action_type=EmailActionType.SUBJECT_IDENTITY_VERIFICATION, | ||
to_email="[email protected]", | ||
email_body_params=SubjectIdentityVerificationBodyParams(access_code="2348"), | ||
email_body_params=SubjectIdentityVerificationBodyParams( | ||
verification_code="2348", verification_code_ttl_seconds=600 | ||
), | ||
) | ||
assert ( | ||
exc.value.args[0] | ||
|
@@ -108,7 +114,7 @@ def test_email_dispatch_mailgun_failed_email(db: Session, email_config) -> None: | |
action_type=EmailActionType.SUBJECT_IDENTITY_VERIFICATION, | ||
to_email="[email protected]", | ||
email_body_params=SubjectIdentityVerificationBodyParams( | ||
access_code="2348" | ||
verification_code="2348", verification_code_ttl_seconds=600 | ||
), | ||
) | ||
assert ( | ||
|