test_registration.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # Copyright 2014-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 twisted.test.proto_helpers import MemoryReactor
  15. from synapse.api.constants import UserTypes
  16. from synapse.api.errors import ThreepidValidationError
  17. from synapse.server import HomeServer
  18. from synapse.types import JsonDict, UserID
  19. from synapse.util import Clock
  20. from tests.unittest import HomeserverTestCase, override_config
  21. class RegistrationStoreTestCase(HomeserverTestCase):
  22. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  23. self.store = hs.get_datastores().main
  24. self.user_id = "@my-user:test"
  25. self.tokens = ["AbCdEfGhIjKlMnOpQrStUvWxYz", "BcDeFgHiJkLmNoPqRsTuVwXyZa"]
  26. self.pwhash = "{xx1}123456789"
  27. self.device_id = "akgjhdjklgshg"
  28. def test_register(self) -> None:
  29. self.get_success(self.store.register_user(self.user_id, self.pwhash))
  30. self.assertEqual(
  31. {
  32. # TODO(paul): Surely this field should be 'user_id', not 'name'
  33. "name": self.user_id,
  34. "password_hash": self.pwhash,
  35. "admin": 0,
  36. "is_guest": 0,
  37. "consent_version": None,
  38. "consent_ts": None,
  39. "consent_server_notice_sent": None,
  40. "appservice_id": None,
  41. "creation_ts": 0,
  42. "user_type": None,
  43. "deactivated": 0,
  44. "shadow_banned": 0,
  45. "approved": 1,
  46. },
  47. (self.get_success(self.store.get_user_by_id(self.user_id))),
  48. )
  49. def test_consent(self) -> None:
  50. self.get_success(self.store.register_user(self.user_id, self.pwhash))
  51. before_consent = self.clock.time_msec()
  52. self.reactor.advance(5)
  53. self.get_success(self.store.user_set_consent_version(self.user_id, "1"))
  54. self.reactor.advance(5)
  55. user = self.get_success(self.store.get_user_by_id(self.user_id))
  56. assert user
  57. self.assertEqual(user["consent_version"], "1")
  58. self.assertGreater(user["consent_ts"], before_consent)
  59. self.assertLess(user["consent_ts"], self.clock.time_msec())
  60. def test_add_tokens(self) -> None:
  61. self.get_success(self.store.register_user(self.user_id, self.pwhash))
  62. self.get_success(
  63. self.store.add_access_token_to_user(
  64. self.user_id, self.tokens[1], self.device_id, valid_until_ms=None
  65. )
  66. )
  67. result = self.get_success(self.store.get_user_by_access_token(self.tokens[1]))
  68. assert result
  69. self.assertEqual(result.user_id, self.user_id)
  70. self.assertEqual(result.device_id, self.device_id)
  71. self.assertIsNotNone(result.token_id)
  72. def test_user_delete_access_tokens(self) -> None:
  73. # add some tokens
  74. self.get_success(self.store.register_user(self.user_id, self.pwhash))
  75. self.get_success(
  76. self.store.add_access_token_to_user(
  77. self.user_id, self.tokens[0], device_id=None, valid_until_ms=None
  78. )
  79. )
  80. self.get_success(
  81. self.store.add_access_token_to_user(
  82. self.user_id, self.tokens[1], self.device_id, valid_until_ms=None
  83. )
  84. )
  85. # now delete some
  86. self.get_success(
  87. self.store.user_delete_access_tokens(self.user_id, device_id=self.device_id)
  88. )
  89. # check they were deleted
  90. user = self.get_success(self.store.get_user_by_access_token(self.tokens[1]))
  91. self.assertIsNone(user, "access token was not deleted by device_id")
  92. # check the one not associated with the device was not deleted
  93. user = self.get_success(self.store.get_user_by_access_token(self.tokens[0]))
  94. assert user
  95. self.assertEqual(self.user_id, user.user_id)
  96. # now delete the rest
  97. self.get_success(self.store.user_delete_access_tokens(self.user_id))
  98. user = self.get_success(self.store.get_user_by_access_token(self.tokens[0]))
  99. self.assertIsNone(user, "access token was not deleted without device_id")
  100. def test_is_support_user(self) -> None:
  101. TEST_USER = "@test:test"
  102. SUPPORT_USER = "@support:test"
  103. res = self.get_success(self.store.is_support_user(None)) # type: ignore[arg-type]
  104. self.assertFalse(res)
  105. self.get_success(
  106. self.store.register_user(user_id=TEST_USER, password_hash=None)
  107. )
  108. res = self.get_success(self.store.is_support_user(TEST_USER))
  109. self.assertFalse(res)
  110. self.get_success(
  111. self.store.register_user(
  112. user_id=SUPPORT_USER, password_hash=None, user_type=UserTypes.SUPPORT
  113. )
  114. )
  115. res = self.get_success(self.store.is_support_user(SUPPORT_USER))
  116. self.assertTrue(res)
  117. def test_3pid_inhibit_invalid_validation_session_error(self) -> None:
  118. """Tests that enabling the configuration option to inhibit 3PID errors on
  119. /requestToken also inhibits validation errors caused by an unknown session ID.
  120. """
  121. # Check that, with the config setting set to false (the default value), a
  122. # validation error is caused by the unknown session ID.
  123. e = self.get_failure(
  124. self.store.validate_threepid_session(
  125. "fake_sid",
  126. "fake_client_secret",
  127. "fake_token",
  128. 0,
  129. ),
  130. ThreepidValidationError,
  131. )
  132. self.assertEqual(e.value.msg, "Unknown session_id", e)
  133. # Set the config setting to true.
  134. self.store._ignore_unknown_session_error = True
  135. # Check that now the validation error is caused by the token not matching.
  136. e = self.get_failure(
  137. self.store.validate_threepid_session(
  138. "fake_sid",
  139. "fake_client_secret",
  140. "fake_token",
  141. 0,
  142. ),
  143. ThreepidValidationError,
  144. )
  145. self.assertEqual(e.value.msg, "Validation token not found or has expired", e)
  146. class ApprovalRequiredRegistrationTestCase(HomeserverTestCase):
  147. def default_config(self) -> JsonDict:
  148. config = super().default_config()
  149. # If there's already some config for this feature in the default config, it
  150. # means we're overriding it with @override_config. In this case we don't want
  151. # to do anything more with it.
  152. msc3866_config = config.get("experimental_features", {}).get("msc3866")
  153. if msc3866_config is not None:
  154. return config
  155. # Require approval for all new accounts.
  156. config["experimental_features"] = {
  157. "msc3866": {
  158. "enabled": True,
  159. "require_approval_for_new_accounts": True,
  160. }
  161. }
  162. return config
  163. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  164. self.store = hs.get_datastores().main
  165. self.user_id = "@my-user:test"
  166. self.pwhash = "{xx1}123456789"
  167. @override_config(
  168. {
  169. "experimental_features": {
  170. "msc3866": {
  171. "enabled": True,
  172. "require_approval_for_new_accounts": False,
  173. }
  174. }
  175. }
  176. )
  177. def test_approval_not_required(self) -> None:
  178. """Tests that if we don't require approval for new accounts, newly created
  179. accounts are automatically marked as approved.
  180. """
  181. self.get_success(self.store.register_user(self.user_id, self.pwhash))
  182. user = self.get_success(self.store.get_user_by_id(self.user_id))
  183. assert user is not None
  184. self.assertTrue(user["approved"])
  185. approved = self.get_success(self.store.is_user_approved(self.user_id))
  186. self.assertTrue(approved)
  187. def test_approval_required(self) -> None:
  188. """Tests that if we require approval for new accounts, newly created accounts
  189. are not automatically marked as approved.
  190. """
  191. self.get_success(self.store.register_user(self.user_id, self.pwhash))
  192. user = self.get_success(self.store.get_user_by_id(self.user_id))
  193. assert user is not None
  194. self.assertFalse(user["approved"])
  195. approved = self.get_success(self.store.is_user_approved(self.user_id))
  196. self.assertFalse(approved)
  197. def test_override(self) -> None:
  198. """Tests that if we require approval for new accounts, but we explicitly say the
  199. new user should be considered approved, they're marked as approved.
  200. """
  201. self.get_success(
  202. self.store.register_user(
  203. self.user_id,
  204. self.pwhash,
  205. approved=True,
  206. )
  207. )
  208. user = self.get_success(self.store.get_user_by_id(self.user_id))
  209. self.assertIsNotNone(user)
  210. assert user is not None
  211. self.assertEqual(user["approved"], 1)
  212. approved = self.get_success(self.store.is_user_approved(self.user_id))
  213. self.assertTrue(approved)
  214. def test_approve_user(self) -> None:
  215. """Tests that approving the user updates their approval status."""
  216. self.get_success(self.store.register_user(self.user_id, self.pwhash))
  217. approved = self.get_success(self.store.is_user_approved(self.user_id))
  218. self.assertFalse(approved)
  219. self.get_success(
  220. self.store.update_user_approval_status(
  221. UserID.from_string(self.user_id), True
  222. )
  223. )
  224. approved = self.get_success(self.store.is_user_approved(self.user_id))
  225. self.assertTrue(approved)