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

Add type hints to tests/rest/client #12072

Merged
merged 2 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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/12072.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type hints to `tests/rest/client`.
19 changes: 12 additions & 7 deletions tests/rest/client/test_consent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
# limitations under the License.

import os
from http import HTTPStatus

from twisted.test.proto_helpers import MemoryReactor

import synapse.rest.admin
from synapse.api.urls import ConsentURIBuilder
from synapse.rest.client import login, room
from synapse.rest.consent import consent_resource
from synapse.server import HomeServer
from synapse.util import Clock

from tests import unittest
from tests.server import FakeSite, make_request
Expand All @@ -32,7 +37,7 @@ class ConsentResourceTestCase(unittest.HomeserverTestCase):
user_id = True
hijack_auth = False

def make_homeserver(self, reactor, clock):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:

config = self.default_config()
config["form_secret"] = "123abc"
Expand All @@ -56,7 +61,7 @@ def make_homeserver(self, reactor, clock):
hs = self.setup_test_homeserver(config=config)
return hs

def test_render_public_consent(self):
def test_render_public_consent(self) -> None:
"""You can observe the terms form without specifying a user"""
resource = consent_resource.ConsentResource(self.hs)
channel = make_request(
Expand All @@ -66,9 +71,9 @@ def test_render_public_consent(self):
"/consent?v=1",
shorthand=False,
)
self.assertEqual(channel.code, 200)
self.assertEqual(channel.code, HTTPStatus.OK)

def test_accept_consent(self):
def test_accept_consent(self) -> None:
"""
A user can use the consent form to accept the terms.
"""
Expand All @@ -92,7 +97,7 @@ def test_accept_consent(self):
access_token=access_token,
shorthand=False,
)
self.assertEqual(channel.code, 200)
self.assertEqual(channel.code, HTTPStatus.OK)

# Get the version from the body, and whether we've consented
version, consented = channel.result["body"].decode("ascii").split(",")
Expand All @@ -107,7 +112,7 @@ def test_accept_consent(self):
access_token=access_token,
shorthand=False,
)
self.assertEqual(channel.code, 200)
self.assertEqual(channel.code, HTTPStatus.OK)

# Fetch the consent page, to get the consent version -- it should have
# changed
Expand All @@ -119,7 +124,7 @@ def test_accept_consent(self):
access_token=access_token,
shorthand=False,
)
self.assertEqual(channel.code, 200)
self.assertEqual(channel.code, HTTPStatus.OK)

# Get the version from the body, and check that it's the version we
# agreed to, and that we've consented to it.
Expand Down
16 changes: 10 additions & 6 deletions tests/rest/client/test_device_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# 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 http import HTTPStatus

from synapse.rest import admin, devices, room, sync
from synapse.rest.client import account, login, register

Expand All @@ -30,7 +32,7 @@ class DeviceListsTestCase(unittest.HomeserverTestCase):
devices.register_servlets,
]

def test_receiving_local_device_list_changes(self):
def test_receiving_local_device_list_changes(self) -> None:
"""Tests that a local users that share a room receive each other's device list
changes.
"""
Expand Down Expand Up @@ -84,7 +86,7 @@ def test_receiving_local_device_list_changes(self):
},
access_token=alice_access_token,
)
self.assertEqual(channel.code, 200, channel.json_body)
self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body)

# Check that bob's incremental sync contains the updated device list.
# If not, the client would only receive the device list update on the
Expand All @@ -97,7 +99,7 @@ def test_receiving_local_device_list_changes(self):
)
self.assertIn(alice_user_id, changed_device_lists, bob_sync_channel.json_body)

def test_not_receiving_local_device_list_changes(self):
def test_not_receiving_local_device_list_changes(self) -> None:
"""Tests a local users DO NOT receive device updates from each other if they do not
share a room.
"""
Expand All @@ -119,7 +121,7 @@ def test_not_receiving_local_device_list_changes(self):
"/sync",
access_token=bob_access_token,
)
self.assertEqual(channel.code, 200, channel.json_body)
self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body)
next_batch_token = channel.json_body["next_batch"]

# ...and then an incremental sync. This should block until the sync stream is woken up,
Expand All @@ -141,11 +143,13 @@ def test_not_receiving_local_device_list_changes(self):
},
access_token=alice_access_token,
)
self.assertEqual(channel.code, 200, channel.json_body)
self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body)

# Check that bob's incremental sync does not contain the updated device list.
bob_sync_channel.await_result()
self.assertEqual(bob_sync_channel.code, 200, bob_sync_channel.json_body)
self.assertEqual(
bob_sync_channel.code, HTTPStatus.OK, bob_sync_channel.json_body
)

changed_device_lists = bob_sync_channel.json_body.get("device_lists", {}).get(
"changed", []
Expand Down
19 changes: 14 additions & 5 deletions tests/rest/client/test_ephemeral_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@
# 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 http import HTTPStatus

from twisted.test.proto_helpers import MemoryReactor

from synapse.api.constants import EventContentFields, EventTypes
from synapse.rest import admin
from synapse.rest.client import room
from synapse.server import HomeServer
from synapse.types import JsonDict
from synapse.util import Clock

from tests import unittest

Expand All @@ -27,18 +34,18 @@ class EphemeralMessageTestCase(unittest.HomeserverTestCase):
room.register_servlets,
]

def make_homeserver(self, reactor, clock):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
config = self.default_config()

config["enable_ephemeral_messages"] = True

self.hs = self.setup_test_homeserver(config=config)
return self.hs

def prepare(self, reactor, clock, homeserver):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.room_id = self.helper.create_room_as(self.user_id)

def test_message_expiry_no_delay(self):
def test_message_expiry_no_delay(self) -> None:
"""Tests that sending a message sent with a m.self_destruct_after field set to the
past results in that event being deleted right away.
"""
Expand All @@ -61,7 +68,7 @@ def test_message_expiry_no_delay(self):
event_content = self.get_event(self.room_id, event_id)["content"]
self.assertFalse(bool(event_content), event_content)

def test_message_expiry_delay(self):
def test_message_expiry_delay(self) -> None:
"""Tests that sending a message with a m.self_destruct_after field set to the
future results in that event not being deleted right away, but advancing the
clock to after that expiry timestamp causes the event to be deleted.
Expand Down Expand Up @@ -89,7 +96,9 @@ def test_message_expiry_delay(self):
event_content = self.get_event(self.room_id, event_id)["content"]
self.assertFalse(bool(event_content), event_content)

def get_event(self, room_id, event_id, expected_code=200):
def get_event(
self, room_id: str, event_id: str, expected_code: int = HTTPStatus.OK
) -> JsonDict:
url = "/_matrix/client/r0/rooms/%s/event/%s" % (room_id, event_id)

channel = self.make_request("GET", url)
Expand Down
13 changes: 9 additions & 4 deletions tests/rest/client/test_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
# limitations under the License.

import json
from http import HTTPStatus

from twisted.test.proto_helpers import MemoryReactor

import synapse.rest.admin
from synapse.rest.client import login, room
from synapse.server import HomeServer
from synapse.util import Clock

from tests import unittest

Expand All @@ -28,22 +33,22 @@ class IdentityTestCase(unittest.HomeserverTestCase):
login.register_servlets,
]

def make_homeserver(self, reactor, clock):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:

config = self.default_config()
config["enable_3pid_lookup"] = False
self.hs = self.setup_test_homeserver(config=config)

return self.hs

def test_3pid_lookup_disabled(self):
def test_3pid_lookup_disabled(self) -> None:
self.hs.config.registration.enable_3pid_lookup = False

self.register_user("kermit", "monkey")
tok = self.login("kermit", "monkey")

channel = self.make_request(b"POST", "/createRoom", b"{}", access_token=tok)
self.assertEquals(channel.result["code"], b"200", channel.result)
self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
room_id = channel.json_body["room_id"]

params = {
Expand All @@ -56,4 +61,4 @@ def test_3pid_lookup_disabled(self):
channel = self.make_request(
b"POST", request_url, request_data, access_token=tok
)
self.assertEquals(channel.result["code"], b"403", channel.result)
self.assertEqual(channel.code, HTTPStatus.FORBIDDEN, channel.result)
6 changes: 3 additions & 3 deletions tests/rest/client/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class KeyQueryTestCase(unittest.HomeserverTestCase):
login.register_servlets,
]

def test_rejects_device_id_ice_key_outside_of_list(self):
def test_rejects_device_id_ice_key_outside_of_list(self) -> None:
self.register_user("alice", "wonderland")
alice_token = self.login("alice", "wonderland")
bob = self.register_user("bob", "uncle")
Expand All @@ -49,7 +49,7 @@ def test_rejects_device_id_ice_key_outside_of_list(self):
channel.result,
)

def test_rejects_device_key_given_as_map_to_bool(self):
def test_rejects_device_key_given_as_map_to_bool(self) -> None:
self.register_user("alice", "wonderland")
alice_token = self.login("alice", "wonderland")
bob = self.register_user("bob", "uncle")
Expand All @@ -73,7 +73,7 @@ def test_rejects_device_key_given_as_map_to_bool(self):
channel.result,
)

def test_requires_device_key(self):
def test_requires_device_key(self) -> None:
"""`device_keys` is required. We should complain if it's missing."""
self.register_user("alice", "wonderland")
alice_token = self.login("alice", "wonderland")
Expand Down
Loading