You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

271 lines
10 KiB

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