test_store_invite.py 2.1 KB

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