1
0

test_email.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. import os.path
  14. from unittest.mock import patch
  15. from twisted.trial import unittest
  16. from tests.utils import make_request, make_sydent
  17. class TestRequestCode(unittest.TestCase):
  18. def setUp(self):
  19. # Create a new sydent
  20. config = {
  21. "general": {
  22. "templates.path": os.path.join(
  23. os.path.dirname(os.path.dirname(__file__)), "res"
  24. ),
  25. },
  26. }
  27. self.sydent = make_sydent(test_config=config)
  28. def _render_request(self, request):
  29. # Patch out the email sending so we can investigate the resulting email.
  30. with patch("sydent.util.emailutils.smtplib") as smtplib:
  31. request.render(self.sydent.servlets.emailRequestCode)
  32. # Fish out the SMTP object and return it.
  33. smtp = smtplib.SMTP.return_value
  34. smtp.sendmail.assert_called_once()
  35. return smtp
  36. def test_request_code(self):
  37. self.sydent.run()
  38. request, channel = make_request(
  39. self.sydent.reactor,
  40. "POST",
  41. "/_matrix/identity/api/v1/validate/email/requestToken",
  42. {
  43. "email": "test@test",
  44. "client_secret": "oursecret",
  45. "send_attempt": 0,
  46. },
  47. )
  48. smtp = self._render_request(request)
  49. self.assertEqual(channel.code, 200)
  50. # Ensure the email is as expected.
  51. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  52. self.assertIn("Confirm your email address for Matrix", email_contents)
  53. def test_request_code_via_url_query_params(self):
  54. self.sydent.run()
  55. url = (
  56. "/_matrix/identity/api/v1/validate/email/requestToken?"
  57. "email=test@test"
  58. "&client_secret=oursecret"
  59. "&send_attempt=0"
  60. )
  61. request, channel = make_request(self.sydent.reactor, "POST", url)
  62. smtp = self._render_request(request)
  63. self.assertEqual(channel.code, 200)
  64. # Ensure the email is as expected.
  65. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  66. self.assertIn("Confirm your email address for Matrix", email_contents)
  67. def test_branded_request_code(self):
  68. self.sydent.run()
  69. request, channel = make_request(
  70. self.sydent.reactor,
  71. "POST",
  72. "/_matrix/identity/api/v1/validate/email/requestToken?brand=vector-im",
  73. {
  74. "email": "test@test",
  75. "client_secret": "oursecret",
  76. "send_attempt": 0,
  77. },
  78. )
  79. smtp = self._render_request(request)
  80. self.assertEqual(channel.code, 200)
  81. # Ensure the email is as expected.
  82. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  83. self.assertIn("Confirm your email address for Element", email_contents)