mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-08-03 19:44:14 -04:00
Add final type hint to tests.unittest. (#15072)
Adds a return type to HomeServerTestCase.make_homeserver and deal with any variables which are no longer Any.
This commit is contained in:
parent
119e0795a5
commit
42aea0d8af
55 changed files with 433 additions and 320 deletions
|
@ -2913,7 +2913,8 @@ class UserMembershipRestTestCase(unittest.HomeserverTestCase):
|
|||
other_user_tok = self.login("user", "pass")
|
||||
event_builder_factory = self.hs.get_event_builder_factory()
|
||||
event_creation_handler = self.hs.get_event_creation_handler()
|
||||
storage_controllers = self.hs.get_storage_controllers()
|
||||
persistence = self.hs.get_storage_controllers().persistence
|
||||
assert persistence is not None
|
||||
|
||||
# Create two rooms, one with a local user only and one with both a local
|
||||
# and remote user.
|
||||
|
@ -2940,7 +2941,7 @@ class UserMembershipRestTestCase(unittest.HomeserverTestCase):
|
|||
|
||||
context = self.get_success(unpersisted_context.persist(event))
|
||||
|
||||
self.get_success(storage_controllers.persistence.persist_event(event, context))
|
||||
self.get_success(persistence.persist_event(event, context))
|
||||
|
||||
# Now get rooms
|
||||
url = "/_synapse/admin/v1/users/@joiner:remote_hs/joined_rooms"
|
||||
|
|
|
@ -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 typing import Optional
|
||||
|
||||
from twisted.test.proto_helpers import MemoryReactor
|
||||
|
||||
import synapse.rest.admin
|
||||
|
@ -33,9 +35,14 @@ class UsernameAvailableTestCase(unittest.HomeserverTestCase):
|
|||
self.register_user("admin", "pass", admin=True)
|
||||
self.admin_user_tok = self.login("admin", "pass")
|
||||
|
||||
async def check_username(username: str) -> bool:
|
||||
if username == "allowed":
|
||||
return True
|
||||
async def check_username(
|
||||
localpart: str,
|
||||
guest_access_token: Optional[str] = None,
|
||||
assigned_user_id: Optional[str] = None,
|
||||
inhibit_user_in_use_error: bool = False,
|
||||
) -> None:
|
||||
if localpart == "allowed":
|
||||
return
|
||||
raise SynapseError(
|
||||
400,
|
||||
"User ID already taken.",
|
||||
|
@ -43,7 +50,7 @@ class UsernameAvailableTestCase(unittest.HomeserverTestCase):
|
|||
)
|
||||
|
||||
handler = self.hs.get_registration_handler()
|
||||
handler.check_username = check_username
|
||||
handler.check_username = check_username # type: ignore[assignment]
|
||||
|
||||
def test_username_available(self) -> None:
|
||||
"""
|
||||
|
|
|
@ -1193,7 +1193,7 @@ class AccountStatusTestCase(unittest.HomeserverTestCase):
|
|||
return {}
|
||||
|
||||
# Register a mock that will return the expected result depending on the remote.
|
||||
self.hs.get_federation_http_client().post_json = Mock(side_effect=post_json)
|
||||
self.hs.get_federation_http_client().post_json = Mock(side_effect=post_json) # type: ignore[assignment]
|
||||
|
||||
# Check that we've got the correct response from the client-side endpoint.
|
||||
self._test_status(
|
||||
|
|
|
@ -63,14 +63,14 @@ class FilterTestCase(unittest.HomeserverTestCase):
|
|||
|
||||
def test_add_filter_non_local_user(self) -> None:
|
||||
_is_mine = self.hs.is_mine
|
||||
self.hs.is_mine = lambda target_user: False
|
||||
self.hs.is_mine = lambda target_user: False # type: ignore[assignment]
|
||||
channel = self.make_request(
|
||||
"POST",
|
||||
"/_matrix/client/r0/user/%s/filter" % (self.user_id),
|
||||
self.EXAMPLE_FILTER_JSON,
|
||||
)
|
||||
|
||||
self.hs.is_mine = _is_mine
|
||||
self.hs.is_mine = _is_mine # type: ignore[assignment]
|
||||
self.assertEqual(channel.code, 403)
|
||||
self.assertEqual(channel.json_body["errcode"], Codes.FORBIDDEN)
|
||||
|
||||
|
|
|
@ -36,14 +36,14 @@ class PresenceTestCase(unittest.HomeserverTestCase):
|
|||
|
||||
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
||||
|
||||
presence_handler = Mock(spec=PresenceHandler)
|
||||
presence_handler.set_state.return_value = make_awaitable(None)
|
||||
self.presence_handler = Mock(spec=PresenceHandler)
|
||||
self.presence_handler.set_state.return_value = make_awaitable(None)
|
||||
|
||||
hs = self.setup_test_homeserver(
|
||||
"red",
|
||||
federation_http_client=None,
|
||||
federation_client=Mock(),
|
||||
presence_handler=presence_handler,
|
||||
presence_handler=self.presence_handler,
|
||||
)
|
||||
|
||||
return hs
|
||||
|
@ -61,7 +61,7 @@ class PresenceTestCase(unittest.HomeserverTestCase):
|
|||
)
|
||||
|
||||
self.assertEqual(channel.code, HTTPStatus.OK)
|
||||
self.assertEqual(self.hs.get_presence_handler().set_state.call_count, 1)
|
||||
self.assertEqual(self.presence_handler.set_state.call_count, 1)
|
||||
|
||||
@unittest.override_config({"use_presence": False})
|
||||
def test_put_presence_disabled(self) -> None:
|
||||
|
@ -76,4 +76,4 @@ class PresenceTestCase(unittest.HomeserverTestCase):
|
|||
)
|
||||
|
||||
self.assertEqual(channel.code, HTTPStatus.OK)
|
||||
self.assertEqual(self.hs.get_presence_handler().set_state.call_count, 0)
|
||||
self.assertEqual(self.presence_handler.set_state.call_count, 0)
|
||||
|
|
|
@ -151,7 +151,7 @@ class RegisterRestServletTestCase(unittest.HomeserverTestCase):
|
|||
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
||||
|
||||
def test_POST_guest_registration(self) -> None:
|
||||
self.hs.config.key.macaroon_secret_key = "test"
|
||||
self.hs.config.key.macaroon_secret_key = b"test"
|
||||
self.hs.config.registration.allow_guest_access = True
|
||||
|
||||
channel = self.make_request(b"POST", self.url + b"?kind=guest", b"{}")
|
||||
|
@ -1166,12 +1166,15 @@ class AccountValidityBackgroundJobTestCase(unittest.HomeserverTestCase):
|
|||
"""
|
||||
user_id = self.register_user("kermit_delta", "user")
|
||||
|
||||
self.hs.config.account_validity.startup_job_max_delta = self.max_delta
|
||||
self.hs.config.account_validity.account_validity_startup_job_max_delta = (
|
||||
self.max_delta
|
||||
)
|
||||
|
||||
now_ms = self.hs.get_clock().time_msec()
|
||||
self.get_success(self.store._set_expiration_date_when_missing())
|
||||
|
||||
res = self.get_success(self.store.get_expiration_ts_for_user(user_id))
|
||||
assert res is not None
|
||||
|
||||
self.assertGreaterEqual(res, now_ms + self.validity_period - self.max_delta)
|
||||
self.assertLessEqual(res, now_ms + self.validity_period)
|
||||
|
|
|
@ -136,6 +136,7 @@ class RetentionTestCase(unittest.HomeserverTestCase):
|
|||
# Send a first event, which should be filtered out at the end of the test.
|
||||
resp = self.helper.send(room_id=room_id, body="1", tok=self.token)
|
||||
first_event_id = resp.get("event_id")
|
||||
assert isinstance(first_event_id, str)
|
||||
|
||||
# Advance the time by 2 days. We're using the default retention policy, therefore
|
||||
# after this the first event will still be valid.
|
||||
|
@ -144,6 +145,7 @@ class RetentionTestCase(unittest.HomeserverTestCase):
|
|||
# Send another event, which shouldn't get filtered out.
|
||||
resp = self.helper.send(room_id=room_id, body="2", tok=self.token)
|
||||
valid_event_id = resp.get("event_id")
|
||||
assert isinstance(valid_event_id, str)
|
||||
|
||||
# Advance the time by another 2 days. After this, the first event should be
|
||||
# outdated but not the second one.
|
||||
|
@ -229,7 +231,7 @@ class RetentionTestCase(unittest.HomeserverTestCase):
|
|||
|
||||
# Check that we can still access state events that were sent before the event that
|
||||
# has been purged.
|
||||
self.get_event(room_id, create_event.event_id)
|
||||
self.get_event(room_id, bool(create_event))
|
||||
|
||||
def get_event(self, event_id: str, expect_none: bool = False) -> JsonDict:
|
||||
event = self.get_success(self.store.get_event(event_id, allow_none=True))
|
||||
|
@ -238,7 +240,7 @@ class RetentionTestCase(unittest.HomeserverTestCase):
|
|||
self.assertIsNone(event)
|
||||
return {}
|
||||
|
||||
self.assertIsNotNone(event)
|
||||
assert event is not None
|
||||
|
||||
time_now = self.clock.time_msec()
|
||||
serialized = self.serializer.serialize_event(event, time_now)
|
||||
|
|
|
@ -3382,8 +3382,8 @@ class ThreepidInviteTestCase(unittest.HomeserverTestCase):
|
|||
# a remote IS. We keep the mock for make_and_store_3pid_invite around so we
|
||||
# can check its call_count later on during the test.
|
||||
make_invite_mock = Mock(return_value=make_awaitable((Mock(event_id="abc"), 0)))
|
||||
self.hs.get_room_member_handler()._make_and_store_3pid_invite = make_invite_mock
|
||||
self.hs.get_identity_handler().lookup_3pid = Mock(
|
||||
self.hs.get_room_member_handler()._make_and_store_3pid_invite = make_invite_mock # type: ignore[assignment]
|
||||
self.hs.get_identity_handler().lookup_3pid = Mock( # type: ignore[assignment]
|
||||
return_value=make_awaitable(None),
|
||||
)
|
||||
|
||||
|
@ -3443,8 +3443,8 @@ class ThreepidInviteTestCase(unittest.HomeserverTestCase):
|
|||
# a remote IS. We keep the mock for make_and_store_3pid_invite around so we
|
||||
# can check its call_count later on during the test.
|
||||
make_invite_mock = Mock(return_value=make_awaitable((Mock(event_id="abc"), 0)))
|
||||
self.hs.get_room_member_handler()._make_and_store_3pid_invite = make_invite_mock
|
||||
self.hs.get_identity_handler().lookup_3pid = Mock(
|
||||
self.hs.get_room_member_handler()._make_and_store_3pid_invite = make_invite_mock # type: ignore[assignment]
|
||||
self.hs.get_identity_handler().lookup_3pid = Mock( # type: ignore[assignment]
|
||||
return_value=make_awaitable(None),
|
||||
)
|
||||
|
||||
|
@ -3563,8 +3563,10 @@ class TimestampLookupTestCase(unittest.HomeserverTestCase):
|
|||
)
|
||||
|
||||
event.internal_metadata.outlier = True
|
||||
persistence = self._storage_controllers.persistence
|
||||
assert persistence is not None
|
||||
self.get_success(
|
||||
self._storage_controllers.persistence.persist_event(
|
||||
persistence.persist_event(
|
||||
event, EventContext.for_outlier(self._storage_controllers)
|
||||
)
|
||||
)
|
||||
|
|
|
@ -84,7 +84,7 @@ class RoomTestCase(_ShadowBannedBase):
|
|||
def test_invite_3pid(self) -> None:
|
||||
"""Ensure that a 3PID invite does not attempt to contact the identity server."""
|
||||
identity_handler = self.hs.get_identity_handler()
|
||||
identity_handler.lookup_3pid = Mock(
|
||||
identity_handler.lookup_3pid = Mock( # type: ignore[assignment]
|
||||
side_effect=AssertionError("This should not get called")
|
||||
)
|
||||
|
||||
|
@ -222,7 +222,7 @@ class RoomTestCase(_ShadowBannedBase):
|
|||
event_source.get_new_events(
|
||||
user=UserID.from_string(self.other_user_id),
|
||||
from_key=0,
|
||||
limit=None,
|
||||
limit=10,
|
||||
room_ids=[room_id],
|
||||
is_guest=False,
|
||||
)
|
||||
|
@ -286,6 +286,7 @@ class ProfileTestCase(_ShadowBannedBase):
|
|||
self.banned_user_id,
|
||||
)
|
||||
)
|
||||
assert event is not None
|
||||
self.assertEqual(
|
||||
event.content, {"membership": "join", "displayname": original_display_name}
|
||||
)
|
||||
|
@ -321,6 +322,7 @@ class ProfileTestCase(_ShadowBannedBase):
|
|||
self.banned_user_id,
|
||||
)
|
||||
)
|
||||
assert event is not None
|
||||
self.assertEqual(
|
||||
event.content, {"membership": "join", "displayname": original_display_name}
|
||||
)
|
||||
|
|
|
@ -84,7 +84,7 @@ class UpgradeRoomTest(unittest.HomeserverTestCase):
|
|||
self.room_id, EventTypes.Tombstone, ""
|
||||
)
|
||||
)
|
||||
self.assertIsNotNone(tombstone_event)
|
||||
assert tombstone_event is not None
|
||||
self.assertEqual(new_room_id, tombstone_event.content["replacement_room"])
|
||||
|
||||
# Check that the new room exists.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue