utils.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2018-2019 New Vector 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. import atexit
  16. import os
  17. from synapse.api.constants import EventTypes
  18. from synapse.api.room_versions import RoomVersions
  19. from synapse.config.homeserver import HomeServerConfig
  20. from synapse.config.server import DEFAULT_ROOM_VERSION
  21. from synapse.logging.context import current_context, set_current_context
  22. from synapse.storage.database import LoggingDatabaseConnection
  23. from synapse.storage.engines import create_engine
  24. from synapse.storage.prepare_database import prepare_database
  25. # set this to True to run the tests against postgres instead of sqlite.
  26. #
  27. # When running under postgres, we first create a base database with the name
  28. # POSTGRES_BASE_DB and update it to the current schema. Then, for each test case, we
  29. # create another unique database, using the base database as a template.
  30. USE_POSTGRES_FOR_TESTS = os.environ.get("SYNAPSE_POSTGRES", False)
  31. LEAVE_DB = os.environ.get("SYNAPSE_LEAVE_DB", False)
  32. POSTGRES_USER = os.environ.get("SYNAPSE_POSTGRES_USER", None)
  33. POSTGRES_HOST = os.environ.get("SYNAPSE_POSTGRES_HOST", None)
  34. POSTGRES_PASSWORD = os.environ.get("SYNAPSE_POSTGRES_PASSWORD", None)
  35. POSTGRES_PORT = (
  36. int(os.environ["SYNAPSE_POSTGRES_PORT"])
  37. if "SYNAPSE_POSTGRES_PORT" in os.environ
  38. else None
  39. )
  40. POSTGRES_BASE_DB = "_synapse_unit_tests_base_%s" % (os.getpid(),)
  41. # When debugging a specific test, it's occasionally useful to write the
  42. # DB to disk and query it with the sqlite CLI.
  43. SQLITE_PERSIST_DB = os.environ.get("SYNAPSE_TEST_PERSIST_SQLITE_DB") is not None
  44. # the dbname we will connect to in order to create the base database.
  45. POSTGRES_DBNAME_FOR_INITIAL_CREATE = "postgres"
  46. def setupdb():
  47. # If we're using PostgreSQL, set up the db once
  48. if USE_POSTGRES_FOR_TESTS:
  49. # create a PostgresEngine
  50. db_engine = create_engine({"name": "psycopg2", "args": {}})
  51. # connect to postgres to create the base database.
  52. db_conn = db_engine.module.connect(
  53. user=POSTGRES_USER,
  54. host=POSTGRES_HOST,
  55. port=POSTGRES_PORT,
  56. password=POSTGRES_PASSWORD,
  57. dbname=POSTGRES_DBNAME_FOR_INITIAL_CREATE,
  58. )
  59. db_conn.autocommit = True
  60. cur = db_conn.cursor()
  61. cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB,))
  62. cur.execute(
  63. "CREATE DATABASE %s ENCODING 'UTF8' LC_COLLATE='C' LC_CTYPE='C' "
  64. "template=template0;" % (POSTGRES_BASE_DB,)
  65. )
  66. cur.close()
  67. db_conn.close()
  68. # Set up in the db
  69. db_conn = db_engine.module.connect(
  70. database=POSTGRES_BASE_DB,
  71. user=POSTGRES_USER,
  72. host=POSTGRES_HOST,
  73. port=POSTGRES_PORT,
  74. password=POSTGRES_PASSWORD,
  75. )
  76. db_conn = LoggingDatabaseConnection(db_conn, db_engine, "tests")
  77. prepare_database(db_conn, db_engine, None)
  78. db_conn.close()
  79. def _cleanup():
  80. db_conn = db_engine.module.connect(
  81. user=POSTGRES_USER,
  82. host=POSTGRES_HOST,
  83. port=POSTGRES_PORT,
  84. password=POSTGRES_PASSWORD,
  85. dbname=POSTGRES_DBNAME_FOR_INITIAL_CREATE,
  86. )
  87. db_conn.autocommit = True
  88. cur = db_conn.cursor()
  89. cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB,))
  90. cur.close()
  91. db_conn.close()
  92. atexit.register(_cleanup)
  93. def default_config(name, parse=False):
  94. """
  95. Create a reasonable test config.
  96. """
  97. config_dict = {
  98. "server_name": name,
  99. "send_federation": False,
  100. "media_store_path": "media",
  101. # the test signing key is just an arbitrary ed25519 key to keep the config
  102. # parser happy
  103. "signing_key": "ed25519 a_lPym qvioDNmfExFBRPgdTU+wtFYKq4JfwFRv7sYVgWvmgJg",
  104. "event_cache_size": 1,
  105. "enable_registration": True,
  106. "enable_registration_captcha": False,
  107. "macaroon_secret_key": "not even a little secret",
  108. "password_providers": [],
  109. "worker_replication_url": "",
  110. "worker_app": None,
  111. "block_non_admin_invites": False,
  112. "federation_domain_whitelist": None,
  113. "filter_timeline_limit": 5000,
  114. "user_directory_search_all_users": False,
  115. "user_consent_server_notice_content": None,
  116. "block_events_without_consent_error": None,
  117. "user_consent_at_registration": False,
  118. "user_consent_policy_name": "Privacy Policy",
  119. "media_storage_providers": [],
  120. "autocreate_auto_join_rooms": True,
  121. "auto_join_rooms": [],
  122. "limit_usage_by_mau": False,
  123. "hs_disabled": False,
  124. "hs_disabled_message": "",
  125. "max_mau_value": 50,
  126. "mau_trial_days": 0,
  127. "mau_stats_only": False,
  128. "mau_limits_reserved_threepids": [],
  129. "admin_contact": None,
  130. "rc_message": {"per_second": 10000, "burst_count": 10000},
  131. "rc_registration": {"per_second": 10000, "burst_count": 10000},
  132. "rc_login": {
  133. "address": {"per_second": 10000, "burst_count": 10000},
  134. "account": {"per_second": 10000, "burst_count": 10000},
  135. "failed_attempts": {"per_second": 10000, "burst_count": 10000},
  136. },
  137. "rc_joins": {
  138. "local": {"per_second": 10000, "burst_count": 10000},
  139. "remote": {"per_second": 10000, "burst_count": 10000},
  140. },
  141. "rc_invites": {
  142. "per_room": {"per_second": 10000, "burst_count": 10000},
  143. "per_user": {"per_second": 10000, "burst_count": 10000},
  144. },
  145. "rc_3pid_validation": {"per_second": 10000, "burst_count": 10000},
  146. "saml2_enabled": False,
  147. "public_baseurl": None,
  148. "default_identity_server": None,
  149. "key_refresh_interval": 24 * 60 * 60 * 1000,
  150. "old_signing_keys": {},
  151. "tls_fingerprints": [],
  152. "use_frozen_dicts": False,
  153. # We need a sane default_room_version, otherwise attempts to create
  154. # rooms will fail.
  155. "default_room_version": DEFAULT_ROOM_VERSION,
  156. # disable user directory updates, because they get done in the
  157. # background, which upsets the test runner.
  158. "update_user_directory": False,
  159. "caches": {"global_factor": 1},
  160. "listeners": [{"port": 0, "type": "http"}],
  161. }
  162. if parse:
  163. config = HomeServerConfig()
  164. config.parse_config_dict(config_dict, "", "")
  165. return config
  166. return config_dict
  167. def mock_getRawHeaders(headers=None):
  168. headers = headers if headers is not None else {}
  169. def getRawHeaders(name, default=None):
  170. return headers.get(name, default)
  171. return getRawHeaders
  172. class MockClock:
  173. now = 1000
  174. def __init__(self):
  175. # list of lists of [absolute_time, callback, expired] in no particular
  176. # order
  177. self.timers = []
  178. self.loopers = []
  179. def time(self):
  180. return self.now
  181. def time_msec(self):
  182. return self.time() * 1000
  183. def call_later(self, delay, callback, *args, **kwargs):
  184. ctx = current_context()
  185. def wrapped_callback():
  186. set_current_context(ctx)
  187. callback(*args, **kwargs)
  188. t = [self.now + delay, wrapped_callback, False]
  189. self.timers.append(t)
  190. return t
  191. def looping_call(self, function, interval, *args, **kwargs):
  192. self.loopers.append([function, interval / 1000.0, self.now, args, kwargs])
  193. def cancel_call_later(self, timer, ignore_errs=False):
  194. if timer[2]:
  195. if not ignore_errs:
  196. raise Exception("Cannot cancel an expired timer")
  197. timer[2] = True
  198. self.timers = [t for t in self.timers if t != timer]
  199. # For unit testing
  200. def advance_time(self, secs):
  201. self.now += secs
  202. timers = self.timers
  203. self.timers = []
  204. for t in timers:
  205. time, callback, expired = t
  206. if expired:
  207. raise Exception("Timer already expired")
  208. if self.now >= time:
  209. t[2] = True
  210. callback()
  211. else:
  212. self.timers.append(t)
  213. for looped in self.loopers:
  214. func, interval, last, args, kwargs = looped
  215. if last + interval < self.now:
  216. func(*args, **kwargs)
  217. looped[2] = self.now
  218. def advance_time_msec(self, ms):
  219. self.advance_time(ms / 1000.0)
  220. def time_bound_deferred(self, d, *args, **kwargs):
  221. # We don't bother timing things out for now.
  222. return d
  223. async def create_room(hs, room_id: str, creator_id: str):
  224. """Creates and persist a creation event for the given room"""
  225. persistence_store = hs.get_storage().persistence
  226. store = hs.get_datastores().main
  227. event_builder_factory = hs.get_event_builder_factory()
  228. event_creation_handler = hs.get_event_creation_handler()
  229. await store.store_room(
  230. room_id=room_id,
  231. room_creator_user_id=creator_id,
  232. is_public=False,
  233. room_version=RoomVersions.V1,
  234. )
  235. builder = event_builder_factory.for_room_version(
  236. RoomVersions.V1,
  237. {
  238. "type": EventTypes.Create,
  239. "state_key": "",
  240. "sender": creator_id,
  241. "room_id": room_id,
  242. "content": {},
  243. },
  244. )
  245. event, context = await event_creation_handler.create_new_client_event(builder)
  246. await persistence_store.persist_event(event, context)