test_jinja_templates.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os.path
  15. import urllib
  16. from unittest.mock import Mock, patch
  17. from twisted.trial import unittest
  18. from sydent.util.emailutils import sendEmail
  19. from tests.utils import make_sydent
  20. class TestTemplate(unittest.TestCase):
  21. def setUp(self):
  22. # Create a new sydent
  23. self.sydent = make_sydent()
  24. def test_jinja_vector_invite(self):
  25. substitutions = {
  26. "address": "foo@example.com",
  27. "medium": "email",
  28. "room_alias": "#somewhere:exmaple.org",
  29. "room_avatar_url": "mxc://example.org/s0meM3dia",
  30. "room_id": "!something:example.org",
  31. "room_name": "Bob's Emporium of Messages",
  32. "sender": "@bob:example.com",
  33. "sender_avatar_url": "mxc://example.org/an0th3rM3dia",
  34. "sender_display_name": "<Bob Smith>",
  35. "bracketed_verified_sender": "Bob Smith",
  36. "bracketed_room_name": "Bob's Emporium of Messages",
  37. "to": "person@test.test",
  38. "token": "a_token",
  39. "ephemeral_private_key": "mystery_key",
  40. "web_client_location": "https://app.element.io",
  41. "room_type": "",
  42. }
  43. # self.sydent.config.email.invite_template is deprecated
  44. if self.sydent.config.email.invite_template is None:
  45. templateFile = self.sydent.get_branded_template(
  46. "vector-im",
  47. "invite_template.eml",
  48. )
  49. else:
  50. templateFile = self.sydent.config.email.invite_template
  51. with patch("sydent.util.emailutils.smtplib") as smtplib:
  52. sendEmail(self.sydent, templateFile, "test@test.com", substitutions)
  53. smtp = smtplib.SMTP.return_value
  54. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  55. # test url input is encoded
  56. self.assertIn(urllib.parse.quote("mxc://example.org/s0meM3dia"), email_contents)
  57. # test html input is escaped
  58. self.assertIn("Bob&#39;s Emporium of Messages", email_contents)
  59. # test safe values are not escaped
  60. self.assertIn("<Bob Smith>", email_contents)
  61. # test our link is as expected
  62. expected_url = (
  63. "https://app.element.io/#/room/"
  64. + urllib.parse.quote("!something:example.org")
  65. + "?email="
  66. + urllib.parse.quote("test@test.com")
  67. + "&signurl=https%3A%2F%2Fvector.im%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3D"
  68. + urllib.parse.quote("a_token")
  69. + "%26private_key%3D"
  70. + urllib.parse.quote("mystery_key")
  71. + "&room_name="
  72. + urllib.parse.quote("Bob's Emporium of Messages")
  73. + "&room_avatar_url="
  74. + urllib.parse.quote("mxc://example.org/s0meM3dia")
  75. + "&inviter_name="
  76. + urllib.parse.quote("<Bob Smith>")
  77. + "&guest_access_token=&guest_user_id=&room_type="
  78. )
  79. text = email_contents.splitlines()
  80. link = text[19]
  81. self.assertEqual(link, expected_url)
  82. def test_jinja_matrix_invite(self):
  83. substitutions = {
  84. "address": "foo@example.com",
  85. "medium": "email",
  86. "room_alias": "#somewhere:exmaple.org",
  87. "room_avatar_url": "mxc://example.org/s0meM3dia",
  88. "room_id": "!something:example.org",
  89. "room_name": "Bob's Emporium of Messages",
  90. "sender": "@bob:example.com",
  91. "sender_avatar_url": "mxc://example.org/an0th3rM3dia",
  92. "sender_display_name": "<Bob Smith>",
  93. "bracketed_verified_sender": "Bob Smith",
  94. "bracketed_room_name": "Bob's Emporium of Messages",
  95. "to": "person@test.test",
  96. "token": "a_token",
  97. "ephemeral_private_key": "mystery_key",
  98. "web_client_location": "https://matrix.org",
  99. "room_type": "",
  100. }
  101. # self.sydent.config.email.invite_template is deprecated
  102. if self.sydent.config.email.invite_template is None:
  103. templateFile = self.sydent.get_branded_template(
  104. "matrix-org",
  105. "invite_template.eml",
  106. )
  107. else:
  108. templateFile = self.sydent.config.email.invite_template
  109. with patch("sydent.util.emailutils.smtplib") as smtplib:
  110. sendEmail(self.sydent, templateFile, "test@test.com", substitutions)
  111. smtp = smtplib.SMTP.return_value
  112. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  113. # test url input is encoded
  114. self.assertIn(urllib.parse.quote("mxc://example.org/s0meM3dia"), email_contents)
  115. # test html input is escaped
  116. self.assertIn("Bob&#39;s Emporium of Messages", email_contents)
  117. # test safe values are not escaped
  118. self.assertIn("<Bob Smith>", email_contents)
  119. # test our link is as expected
  120. expected_url = (
  121. "https://matrix.org/#/room/"
  122. + urllib.parse.quote("!something:example.org")
  123. + "?email="
  124. + urllib.parse.quote("test@test.com")
  125. + "&signurl=https%3A%2F%2Fmatrix.org%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3D"
  126. + urllib.parse.quote("a_token")
  127. + "%26private_key%3D"
  128. + urllib.parse.quote("mystery_key")
  129. + "&room_name="
  130. + urllib.parse.quote("Bob's Emporium of Messages")
  131. + "&room_avatar_url="
  132. + urllib.parse.quote("mxc://example.org/s0meM3dia")
  133. + "&inviter_name="
  134. + urllib.parse.quote("<Bob Smith>")
  135. + "&guest_access_token=&guest_user_id=&room_type="
  136. )
  137. text = email_contents.splitlines()
  138. link = text[22]
  139. self.assertEqual(link, expected_url)
  140. def test_jinja_matrix_verification(self):
  141. substitutions = {
  142. "address": "foo@example.com",
  143. "medium": "email",
  144. "to": "person@test.test",
  145. "token": "<<token>>",
  146. "link": "https://link_test.com",
  147. }
  148. templateFile = self.sydent.get_branded_template(
  149. "matrix-org",
  150. "verification_template.eml",
  151. )
  152. with patch("sydent.util.emailutils.smtplib") as smtplib:
  153. sendEmail(self.sydent, templateFile, "test@test.com", substitutions)
  154. smtp = smtplib.SMTP.return_value
  155. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  156. # test html input is escaped
  157. self.assertIn("&lt;&lt;token&gt;&gt;", email_contents)
  158. # test safe values are not escaped
  159. self.assertIn("<<token>>", email_contents)
  160. @patch(
  161. "sydent.util.emailutils.generateAlphanumericTokenOfLength",
  162. Mock(return_value="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
  163. )
  164. def test_jinja_vector_verification(self):
  165. substitutions = {
  166. "address": "foo@example.com",
  167. "medium": "email",
  168. "to": "person@test.test",
  169. "link": "https://link_test.com",
  170. }
  171. templateFile = self.sydent.get_branded_template(
  172. "vector-im",
  173. "verification_template.eml",
  174. )
  175. with patch("sydent.util.emailutils.smtplib") as smtplib:
  176. sendEmail(self.sydent, templateFile, "test@test.com", substitutions)
  177. smtp = smtplib.SMTP.return_value
  178. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  179. path = os.path.join(
  180. self.sydent.config.general.templates_path,
  181. "vector_verification_sample.txt",
  182. )
  183. with open(path, "r") as file:
  184. expected_text = file.read()
  185. # remove the email headers as they are variable
  186. email_contents = email_contents[email_contents.index("Hello") :]
  187. # test all ouput is as expected
  188. self.assertEqual(email_contents, expected_text)