Skip to content

Commit

Permalink
Add requested tests for html_body template
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephraim Schnitzler committed Jun 16, 2023
1 parent 47e3b55 commit d5086b1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/django_otp/plugins/otp_email/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ def test_settings(self):
with self.subTest(field='body'):
self.assertEqual(msg.body, "Test template 2: {}".format(self.device.token))

@override_settings(
OTP_EMAIL_SENDER="webmaster@example.com",
OTP_EMAIL_SUBJECT="Test subject",
OTP_EMAIL_BODY_HTML_TEMPLATE="<div>{{token}}</div>",
)
def test_settings_html_template(self):
self.device.generate_challenge()

self.assertEqual(len(mail.outbox), 1)

msg = mail.outbox[0]

with self.subTest(field='from_email'):
self.assertEqual(msg.from_email, "webmaster@example.com")
with self.subTest(field='subject'):
self.assertEqual(msg.subject, "Test subject")
with self.subTest(field='body'):
self.assertEqual(
msg.body, 'Test template 1: {}\n'.format(self.device.token)
)
with self.subTest(field='alternatives'):
self.assertEqual(
msg.alternatives[0],
('<div>{}</div>'.format(self.device.token), 'text/html'),
)

@override_settings(
OTP_EMAIL_SENDER="webmaster@example.com",
OTP_EMAIL_SUBJECT="Test subject",
Expand All @@ -136,6 +162,32 @@ def test_settings_template_path(self):
msg.body, "Test template 3: {}\n".format(self.device.token)
)

@override_settings(
OTP_EMAIL_SENDER="webmaster@example.com",
OTP_EMAIL_SUBJECT="Test subject",
OTP_EMAIL_BODY_HTML_TEMPLATE_PATH="otp/email/custom_html.html",
)
def test_settings_html_template_path(self):
self.device.generate_challenge()

self.assertEqual(len(mail.outbox), 1)

msg = mail.outbox[0]

with self.subTest(field='from_email'):
self.assertEqual(msg.from_email, "webmaster@example.com")
with self.subTest(field='subject'):
self.assertEqual(msg.subject, "Test subject")
with self.subTest(field='body'):
self.assertEqual(
msg.body, 'Test template 1: {}\n'.format(self.device.token)
)
with self.subTest(field='alternatives'):
self.assertEqual(
msg.alternatives[0],
('<p>{}</p>'.format(self.device.token), 'text/html'),
)

@override_settings(
OTP_EMAIL_SENDER="webmaster@example.com",
OTP_EMAIL_SUBJECT="Test subject",
Expand Down
1 change: 1 addition & 0 deletions test/test_project/templates/otp/email/custom_html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>{{ token }}</p>

0 comments on commit d5086b1

Please sign in to comment.