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

Commit 071f8b0

Browse files
authored
Factor out common code in tests and fix comments. (#14819)
1 parent f4d2a73 commit 071f8b0

File tree

3 files changed

+64
-36
lines changed

3 files changed

+64
-36
lines changed

changelog.d/14819.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor push tests.

stubs/synapse/synapse_rust/push.pyi

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2022 The Matrix.org Foundation C.I.C.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from typing import Any, Collection, Dict, Mapping, Optional, Sequence, Set, Tuple, Union
216

317
from synapse.types import JsonDict
+49-36
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1+
# Copyright 2022 The Matrix.org Foundation C.I.C.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from unittest.mock import patch
216

17+
from twisted.test.proto_helpers import MemoryReactor
18+
319
from synapse.api.room_versions import RoomVersions
420
from synapse.push.bulk_push_rule_evaluator import BulkPushRuleEvaluator
521
from synapse.rest import admin
622
from synapse.rest.client import login, register, room
23+
from synapse.server import HomeServer
724
from synapse.types import create_requester
25+
from synapse.util import Clock
826

927
from tests.test_utils import simple_async_mock
1028
from tests.unittest import HomeserverTestCase, override_config
@@ -19,53 +37,58 @@ class TestBulkPushRuleEvaluator(HomeserverTestCase):
1937
register.register_servlets,
2038
]
2139

40+
def prepare(
41+
self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
42+
) -> None:
43+
# Create a new user and room.
44+
self.alice = self.register_user("alice", "pass")
45+
self.token = self.login(self.alice, "pass")
46+
self.requester = create_requester(self.alice)
47+
48+
self.room_id = self.helper.create_room_as(
49+
self.alice, room_version=RoomVersions.V9.identifier, tok=self.token
50+
)
51+
52+
self.event_creation_handler = self.hs.get_event_creation_handler()
53+
2254
def test_action_for_event_by_user_handles_noninteger_power_levels(self) -> None:
2355
"""We should convert floats and strings to integers before passing to Rust.
2456
2557
Reproduces #14060.
2658
2759
A lack of validation: the gift that keeps on giving.
2860
"""
29-
# Create a new user and room.
30-
alice = self.register_user("alice", "pass")
31-
token = self.login(alice, "pass")
32-
33-
room_id = self.helper.create_room_as(
34-
alice, room_version=RoomVersions.V9.identifier, tok=token
35-
)
3661

3762
# Alter the power levels in that room to include stringy and floaty levels.
3863
# We need to suppress the validation logic or else it will reject these dodgy
3964
# values. (Presumably this validation was not always present.)
40-
event_creation_handler = self.hs.get_event_creation_handler()
41-
requester = create_requester(alice)
4265
with patch("synapse.events.validator.validate_canonicaljson"), patch(
4366
"synapse.events.validator.jsonschema.validate"
4467
):
4568
self.helper.send_state(
46-
room_id,
69+
self.room_id,
4770
"m.room.power_levels",
4871
{
49-
"users": {alice: "100"}, # stringy
72+
"users": {self.alice: "100"}, # stringy
5073
"notifications": {"room": 100.0}, # float
5174
},
52-
token,
75+
self.token,
5376
state_key="",
5477
)
5578

5679
# Create a new message event, and try to evaluate it under the dodgy
5780
# power level event.
5881
event, context = self.get_success(
59-
event_creation_handler.create_event(
60-
requester,
82+
self.event_creation_handler.create_event(
83+
self.requester,
6184
{
6285
"type": "m.room.message",
63-
"room_id": room_id,
86+
"room_id": self.room_id,
6487
"content": {
6588
"msgtype": "m.text",
6689
"body": "helo",
6790
},
68-
"sender": alice,
91+
"sender": self.alice,
6992
},
7093
)
7194
)
@@ -77,39 +100,29 @@ def test_action_for_event_by_user_handles_noninteger_power_levels(self) -> None:
77100
@override_config({"push": {"enabled": False}})
78101
def test_action_for_event_by_user_disabled_by_config(self) -> None:
79102
"""Ensure that push rules are not calculated when disabled in the config"""
80-
# Create a new user and room.
81-
alice = self.register_user("alice", "pass")
82-
token = self.login(alice, "pass")
83103

84-
room_id = self.helper.create_room_as(
85-
alice, room_version=RoomVersions.V9.identifier, tok=token
86-
)
87-
88-
# Alter the power levels in that room to include stringy and floaty levels.
89-
# We need to suppress the validation logic or else it will reject these dodgy
90-
# values. (Presumably this validation was not always present.)
91-
event_creation_handler = self.hs.get_event_creation_handler()
92-
requester = create_requester(alice)
93-
94-
# Create a new message event, and try to evaluate it under the dodgy
95-
# power level event.
104+
# Create a new message event which should cause a notification.
96105
event, context = self.get_success(
97-
event_creation_handler.create_event(
98-
requester,
106+
self.event_creation_handler.create_event(
107+
self.requester,
99108
{
100109
"type": "m.room.message",
101-
"room_id": room_id,
110+
"room_id": self.room_id,
102111
"content": {
103112
"msgtype": "m.text",
104113
"body": "helo",
105114
},
106-
"sender": alice,
115+
"sender": self.alice,
107116
},
108117
)
109118
)
110119

111120
bulk_evaluator = BulkPushRuleEvaluator(self.hs)
121+
# Mock the method which calculates push rules -- we do this instead of
122+
# e.g. checking the results in the database because we want to ensure
123+
# that code isn't even running.
112124
bulk_evaluator._action_for_event_by_user = simple_async_mock() # type: ignore[assignment]
113-
# should not raise
125+
126+
# Ensure no actions are generated!
114127
self.get_success(bulk_evaluator.action_for_events_by_user([(event, context)]))
115128
bulk_evaluator._action_for_event_by_user.assert_not_called()

0 commit comments

Comments
 (0)