test_threepidunbind.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. from http import HTTPStatus
  15. from unittest.mock import patch
  16. import twisted.internet.error
  17. import twisted.web.client
  18. from parameterized import parameterized
  19. from twisted.trial import unittest
  20. from tests.utils import make_request, make_sydent
  21. class ThreepidUnbindTestCase(unittest.TestCase):
  22. """Tests Sydent's threepidunbind servlet"""
  23. def setUp(self) -> None:
  24. # Create a new sydent
  25. self.sydent = make_sydent()
  26. # Duplicated from TestRegisterServelet. Is there a way for us to keep
  27. # ourselves DRY?
  28. @parameterized.expand(
  29. [
  30. (twisted.internet.error.DNSLookupError(),),
  31. (twisted.internet.error.TimeoutError(),),
  32. (twisted.internet.error.ConnectionRefusedError(),),
  33. # Naughty: strictly we're supposed to initialise a ResponseNeverReceived
  34. # with a list of 1 or more failures.
  35. (twisted.web.client.ResponseNeverReceived([]),),
  36. ]
  37. )
  38. def test_connection_failure(self, exc: Exception) -> None:
  39. """Check we respond sensibly if we can't contact the homeserver."""
  40. self.sydent.run()
  41. with patch.object(
  42. self.sydent.sig_verifier, "authenticate_request", side_effect=exc
  43. ):
  44. request, channel = make_request(
  45. self.sydent.reactor,
  46. self.sydent.clientApiHttpServer.factory,
  47. "POST",
  48. "/_matrix/identity/v2/3pid/unbind",
  49. content={
  50. "mxid": "@alice:wonderland",
  51. "threepid": {
  52. "address": "alice.cooper@wonderland.biz",
  53. "medium": "email",
  54. },
  55. },
  56. )
  57. self.assertEqual(channel.code, HTTPStatus.INTERNAL_SERVER_ERROR)
  58. self.assertEqual(channel.json_body["errcode"], "M_UNKNOWN")
  59. self.assertIn("contact", channel.json_body["error"])