test_registration.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 tests import unittest
  18. from tests.utils import setup_test_homeserver
  19. class RegistrationStoreTestCase(unittest.TestCase):
  20. @defer.inlineCallbacks
  21. def setUp(self):
  22. hs = yield setup_test_homeserver(self.addCleanup)
  23. self.db_pool = hs.get_db_pool()
  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 self.store.register(self.user_id, self.tokens[0], 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. "is_guest": 0,
  38. "consent_version": None,
  39. "consent_server_notice_sent": None,
  40. "appservice_id": None,
  41. "creation_ts": 1000,
  42. },
  43. (yield self.store.get_user_by_id(self.user_id)),
  44. )
  45. result = yield self.store.get_user_by_access_token(self.tokens[0])
  46. self.assertDictContainsSubset({"name": self.user_id}, result)
  47. self.assertTrue("token_id" in result)
  48. @defer.inlineCallbacks
  49. def test_add_tokens(self):
  50. yield self.store.register(self.user_id, self.tokens[0], self.pwhash)
  51. yield self.store.add_access_token_to_user(
  52. self.user_id, self.tokens[1], self.device_id
  53. )
  54. result = yield self.store.get_user_by_access_token(self.tokens[1])
  55. self.assertDictContainsSubset(
  56. {"name": self.user_id, "device_id": self.device_id}, result
  57. )
  58. self.assertTrue("token_id" in result)
  59. @defer.inlineCallbacks
  60. def test_user_delete_access_tokens(self):
  61. # add some tokens
  62. yield self.store.register(self.user_id, self.tokens[0], self.pwhash)
  63. yield self.store.add_access_token_to_user(
  64. self.user_id, self.tokens[1], self.device_id
  65. )
  66. # now delete some
  67. yield self.store.user_delete_access_tokens(
  68. self.user_id, device_id=self.device_id
  69. )
  70. # check they were deleted
  71. user = yield self.store.get_user_by_access_token(self.tokens[1])
  72. self.assertIsNone(user, "access token was not deleted by device_id")
  73. # check the one not associated with the device was not deleted
  74. user = yield self.store.get_user_by_access_token(self.tokens[0])
  75. self.assertEqual(self.user_id, user["name"])
  76. # now delete the rest
  77. yield self.store.user_delete_access_tokens(self.user_id)
  78. user = yield self.store.get_user_by_access_token(self.tokens[0])
  79. self.assertIsNone(user, "access token was not deleted without device_id")
  80. @defer.inlineCallbacks
  81. def test_is_support_user(self):
  82. TEST_USER = "@test:test"
  83. SUPPORT_USER = "@support:test"
  84. res = yield self.store.is_support_user(None)
  85. self.assertFalse(res)
  86. yield self.store.register(user_id=TEST_USER, token="123", password_hash=None)
  87. res = yield self.store.is_support_user(TEST_USER)
  88. self.assertFalse(res)
  89. yield self.store.register(
  90. user_id=SUPPORT_USER,
  91. token="456",
  92. password_hash=None,
  93. user_type=UserTypes.SUPPORT
  94. )
  95. res = yield self.store.is_support_user(SUPPORT_USER)
  96. self.assertTrue(res)
  97. class TokenGenerator:
  98. def __init__(self):
  99. self._last_issued_token = 0
  100. def generate(self, user_id):
  101. self._last_issued_token += 1
  102. return u"%s-%d" % (user_id, self._last_issued_token)