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 1 commit
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
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
2 changes: 1 addition & 1 deletion tests/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def getResourceFor(self, request: Request) -> IResource:


def make_request(
reactor,
reactor: MemoryReactorClock,
site: Union[Site, FakeSite],
method: Union[bytes, str],
path: Union[bytes, str],
Expand Down
17 changes: 15 additions & 2 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
Type,
TypeVar,
Union,
cast,
)
from unittest.mock import Mock, patch

Expand All @@ -45,7 +46,7 @@
from twisted.internet.defer import Deferred, ensureDeferred
from twisted.python.failure import Failure
from twisted.python.threadpool import ThreadPool
from twisted.test.proto_helpers import MemoryReactor
from twisted.test.proto_helpers import MemoryReactor, MemoryReactorClock
from twisted.trial import unittest
from twisted.web.resource import Resource
from twisted.web.server import Request
Expand Down Expand Up @@ -296,7 +297,19 @@ def setUp(self) -> None:

from tests.rest.client.utils import RestHelper

self.helper = RestHelper(self.hs, self.site, getattr(self, "user_id", None))
# HomeServer's reactor is ISynapseReactor, but for tests it should be
# MemoryReactorClock, which some of the internal mechanisms of tests
# depend on.
#
# Attempting to assert that here causes mypy to think the rest the code
# below the assertion to be unreachable, so just cast it. Hopefully this
# is true!
clokep marked this conversation as resolved.
Show resolved Hide resolved
self.helper = RestHelper(
self.hs,
cast(MemoryReactorClock, self.hs.get_reactor()),
self.site,
getattr(self, "user_id", None),
)

if hasattr(self, "user_id"):
if self.hijack_auth:
Expand Down