test_registration.py 10 KB

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