test_threepidunbind.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. request, channel = make_request(
  42. self.sydent.reactor,
  43. "POST",
  44. "/_matrix/identity/v2/3pid/unbind",
  45. content={
  46. "mxid": "@alice:wonderland",
  47. "threepid": {
  48. "address": "alice.cooper@wonderland.biz",
  49. "medium": "email",
  50. },
  51. },
  52. )
  53. with patch.object(
  54. self.sydent.sig_verifier, "authenticate_request", side_effect=exc
  55. ):
  56. request.render(self.sydent.servlets.threepidUnbind)
  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"])