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

Commit

Permalink
Merge pull request #8946 from matrix-org/rav/refactor_send_request
Browse files Browse the repository at this point in the history
Remove `Request` return value from `make_request`
  • Loading branch information
richvdh committed Dec 16, 2020
2 parents 3ad699c + c9dd47d commit 651e1ae
Show file tree
Hide file tree
Showing 60 changed files with 776 additions and 1,028 deletions.
1 change: 1 addition & 0 deletions changelog.d/8946.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor test utilities for injecting HTTP requests.
4 changes: 2 additions & 2 deletions tests/app/test_frontend_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_listen_http_with_presence_enabled(self):
self.assertEqual(len(self.reactor.tcpServers), 1)
site = self.reactor.tcpServers[0][1]

_, channel = make_request(self.reactor, site, "PUT", "presence/a/status")
channel = make_request(self.reactor, site, "PUT", "presence/a/status")

# 400 + unrecognised, because nothing is registered
self.assertEqual(channel.code, 400)
Expand All @@ -77,7 +77,7 @@ def test_listen_http_with_presence_disabled(self):
self.assertEqual(len(self.reactor.tcpServers), 1)
site = self.reactor.tcpServers[0][1]

_, channel = make_request(self.reactor, site, "PUT", "presence/a/status")
channel = make_request(self.reactor, site, "PUT", "presence/a/status")

# 401, because the stub servlet still checks authentication
self.assertEqual(channel.code, 401)
Expand Down
4 changes: 2 additions & 2 deletions tests/app/test_openid_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_openid_listener(self, names, expectation):
return
raise

_, channel = make_request(
channel = make_request(
self.reactor, site, "GET", "/_matrix/federation/v1/openid/userinfo"
)

Expand Down Expand Up @@ -121,7 +121,7 @@ def test_openid_listener(self, names, expectation):
return
raise

_, channel = make_request(
channel = make_request(
self.reactor, site, "GET", "/_matrix/federation/v1/openid/userinfo"
)

Expand Down
4 changes: 2 additions & 2 deletions tests/federation/test_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_complexity_simple(self):
)

# Get the room complexity
request, channel = self.make_request(
channel = self.make_request(
"GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
)
self.assertEquals(200, channel.code)
Expand All @@ -60,7 +60,7 @@ def test_complexity_simple(self):
store.get_current_state_event_counts = lambda x: make_awaitable(500 * 1.23)

# Get the room complexity again -- make sure it's our artificial value
request, channel = self.make_request(
channel = self.make_request(
"GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
)
self.assertEquals(200, channel.code)
Expand Down
6 changes: 3 additions & 3 deletions tests/federation/test_federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_bad_request(self, query_content):

"/get_missing_events/(?P<room_id>[^/]*)/?"

request, channel = self.make_request(
channel = self.make_request(
"POST",
"/_matrix/federation/v1/get_missing_events/%s" % (room_1,),
query_content,
Expand Down Expand Up @@ -95,7 +95,7 @@ def test_without_event_id(self):
room_1 = self.helper.create_room_as(u1, tok=u1_token)
self.inject_room_member(room_1, "@user:other.example.com", "join")

request, channel = self.make_request(
channel = self.make_request(
"GET", "/_matrix/federation/v1/state/%s" % (room_1,)
)
self.assertEquals(200, channel.code, channel.result)
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_needs_to_be_in_room(self):

room_1 = self.helper.create_room_as(u1, tok=u1_token)

request, channel = self.make_request(
channel = self.make_request(
"GET", "/_matrix/federation/v1/state/%s" % (room_1,)
)
self.assertEquals(403, channel.code, channel.result)
Expand Down
8 changes: 2 additions & 6 deletions tests/federation/transport/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ def authenticate_request(self, request, content):

@override_config({"allow_public_rooms_over_federation": False})
def test_blocked_public_room_list_over_federation(self):
request, channel = self.make_request(
"GET", "/_matrix/federation/v1/publicRooms"
)
channel = self.make_request("GET", "/_matrix/federation/v1/publicRooms")
self.assertEquals(403, channel.code)

@override_config({"allow_public_rooms_over_federation": True})
def test_open_public_room_list_over_federation(self):
request, channel = self.make_request(
"GET", "/_matrix/federation/v1/publicRooms"
)
channel = self.make_request("GET", "/_matrix/federation/v1/publicRooms")
self.assertEquals(200, channel.code)
12 changes: 6 additions & 6 deletions tests/handlers/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def prepare(self, reactor, clock, hs):
def test_denied(self):
room_id = self.helper.create_room_as(self.user_id)

request, channel = self.make_request(
channel = self.make_request(
"PUT",
b"directory/room/%23test%3Atest",
('{"room_id":"%s"}' % (room_id,)).encode("ascii"),
Expand All @@ -415,7 +415,7 @@ def test_denied(self):
def test_allowed(self):
room_id = self.helper.create_room_as(self.user_id)

request, channel = self.make_request(
channel = self.make_request(
"PUT",
b"directory/room/%23unofficial_test%3Atest",
('{"room_id":"%s"}' % (room_id,)).encode("ascii"),
Expand All @@ -431,7 +431,7 @@ class TestRoomListSearchDisabled(unittest.HomeserverTestCase):
def prepare(self, reactor, clock, hs):
room_id = self.helper.create_room_as(self.user_id)

request, channel = self.make_request(
channel = self.make_request(
"PUT", b"directory/list/room/%s" % (room_id.encode("ascii"),), b"{}"
)
self.assertEquals(200, channel.code, channel.result)
Expand All @@ -446,21 +446,21 @@ def test_disabling_room_list(self):
self.directory_handler.enable_room_list_search = True

# Room list is enabled so we should get some results
request, channel = self.make_request("GET", b"publicRooms")
channel = self.make_request("GET", b"publicRooms")
self.assertEquals(200, channel.code, channel.result)
self.assertTrue(len(channel.json_body["chunk"]) > 0)

self.room_list_handler.enable_room_list_search = False
self.directory_handler.enable_room_list_search = False

# Room list disabled so we should get no results
request, channel = self.make_request("GET", b"publicRooms")
channel = self.make_request("GET", b"publicRooms")
self.assertEquals(200, channel.code, channel.result)
self.assertTrue(len(channel.json_body["chunk"]) == 0)

# Room list disabled so we shouldn't be allowed to publish rooms
room_id = self.helper.create_room_as(self.user_id)
request, channel = self.make_request(
channel = self.make_request(
"PUT", b"directory/list/room/%s" % (room_id.encode("ascii"),), b"{}"
)
self.assertEquals(403, channel.code, channel.result)
2 changes: 1 addition & 1 deletion tests/handlers/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_deny_redact_server_acl(self):

# Redaction of event should fail.
path = "/_matrix/client/r0/rooms/%s/redact/%s" % (self.room_id, event_id)
request, channel = self.make_request(
channel = self.make_request(
"POST", path, content={}, access_token=self.access_token
)
self.assertEqual(int(channel.result["code"]), 403)
6 changes: 3 additions & 3 deletions tests/handlers/test_password_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def test_custom_auth_no_local_user_fallback(self):
self.assertEqual(channel.code, 400, channel.result)

def _get_login_flows(self) -> JsonDict:
_, channel = self.make_request("GET", "/_matrix/client/r0/login")
channel = self.make_request("GET", "/_matrix/client/r0/login")
self.assertEqual(channel.code, 200, channel.result)
return channel.json_body["flows"]

Expand All @@ -560,7 +560,7 @@ def _send_password_login(self, user: str, password: str) -> FakeChannel:

def _send_login(self, type, user, **params) -> FakeChannel:
params.update({"identifier": {"type": "m.id.user", "user": user}, "type": type})
_, channel = self.make_request("POST", "/_matrix/client/r0/login", params)
channel = self.make_request("POST", "/_matrix/client/r0/login", params)
return channel

def _start_delete_device_session(self, access_token, device_id) -> str:
Expand Down Expand Up @@ -597,7 +597,7 @@ def _delete_device(
self, access_token: str, device: str, body: Union[JsonDict, bytes] = b"",
) -> FakeChannel:
"""Delete an individual device."""
_, channel = self.make_request(
channel = self.make_request(
"DELETE", "devices/" + device, body, access_token=access_token
)
return channel
2 changes: 1 addition & 1 deletion tests/handlers/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test_started_typing_remote_recv(self):

self.assertEquals(self.event_source.get_current_key(), 0)

(request, channel) = self.make_request(
channel = self.make_request(
"PUT",
"/_matrix/federation/v1/send/1000000",
_make_edu_transaction_json(
Expand Down
4 changes: 2 additions & 2 deletions tests/handlers/test_user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,15 +534,15 @@ def test_disabling_room_list(self):
self.helper.join(room, user=u2)

# Assert user directory is not empty
request, channel = self.make_request(
channel = self.make_request(
"POST", b"user_directory/search", b'{"search_term":"user2"}'
)
self.assertEquals(200, channel.code, channel.result)
self.assertTrue(len(channel.json_body["results"]) > 0)

# Disable user directory and check search returns nothing
self.config.user_directory_search_enabled = False
request, channel = self.make_request(
channel = self.make_request(
"POST", b"user_directory/search", b'{"search_term":"user2"}'
)
self.assertEquals(200, channel.code, channel.result)
Expand Down
8 changes: 4 additions & 4 deletions tests/http/test_additional_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ def test_async(self):
handler = _AsyncTestCustomEndpoint({}, None).handle_request
resource = AdditionalResource(self.hs, handler)

request, channel = make_request(self.reactor, FakeSite(resource), "GET", "/")
channel = make_request(self.reactor, FakeSite(resource), "GET", "/")

self.assertEqual(request.code, 200)
self.assertEqual(channel.code, 200)
self.assertEqual(channel.json_body, {"some_key": "some_value_async"})

def test_sync(self):
handler = _SyncTestCustomEndpoint({}, None).handle_request
resource = AdditionalResource(self.hs, handler)

request, channel = make_request(self.reactor, FakeSite(resource), "GET", "/")
channel = make_request(self.reactor, FakeSite(resource), "GET", "/")

self.assertEqual(request.code, 200)
self.assertEqual(channel.code, 200)
self.assertEqual(channel.json_body, {"some_key": "some_value_sync"})
2 changes: 1 addition & 1 deletion tests/push/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ def _test_push_unread_count(self):
# This will actually trigger a new notification to be sent out so that
# even if the user does not receive another message, their unread
# count goes down
request, channel = self.make_request(
channel = self.make_request(
"POST",
"/rooms/%s/receipt/m.read/%s" % (room_id, first_message_event_id),
{},
Expand Down
26 changes: 12 additions & 14 deletions tests/replication/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import Tuple

from synapse.http.site import SynapseRequest
from synapse.rest.client.v2_alpha import register

from tests.replication._base import BaseMultiWorkerStreamTestCase
Expand Down Expand Up @@ -47,7 +45,7 @@ def _get_worker_hs_config(self) -> dict:

return config

def _test_register(self) -> Tuple[SynapseRequest, FakeChannel]:
def _test_register(self) -> FakeChannel:
"""Run the actual test:
1. Create a worker homeserver.
Expand All @@ -59,14 +57,14 @@ def _test_register(self) -> Tuple[SynapseRequest, FakeChannel]:
worker_hs = self.make_worker_hs("synapse.app.client_reader")
site = self._hs_to_site[worker_hs]

request_1, channel_1 = make_request(
channel_1 = make_request(
self.reactor,
site,
"POST",
"register",
{"username": "user", "type": "m.login.password", "password": "bar"},
) # type: SynapseRequest, FakeChannel
self.assertEqual(request_1.code, 401)
)
self.assertEqual(channel_1.code, 401)

# Grab the session
session = channel_1.json_body["session"]
Expand All @@ -83,8 +81,8 @@ def _test_register(self) -> Tuple[SynapseRequest, FakeChannel]:
def test_no_auth(self):
"""With no authentication the request should finish.
"""
request, channel = self._test_register()
self.assertEqual(request.code, 200)
channel = self._test_register()
self.assertEqual(channel.code, 200)

# We're given a registered user.
self.assertEqual(channel.json_body["user_id"], "@user:test")
Expand All @@ -93,8 +91,8 @@ def test_no_auth(self):
def test_missing_auth(self):
"""If the main process expects a secret that is not provided, an error results.
"""
request, channel = self._test_register()
self.assertEqual(request.code, 500)
channel = self._test_register()
self.assertEqual(channel.code, 500)

@override_config(
{
Expand All @@ -105,15 +103,15 @@ def test_missing_auth(self):
def test_unauthorized(self):
"""If the main process receives the wrong secret, an error results.
"""
request, channel = self._test_register()
self.assertEqual(request.code, 500)
channel = self._test_register()
self.assertEqual(channel.code, 500)

@override_config({"worker_replication_secret": "my-secret"})
def test_authorized(self):
"""The request should finish when the worker provides the authentication header.
"""
request, channel = self._test_register()
self.assertEqual(request.code, 200)
channel = self._test_register()
self.assertEqual(channel.code, 200)

# We're given a registered user.
self.assertEqual(channel.json_body["user_id"], "@user:test")
Loading

0 comments on commit 651e1ae

Please sign in to comment.