1
0

test_new_matrix_user.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright 2018 New Vector
  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 typing import List, Optional
  15. from unittest.mock import Mock, patch
  16. from synapse._scripts.register_new_matrix_user import request_registration
  17. from synapse.types import JsonDict
  18. from tests.unittest import TestCase
  19. class RegisterTestCase(TestCase):
  20. def test_success(self) -> None:
  21. """
  22. The script will fetch a nonce, and then generate a MAC with it, and then
  23. post that MAC.
  24. """
  25. def get(url: str, verify: Optional[bool] = None) -> Mock:
  26. r = Mock()
  27. r.status_code = 200
  28. r.json = lambda: {"nonce": "a"}
  29. return r
  30. def post(
  31. url: str, json: Optional[JsonDict] = None, verify: Optional[bool] = None
  32. ) -> Mock:
  33. # Make sure we are sent the correct info
  34. assert json is not None
  35. self.assertEqual(json["username"], "user")
  36. self.assertEqual(json["password"], "pass")
  37. self.assertEqual(json["nonce"], "a")
  38. # We want a 40-char hex MAC
  39. self.assertEqual(len(json["mac"]), 40)
  40. r = Mock()
  41. r.status_code = 200
  42. return r
  43. requests = Mock()
  44. requests.get = get
  45. requests.post = post
  46. # The fake stdout will be written here
  47. out: List[str] = []
  48. err_code: List[int] = []
  49. with patch("synapse._scripts.register_new_matrix_user.requests", requests):
  50. request_registration(
  51. "user",
  52. "pass",
  53. "matrix.org",
  54. "shared",
  55. admin=False,
  56. _print=out.append,
  57. exit=err_code.append,
  58. )
  59. # We should get the success message making sure everything is OK.
  60. self.assertIn("Success!", out)
  61. # sys.exit shouldn't have been called.
  62. self.assertEqual(err_code, [])
  63. def test_failure_nonce(self) -> None:
  64. """
  65. If the script fails to fetch a nonce, it throws an error and quits.
  66. """
  67. def get(url: str, verify: Optional[bool] = None) -> Mock:
  68. r = Mock()
  69. r.status_code = 404
  70. r.reason = "Not Found"
  71. r.json = lambda: {"not": "error"}
  72. return r
  73. requests = Mock()
  74. requests.get = get
  75. # The fake stdout will be written here
  76. out: List[str] = []
  77. err_code: List[int] = []
  78. with patch("synapse._scripts.register_new_matrix_user.requests", requests):
  79. request_registration(
  80. "user",
  81. "pass",
  82. "matrix.org",
  83. "shared",
  84. admin=False,
  85. _print=out.append,
  86. exit=err_code.append,
  87. )
  88. # Exit was called
  89. self.assertEqual(err_code, [1])
  90. # We got an error message
  91. self.assertIn("ERROR! Received 404 Not Found", out)
  92. self.assertNotIn("Success!", out)
  93. def test_failure_post(self) -> None:
  94. """
  95. The script will fetch a nonce, and then if the final POST fails, will
  96. report an error and quit.
  97. """
  98. def get(url: str, verify: Optional[bool] = None) -> Mock:
  99. r = Mock()
  100. r.status_code = 200
  101. r.json = lambda: {"nonce": "a"}
  102. return r
  103. def post(
  104. url: str, json: Optional[JsonDict] = None, verify: Optional[bool] = None
  105. ) -> Mock:
  106. # Make sure we are sent the correct info
  107. assert json is not None
  108. self.assertEqual(json["username"], "user")
  109. self.assertEqual(json["password"], "pass")
  110. self.assertEqual(json["nonce"], "a")
  111. # We want a 40-char hex MAC
  112. self.assertEqual(len(json["mac"]), 40)
  113. r = Mock()
  114. # Then 500 because we're jerks
  115. r.status_code = 500
  116. r.reason = "Broken"
  117. return r
  118. requests = Mock()
  119. requests.get = get
  120. requests.post = post
  121. # The fake stdout will be written here
  122. out: List[str] = []
  123. err_code: List[int] = []
  124. with patch("synapse._scripts.register_new_matrix_user.requests", requests):
  125. request_registration(
  126. "user",
  127. "pass",
  128. "matrix.org",
  129. "shared",
  130. admin=False,
  131. _print=out.append,
  132. exit=err_code.append,
  133. )
  134. # Exit was called
  135. self.assertEqual(err_code, [1])
  136. # We got an error message
  137. self.assertIn("ERROR! Received 500 Broken", out)
  138. self.assertNotIn("Success!", out)