2019-01-29 15:07:00 +03:00
|
|
|
|
#
|
2023-11-21 23:29:58 +03:00
|
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
|
#
|
2024-01-23 14:26:48 +03:00
|
|
|
|
# Copyright 2019-2021 The Matrix.org Foundation C.I.C.
|
2023-11-21 23:29:58 +03:00
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
|
#
|
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
|
#
|
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2019-01-29 15:07:00 +03:00
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Utilities for running the unit tests
|
|
|
|
|
"""
|
2024-09-02 14:39:04 +03:00
|
|
|
|
|
2022-10-25 17:25:02 +03:00
|
|
|
|
import json
|
2020-10-30 14:15:07 +03:00
|
|
|
|
import sys
|
|
|
|
|
import warnings
|
2021-09-16 19:01:14 +03:00
|
|
|
|
from binascii import unhexlify
|
2023-08-25 16:27:21 +03:00
|
|
|
|
from typing import TYPE_CHECKING, Awaitable, Callable, Tuple, TypeVar
|
2020-12-15 23:56:10 +03:00
|
|
|
|
|
2020-12-02 21:22:01 +03:00
|
|
|
|
import attr
|
2022-10-25 17:25:02 +03:00
|
|
|
|
import zope.interface
|
2020-12-02 21:22:01 +03:00
|
|
|
|
|
2023-02-09 00:29:49 +03:00
|
|
|
|
from twisted.internet.interfaces import IProtocol
|
2020-12-02 21:22:01 +03:00
|
|
|
|
from twisted.python.failure import Failure
|
|
|
|
|
from twisted.web.client import ResponseDone
|
2022-10-25 17:25:02 +03:00
|
|
|
|
from twisted.web.http import RESPONSES
|
|
|
|
|
from twisted.web.http_headers import Headers
|
|
|
|
|
from twisted.web.iweb import IResponse
|
|
|
|
|
|
2023-05-22 16:48:57 +03:00
|
|
|
|
from synapse.types import JsonSerializable
|
2020-12-02 21:22:01 +03:00
|
|
|
|
|
2023-02-09 00:29:49 +03:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from sys import UnraisableHookArgs
|
|
|
|
|
|
2020-04-29 14:30:36 +03:00
|
|
|
|
TV = TypeVar("TV")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_awaitable_result(awaitable: Awaitable[TV]) -> TV:
|
|
|
|
|
"""Get the result from an Awaitable which should have completed
|
|
|
|
|
|
|
|
|
|
Asserts that the given awaitable has a result ready, and returns its value
|
|
|
|
|
"""
|
|
|
|
|
i = awaitable.__await__()
|
|
|
|
|
try:
|
|
|
|
|
next(i)
|
|
|
|
|
except StopIteration as e:
|
|
|
|
|
# awaitable returned a result
|
|
|
|
|
return e.value
|
|
|
|
|
|
|
|
|
|
# if next didn't raise, the awaitable hasn't completed.
|
|
|
|
|
raise Exception("awaitable has not yet completed")
|
2020-07-24 17:59:51 +03:00
|
|
|
|
|
|
|
|
|
|
2020-10-30 14:15:07 +03:00
|
|
|
|
def setup_awaitable_errors() -> Callable[[], None]:
|
|
|
|
|
"""
|
|
|
|
|
Convert warnings from a non-awaited coroutines into errors.
|
|
|
|
|
"""
|
|
|
|
|
warnings.simplefilter("error", RuntimeWarning)
|
|
|
|
|
|
|
|
|
|
# State shared between unraisablehook and check_for_unraisable_exceptions.
|
|
|
|
|
unraisable_exceptions = []
|
2022-04-27 16:03:44 +03:00
|
|
|
|
orig_unraisablehook = sys.unraisablehook
|
2020-10-30 14:15:07 +03:00
|
|
|
|
|
2023-02-09 00:29:49 +03:00
|
|
|
|
def unraisablehook(unraisable: "UnraisableHookArgs") -> None:
|
2020-10-30 14:15:07 +03:00
|
|
|
|
unraisable_exceptions.append(unraisable.exc_value)
|
|
|
|
|
|
2023-02-09 00:29:49 +03:00
|
|
|
|
def cleanup() -> None:
|
2020-10-30 14:15:07 +03:00
|
|
|
|
"""
|
|
|
|
|
A method to be used as a clean-up that fails a test-case if there are any new unraisable exceptions.
|
|
|
|
|
"""
|
2022-04-27 16:03:44 +03:00
|
|
|
|
sys.unraisablehook = orig_unraisablehook
|
2020-10-30 14:15:07 +03:00
|
|
|
|
if unraisable_exceptions:
|
2023-02-09 00:29:49 +03:00
|
|
|
|
exc = unraisable_exceptions.pop()
|
|
|
|
|
assert exc is not None
|
|
|
|
|
raise exc
|
2020-10-30 14:15:07 +03:00
|
|
|
|
|
2022-04-27 16:03:44 +03:00
|
|
|
|
sys.unraisablehook = unraisablehook
|
2020-10-30 14:15:07 +03:00
|
|
|
|
|
|
|
|
|
return cleanup
|
2020-12-02 21:22:01 +03:00
|
|
|
|
|
|
|
|
|
|
2022-10-25 17:25:02 +03:00
|
|
|
|
# Type ignore: it does not fully implement IResponse, but is good enough for tests
|
|
|
|
|
@zope.interface.implementer(IResponse)
|
|
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
|
class FakeResponse: # type: ignore[misc]
|
2020-12-02 21:22:01 +03:00
|
|
|
|
"""A fake twisted.web.IResponse object
|
|
|
|
|
|
|
|
|
|
there is a similar class at treq.test.test_response, but it lacks a `phrase`
|
|
|
|
|
attribute, and didn't support deliverBody until recently.
|
|
|
|
|
"""
|
|
|
|
|
|
2022-10-25 17:25:02 +03:00
|
|
|
|
version: Tuple[bytes, int, int] = (b"HTTP", 1, 1)
|
2020-12-02 21:22:01 +03:00
|
|
|
|
|
2022-10-25 17:25:02 +03:00
|
|
|
|
# HTTP response code
|
|
|
|
|
code: int = 200
|
2020-12-02 21:22:01 +03:00
|
|
|
|
|
|
|
|
|
# body of the response
|
2022-10-25 17:25:02 +03:00
|
|
|
|
body: bytes = b""
|
|
|
|
|
|
|
|
|
|
headers: Headers = attr.Factory(Headers)
|
|
|
|
|
|
|
|
|
|
@property
|
2023-02-09 00:29:49 +03:00
|
|
|
|
def phrase(self) -> bytes:
|
2022-10-25 17:25:02 +03:00
|
|
|
|
return RESPONSES.get(self.code, b"Unknown Status")
|
|
|
|
|
|
|
|
|
|
@property
|
2023-02-09 00:29:49 +03:00
|
|
|
|
def length(self) -> int:
|
2022-10-25 17:25:02 +03:00
|
|
|
|
return len(self.body)
|
2020-12-02 21:22:01 +03:00
|
|
|
|
|
2023-02-09 00:29:49 +03:00
|
|
|
|
def deliverBody(self, protocol: IProtocol) -> None:
|
2020-12-02 21:22:01 +03:00
|
|
|
|
protocol.dataReceived(self.body)
|
|
|
|
|
protocol.connectionLost(Failure(ResponseDone()))
|
2021-09-16 19:01:14 +03:00
|
|
|
|
|
2022-10-25 17:25:02 +03:00
|
|
|
|
@classmethod
|
2023-05-22 16:48:57 +03:00
|
|
|
|
def json(cls, *, code: int = 200, payload: JsonSerializable) -> "FakeResponse":
|
2022-10-25 17:25:02 +03:00
|
|
|
|
headers = Headers({"Content-Type": ["application/json"]})
|
|
|
|
|
body = json.dumps(payload).encode("utf-8")
|
|
|
|
|
return cls(code=code, body=body, headers=headers)
|
|
|
|
|
|
2021-09-16 19:01:14 +03:00
|
|
|
|
|
|
|
|
|
# A small image used in some tests.
|
|
|
|
|
#
|
|
|
|
|
# Resolution: 1×1, MIME type: image/png, Extension: png, Size: 67 B
|
|
|
|
|
SMALL_PNG = unhexlify(
|
|
|
|
|
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
|
|
|
|
|
b"0000001f15c4890000000a49444154789c63000100000500010d"
|
|
|
|
|
b"0a2db40000000049454e44ae426082"
|
|
|
|
|
)
|