test_registration.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.internet import defer
  16. from synapse.api.constants import UserTypes
  17. from synapse.api.errors import ThreepidValidationError
  18. from tests import unittest
  19. from tests.utils import setup_test_homeserver
  20. class RegistrationStoreTestCase(unittest.TestCase):
  21. @defer.inlineCallbacks
  22. def setUp(self):
  23. hs = yield setup_test_homeserver(self.addCleanup)
  24. self.store = hs.get_datastore()
  25. self.user_id = "@my-user:test"
  26. self.tokens = ["AbCdEfGhIjKlMnOpQrStUvWxYz", "BcDeFgHiJkLmNoPqRsTuVwXyZa"]
  27. self.pwhash = "{xx1}123456789"
  28. self.device_id = "akgjhdjklgshg"
  29. @defer.inlineCallbacks
  30. def test_register(self):
  31. yield defer.ensureDeferred(self.store.register_user(self.user_id, self.pwhash))
  32. self.assertEquals(
  33. {
  34. # TODO(paul): Surely this field should be 'user_id', not 'name'
  35. "name": self.user_id,
  36. "password_hash": self.pwhash,
  37. "admin": 0,
  38. "is_guest": 0,
  39. "consent_version": None,
  40. "consent_server_notice_sent": None,
  41. "appservice_id": None,
  42. "creation_ts": 1000,
  43. "user_type": None,
  44. "deactivated": 0,
  45. },
  46. (yield defer.ensureDeferred(self.store.get_user_by_id(self.user_id))),
  47. )
  48. @defer.inlineCallbacks
  49. def test_add_tokens(self):
  50. yield defer.ensureDeferred(self.store.register_user(self.user_id, self.pwhash))
  51. yield defer.ensureDeferred(
  52. self.store.add_access_token_to_user(
  53. self.user_id, self.tokens[1], self.device_id, valid_until_ms=None
  54. )
  55. )
  56. result = yield defer.ensureDeferred(
  57. self.store.get_user_by_access_token(self.tokens[1])
  58. )
  59. self.assertDictContainsSubset(
  60. {"name": self.user_id, "device_id": self.device_id}, result
  61. )
  62. self.assertTrue("token_id" in result)
  63. @defer.inlineCallbacks
  64. def test_user_delete_access_tokens(self):
  65. # add some tokens
  66. yield defer.ensureDeferred(self.store.register_user(self.user_id, self.pwhash))
  67. yield defer.ensureDeferred(
  68. self.store.add_access_token_to_user(
  69. self.user_id, self.tokens[0], device_id=None, valid_until_ms=None
  70. )
  71. )
  72. yield defer.ensureDeferred(
  73. self.store.add_access_token_to_user(
  74. self.user_id, self.tokens[1], self.device_id, valid_until_ms=None
  75. )
  76. )
  77. # now delete some
  78. yield defer.ensureDeferred(
  79. self.store.user_delete_access_tokens(self.user_id, device_id=self.device_id)
  80. )
  81. # check they were deleted
  82. user = yield defer.ensureDeferred(
  83. self.store.get_user_by_access_token(self.tokens[1])
  84. )
  85. self.assertIsNone(user, "access token was not deleted by device_id")
  86. # check the one not associated with the device was not deleted
  87. user = yield defer.ensureDeferred(
  88. self.store.get_user_by_access_token(self.tokens[0])
  89. )
  90. self.assertEqual(self.user_id, user["name"])
  91. # now delete the rest
  92. yield defer.ensureDeferred(self.store.user_delete_access_tokens(self.user_id))
  93. user = yield defer.ensureDeferred(
  94. self.store.get_user_by_access_token(self.tokens[0])
  95. )
  96. self.assertIsNone(user, "access token was not deleted without device_id")
  97. @defer.inlineCallbacks
  98. def test_is_support_user(self):
  99. TEST_USER = "@test:test"
  100. SUPPORT_USER = "@support:test"
  101. res = yield defer.ensureDeferred(self.store.is_support_user(None))
  102. self.assertFalse(res)
  103. yield defer.ensureDeferred(
  104. self.store.register_user(user_id=TEST_USER, password_hash=None)
  105. )
  106. res = yield defer.ensureDeferred(self.store.is_support_user(TEST_USER))
  107. self.assertFalse(res)
  108. yield defer.ensureDeferred(
  109. self.store.register_user(
  110. user_id=SUPPORT_USER, password_hash=None, user_type=UserTypes.SUPPORT
  111. )
  112. )
  113. res = yield defer.ensureDeferred(self.store.is_support_user(SUPPORT_USER))
  114. self.assertTrue(res)
  115. @defer.inlineCallbacks
  116. def test_3pid_inhibit_invalid_validation_session_error(self):
  117. """Tests that enabling the configuration option to inhibit 3PID errors on
  118. /requestToken also inhibits validation errors caused by an unknown session ID.
  119. """
  120. # Check that, with the config setting set to false (the default value), a
  121. # validation error is caused by the unknown session ID.
  122. try:
  123. yield defer.ensureDeferred(
  124. self.store.validate_threepid_session(
  125. "fake_sid", "fake_client_secret", "fake_token", 0,
  126. )
  127. )
  128. except ThreepidValidationError as e:
  129. self.assertEquals(e.msg, "Unknown session_id", e)
  130. # Set the config setting to true.
  131. self.store._ignore_unknown_session_error = True
  132. # Check that now the validation error is caused by the token not matching.
  133. try:
  134. yield defer.ensureDeferred(
  135. self.store.validate_threepid_session(
  136. "fake_sid", "fake_client_secret", "fake_token", 0,
  137. )
  138. )
  139. except ThreepidValidationError as e:
  140. self.assertEquals(e.msg, "Validation token not found or has expired", e)