1
0

test_jinja_templates.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. config = {
  24. "general": {
  25. "templates.path": os.path.join(
  26. os.path.dirname(os.path.dirname(__file__)), "res"
  27. ),
  28. },
  29. }
  30. self.sydent = make_sydent(test_config=config)
  31. def test_jinja_vector_invite(self):
  32. substitutions = {
  33. "address": "foo@example.com",
  34. "medium": "email",
  35. "room_alias": "#somewhere:exmaple.org",
  36. "room_avatar_url": "mxc://example.org/s0meM3dia",
  37. "room_id": "!something:example.org",
  38. "room_name": "Bob's Emporium of Messages",
  39. "sender": "@bob:example.com",
  40. "sender_avatar_url": "mxc://example.org/an0th3rM3dia",
  41. "sender_display_name": "<Bob Smith>",
  42. "bracketed_verified_sender": "Bob Smith",
  43. "bracketed_room_name": "Bob's Emporium of Messages",
  44. "to": "person@test.test",
  45. "token": "a_token",
  46. "ephemeral_private_key": "mystery_key",
  47. "web_client_location": "https://app.element.io",
  48. "room_type": "",
  49. }
  50. # self.sydent.config.email.invite_template is deprecated
  51. if self.sydent.config.email.invite_template is None:
  52. templateFile = self.sydent.get_branded_template(
  53. "vector-im",
  54. "invite_template.eml",
  55. )
  56. else:
  57. templateFile = self.sydent.config.email.invite_template
  58. with patch("sydent.util.emailutils.smtplib") as smtplib:
  59. sendEmail(self.sydent, templateFile, "test@test.com", substitutions)
  60. smtp = smtplib.SMTP.return_value
  61. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  62. # test url input is encoded
  63. self.assertIn(urllib.parse.quote("mxc://example.org/s0meM3dia"), email_contents)
  64. # test html input is escaped
  65. self.assertIn("Bob&#39;s Emporium of Messages", email_contents)
  66. # test safe values are not escaped
  67. self.assertIn("<Bob Smith>", email_contents)
  68. # test our link is as expected
  69. expected_url = (
  70. "https://app.element.io/#/room/"
  71. + urllib.parse.quote("!something:example.org")
  72. + "?email="
  73. + urllib.parse.quote("test@test.com")
  74. + "&signurl=https%3A%2F%2Fvector.im%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3D"
  75. + urllib.parse.quote("a_token")
  76. + "%26private_key%3D"
  77. + urllib.parse.quote("mystery_key")
  78. + "&room_name="
  79. + urllib.parse.quote("Bob's Emporium of Messages")
  80. + "&room_avatar_url="
  81. + urllib.parse.quote("mxc://example.org/s0meM3dia")
  82. + "&inviter_name="
  83. + urllib.parse.quote("<Bob Smith>")
  84. + "&guest_access_token=&guest_user_id=&room_type="
  85. )
  86. text = email_contents.splitlines()
  87. link = text[19]
  88. self.assertEqual(link, expected_url)
  89. def test_jinja_matrix_invite(self):
  90. substitutions = {
  91. "address": "foo@example.com",
  92. "medium": "email",
  93. "room_alias": "#somewhere:exmaple.org",
  94. "room_avatar_url": "mxc://example.org/s0meM3dia",
  95. "room_id": "!something:example.org",
  96. "room_name": "Bob's Emporium of Messages",
  97. "sender": "@bob:example.com",
  98. "sender_avatar_url": "mxc://example.org/an0th3rM3dia",
  99. "sender_display_name": "<Bob Smith>",
  100. "bracketed_verified_sender": "Bob Smith",
  101. "bracketed_room_name": "Bob's Emporium of Messages",
  102. "to": "person@test.test",
  103. "token": "a_token",
  104. "ephemeral_private_key": "mystery_key",
  105. "web_client_location": "https://matrix.org",
  106. "room_type": "",
  107. }
  108. # self.sydent.config.email.invite_template is deprecated
  109. if self.sydent.config.email.invite_template is None:
  110. templateFile = self.sydent.get_branded_template(
  111. "matrix-org",
  112. "invite_template.eml",
  113. )
  114. else:
  115. templateFile = self.sydent.config.email.invite_template
  116. with patch("sydent.util.emailutils.smtplib") as smtplib:
  117. sendEmail(self.sydent, templateFile, "test@test.com", substitutions)
  118. smtp = smtplib.SMTP.return_value
  119. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  120. # test url input is encoded
  121. self.assertIn(urllib.parse.quote("mxc://example.org/s0meM3dia"), email_contents)
  122. # test html input is escaped
  123. self.assertIn("Bob&#39;s Emporium of Messages", email_contents)
  124. # test safe values are not escaped
  125. self.assertIn("<Bob Smith>", email_contents)
  126. # test our link is as expected
  127. expected_url = (
  128. "https://matrix.org/#/room/"
  129. + urllib.parse.quote("!something:example.org")
  130. + "?email="
  131. + urllib.parse.quote("test@test.com")
  132. + "&signurl=https%3A%2F%2Fmatrix.org%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3D"
  133. + urllib.parse.quote("a_token")
  134. + "%26private_key%3D"
  135. + urllib.parse.quote("mystery_key")
  136. + "&room_name="
  137. + urllib.parse.quote("Bob's Emporium of Messages")
  138. + "&room_avatar_url="
  139. + urllib.parse.quote("mxc://example.org/s0meM3dia")
  140. + "&inviter_name="
  141. + urllib.parse.quote("<Bob Smith>")
  142. + "&guest_access_token=&guest_user_id=&room_type="
  143. )
  144. text = email_contents.splitlines()
  145. link = text[22]
  146. self.assertEqual(link, expected_url)
  147. def test_jinja_matrix_verification(self):
  148. substitutions = {
  149. "address": "foo@example.com",
  150. "medium": "email",
  151. "to": "person@test.test",
  152. "token": "<<token>>",
  153. "link": "https://link_test.com",
  154. }
  155. templateFile = self.sydent.get_branded_template(
  156. "matrix-org",
  157. "verification_template.eml",
  158. )
  159. with patch("sydent.util.emailutils.smtplib") as smtplib:
  160. sendEmail(self.sydent, templateFile, "test@test.com", substitutions)
  161. smtp = smtplib.SMTP.return_value
  162. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  163. # test html input is escaped
  164. self.assertIn("&lt;&lt;token&gt;&gt;", email_contents)
  165. # test safe values are not escaped
  166. self.assertIn("<<token>>", email_contents)
  167. @patch(
  168. "sydent.util.emailutils.generateAlphanumericTokenOfLength",
  169. Mock(return_value="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
  170. )
  171. def test_jinja_vector_verification(self):
  172. substitutions = {
  173. "address": "foo@example.com",
  174. "medium": "email",
  175. "to": "person@test.test",
  176. "link": "https://link_test.com",
  177. }
  178. templateFile = self.sydent.get_branded_template(
  179. "vector-im",
  180. "verification_template.eml",
  181. )
  182. with patch("sydent.util.emailutils.smtplib") as smtplib:
  183. sendEmail(self.sydent, templateFile, "test@test.com", substitutions)
  184. smtp = smtplib.SMTP.return_value
  185. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  186. path = os.path.join(
  187. self.sydent.config.general.templates_path,
  188. "vector_verification_sample.txt",
  189. )
  190. with open(path, "r") as file:
  191. expected_text = file.read()
  192. # remove the email headers as they are variable
  193. email_contents = email_contents[email_contents.index("Hello") :]
  194. # test all ouput is as expected
  195. self.assertEqual(email_contents, expected_text)