Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix-up type hints in tests/server.py. (#15084)
Browse files Browse the repository at this point in the history
This file was being ignored by mypy, we remove that
and add the missing type hints & deal with any fallout.
  • Loading branch information
clokep authored Feb 17, 2023
1 parent 61bfcd6 commit c9b9143
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 129 deletions.
1 change: 1 addition & 0 deletions changelog.d/15084.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type hints.
2 changes: 0 additions & 2 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ exclude = (?x)
|synapse/storage/databases/__init__.py
|synapse/storage/databases/main/cache.py
|synapse/storage/schema/

|tests/server.py
)$

[mypy-synapse.federation.transport.client]
Expand Down
6 changes: 2 additions & 4 deletions tests/appservice/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING, List, Optional, Sequence, Tuple, cast
from typing import List, Optional, Sequence, Tuple, cast
from unittest.mock import Mock

from typing_extensions import TypeAlias

from twisted.internet import defer
from twisted.test.proto_helpers import MemoryReactor

from synapse.appservice import (
ApplicationService,
Expand All @@ -40,9 +41,6 @@

from ..utils import MockClock

if TYPE_CHECKING:
from twisted.internet.testing import MemoryReactor


class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
def setUp(self) -> None:
Expand Down
5 changes: 3 additions & 2 deletions tests/http/federation/test_matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
IOpenSSLClientConnectionCreator,
IProtocolFactory,
)
from twisted.internet.protocol import Factory
from twisted.internet.protocol import Factory, Protocol
from twisted.protocols.tls import TLSMemoryBIOFactory, TLSMemoryBIOProtocol
from twisted.web._newclient import ResponseNeverReceived
from twisted.web.client import Agent
Expand Down Expand Up @@ -466,7 +466,8 @@ def _do_get_via_proxy(
else:
assert isinstance(proxy_server_transport, FakeTransport)
client_protocol = proxy_server_transport.other
c2s_transport = client_protocol.transport
assert isinstance(client_protocol, Protocol)
c2s_transport = checked_cast(FakeTransport, client_protocol.transport)
c2s_transport.other = server_ssl_protocol

self.reactor.advance(0)
Expand Down
5 changes: 3 additions & 2 deletions tests/http/test_proxyagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
_WrappingProtocol,
)
from twisted.internet.interfaces import IProtocol, IProtocolFactory
from twisted.internet.protocol import Factory
from twisted.internet.protocol import Factory, Protocol
from twisted.protocols.tls import TLSMemoryBIOFactory, TLSMemoryBIOProtocol
from twisted.web.http import HTTPChannel

Expand Down Expand Up @@ -644,7 +644,8 @@ def _do_https_request_via_proxy(
else:
assert isinstance(proxy_server_transport, FakeTransport)
client_protocol = proxy_server_transport.other
c2s_transport = client_protocol.transport
assert isinstance(client_protocol, Protocol)
c2s_transport = checked_cast(FakeTransport, client_protocol.transport)
c2s_transport.other = server_ssl_protocol

self.reactor.advance(0)
Expand Down
14 changes: 3 additions & 11 deletions tests/rest/client/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from tests import unittest
from tests.handlers.test_oidc import HAS_OIDC
from tests.rest.client.utils import TEST_OIDC_CONFIG, TEST_OIDC_ISSUER
from tests.server import FakeChannel, make_request
from tests.server import FakeChannel
from tests.unittest import override_config, skip_unless


Expand Down Expand Up @@ -1322,16 +1322,8 @@ def test_logout_during_login(self) -> None:
channel = self.submit_logout_token(logout_token)
self.assertEqual(channel.code, 200)

# Now try to exchange the login token
channel = make_request(
self.hs.get_reactor(),
self.site,
"POST",
"/login",
content={"type": "m.login.token", "token": login_token},
)
# It should have failed
self.assertEqual(channel.code, 403)
# Now try to exchange the login token, it should fail.
self.helper.login_via_token(login_token, 403)

@override_config(
{
Expand Down
58 changes: 38 additions & 20 deletions tests/rest/client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import attr
from typing_extensions import Literal

from twisted.test.proto_helpers import MemoryReactorClock
from twisted.web.resource import Resource
from twisted.web.server import Site

Expand Down Expand Up @@ -67,6 +68,7 @@ class RestHelper:
"""

hs: HomeServer
reactor: MemoryReactorClock
site: Site
auth_user_id: Optional[str]

Expand Down Expand Up @@ -142,7 +144,7 @@ def create_room_as(
path = path + "?access_token=%s" % tok

channel = make_request(
self.hs.get_reactor(),
self.reactor,
self.site,
"POST",
path,
Expand Down Expand Up @@ -216,7 +218,7 @@ def knock(
data["reason"] = reason

channel = make_request(
self.hs.get_reactor(),
self.reactor,
self.site,
"POST",
path,
Expand Down Expand Up @@ -313,7 +315,7 @@ def change_membership(
data.update(extra_data or {})

channel = make_request(
self.hs.get_reactor(),
self.reactor,
self.site,
"PUT",
path,
Expand Down Expand Up @@ -394,7 +396,7 @@ def send_event(
path = path + "?access_token=%s" % tok

channel = make_request(
self.hs.get_reactor(),
self.reactor,
self.site,
"PUT",
path,
Expand Down Expand Up @@ -433,7 +435,7 @@ def get_event(
path = path + f"?access_token={tok}"

channel = make_request(
self.hs.get_reactor(),
self.reactor,
self.site,
"GET",
path,
Expand Down Expand Up @@ -488,7 +490,7 @@ def _read_write_state(
if body is not None:
content = json.dumps(body).encode("utf8")

channel = make_request(self.hs.get_reactor(), self.site, method, path, content)
channel = make_request(self.reactor, self.site, method, path, content)

assert channel.code == expect_code, "Expected: %d, got: %d, resp: %r" % (
expect_code,
Expand Down Expand Up @@ -573,8 +575,8 @@ def upload_media(
image_length = len(image_data)
path = "/_matrix/media/r0/upload?filename=%s" % (filename,)
channel = make_request(
self.hs.get_reactor(),
FakeSite(resource, self.hs.get_reactor()),
self.reactor,
FakeSite(resource, self.reactor),
"POST",
path,
content=image_data,
Expand Down Expand Up @@ -603,7 +605,7 @@ def whoami(
expect_code: The return code to expect from attempting the whoami request
"""
channel = make_request(
self.hs.get_reactor(),
self.reactor,
self.site,
"GET",
"account/whoami",
Expand Down Expand Up @@ -642,7 +644,7 @@ def login_via_oidc(
) -> Tuple[JsonDict, FakeAuthorizationGrant]:
"""Log in (as a new user) via OIDC
Returns the result of the final token login.
Returns the result of the final token login and the fake authorization grant.
Requires that "oidc_config" in the homeserver config be set appropriately
(TEST_OIDC_CONFIG is a suitable example) - and by implication, needs a
Expand Down Expand Up @@ -672,10 +674,28 @@ def login_via_oidc(
assert m, channel.text_body
login_token = m.group(1)

# finally, submit the matrix login token to the login API, which gives us our
# matrix access token and device id.
return self.login_via_token(login_token, expected_status), grant

def login_via_token(
self,
login_token: str,
expected_status: int = 200,
) -> JsonDict:
"""Submit the matrix login token to the login API, which gives us our
matrix access token and device id.Log in (as a new user) via OIDC
Returns the result of the token login.
Requires that "oidc_config" in the homeserver config be set appropriately
(TEST_OIDC_CONFIG is a suitable example) - and by implication, needs a
"public_base_url".
Also requires the login servlet and the OIDC callback resource to be mounted at
the normal places.
"""

channel = make_request(
self.hs.get_reactor(),
self.reactor,
self.site,
"POST",
"/login",
Expand All @@ -684,7 +704,7 @@ def login_via_oidc(
assert (
channel.code == expected_status
), f"unexpected status in response: {channel.code}"
return channel.json_body, grant
return channel.json_body

def auth_via_oidc(
self,
Expand Down Expand Up @@ -805,7 +825,7 @@ def complete_oidc_auth(
with fake_serer.patch_homeserver(hs=self.hs):
# now hit the callback URI with the right params and a made-up code
channel = make_request(
self.hs.get_reactor(),
self.reactor,
self.site,
"GET",
callback_uri,
Expand Down Expand Up @@ -849,7 +869,7 @@ def initiate_sso_login(
# is the easiest way of figuring out what the Host header ought to be set to
# to keep Synapse happy.
channel = make_request(
self.hs.get_reactor(),
self.reactor,
self.site,
"GET",
uri,
Expand All @@ -867,7 +887,7 @@ def get_location(channel: FakeChannel) -> str:
location = get_location(channel)
parts = urllib.parse.urlsplit(location)
channel = make_request(
self.hs.get_reactor(),
self.reactor,
self.site,
"GET",
urllib.parse.urlunsplit(("", "") + parts[2:]),
Expand Down Expand Up @@ -900,9 +920,7 @@ def initiate_sso_ui_auth(
+ urllib.parse.urlencode({"session": ui_auth_session_id})
)
# hit the redirect url (which will issue a cookie and state)
channel = make_request(
self.hs.get_reactor(), self.site, "GET", sso_redirect_endpoint
)
channel = make_request(self.reactor, self.site, "GET", sso_redirect_endpoint)
# that should serve a confirmation page
assert channel.code == HTTPStatus.OK, channel.text_body
channel.extract_cookies(cookies)
Expand Down
Loading

0 comments on commit c9b9143

Please sign in to comment.