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

Commit c9b9143

Browse files
authored
Fix-up type hints in tests/server.py. (#15084)
This file was being ignored by mypy, we remove that and add the missing type hints & deal with any fallout.
1 parent 61bfcd6 commit c9b9143

File tree

9 files changed

+226
-129
lines changed

9 files changed

+226
-129
lines changed

changelog.d/15084.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve type hints.

mypy.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ exclude = (?x)
3131
|synapse/storage/databases/__init__.py
3232
|synapse/storage/databases/main/cache.py
3333
|synapse/storage/schema/
34-
35-
|tests/server.py
3634
)$
3735

3836
[mypy-synapse.federation.transport.client]

tests/appservice/test_scheduler.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import TYPE_CHECKING, List, Optional, Sequence, Tuple, cast
14+
from typing import List, Optional, Sequence, Tuple, cast
1515
from unittest.mock import Mock
1616

1717
from typing_extensions import TypeAlias
1818

1919
from twisted.internet import defer
20+
from twisted.test.proto_helpers import MemoryReactor
2021

2122
from synapse.appservice import (
2223
ApplicationService,
@@ -40,9 +41,6 @@
4041

4142
from ..utils import MockClock
4243

43-
if TYPE_CHECKING:
44-
from twisted.internet.testing import MemoryReactor
45-
4644

4745
class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
4846
def setUp(self) -> None:

tests/http/federation/test_matrix_federation_agent.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
IOpenSSLClientConnectionCreator,
3131
IProtocolFactory,
3232
)
33-
from twisted.internet.protocol import Factory
33+
from twisted.internet.protocol import Factory, Protocol
3434
from twisted.protocols.tls import TLSMemoryBIOFactory, TLSMemoryBIOProtocol
3535
from twisted.web._newclient import ResponseNeverReceived
3636
from twisted.web.client import Agent
@@ -466,7 +466,8 @@ def _do_get_via_proxy(
466466
else:
467467
assert isinstance(proxy_server_transport, FakeTransport)
468468
client_protocol = proxy_server_transport.other
469-
c2s_transport = client_protocol.transport
469+
assert isinstance(client_protocol, Protocol)
470+
c2s_transport = checked_cast(FakeTransport, client_protocol.transport)
470471
c2s_transport.other = server_ssl_protocol
471472

472473
self.reactor.advance(0)

tests/http/test_proxyagent.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
_WrappingProtocol,
2929
)
3030
from twisted.internet.interfaces import IProtocol, IProtocolFactory
31-
from twisted.internet.protocol import Factory
31+
from twisted.internet.protocol import Factory, Protocol
3232
from twisted.protocols.tls import TLSMemoryBIOFactory, TLSMemoryBIOProtocol
3333
from twisted.web.http import HTTPChannel
3434

@@ -644,7 +644,8 @@ def _do_https_request_via_proxy(
644644
else:
645645
assert isinstance(proxy_server_transport, FakeTransport)
646646
client_protocol = proxy_server_transport.other
647-
c2s_transport = client_protocol.transport
647+
assert isinstance(client_protocol, Protocol)
648+
c2s_transport = checked_cast(FakeTransport, client_protocol.transport)
648649
c2s_transport.other = server_ssl_protocol
649650

650651
self.reactor.advance(0)

tests/rest/client/test_auth.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from tests import unittest
3535
from tests.handlers.test_oidc import HAS_OIDC
3636
from tests.rest.client.utils import TEST_OIDC_CONFIG, TEST_OIDC_ISSUER
37-
from tests.server import FakeChannel, make_request
37+
from tests.server import FakeChannel
3838
from tests.unittest import override_config, skip_unless
3939

4040

@@ -1322,16 +1322,8 @@ def test_logout_during_login(self) -> None:
13221322
channel = self.submit_logout_token(logout_token)
13231323
self.assertEqual(channel.code, 200)
13241324

1325-
# Now try to exchange the login token
1326-
channel = make_request(
1327-
self.hs.get_reactor(),
1328-
self.site,
1329-
"POST",
1330-
"/login",
1331-
content={"type": "m.login.token", "token": login_token},
1332-
)
1333-
# It should have failed
1334-
self.assertEqual(channel.code, 403)
1325+
# Now try to exchange the login token, it should fail.
1326+
self.helper.login_via_token(login_token, 403)
13351327

13361328
@override_config(
13371329
{

tests/rest/client/utils.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import attr
3737
from typing_extensions import Literal
3838

39+
from twisted.test.proto_helpers import MemoryReactorClock
3940
from twisted.web.resource import Resource
4041
from twisted.web.server import Site
4142

@@ -67,6 +68,7 @@ class RestHelper:
6768
"""
6869

6970
hs: HomeServer
71+
reactor: MemoryReactorClock
7072
site: Site
7173
auth_user_id: Optional[str]
7274

@@ -142,7 +144,7 @@ def create_room_as(
142144
path = path + "?access_token=%s" % tok
143145

144146
channel = make_request(
145-
self.hs.get_reactor(),
147+
self.reactor,
146148
self.site,
147149
"POST",
148150
path,
@@ -216,7 +218,7 @@ def knock(
216218
data["reason"] = reason
217219

218220
channel = make_request(
219-
self.hs.get_reactor(),
221+
self.reactor,
220222
self.site,
221223
"POST",
222224
path,
@@ -313,7 +315,7 @@ def change_membership(
313315
data.update(extra_data or {})
314316

315317
channel = make_request(
316-
self.hs.get_reactor(),
318+
self.reactor,
317319
self.site,
318320
"PUT",
319321
path,
@@ -394,7 +396,7 @@ def send_event(
394396
path = path + "?access_token=%s" % tok
395397

396398
channel = make_request(
397-
self.hs.get_reactor(),
399+
self.reactor,
398400
self.site,
399401
"PUT",
400402
path,
@@ -433,7 +435,7 @@ def get_event(
433435
path = path + f"?access_token={tok}"
434436

435437
channel = make_request(
436-
self.hs.get_reactor(),
438+
self.reactor,
437439
self.site,
438440
"GET",
439441
path,
@@ -488,7 +490,7 @@ def _read_write_state(
488490
if body is not None:
489491
content = json.dumps(body).encode("utf8")
490492

491-
channel = make_request(self.hs.get_reactor(), self.site, method, path, content)
493+
channel = make_request(self.reactor, self.site, method, path, content)
492494

493495
assert channel.code == expect_code, "Expected: %d, got: %d, resp: %r" % (
494496
expect_code,
@@ -573,8 +575,8 @@ def upload_media(
573575
image_length = len(image_data)
574576
path = "/_matrix/media/r0/upload?filename=%s" % (filename,)
575577
channel = make_request(
576-
self.hs.get_reactor(),
577-
FakeSite(resource, self.hs.get_reactor()),
578+
self.reactor,
579+
FakeSite(resource, self.reactor),
578580
"POST",
579581
path,
580582
content=image_data,
@@ -603,7 +605,7 @@ def whoami(
603605
expect_code: The return code to expect from attempting the whoami request
604606
"""
605607
channel = make_request(
606-
self.hs.get_reactor(),
608+
self.reactor,
607609
self.site,
608610
"GET",
609611
"account/whoami",
@@ -642,7 +644,7 @@ def login_via_oidc(
642644
) -> Tuple[JsonDict, FakeAuthorizationGrant]:
643645
"""Log in (as a new user) via OIDC
644646
645-
Returns the result of the final token login.
647+
Returns the result of the final token login and the fake authorization grant.
646648
647649
Requires that "oidc_config" in the homeserver config be set appropriately
648650
(TEST_OIDC_CONFIG is a suitable example) - and by implication, needs a
@@ -672,10 +674,28 @@ def login_via_oidc(
672674
assert m, channel.text_body
673675
login_token = m.group(1)
674676

675-
# finally, submit the matrix login token to the login API, which gives us our
676-
# matrix access token and device id.
677+
return self.login_via_token(login_token, expected_status), grant
678+
679+
def login_via_token(
680+
self,
681+
login_token: str,
682+
expected_status: int = 200,
683+
) -> JsonDict:
684+
"""Submit the matrix login token to the login API, which gives us our
685+
matrix access token and device id.Log in (as a new user) via OIDC
686+
687+
Returns the result of the token login.
688+
689+
Requires that "oidc_config" in the homeserver config be set appropriately
690+
(TEST_OIDC_CONFIG is a suitable example) - and by implication, needs a
691+
"public_base_url".
692+
693+
Also requires the login servlet and the OIDC callback resource to be mounted at
694+
the normal places.
695+
"""
696+
677697
channel = make_request(
678-
self.hs.get_reactor(),
698+
self.reactor,
679699
self.site,
680700
"POST",
681701
"/login",
@@ -684,7 +704,7 @@ def login_via_oidc(
684704
assert (
685705
channel.code == expected_status
686706
), f"unexpected status in response: {channel.code}"
687-
return channel.json_body, grant
707+
return channel.json_body
688708

689709
def auth_via_oidc(
690710
self,
@@ -805,7 +825,7 @@ def complete_oidc_auth(
805825
with fake_serer.patch_homeserver(hs=self.hs):
806826
# now hit the callback URI with the right params and a made-up code
807827
channel = make_request(
808-
self.hs.get_reactor(),
828+
self.reactor,
809829
self.site,
810830
"GET",
811831
callback_uri,
@@ -849,7 +869,7 @@ def initiate_sso_login(
849869
# is the easiest way of figuring out what the Host header ought to be set to
850870
# to keep Synapse happy.
851871
channel = make_request(
852-
self.hs.get_reactor(),
872+
self.reactor,
853873
self.site,
854874
"GET",
855875
uri,
@@ -867,7 +887,7 @@ def get_location(channel: FakeChannel) -> str:
867887
location = get_location(channel)
868888
parts = urllib.parse.urlsplit(location)
869889
channel = make_request(
870-
self.hs.get_reactor(),
890+
self.reactor,
871891
self.site,
872892
"GET",
873893
urllib.parse.urlunsplit(("", "") + parts[2:]),
@@ -900,9 +920,7 @@ def initiate_sso_ui_auth(
900920
+ urllib.parse.urlencode({"session": ui_auth_session_id})
901921
)
902922
# hit the redirect url (which will issue a cookie and state)
903-
channel = make_request(
904-
self.hs.get_reactor(), self.site, "GET", sso_redirect_endpoint
905-
)
923+
channel = make_request(self.reactor, self.site, "GET", sso_redirect_endpoint)
906924
# that should serve a confirmation page
907925
assert channel.code == HTTPStatus.OK, channel.text_body
908926
channel.extract_cookies(cookies)

0 commit comments

Comments
 (0)