test_store_invite.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright 2021 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 unittest.mock import patch
  16. from parameterized import parameterized
  17. from twisted.trial import unittest
  18. from sydent.users.accounts import Account
  19. from tests.utils import make_request, make_sydent
  20. class StoreInviteTestCase(unittest.TestCase):
  21. """Tests Sydent's register servlet"""
  22. def setUp(self) -> None:
  23. # Create a new sydent
  24. config = {
  25. "general": {
  26. "templates.path": os.path.join(
  27. os.path.dirname(os.path.dirname(__file__)), "res"
  28. ),
  29. },
  30. "email": {
  31. "email.from": "Sydent Validation <noreply@hostname>",
  32. },
  33. }
  34. self.sydent = make_sydent(test_config=config)
  35. self.sender = "@alice:wonderland"
  36. @parameterized.expand(
  37. [
  38. ("not@an@email@address",),
  39. ("Naughty Nigel <perfectly.valid@mail.address>",),
  40. ]
  41. )
  42. def test_invalid_email_returns_400(self, address: str) -> None:
  43. self.sydent.run()
  44. request, channel = make_request(
  45. self.sydent.reactor,
  46. "POST",
  47. "/_matrix/identity/v2/account/store-invite",
  48. content={
  49. "address": address,
  50. "medium": "email",
  51. "room_id": "!myroom:test",
  52. "sender": self.sender,
  53. },
  54. )
  55. with patch("sydent.http.servlets.store_invite_servlet.authV2") as authV2:
  56. authV2.return_value = Account(self.sender, 0, None)
  57. request.render(self.sydent.servlets.storeInviteServletV2)
  58. self.assertEqual(channel.code, 400)
  59. self.assertEqual(channel.json_body["errcode"], "M_INVALID_EMAIL")