1
0

test_email.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. from typing import Optional
  14. from unittest.mock import Mock, patch
  15. from twisted.trial import unittest
  16. from sydent.types import JsonDict
  17. from tests.utils import make_request, make_sydent
  18. class TestRequestCode(unittest.TestCase):
  19. def setUp(self) -> None:
  20. # Create a new sydent
  21. self.sydent = make_sydent()
  22. def _make_request(self, url: str, body: Optional[JsonDict] = None) -> Mock:
  23. # Patch out the email sending so we can investigate the resulting email.
  24. with patch("sydent.util.emailutils.smtplib") as smtplib:
  25. request, channel = make_request(
  26. self.sydent.reactor,
  27. self.sydent.clientApiHttpServer.factory,
  28. "POST",
  29. url,
  30. body,
  31. )
  32. self.assertEqual(channel.code, 200)
  33. # Fish out the SMTP object and return it.
  34. smtp = smtplib.SMTP.return_value
  35. smtp.sendmail.assert_called_once()
  36. return smtp
  37. def test_request_code(self) -> None:
  38. self.sydent.run()
  39. smtp = self._make_request(
  40. "/_matrix/identity/api/v1/validate/email/requestToken",
  41. {
  42. "email": "test@test",
  43. "client_secret": "oursecret",
  44. "send_attempt": 0,
  45. },
  46. )
  47. # Ensure the email is as expected.
  48. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  49. self.assertIn("Confirm your email address for Matrix", email_contents)
  50. def test_request_code_via_url_query_params(self) -> None:
  51. self.sydent.run()
  52. url = (
  53. "/_matrix/identity/api/v1/validate/email/requestToken?"
  54. "email=test@test"
  55. "&client_secret=oursecret"
  56. "&send_attempt=0"
  57. )
  58. smtp = self._make_request(url)
  59. # Ensure the email is as expected.
  60. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  61. self.assertIn("Confirm your email address for Matrix", email_contents)
  62. def test_branded_request_code(self) -> None:
  63. self.sydent.run()
  64. smtp = self._make_request(
  65. "/_matrix/identity/api/v1/validate/email/requestToken?brand=vector-im",
  66. {
  67. "email": "test@test",
  68. "client_secret": "oursecret",
  69. "send_attempt": 0,
  70. },
  71. )
  72. # Ensure the email is as expected.
  73. email_contents = smtp.sendmail.call_args[0][2].decode("utf-8")
  74. self.assertIn("Confirm your email address for Element", email_contents)