test_profile.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.server import HomeServer
  16. from synapse.storage.database import LoggingTransaction
  17. from synapse.storage.engines import PostgresEngine
  18. from synapse.types import UserID
  19. from synapse.util import Clock
  20. from tests import unittest
  21. class ProfileStoreTestCase(unittest.HomeserverTestCase):
  22. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  23. self.store = hs.get_datastores().main
  24. self.u_frank = UserID.from_string("@frank:test")
  25. def test_displayname(self) -> None:
  26. self.get_success(self.store.create_profile(self.u_frank))
  27. self.get_success(self.store.set_profile_displayname(self.u_frank, "Frank"))
  28. self.assertEqual(
  29. "Frank",
  30. (self.get_success(self.store.get_profile_displayname(self.u_frank))),
  31. )
  32. # test set to None
  33. self.get_success(self.store.set_profile_displayname(self.u_frank, None))
  34. self.assertIsNone(
  35. self.get_success(self.store.get_profile_displayname(self.u_frank))
  36. )
  37. def test_avatar_url(self) -> None:
  38. self.get_success(self.store.create_profile(self.u_frank))
  39. self.get_success(
  40. self.store.set_profile_avatar_url(self.u_frank, "http://my.site/here")
  41. )
  42. self.assertEqual(
  43. "http://my.site/here",
  44. (self.get_success(self.store.get_profile_avatar_url(self.u_frank))),
  45. )
  46. # test set to None
  47. self.get_success(self.store.set_profile_avatar_url(self.u_frank, None))
  48. self.assertIsNone(
  49. self.get_success(self.store.get_profile_avatar_url(self.u_frank))
  50. )
  51. def test_profiles_bg_migration(self) -> None:
  52. """
  53. Test background job that copies entries from column user_id to full_user_id, adding
  54. the hostname in the process.
  55. """
  56. updater = self.hs.get_datastores().main.db_pool.updates
  57. # drop the constraint so we can insert nulls in full_user_id to populate the test
  58. if isinstance(self.store.database_engine, PostgresEngine):
  59. def f(txn: LoggingTransaction) -> None:
  60. txn.execute(
  61. "ALTER TABLE profiles DROP CONSTRAINT full_user_id_not_null"
  62. )
  63. self.get_success(self.store.db_pool.runInteraction("", f))
  64. for i in range(70):
  65. self.get_success(
  66. self.store.db_pool.simple_insert(
  67. "profiles",
  68. {"user_id": f"hello{i:02}"},
  69. )
  70. )
  71. # re-add the constraint so that when it's validated it actually exists
  72. if isinstance(self.store.database_engine, PostgresEngine):
  73. def f(txn: LoggingTransaction) -> None:
  74. txn.execute(
  75. "ALTER TABLE profiles ADD CONSTRAINT full_user_id_not_null CHECK (full_user_id IS NOT NULL) NOT VALID"
  76. )
  77. self.get_success(self.store.db_pool.runInteraction("", f))
  78. self.get_success(
  79. self.store.db_pool.simple_insert(
  80. "background_updates",
  81. values={
  82. "update_name": "populate_full_user_id_profiles",
  83. "progress_json": "{}",
  84. },
  85. )
  86. )
  87. self.get_success(
  88. updater.run_background_updates(False),
  89. )
  90. expected_values = []
  91. for i in range(70):
  92. expected_values.append((f"@hello{i:02}:{self.hs.hostname}",))
  93. res = self.get_success(
  94. self.store.db_pool.execute(
  95. "", "SELECT full_user_id from profiles ORDER BY full_user_id"
  96. )
  97. )
  98. self.assertEqual(len(res), len(expected_values))
  99. self.assertEqual(res, expected_values)