From 75fa5907fc6c8ab618c820cfb622237cef3a9b7a Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Tue, 22 Jun 2021 09:43:35 -0700 Subject: [PATCH 01/12] convert threepid/bind.py to async --- sydent/threepid/bind.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sydent/threepid/bind.py b/sydent/threepid/bind.py index 7172536c..a8409116 100644 --- a/sydent/threepid/bind.py +++ b/sydent/threepid/bind.py @@ -109,7 +109,7 @@ def addBinding(self, medium: str, address: str, mxid: str) -> Dict[str, Any]: signer = Signer(self.sydent) sgassoc = signer.signedThreePidAssociation(assoc) - self._notify(sgassoc, 0) + defer.ensureDeferred(self._notify(sgassoc, 0)) return sgassoc @@ -126,8 +126,7 @@ def removeBinding(self, threepid: Dict[str, str], mxid: str) -> None: localAssocStore.removeAssociation(threepid, mxid) self.sydent.pusher.doLocalPush() - @defer.inlineCallbacks - def _notify(self, assoc: Dict[str, Any], attempt: int) -> Generator: + async def _notify(self, assoc: Dict[str, Any], attempt: int) -> Generator: """ Sends data about a new association (and, if necessary, the associated invites) to the associated MXID's homeserver. @@ -163,7 +162,7 @@ def _notify(self, assoc: Dict[str, Any], attempt: int) -> Generator: # Make a POST to the chosen Synapse server http_client = FederationHttpClient(self.sydent) try: - response = yield http_client.post_json_get_nothing(post_url, assoc, {}) + response = await http_client.post_json_get_nothing(post_url, assoc, {}) except Exception as e: self._notifyErrback(assoc, attempt, e) return From ec2e18f89e489afe92f11273e78d07ffda99e477 Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Tue, 22 Jun 2021 10:07:43 -0700 Subject: [PATCH 02/12] Revert "convert threepid/bind.py to async" This reverts commit 75fa5907fc6c8ab618c820cfb622237cef3a9b7a. --- sydent/threepid/bind.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sydent/threepid/bind.py b/sydent/threepid/bind.py index a8409116..7172536c 100644 --- a/sydent/threepid/bind.py +++ b/sydent/threepid/bind.py @@ -109,7 +109,7 @@ def addBinding(self, medium: str, address: str, mxid: str) -> Dict[str, Any]: signer = Signer(self.sydent) sgassoc = signer.signedThreePidAssociation(assoc) - defer.ensureDeferred(self._notify(sgassoc, 0)) + self._notify(sgassoc, 0) return sgassoc @@ -126,7 +126,8 @@ def removeBinding(self, threepid: Dict[str, str], mxid: str) -> None: localAssocStore.removeAssociation(threepid, mxid) self.sydent.pusher.doLocalPush() - async def _notify(self, assoc: Dict[str, Any], attempt: int) -> Generator: + @defer.inlineCallbacks + def _notify(self, assoc: Dict[str, Any], attempt: int) -> Generator: """ Sends data about a new association (and, if necessary, the associated invites) to the associated MXID's homeserver. @@ -162,7 +163,7 @@ async def _notify(self, assoc: Dict[str, Any], attempt: int) -> Generator: # Make a POST to the chosen Synapse server http_client = FederationHttpClient(self.sydent) try: - response = await http_client.post_json_get_nothing(post_url, assoc, {}) + response = yield http_client.post_json_get_nothing(post_url, assoc, {}) except Exception as e: self._notifyErrback(assoc, attempt, e) return From b3ccb045302e16d775a4c057474fe124275a2502 Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Tue, 22 Jun 2021 10:38:09 -0700 Subject: [PATCH 03/12] convert sydent/threepid/bind.py and associated files to async/await --- sydent/http/httpclient.py | 7 +++---- sydent/threepid/bind.py | 7 +++---- tests/test_invites.py | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/sydent/http/httpclient.py b/sydent/http/httpclient.py index d69ed5e2..28f7fdfd 100644 --- a/sydent/http/httpclient.py +++ b/sydent/http/httpclient.py @@ -69,8 +69,7 @@ def get_json(self, uri: str, max_size: Optional[int] = None) -> Generator: raise return json_body - @defer.inlineCallbacks - def post_json_get_nothing( + async def post_json_get_nothing( self, uri: str, post_json: Dict[Any, Any], opts: Dict[str, Any] ) -> Generator: """Make a POST request to an endpoint returning JSON and parse result @@ -102,7 +101,7 @@ def post_json_get_nothing( logger.debug("HTTP POST %s -> %s", json_bytes, uri) - response = yield self.agent.request( + response = await self.agent.request( b"POST", uri.encode("utf8"), headers, @@ -114,7 +113,7 @@ def post_json_get_nothing( # https://twistedmatrix.com/documents/current/web/howto/client.html try: # TODO Will this cause the server to think the request was a failure? - yield read_body_with_max_size(response, 0) + await read_body_with_max_size(response, 0) except BodyExceededMaxSize: pass diff --git a/sydent/threepid/bind.py b/sydent/threepid/bind.py index 7172536c..a8409116 100644 --- a/sydent/threepid/bind.py +++ b/sydent/threepid/bind.py @@ -109,7 +109,7 @@ def addBinding(self, medium: str, address: str, mxid: str) -> Dict[str, Any]: signer = Signer(self.sydent) sgassoc = signer.signedThreePidAssociation(assoc) - self._notify(sgassoc, 0) + defer.ensureDeferred(self._notify(sgassoc, 0)) return sgassoc @@ -126,8 +126,7 @@ def removeBinding(self, threepid: Dict[str, str], mxid: str) -> None: localAssocStore.removeAssociation(threepid, mxid) self.sydent.pusher.doLocalPush() - @defer.inlineCallbacks - def _notify(self, assoc: Dict[str, Any], attempt: int) -> Generator: + async def _notify(self, assoc: Dict[str, Any], attempt: int) -> Generator: """ Sends data about a new association (and, if necessary, the associated invites) to the associated MXID's homeserver. @@ -163,7 +162,7 @@ def _notify(self, assoc: Dict[str, Any], attempt: int) -> Generator: # Make a POST to the chosen Synapse server http_client = FederationHttpClient(self.sydent) try: - response = yield http_client.post_json_get_nothing(post_url, assoc, {}) + response = await http_client.post_json_get_nothing(post_url, assoc, {}) except Exception as e: self._notifyErrback(assoc, attempt, e) return diff --git a/tests/test_invites.py b/tests/test_invites.py index 1d79d960..e584b2f5 100644 --- a/tests/test_invites.py +++ b/tests/test_invites.py @@ -6,7 +6,7 @@ from sydent.db.invite_tokens import JoinTokenStore from sydent.http.httpclient import FederationHttpClient from sydent.http.servlets.store_invite_servlet import StoreInviteServlet -from tests.utils import make_sydent +from tests.utils import make_sydent, AsyncMock class ThreepidInvitesTestCase(unittest.TestCase): @@ -37,7 +37,7 @@ def test_delete_on_bind(self): def post_json_get_nothing(uri, post_json, opts): return Response((b"HTTP", 1, 1), 200, b"OK", None, None) - FederationHttpClient.post_json_get_nothing = Mock( + FederationHttpClient.post_json_get_nothing = AsyncMock( side_effect=post_json_get_nothing, ) From e72b2359b34662ef421ae7d63f2c1587dc079935 Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Tue, 22 Jun 2021 10:46:43 -0700 Subject: [PATCH 04/12] convert http/httpclient.py and associated files to async/await --- sydent/hs_federation/verifier.py | 4 ++-- sydent/http/httpclient.py | 7 +++---- sydent/http/servlets/registerservlet.py | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/sydent/hs_federation/verifier.py b/sydent/hs_federation/verifier.py index 47ee6fde..d9348863 100644 --- a/sydent/hs_federation/verifier.py +++ b/sydent/hs_federation/verifier.py @@ -81,9 +81,9 @@ def _getKeysForServer(self, server_name: str) -> Generator: return self.cache[server_name]["verify_keys"] client = FederationHttpClient(self.sydent) - result = yield client.get_json( + result = yield defer.ensureDeffered(client.get_json( "matrix://%s/_matrix/key/v2/server/" % server_name, 1024 * 50 - ) + )) if "verify_keys" not in result: raise SignatureVerifyException("No key found in response") diff --git a/sydent/http/httpclient.py b/sydent/http/httpclient.py index 28f7fdfd..de5d395e 100644 --- a/sydent/http/httpclient.py +++ b/sydent/http/httpclient.py @@ -41,8 +41,7 @@ class HTTPClient: agent: IAgent - @defer.inlineCallbacks - def get_json(self, uri: str, max_size: Optional[int] = None) -> Generator: + async def get_json(self, uri: str, max_size: Optional[int] = None) -> Generator: """Make a GET request to an endpoint returning JSON and parse result :param uri: The URI to make a GET request to. @@ -56,11 +55,11 @@ def get_json(self, uri: str, max_size: Optional[int] = None) -> Generator: """ logger.debug("HTTP GET %s", uri) - response = yield self.agent.request( + response = await self.agent.request( b"GET", uri.encode("utf8"), ) - body = yield read_body_with_max_size(response, max_size) + body = await read_body_with_max_size(response, max_size) try: # json.loads doesn't allow bytes in Python 3.5 json_body = json_decoder.decode(body.decode("UTF-8")) diff --git a/sydent/http/servlets/registerservlet.py b/sydent/http/servlets/registerservlet.py index ecac8d10..6f151cab 100644 --- a/sydent/http/servlets/registerservlet.py +++ b/sydent/http/servlets/registerservlet.py @@ -57,14 +57,14 @@ def render_POST(self, request: Request) -> Generator: "error": "matrix_server_name must be a valid Matrix server name (IP address or hostname)", } - result = yield self.client.get_json( + result = yield defer.ensureDeferred(self.client.get_json( "matrix://%s/_matrix/federation/v1/openid/userinfo?access_token=%s" % ( matrix_server, urllib.parse.quote(args["access_token"]), ), 1024 * 5, - ) + )) if "sub" not in result: raise Exception("Invalid response from homeserver") From 0cc9297f69f0eaf3a03c7045d7a4cf0fc0814615 Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Tue, 22 Jun 2021 11:17:05 -0700 Subject: [PATCH 05/12] convert sydent/hs_federation/verifier.py to async/await --- sydent/hs_federation/verifier.py | 21 +++++++++------------ sydent/http/httpclient.py | 4 ++-- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/sydent/hs_federation/verifier.py b/sydent/hs_federation/verifier.py index d9348863..30984fcc 100644 --- a/sydent/hs_federation/verifier.py +++ b/sydent/hs_federation/verifier.py @@ -63,8 +63,7 @@ def __init__(self, sydent: "Sydent") -> None: # server_name: , } - @defer.inlineCallbacks - def _getKeysForServer(self, server_name: str) -> Generator: + async def _getKeysForServer(self, server_name: str): """Get the signing key data from a homeserver. :param server_name: The name of the server to request the keys from. @@ -81,9 +80,9 @@ def _getKeysForServer(self, server_name: str) -> Generator: return self.cache[server_name]["verify_keys"] client = FederationHttpClient(self.sydent) - result = yield defer.ensureDeffered(client.get_json( + result = await client.get_json( "matrix://%s/_matrix/key/v2/server/" % server_name, 1024 * 50 - )) + ) if "verify_keys" not in result: raise SignatureVerifyException("No key found in response") @@ -106,12 +105,11 @@ def _getKeysForServer(self, server_name: str) -> Generator: return result["verify_keys"] - @defer.inlineCallbacks - def verifyServerSignedJson( + async def verifyServerSignedJson( self, signed_json: Dict[str, Any], acceptable_server_names: Optional[List[str]] = None, - ) -> Generator: + ): """Given a signed json object, try to verify any one of the signatures on it @@ -137,7 +135,7 @@ def verifyServerSignedJson( if server_name not in acceptable_server_names: continue - server_keys = yield self._getKeysForServer(server_name) + server_keys = await self._getKeysForServer(server_name) for key_name, sig in sigs.items(): if key_name in server_keys: if "key" not in server_keys[key_name]: @@ -167,10 +165,9 @@ def verifyServerSignedJson( ) raise SignatureVerifyException("No matching signature found") - @defer.inlineCallbacks - def authenticate_request( + async def authenticate_request( self, request: "Request", content: Optional[bytes] - ) -> Generator: + ): """Authenticates a Matrix federation request based on the X-Matrix header XXX: Copied largely from synapse @@ -241,7 +238,7 @@ def strip_quotes(value): "X-Matrix header's origin parameter must be a valid Matrix server name" ) - yield self.verifyServerSignedJson(json_request, [origin]) + await self.verifyServerSignedJson(json_request, [origin]) logger.info("Verified request from HS %s", origin) diff --git a/sydent/http/httpclient.py b/sydent/http/httpclient.py index de5d395e..d4adc497 100644 --- a/sydent/http/httpclient.py +++ b/sydent/http/httpclient.py @@ -41,7 +41,7 @@ class HTTPClient: agent: IAgent - async def get_json(self, uri: str, max_size: Optional[int] = None) -> Generator: + async def get_json(self, uri: str, max_size: Optional[int] = None): """Make a GET request to an endpoint returning JSON and parse result :param uri: The URI to make a GET request to. @@ -70,7 +70,7 @@ async def get_json(self, uri: str, max_size: Optional[int] = None) -> Generator: async def post_json_get_nothing( self, uri: str, post_json: Dict[Any, Any], opts: Dict[str, Any] - ) -> Generator: + ): """Make a POST request to an endpoint returning JSON and parse result :param uri: The URI to make a POST request to. From 0a4588d862440acf377c9aaf6d8ca7b2340f2be7 Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Tue, 22 Jun 2021 11:32:49 -0700 Subject: [PATCH 06/12] convert http/matrixfederationagent.py to async/await --- sydent/http/matrixfederationagent.py | 32 +++++++++++----------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/sydent/http/matrixfederationagent.py b/sydent/http/matrixfederationagent.py index 114b0e90..ef4843a9 100644 --- a/sydent/http/matrixfederationagent.py +++ b/sydent/http/matrixfederationagent.py @@ -114,8 +114,7 @@ def __init__( # `None`: there is no (valid) .well-known here self._well_known_cache = _well_known_cache - @defer.inlineCallbacks - def request( + async def request( self, method: bytes, uri: bytes, @@ -146,7 +145,7 @@ def request( :rtype: Deferred[twisted.web.iweb.IResponse] """ parsed_uri = URI.fromBytes(uri, defaultPort=-1) - res = yield self._route_matrix_uri(parsed_uri) + res = await self._route_matrix_uri(parsed_uri) # set up the TLS connection params # @@ -183,13 +182,12 @@ def endpointForURI(_uri): return ep agent = Agent.usingEndpointFactory(self._reactor, EndpointFactory(), self._pool) - res = yield agent.request(method, uri, headers, bodyProducer) + res = await agent.request(method, uri, headers, bodyProducer) return res - @defer.inlineCallbacks - def _route_matrix_uri( + async def _route_matrix_uri( self, parsed_uri: "URI", lookup_well_known: bool = True - ) -> Generator: + ): """Helper for `request`: determine the routing for a Matrix URI :param parsed_uri: uri to route. Note that it should be parsed with @@ -232,7 +230,7 @@ def _route_matrix_uri( if lookup_well_known: # try a .well-known lookup - well_known_server = yield self._get_well_known(parsed_uri.host) + well_known_server = await self._get_well_known(parsed_uri.host) if well_known_server: # if we found a .well-known, start again, but don't do another @@ -263,14 +261,12 @@ def _route_matrix_uri( fragment=parsed_uri.fragment, ) - res = yield self._route_matrix_uri(new_uri, lookup_well_known=False) + res = await self._route_matrix_uri(new_uri, lookup_well_known=False) return res # try a SRV lookup service_name = b"_matrix._tcp.%s" % (parsed_uri.host,) - server_list = yield defer.ensureDeferred( - self._srv_resolver.resolve_service(service_name) - ) + server_list = await self._srv_resolver.resolve_service(service_name) if not server_list: target_host = parsed_uri.host @@ -297,8 +293,7 @@ def _route_matrix_uri( target_port=port, ) - @defer.inlineCallbacks - def _get_well_known(self, server_name: bytes) -> Generator: + async def _get_well_known(self, server_name: bytes): """Attempt to fetch and parse a .well-known file for the given server :param server_name: Name of the server, from the requested url. @@ -313,15 +308,14 @@ def _get_well_known(self, server_name: bytes) -> Generator: except KeyError: # TODO: should we linearise so that we don't end up doing two .well-known # requests for the same server in parallel? - result, cache_period = yield self._do_get_well_known(server_name) + result, cache_period = await self._do_get_well_known(server_name) if cache_period > 0: self._well_known_cache.set(server_name, result, cache_period) return result - @defer.inlineCallbacks - def _do_get_well_known(self, server_name: bytes) -> Generator: + async def _do_get_well_known(self, server_name: bytes): """Actually fetch and parse a .well-known, without checking the cache :param server_name: Name of the server, from the requested url @@ -337,8 +331,8 @@ def _do_get_well_known(self, server_name: bytes) -> Generator: uri_str = uri.decode("ascii") logger.info("Fetching %s", uri_str) try: - response = yield self._well_known_agent.request(b"GET", uri) - body = yield read_body_with_max_size(response, WELL_KNOWN_MAX_SIZE) + response = await self._well_known_agent.request(b"GET", uri) + body = await read_body_with_max_size(response, WELL_KNOWN_MAX_SIZE) if response.code != 200: raise Exception("Non-200 response %s" % (response.code,)) From cdf758a95309d58612363f98f3b2d9c68e5295c3 Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Tue, 22 Jun 2021 11:43:04 -0700 Subject: [PATCH 07/12] add asyncmock reference to fix tests/test_invites.py --- tests/test_invites.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_invites.py b/tests/test_invites.py index e584b2f5..27bd313c 100644 --- a/tests/test_invites.py +++ b/tests/test_invites.py @@ -122,7 +122,7 @@ def test_no_delete_on_bind(self): def post_json_get_nothing(uri, post_json, opts): return Response((b"HTTP", 1, 1), 200, b"OK", None, None) - FederationHttpClient.post_json_get_nothing = Mock( + FederationHttpClient.post_json_get_nothing = AsyncMock( side_effect=post_json_get_nothing, ) From a1972f982fbd3b1d3a2207c6120b011b0ac326a1 Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Tue, 22 Jun 2021 11:46:44 -0700 Subject: [PATCH 08/12] lints --- sydent/hs_federation/verifier.py | 7 ++----- sydent/http/httpclient.py | 3 +-- sydent/http/matrixfederationagent.py | 1 - sydent/http/servlets/registerservlet.py | 18 ++++++++++-------- tests/test_invites.py | 4 +--- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/sydent/hs_federation/verifier.py b/sydent/hs_federation/verifier.py index 30984fcc..ead33826 100644 --- a/sydent/hs_federation/verifier.py +++ b/sydent/hs_federation/verifier.py @@ -14,12 +14,11 @@ import logging import time -from typing import TYPE_CHECKING, Any, Dict, Generator, List, Optional, Tuple +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple import signedjson.key # type: ignore import signedjson.sign # type: ignore from signedjson.sign import SignatureVerifyException -from twisted.internet import defer from twisted.web.server import Request from unpaddedbase64 import decode_base64 @@ -165,9 +164,7 @@ async def verifyServerSignedJson( ) raise SignatureVerifyException("No matching signature found") - async def authenticate_request( - self, request: "Request", content: Optional[bytes] - ): + async def authenticate_request(self, request: "Request", content: Optional[bytes]): """Authenticates a Matrix federation request based on the X-Matrix header XXX: Copied largely from synapse diff --git a/sydent/http/httpclient.py b/sydent/http/httpclient.py index d4adc497..dfff5f77 100644 --- a/sydent/http/httpclient.py +++ b/sydent/http/httpclient.py @@ -15,9 +15,8 @@ import json import logging from io import BytesIO -from typing import TYPE_CHECKING, Any, Dict, Generator, Optional +from typing import TYPE_CHECKING, Any, Dict, Optional -from twisted.internet import defer from twisted.web.client import Agent, FileBodyProducer from twisted.web.http_headers import Headers from twisted.web.iweb import IAgent diff --git a/sydent/http/matrixfederationagent.py b/sydent/http/matrixfederationagent.py index ef4843a9..3b234648 100644 --- a/sydent/http/matrixfederationagent.py +++ b/sydent/http/matrixfederationagent.py @@ -19,7 +19,6 @@ import attr from netaddr import IPAddress # type: ignore -from twisted.internet import defer from twisted.internet.endpoints import HostnameEndpoint, wrapClientTLS from twisted.internet.interfaces import IStreamClientEndpoint from twisted.web.client import URI, Agent, HTTPConnectionPool, RedirectAgent diff --git a/sydent/http/servlets/registerservlet.py b/sydent/http/servlets/registerservlet.py index 6f151cab..c0e92578 100644 --- a/sydent/http/servlets/registerservlet.py +++ b/sydent/http/servlets/registerservlet.py @@ -57,14 +57,16 @@ def render_POST(self, request: Request) -> Generator: "error": "matrix_server_name must be a valid Matrix server name (IP address or hostname)", } - result = yield defer.ensureDeferred(self.client.get_json( - "matrix://%s/_matrix/federation/v1/openid/userinfo?access_token=%s" - % ( - matrix_server, - urllib.parse.quote(args["access_token"]), - ), - 1024 * 5, - )) + result = yield defer.ensureDeferred( + self.client.get_json( + "matrix://%s/_matrix/federation/v1/openid/userinfo?access_token=%s" + % ( + matrix_server, + urllib.parse.quote(args["access_token"]), + ), + 1024 * 5, + ) + ) if "sub" not in result: raise Exception("Invalid response from homeserver") diff --git a/tests/test_invites.py b/tests/test_invites.py index 27bd313c..f6ebfeff 100644 --- a/tests/test_invites.py +++ b/tests/test_invites.py @@ -1,12 +1,10 @@ -from unittest.mock import Mock - from twisted.trial import unittest from twisted.web.client import Response from sydent.db.invite_tokens import JoinTokenStore from sydent.http.httpclient import FederationHttpClient from sydent.http.servlets.store_invite_servlet import StoreInviteServlet -from tests.utils import make_sydent, AsyncMock +from tests.utils import AsyncMock, make_sydent class ThreepidInvitesTestCase(unittest.TestCase): From 268f8a5a46470e7578ee8420b3692c1d009f13d6 Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Tue, 22 Jun 2021 11:54:58 -0700 Subject: [PATCH 09/12] add changelog entry Signed-off-by: H.Shay --- changelog.d/368.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/368.misc diff --git a/changelog.d/368.misc b/changelog.d/368.misc new file mode 100644 index 00000000..ce3f6734 --- /dev/null +++ b/changelog.d/368.misc @@ -0,0 +1 @@ +Convert inlineCallbacks to async/await. \ No newline at end of file From 853ae2b7a2179bb233638c48dc2fc45b365d1c4e Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Tue, 22 Jun 2021 13:01:45 -0700 Subject: [PATCH 10/12] requested changes --- tests/test_invites.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/test_invites.py b/tests/test_invites.py index f6ebfeff..65d410a3 100644 --- a/tests/test_invites.py +++ b/tests/test_invites.py @@ -1,3 +1,5 @@ +from unittest.mock import Mock + from twisted.trial import unittest from twisted.web.client import Response @@ -32,10 +34,10 @@ def test_delete_on_bind(self): address = "john@example.com" # Mock post_json_get_nothing so the /onBind call doesn't fail. - def post_json_get_nothing(uri, post_json, opts): + async def post_json_get_nothing(uri, post_json, opts): return Response((b"HTTP", 1, 1), 200, b"OK", None, None) - FederationHttpClient.post_json_get_nothing = AsyncMock( + FederationHttpClient.post_json_get_nothing = Mock( side_effect=post_json_get_nothing, ) @@ -117,10 +119,10 @@ def test_no_delete_on_bind(self): address = "john@example.com" # Mock post_json_get_nothing so the /onBind call doesn't fail. - def post_json_get_nothing(uri, post_json, opts): + async def post_json_get_nothing(uri, post_json, opts): return Response((b"HTTP", 1, 1), 200, b"OK", None, None) - FederationHttpClient.post_json_get_nothing = AsyncMock( + FederationHttpClient.post_json_get_nothing = Mock( side_effect=post_json_get_nothing, ) From eb6382b8e405b750cb95de9427ee7c838e309a1b Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Tue, 22 Jun 2021 13:02:45 -0700 Subject: [PATCH 11/12] lints --- tests/test_invites.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_invites.py b/tests/test_invites.py index 65d410a3..bca426f2 100644 --- a/tests/test_invites.py +++ b/tests/test_invites.py @@ -6,7 +6,7 @@ from sydent.db.invite_tokens import JoinTokenStore from sydent.http.httpclient import FederationHttpClient from sydent.http.servlets.store_invite_servlet import StoreInviteServlet -from tests.utils import AsyncMock, make_sydent +from tests.utils import make_sydent class ThreepidInvitesTestCase(unittest.TestCase): From db40091dc154b3a81678df9e15ae09b6997245ec Mon Sep 17 00:00:00 2001 From: "H.Shay" Date: Thu, 24 Jun 2021 08:39:47 -0700 Subject: [PATCH 12/12] requested changes --- sydent/http/matrixfederationagent.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sydent/http/matrixfederationagent.py b/sydent/http/matrixfederationagent.py index 3b234648..f9bce0a9 100644 --- a/sydent/http/matrixfederationagent.py +++ b/sydent/http/matrixfederationagent.py @@ -19,6 +19,7 @@ import attr from netaddr import IPAddress # type: ignore +from twisted.internet import defer from twisted.internet.endpoints import HostnameEndpoint, wrapClientTLS from twisted.internet.interfaces import IStreamClientEndpoint from twisted.web.client import URI, Agent, HTTPConnectionPool, RedirectAgent @@ -113,7 +114,8 @@ def __init__( # `None`: there is no (valid) .well-known here self._well_known_cache = _well_known_cache - async def request( + @defer.inlineCallbacks + def request( self, method: bytes, uri: bytes, @@ -144,7 +146,7 @@ async def request( :rtype: Deferred[twisted.web.iweb.IResponse] """ parsed_uri = URI.fromBytes(uri, defaultPort=-1) - res = await self._route_matrix_uri(parsed_uri) + res = yield defer.ensureDeferred(self._route_matrix_uri(parsed_uri)) # set up the TLS connection params # @@ -181,7 +183,7 @@ def endpointForURI(_uri): return ep agent = Agent.usingEndpointFactory(self._reactor, EndpointFactory(), self._pool) - res = await agent.request(method, uri, headers, bodyProducer) + res = yield agent.request(method, uri, headers, bodyProducer) return res async def _route_matrix_uri(