test_email.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. from mock import Mock, patch
  16. from twisted.web.client import Response
  17. from twisted.trial import unittest
  18. from sydent.db.invite_tokens import JoinTokenStore
  19. from sydent.http.httpclient import FederationHttpClient
  20. from sydent.http.servlets.store_invite_servlet import StoreInviteServlet
  21. from tests.utils import make_request, make_sydent
  22. class TestRequestCode(unittest.TestCase):
  23. def setUp(self):
  24. # Create a new sydent
  25. config = {
  26. "general": {
  27. "templates.path": os.path.join(os.path.dirname(os.path.dirname(__file__)), "res"),
  28. },
  29. }
  30. self.sydent = make_sydent(test_config=config)
  31. def _render_request(self, request):
  32. # Patch out the email sending so we can investigate the resulting email.
  33. with patch("sydent.util.emailutils.smtplib") as smtplib:
  34. request.render(self.sydent.servlets.emailRequestCode)
  35. # Fish out the SMTP object and return it.
  36. smtp = smtplib.SMTP.return_value
  37. smtp.sendmail.assert_called_once()
  38. return smtp
  39. def test_request_code(self):
  40. self.sydent.run()
  41. request, channel = make_request(
  42. self.sydent.reactor, "POST", "/_matrix/identity/v1/validate/email/requestToken",
  43. {
  44. "email": "test@test",
  45. "client_secret": "oursecret",
  46. "send_attempt": 0,
  47. }
  48. )
  49. smtp = self._render_request(request)
  50. self.assertEqual(channel.code, 200)
  51. # Ensure the email is as expected.
  52. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  53. self.assertIn("Confirm your email address for Matrix", email_contents)
  54. def test_branded_request_code(self):
  55. self.sydent.run()
  56. request, channel = make_request(
  57. self.sydent.reactor, "POST", "/_matrix/identity/v1/validate/email/requestToken?brand=vector-im",
  58. {
  59. "email": "test@test",
  60. "client_secret": "oursecret",
  61. "send_attempt": 0,
  62. }
  63. )
  64. smtp = self._render_request(request)
  65. self.assertEqual(channel.code, 200)
  66. # Ensure the email is as expected.
  67. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  68. self.assertIn("Confirm your email address for Element", email_contents)