test_new_matrix_user.py 4.4 KB

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