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

Commit 4c0380b

Browse files
committed
Add - unit test coverage.
1 parent 216dd62 commit 4c0380b

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

tests/module_api/test_api.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from unittest.mock import Mock
14+
from unittest.mock import MagicMock, Mock, patch
1515

1616
from twisted.internet import defer
1717

1818
from synapse.api.constants import EduTypes, EventTypes
19+
from synapse.api.errors import NotFoundError
1920
from synapse.events import EventBase
2021
from synapse.federation.units import Transaction
2122
from synapse.handlers.presence import UserPresenceState
@@ -26,7 +27,7 @@
2627

2728
from tests.events.test_presence_router import send_presence_update, sync_presence
2829
from tests.replication._base import BaseMultiWorkerStreamTestCase
29-
from tests.test_utils import simple_async_mock
30+
from tests.test_utils import make_awaitable, simple_async_mock
3031
from tests.test_utils.event_injection import inject_member_event
3132
from tests.unittest import HomeserverTestCase, override_config
3233
from tests.utils import USE_POSTGRES_FOR_TESTS
@@ -409,7 +410,8 @@ def test_send_local_online_presence_to_federation(self):
409410

410411
self.assertTrue(found_update)
411412

412-
def test_update_membership(self):
413+
@patch("synapse.handlers.room_member.RoomMemberMasterHandler._remote_join")
414+
def test_update_membership(self, mocked_remote_join):
413415
"""Tests that the module API can update the membership of a user in a room."""
414416
peter = self.register_user("peter", "hackme")
415417
lesley = self.register_user("lesley", "hackme")
@@ -532,6 +534,38 @@ def test_update_membership(self):
532534
self.assertEqual(res["displayname"], "simone")
533535
self.assertIsNone(res["avatar_url"])
534536

537+
# Check that no remote join attempts have occurred thus far.
538+
self.assertFalse(mocked_remote_join.called)
539+
540+
# Necessary to fake a remote join.
541+
fake_stream_id = 1
542+
if isinstance(mocked_remote_join, MagicMock):
543+
# AyncMock is not supported in this Python version.
544+
mocked_remote_join.return_value = make_awaitable(
545+
("fake-event-id", fake_stream_id)
546+
)
547+
else:
548+
mocked_remote_join.return_value = "fake-event-id", fake_stream_id
549+
fake_remote_host = f"{self.module_api.server_name}-remote"
550+
551+
# Given that the join is to be faked, we expect the relevant join event not to
552+
# be persisted and the module API method to raise that.
553+
self.get_failure(
554+
defer.ensureDeferred(
555+
self.module_api.update_room_membership(
556+
sender=peter,
557+
target=peter,
558+
room_id=f"!nonexistent:{fake_remote_host}",
559+
new_membership="join",
560+
remote_room_hosts=[fake_remote_host],
561+
)
562+
),
563+
NotFoundError,
564+
)
565+
566+
# Check that a remote join was attempted.
567+
self.assertEqual(mocked_remote_join.call_count, 1)
568+
535569
def test_get_room_state(self):
536570
"""Tests that a module can retrieve the state of a room through the module API."""
537571
user_id = self.register_user("peter", "hackme")

0 commit comments

Comments
 (0)