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

Stop ignoring tests/server.py #15084

Merged
merged 21 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 1 addition & 3 deletions tests/appservice/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved


class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
def setUp(self) -> None:
Expand Down
5 changes: 4 additions & 1 deletion 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,10 @@ def _do_get_via_proxy(
else:
assert isinstance(proxy_server_transport, FakeTransport)
client_protocol = proxy_server_transport.other
assert isinstance(client_protocol, Protocol)
c2s_transport = client_protocol.transport
assert c2s_transport is not None
assert isinstance(c2s_transport, FakeTransport)
clokep marked this conversation as resolved.
Show resolved Hide resolved
c2s_transport.other = server_ssl_protocol

self.reactor.advance(0)
Expand Down
5 changes: 4 additions & 1 deletion 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,10 @@ def _do_https_request_via_proxy(
else:
assert isinstance(proxy_server_transport, FakeTransport)
client_protocol = proxy_server_transport.other
assert isinstance(client_protocol, Protocol)
c2s_transport = client_protocol.transport
assert c2s_transport is not None
assert isinstance(c2s_transport, FakeTransport)
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