unittest.py 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2018 New Vector
  3. # Copyright 2019 Matrix.org Federation C.I.C
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import functools
  17. import gc
  18. import hashlib
  19. import hmac
  20. import json
  21. import logging
  22. import secrets
  23. import time
  24. from typing import (
  25. Any,
  26. Awaitable,
  27. Callable,
  28. ClassVar,
  29. Dict,
  30. Generic,
  31. Iterable,
  32. List,
  33. NoReturn,
  34. Optional,
  35. Tuple,
  36. Type,
  37. TypeVar,
  38. Union,
  39. )
  40. from unittest.mock import Mock, patch
  41. import canonicaljson
  42. import signedjson.key
  43. import unpaddedbase64
  44. from typing_extensions import Concatenate, ParamSpec, Protocol
  45. from twisted.internet.defer import Deferred, ensureDeferred
  46. from twisted.python.failure import Failure
  47. from twisted.python.threadpool import ThreadPool
  48. from twisted.test.proto_helpers import MemoryReactor, MemoryReactorClock
  49. from twisted.trial import unittest
  50. from twisted.web.resource import Resource
  51. from twisted.web.server import Request
  52. from synapse import events
  53. from synapse.api.constants import EventTypes
  54. from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion
  55. from synapse.config._base import Config, RootConfig
  56. from synapse.config.homeserver import HomeServerConfig
  57. from synapse.config.server import DEFAULT_ROOM_VERSION
  58. from synapse.crypto.event_signing import add_hashes_and_signatures
  59. from synapse.federation.transport.server import TransportLayerServer
  60. from synapse.http.server import JsonResource
  61. from synapse.http.site import SynapseRequest, SynapseSite
  62. from synapse.logging.context import (
  63. SENTINEL_CONTEXT,
  64. LoggingContext,
  65. current_context,
  66. set_current_context,
  67. )
  68. from synapse.rest import RegisterServletsFunc
  69. from synapse.server import HomeServer
  70. from synapse.types import JsonDict, Requester, UserID, create_requester
  71. from synapse.util import Clock
  72. from synapse.util.httpresourcetree import create_resource_tree
  73. from tests.server import (
  74. CustomHeaderType,
  75. FakeChannel,
  76. ThreadedMemoryReactorClock,
  77. get_clock,
  78. make_request,
  79. setup_test_homeserver,
  80. )
  81. from tests.test_utils import event_injection, setup_awaitable_errors
  82. from tests.test_utils.logging_setup import setup_logging
  83. from tests.utils import checked_cast, default_config, setupdb
  84. setupdb()
  85. setup_logging()
  86. TV = TypeVar("TV")
  87. _ExcType = TypeVar("_ExcType", bound=BaseException, covariant=True)
  88. P = ParamSpec("P")
  89. R = TypeVar("R")
  90. S = TypeVar("S")
  91. class _TypedFailure(Generic[_ExcType], Protocol):
  92. """Extension to twisted.Failure, where the 'value' has a certain type."""
  93. @property
  94. def value(self) -> _ExcType:
  95. ...
  96. def around(target: TV) -> Callable[[Callable[Concatenate[S, P], R]], None]:
  97. """A CLOS-style 'around' modifier, which wraps the original method of the
  98. given instance with another piece of code.
  99. @around(self)
  100. def method_name(orig, *args, **kwargs):
  101. return orig(*args, **kwargs)
  102. """
  103. def _around(code: Callable[Concatenate[S, P], R]) -> None:
  104. name = code.__name__
  105. orig = getattr(target, name)
  106. def new(*args: P.args, **kwargs: P.kwargs) -> R:
  107. return code(orig, *args, **kwargs)
  108. setattr(target, name, new)
  109. return _around
  110. _TConfig = TypeVar("_TConfig", Config, RootConfig)
  111. def deepcopy_config(config: _TConfig) -> _TConfig:
  112. new_config: _TConfig
  113. if isinstance(config, RootConfig):
  114. new_config = config.__class__(config.config_files) # type: ignore[arg-type]
  115. else:
  116. new_config = config.__class__(config.root)
  117. for attr_name in config.__dict__:
  118. if attr_name.startswith("__") or attr_name == "root":
  119. continue
  120. attr = getattr(config, attr_name)
  121. if isinstance(attr, Config):
  122. new_attr = deepcopy_config(attr)
  123. else:
  124. new_attr = attr
  125. setattr(new_config, attr_name, new_attr)
  126. return new_config
  127. @functools.lru_cache(maxsize=8)
  128. def _parse_config_dict(config: str) -> RootConfig:
  129. config_obj = HomeServerConfig()
  130. config_obj.parse_config_dict(json.loads(config), "", "")
  131. return config_obj
  132. def make_homeserver_config_obj(config: Dict[str, Any]) -> RootConfig:
  133. """Creates a :class:`HomeServerConfig` instance with the given configuration dict.
  134. This is equivalent to::
  135. config_obj = HomeServerConfig()
  136. config_obj.parse_config_dict(config, "", "")
  137. but it keeps a cache of `HomeServerConfig` instances and deepcopies them as needed,
  138. to avoid validating the whole configuration every time.
  139. """
  140. config_obj = _parse_config_dict(json.dumps(config, sort_keys=True))
  141. return deepcopy_config(config_obj)
  142. class TestCase(unittest.TestCase):
  143. """A subclass of twisted.trial's TestCase which looks for 'loglevel'
  144. attributes on both itself and its individual test methods, to override the
  145. root logger's logging level while that test (case|method) runs."""
  146. def __init__(self, methodName: str):
  147. super().__init__(methodName)
  148. method = getattr(self, methodName)
  149. level = getattr(method, "loglevel", getattr(self, "loglevel", None))
  150. @around(self)
  151. def setUp(orig: Callable[[], R]) -> R:
  152. # if we're not starting in the sentinel logcontext, then to be honest
  153. # all future bets are off.
  154. if current_context():
  155. self.fail(
  156. "Test starting with non-sentinel logging context %s"
  157. % (current_context(),)
  158. )
  159. # Disable GC for duration of test. See below for why.
  160. gc.disable()
  161. old_level = logging.getLogger().level
  162. if level is not None and old_level != level:
  163. @around(self)
  164. def tearDown(orig: Callable[[], R]) -> R:
  165. ret = orig()
  166. logging.getLogger().setLevel(old_level)
  167. return ret
  168. logging.getLogger().setLevel(level)
  169. # Trial messes with the warnings configuration, thus this has to be
  170. # done in the context of an individual TestCase.
  171. self.addCleanup(setup_awaitable_errors())
  172. return orig()
  173. # We want to force a GC to workaround problems with deferreds leaking
  174. # logcontexts when they are GCed (see the logcontext docs).
  175. #
  176. # The easiest way to do this would be to do a full GC after each test
  177. # run, but that is very expensive. Instead, we disable GC (above) for
  178. # the duration of the test and only run a gen-0 GC, which is a lot
  179. # quicker. This doesn't clean up everything, since the TestCase
  180. # instance still holds references to objects created during the test,
  181. # such as HomeServers, so we do a full GC every so often.
  182. @around(self)
  183. def tearDown(orig: Callable[[], R]) -> R:
  184. ret = orig()
  185. gc.collect(0)
  186. # Run a full GC every 50 gen-0 GCs.
  187. gen0_stats = gc.get_stats()[0]
  188. gen0_collections = gen0_stats["collections"]
  189. if gen0_collections % 50 == 0:
  190. gc.collect()
  191. gc.enable()
  192. set_current_context(SENTINEL_CONTEXT)
  193. return ret
  194. def assertObjectHasAttributes(self, attrs: Dict[str, object], obj: object) -> None:
  195. """Asserts that the given object has each of the attributes given, and
  196. that the value of each matches according to assertEqual."""
  197. for key in attrs.keys():
  198. if not hasattr(obj, key):
  199. raise AssertionError("Expected obj to have a '.%s'" % key)
  200. try:
  201. self.assertEqual(attrs[key], getattr(obj, key))
  202. except AssertionError as e:
  203. raise (type(e))(f"Assert error for '.{key}':") from e
  204. def assert_dict(self, required: dict, actual: dict) -> None:
  205. """Does a partial assert of a dict.
  206. Args:
  207. required: The keys and value which MUST be in 'actual'.
  208. actual: The test result. Extra keys will not be checked.
  209. """
  210. for key in required:
  211. self.assertEqual(
  212. required[key], actual[key], msg="%s mismatch. %s" % (key, actual)
  213. )
  214. def DEBUG(target: TV) -> TV:
  215. """A decorator to set the .loglevel attribute to logging.DEBUG.
  216. Can apply to either a TestCase or an individual test method."""
  217. target.loglevel = logging.DEBUG # type: ignore[attr-defined]
  218. return target
  219. def INFO(target: TV) -> TV:
  220. """A decorator to set the .loglevel attribute to logging.INFO.
  221. Can apply to either a TestCase or an individual test method."""
  222. target.loglevel = logging.INFO # type: ignore[attr-defined]
  223. return target
  224. def logcontext_clean(target: TV) -> TV:
  225. """A decorator which marks the TestCase or method as 'logcontext_clean'
  226. ... ie, any logcontext errors should cause a test failure
  227. """
  228. def logcontext_error(msg: str) -> NoReturn:
  229. raise AssertionError("logcontext error: %s" % (msg))
  230. patcher = patch("synapse.logging.context.logcontext_error", new=logcontext_error)
  231. return patcher(target) # type: ignore[call-overload]
  232. class HomeserverTestCase(TestCase):
  233. """
  234. A base TestCase that reduces boilerplate for HomeServer-using test cases.
  235. Defines a setUp method which creates a mock reactor, and instantiates a homeserver
  236. running on that reactor.
  237. There are various hooks for modifying the way that the homeserver is instantiated:
  238. * override make_homeserver, for example by making it pass different parameters into
  239. setup_test_homeserver.
  240. * override default_config, to return a modified configuration dictionary for use
  241. by setup_test_homeserver.
  242. * On a per-test basis, you can use the @override_config decorator to give a
  243. dictionary containing additional configuration settings to be added to the basic
  244. config dict.
  245. Attributes:
  246. servlets: List of servlet registration function.
  247. user_id (str): The user ID to assume if auth is hijacked.
  248. hijack_auth: Whether to hijack auth to return the user specified
  249. in user_id.
  250. """
  251. hijack_auth: ClassVar[bool] = True
  252. needs_threadpool: ClassVar[bool] = False
  253. servlets: ClassVar[List[RegisterServletsFunc]] = []
  254. def __init__(self, methodName: str):
  255. super().__init__(methodName)
  256. # see if we have any additional config for this test
  257. method = getattr(self, methodName)
  258. self._extra_config = getattr(method, "_extra_config", None)
  259. def setUp(self) -> None:
  260. """
  261. Set up the TestCase by calling the homeserver constructor, optionally
  262. hijacking the authentication system to return a fixed user, and then
  263. calling the prepare function.
  264. """
  265. self.reactor, self.clock = get_clock()
  266. self._hs_args = {"clock": self.clock, "reactor": self.reactor}
  267. self.hs = self.make_homeserver(self.reactor, self.clock)
  268. # Honour the `use_frozen_dicts` config option. We have to do this
  269. # manually because this is taken care of in the app `start` code, which
  270. # we don't run. Plus we want to reset it on tearDown.
  271. events.USE_FROZEN_DICTS = self.hs.config.server.use_frozen_dicts
  272. if self.hs is None:
  273. raise Exception("No homeserver returned from make_homeserver.")
  274. if not isinstance(self.hs, HomeServer):
  275. raise Exception("A homeserver wasn't returned, but %r" % (self.hs,))
  276. # create the root resource, and a site to wrap it.
  277. self.resource = self.create_test_resource()
  278. self.site = SynapseSite(
  279. logger_name="synapse.access.http.fake",
  280. site_tag=self.hs.config.server.server_name,
  281. config=self.hs.config.server.listeners[0],
  282. resource=self.resource,
  283. server_version_string="1",
  284. max_request_body_size=4096,
  285. reactor=self.reactor,
  286. hs=self.hs,
  287. )
  288. from tests.rest.client.utils import RestHelper
  289. self.helper = RestHelper(
  290. self.hs,
  291. checked_cast(MemoryReactorClock, self.hs.get_reactor()),
  292. self.site,
  293. getattr(self, "user_id", None),
  294. )
  295. if hasattr(self, "user_id"):
  296. if self.hijack_auth:
  297. assert self.helper.auth_user_id is not None
  298. token = "some_fake_token"
  299. # We need a valid token ID to satisfy foreign key constraints.
  300. token_id = self.get_success(
  301. self.hs.get_datastores().main.add_access_token_to_user(
  302. self.helper.auth_user_id,
  303. token,
  304. None,
  305. None,
  306. )
  307. )
  308. # This has to be a function and not just a Mock, because
  309. # `self.helper.auth_user_id` is temporarily reassigned in some tests
  310. async def get_requester(*args: Any, **kwargs: Any) -> Requester:
  311. assert self.helper.auth_user_id is not None
  312. return create_requester(
  313. user_id=UserID.from_string(self.helper.auth_user_id),
  314. access_token_id=token_id,
  315. )
  316. # Type ignore: mypy doesn't like us assigning to methods.
  317. self.hs.get_auth().get_user_by_req = get_requester # type: ignore[assignment]
  318. self.hs.get_auth().get_user_by_access_token = get_requester # type: ignore[assignment]
  319. self.hs.get_auth().get_access_token_from_request = Mock(return_value=token) # type: ignore[assignment]
  320. if self.needs_threadpool:
  321. self.reactor.threadpool = ThreadPool() # type: ignore[assignment]
  322. self.addCleanup(self.reactor.threadpool.stop)
  323. self.reactor.threadpool.start()
  324. if hasattr(self, "prepare"):
  325. self.prepare(self.reactor, self.clock, self.hs)
  326. def tearDown(self) -> None:
  327. # Reset to not use frozen dicts.
  328. events.USE_FROZEN_DICTS = False
  329. def wait_on_thread(self, deferred: Deferred, timeout: int = 10) -> None:
  330. """
  331. Wait until a Deferred is done, where it's waiting on a real thread.
  332. """
  333. start_time = time.time()
  334. while not deferred.called:
  335. if start_time + timeout < time.time():
  336. raise ValueError("Timed out waiting for threadpool")
  337. self.reactor.advance(0.01)
  338. time.sleep(0.01)
  339. def wait_for_background_updates(self) -> None:
  340. """Block until all background database updates have completed."""
  341. store = self.hs.get_datastores().main
  342. while not self.get_success(
  343. store.db_pool.updates.has_completed_background_updates()
  344. ):
  345. self.get_success(
  346. store.db_pool.updates.do_next_background_update(False), by=0.1
  347. )
  348. def make_homeserver(
  349. self, reactor: ThreadedMemoryReactorClock, clock: Clock
  350. ) -> HomeServer:
  351. """
  352. Make and return a homeserver.
  353. Args:
  354. reactor: A Twisted Reactor, or something that pretends to be one.
  355. clock: The Clock, associated with the reactor.
  356. Returns:
  357. A homeserver suitable for testing.
  358. Function to be overridden in subclasses.
  359. """
  360. hs = self.setup_test_homeserver()
  361. return hs
  362. def create_test_resource(self) -> Resource:
  363. """
  364. Create a the root resource for the test server.
  365. The default calls `self.create_resource_dict` and builds the resultant dict
  366. into a tree.
  367. """
  368. root_resource = Resource()
  369. create_resource_tree(self.create_resource_dict(), root_resource)
  370. return root_resource
  371. def create_resource_dict(self) -> Dict[str, Resource]:
  372. """Create a resource tree for the test server
  373. A resource tree is a mapping from path to twisted.web.resource.
  374. The default implementation creates a JsonResource and calls each function in
  375. `servlets` to register servlets against it.
  376. """
  377. servlet_resource = JsonResource(self.hs)
  378. for servlet in self.servlets:
  379. servlet(self.hs, servlet_resource)
  380. return {
  381. "/_matrix/client": servlet_resource,
  382. "/_synapse/admin": servlet_resource,
  383. }
  384. def default_config(self) -> JsonDict:
  385. """
  386. Get a default HomeServer config dict.
  387. """
  388. config = default_config("test")
  389. # apply any additional config which was specified via the override_config
  390. # decorator.
  391. if self._extra_config is not None:
  392. config.update(self._extra_config)
  393. return config
  394. def prepare(
  395. self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
  396. ) -> None:
  397. """
  398. Prepare for the test. This involves things like mocking out parts of
  399. the homeserver, or building test data common across the whole test
  400. suite.
  401. Args:
  402. reactor: A Twisted Reactor, or something that pretends to be one.
  403. clock: The Clock, associated with the reactor.
  404. homeserver: The HomeServer to test against.
  405. Function to optionally be overridden in subclasses.
  406. """
  407. def make_request(
  408. self,
  409. method: Union[bytes, str],
  410. path: Union[bytes, str],
  411. content: Union[bytes, str, JsonDict] = b"",
  412. access_token: Optional[str] = None,
  413. request: Type[Request] = SynapseRequest,
  414. shorthand: bool = True,
  415. federation_auth_origin: Optional[bytes] = None,
  416. content_is_form: bool = False,
  417. await_result: bool = True,
  418. custom_headers: Optional[Iterable[CustomHeaderType]] = None,
  419. client_ip: str = "127.0.0.1",
  420. ) -> FakeChannel:
  421. """
  422. Create a SynapseRequest at the path using the method and containing the
  423. given content.
  424. Args:
  425. method: The HTTP request method ("verb").
  426. path: The HTTP path, suitably URL encoded (e.g. escaped UTF-8 & spaces
  427. and such). content (bytes or dict): The body of the request.
  428. JSON-encoded, if a dict.
  429. shorthand: Whether to try and be helpful and prefix the given URL
  430. with the usual REST API path, if it doesn't contain it.
  431. federation_auth_origin: if set to not-None, we will add a fake
  432. Authorization header pretenting to be the given server name.
  433. content_is_form: Whether the content is URL encoded form data. Adds the
  434. 'Content-Type': 'application/x-www-form-urlencoded' header.
  435. await_result: whether to wait for the request to complete rendering. If
  436. true (the default), will pump the test reactor until the the renderer
  437. tells the channel the request is finished.
  438. custom_headers: (name, value) pairs to add as request headers
  439. client_ip: The IP to use as the requesting IP. Useful for testing
  440. ratelimiting.
  441. Returns:
  442. The FakeChannel object which stores the result of the request.
  443. """
  444. return make_request(
  445. self.reactor,
  446. self.site,
  447. method,
  448. path,
  449. content,
  450. access_token,
  451. request,
  452. shorthand,
  453. federation_auth_origin,
  454. content_is_form,
  455. await_result,
  456. custom_headers,
  457. client_ip,
  458. )
  459. def setup_test_homeserver(
  460. self, name: Optional[str] = None, **kwargs: Any
  461. ) -> HomeServer:
  462. """
  463. Set up the test homeserver, meant to be called by the overridable
  464. make_homeserver. It automatically passes through the test class's
  465. clock & reactor.
  466. Args:
  467. See tests.utils.setup_test_homeserver.
  468. Returns:
  469. synapse.server.HomeServer
  470. """
  471. kwargs = dict(kwargs)
  472. kwargs.update(self._hs_args)
  473. if "config" not in kwargs:
  474. config = self.default_config()
  475. else:
  476. config = kwargs["config"]
  477. # The server name can be specified using either the `name` argument or a config
  478. # override. The `name` argument takes precedence over any config overrides.
  479. if name is not None:
  480. config["server_name"] = name
  481. # Parse the config from a config dict into a HomeServerConfig
  482. config_obj = make_homeserver_config_obj(config)
  483. kwargs["config"] = config_obj
  484. # The server name in the config is now `name`, if provided, or the `server_name`
  485. # from a config override, or the default of "test". Whichever it is, we
  486. # construct a homeserver with a matching name.
  487. kwargs["name"] = config_obj.server.server_name
  488. async def run_bg_updates() -> None:
  489. with LoggingContext("run_bg_updates"):
  490. self.get_success(stor.db_pool.updates.run_background_updates(False))
  491. hs = setup_test_homeserver(self.addCleanup, **kwargs)
  492. stor = hs.get_datastores().main
  493. # Run the database background updates, when running against "master".
  494. if hs.__class__.__name__ == "TestHomeServer":
  495. self.get_success(run_bg_updates())
  496. return hs
  497. def pump(self, by: float = 0.0) -> None:
  498. """
  499. Pump the reactor enough that Deferreds will fire.
  500. """
  501. self.reactor.pump([by] * 100)
  502. def get_success(self, d: Awaitable[TV], by: float = 0.0) -> TV:
  503. deferred: Deferred[TV] = ensureDeferred(d) # type: ignore[arg-type]
  504. self.pump(by=by)
  505. return self.successResultOf(deferred)
  506. def get_failure(
  507. self, d: Awaitable[Any], exc: Type[_ExcType]
  508. ) -> _TypedFailure[_ExcType]:
  509. """
  510. Run a Deferred and get a Failure from it. The failure must be of the type `exc`.
  511. """
  512. deferred: Deferred[Any] = ensureDeferred(d) # type: ignore[arg-type]
  513. self.pump()
  514. return self.failureResultOf(deferred, exc)
  515. def get_success_or_raise(self, d: Awaitable[TV], by: float = 0.0) -> TV:
  516. """Drive deferred to completion and return result or raise exception
  517. on failure.
  518. """
  519. deferred: Deferred[TV] = ensureDeferred(d) # type: ignore[arg-type]
  520. results: list = []
  521. deferred.addBoth(results.append)
  522. self.pump(by=by)
  523. if not results:
  524. self.fail(
  525. "Success result expected on {!r}, found no result instead".format(
  526. deferred
  527. )
  528. )
  529. result = results[0]
  530. if isinstance(result, Failure):
  531. result.raiseException()
  532. return result
  533. def register_user(
  534. self,
  535. username: str,
  536. password: str,
  537. admin: Optional[bool] = False,
  538. displayname: Optional[str] = None,
  539. ) -> str:
  540. """
  541. Register a user. Requires the Admin API be registered.
  542. Args:
  543. username: The user part of the new user.
  544. password: The password of the new user.
  545. admin: Whether the user should be created as an admin or not.
  546. displayname: The displayname of the new user.
  547. Returns:
  548. The MXID of the new user.
  549. """
  550. self.hs.config.registration.registration_shared_secret = "shared"
  551. # Create the user
  552. channel = self.make_request("GET", "/_synapse/admin/v1/register")
  553. self.assertEqual(channel.code, 200, msg=channel.result)
  554. nonce = channel.json_body["nonce"]
  555. want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
  556. nonce_str = b"\x00".join([username.encode("utf8"), password.encode("utf8")])
  557. if admin:
  558. nonce_str += b"\x00admin"
  559. else:
  560. nonce_str += b"\x00notadmin"
  561. want_mac.update(nonce.encode("ascii") + b"\x00" + nonce_str)
  562. want_mac_digest = want_mac.hexdigest()
  563. body = {
  564. "nonce": nonce,
  565. "username": username,
  566. "displayname": displayname,
  567. "password": password,
  568. "admin": admin,
  569. "mac": want_mac_digest,
  570. "inhibit_login": True,
  571. }
  572. channel = self.make_request("POST", "/_synapse/admin/v1/register", body)
  573. self.assertEqual(channel.code, 200, channel.json_body)
  574. user_id = channel.json_body["user_id"]
  575. return user_id
  576. def register_appservice_user(
  577. self,
  578. username: str,
  579. appservice_token: str,
  580. ) -> Tuple[str, str]:
  581. """Register an appservice user as an application service.
  582. Requires the client-facing registration API be registered.
  583. Args:
  584. username: the user to be registered by an application service.
  585. Should NOT be a full username, i.e. just "localpart" as opposed to "@localpart:hostname"
  586. appservice_token: the acccess token for that application service.
  587. Raises: if the request to '/register' does not return 200 OK.
  588. Returns:
  589. The MXID of the new user, the device ID of the new user's first device.
  590. """
  591. channel = self.make_request(
  592. "POST",
  593. "/_matrix/client/r0/register",
  594. {
  595. "username": username,
  596. "type": "m.login.application_service",
  597. },
  598. access_token=appservice_token,
  599. )
  600. self.assertEqual(channel.code, 200, channel.json_body)
  601. return channel.json_body["user_id"], channel.json_body["device_id"]
  602. def login(
  603. self,
  604. username: str,
  605. password: str,
  606. device_id: Optional[str] = None,
  607. additional_request_fields: Optional[Dict[str, str]] = None,
  608. custom_headers: Optional[Iterable[CustomHeaderType]] = None,
  609. ) -> str:
  610. """
  611. Log in a user, and get an access token. Requires the Login API be registered.
  612. Args:
  613. username: The localpart to assign to the new user.
  614. password: The password to assign to the new user.
  615. device_id: An optional device ID to assign to the new device created during
  616. login.
  617. additional_request_fields: A dictionary containing any additional /login
  618. request fields and their values.
  619. custom_headers: Custom HTTP headers and values to add to the /login request.
  620. Returns:
  621. The newly registered user's Matrix ID.
  622. """
  623. body = {"type": "m.login.password", "user": username, "password": password}
  624. if device_id:
  625. body["device_id"] = device_id
  626. if additional_request_fields:
  627. body.update(additional_request_fields)
  628. channel = self.make_request(
  629. "POST",
  630. "/_matrix/client/r0/login",
  631. body,
  632. custom_headers=custom_headers,
  633. )
  634. self.assertEqual(channel.code, 200, channel.result)
  635. access_token = channel.json_body["access_token"]
  636. return access_token
  637. def create_and_send_event(
  638. self,
  639. room_id: str,
  640. user: UserID,
  641. soft_failed: bool = False,
  642. prev_event_ids: Optional[List[str]] = None,
  643. ) -> str:
  644. """
  645. Create and send an event.
  646. Args:
  647. soft_failed: Whether to create a soft failed event or not
  648. prev_event_ids: Explicitly set the prev events,
  649. or if None just use the default
  650. Returns:
  651. The new event's ID.
  652. """
  653. event_creator = self.hs.get_event_creation_handler()
  654. requester = create_requester(user)
  655. event, unpersisted_context = self.get_success(
  656. event_creator.create_event(
  657. requester,
  658. {
  659. "type": EventTypes.Message,
  660. "room_id": room_id,
  661. "sender": user.to_string(),
  662. "content": {"body": secrets.token_hex(), "msgtype": "m.text"},
  663. },
  664. prev_event_ids=prev_event_ids,
  665. )
  666. )
  667. context = self.get_success(unpersisted_context.persist(event))
  668. if soft_failed:
  669. event.internal_metadata.soft_failed = True
  670. self.get_success(
  671. event_creator.handle_new_client_event(
  672. requester, events_and_context=[(event, context)]
  673. )
  674. )
  675. return event.event_id
  676. def inject_room_member(self, room: str, user: str, membership: str) -> None:
  677. """
  678. Inject a membership event into a room.
  679. Deprecated: use event_injection.inject_room_member directly
  680. Args:
  681. room: Room ID to inject the event into.
  682. user: MXID of the user to inject the membership for.
  683. membership: The membership type.
  684. """
  685. self.get_success(
  686. event_injection.inject_member_event(self.hs, room, user, membership)
  687. )
  688. class FederatingHomeserverTestCase(HomeserverTestCase):
  689. """
  690. A federating homeserver, set up to validate incoming federation requests
  691. """
  692. OTHER_SERVER_NAME = "other.example.com"
  693. OTHER_SERVER_SIGNATURE_KEY = signedjson.key.generate_signing_key("test")
  694. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  695. super().prepare(reactor, clock, hs)
  696. # poke the other server's signing key into the key store, so that we don't
  697. # make requests for it
  698. verify_key = signedjson.key.get_verify_key(self.OTHER_SERVER_SIGNATURE_KEY)
  699. verify_key_id = "%s:%s" % (verify_key.alg, verify_key.version)
  700. self.get_success(
  701. hs.get_datastores().main.store_server_keys_json(
  702. self.OTHER_SERVER_NAME,
  703. verify_key_id,
  704. from_server=self.OTHER_SERVER_NAME,
  705. ts_now_ms=clock.time_msec(),
  706. ts_expires_ms=clock.time_msec() + 10000,
  707. key_json_bytes=canonicaljson.encode_canonical_json(
  708. {
  709. "verify_keys": {
  710. verify_key_id: {
  711. "key": signedjson.key.encode_verify_key_base64(
  712. verify_key
  713. )
  714. }
  715. }
  716. }
  717. ),
  718. )
  719. )
  720. def create_resource_dict(self) -> Dict[str, Resource]:
  721. d = super().create_resource_dict()
  722. d["/_matrix/federation"] = TransportLayerServer(self.hs)
  723. return d
  724. def make_signed_federation_request(
  725. self,
  726. method: str,
  727. path: str,
  728. content: Optional[JsonDict] = None,
  729. await_result: bool = True,
  730. custom_headers: Optional[Iterable[CustomHeaderType]] = None,
  731. client_ip: str = "127.0.0.1",
  732. ) -> FakeChannel:
  733. """Make an inbound signed federation request to this server
  734. The request is signed as if it came from "other.example.com", which our HS
  735. already has the keys for.
  736. """
  737. if custom_headers is None:
  738. custom_headers = []
  739. else:
  740. custom_headers = list(custom_headers)
  741. custom_headers.append(
  742. (
  743. "Authorization",
  744. _auth_header_for_request(
  745. origin=self.OTHER_SERVER_NAME,
  746. destination=self.hs.hostname,
  747. signing_key=self.OTHER_SERVER_SIGNATURE_KEY,
  748. method=method,
  749. path=path,
  750. content=content,
  751. ),
  752. )
  753. )
  754. return make_request(
  755. self.reactor,
  756. self.site,
  757. method=method,
  758. path=path,
  759. content=content if content is not None else "",
  760. shorthand=False,
  761. await_result=await_result,
  762. custom_headers=custom_headers,
  763. client_ip=client_ip,
  764. )
  765. def add_hashes_and_signatures_from_other_server(
  766. self,
  767. event_dict: JsonDict,
  768. room_version: RoomVersion = KNOWN_ROOM_VERSIONS[DEFAULT_ROOM_VERSION],
  769. ) -> JsonDict:
  770. """Adds hashes and signatures to the given event dict
  771. Returns:
  772. The modified event dict, for convenience
  773. """
  774. add_hashes_and_signatures(
  775. room_version,
  776. event_dict,
  777. signature_name=self.OTHER_SERVER_NAME,
  778. signing_key=self.OTHER_SERVER_SIGNATURE_KEY,
  779. )
  780. return event_dict
  781. def _auth_header_for_request(
  782. origin: str,
  783. destination: str,
  784. signing_key: signedjson.key.SigningKey,
  785. method: str,
  786. path: str,
  787. content: Optional[JsonDict],
  788. ) -> str:
  789. """Build a suitable Authorization header for an outgoing federation request"""
  790. request_description: JsonDict = {
  791. "method": method,
  792. "uri": path,
  793. "destination": destination,
  794. "origin": origin,
  795. }
  796. if content is not None:
  797. request_description["content"] = content
  798. signature_base64 = unpaddedbase64.encode_base64(
  799. signing_key.sign(
  800. canonicaljson.encode_canonical_json(request_description)
  801. ).signature
  802. )
  803. return (
  804. f"X-Matrix origin={origin},"
  805. f"key={signing_key.alg}:{signing_key.version},"
  806. f"sig={signature_base64}"
  807. )
  808. def override_config(extra_config: JsonDict) -> Callable[[TV], TV]:
  809. """A decorator which can be applied to test functions to give additional HS config
  810. For use
  811. For example:
  812. class MyTestCase(HomeserverTestCase):
  813. @override_config({"enable_registration": False, ...})
  814. def test_foo(self):
  815. ...
  816. Args:
  817. extra_config: Additional config settings to be merged into the default
  818. config dict before instantiating the test homeserver.
  819. """
  820. def decorator(func: TV) -> TV:
  821. # This attribute is being defined.
  822. func._extra_config = extra_config # type: ignore[attr-defined]
  823. return func
  824. return decorator
  825. def skip_unless(condition: bool, reason: str) -> Callable[[TV], TV]:
  826. """A test decorator which will skip the decorated test unless a condition is set
  827. For example:
  828. class MyTestCase(TestCase):
  829. @skip_unless(HAS_FOO, "Cannot test without foo")
  830. def test_foo(self):
  831. ...
  832. Args:
  833. condition: If true, the test will be skipped
  834. reason: the reason to give for skipping the test
  835. """
  836. def decorator(f: TV) -> TV:
  837. if not condition:
  838. f.skip = reason # type: ignore
  839. return f
  840. return decorator