From 2514694b971c311cfb515f288372bc66192b18a4 Mon Sep 17 00:00:00 2001 From: MattC Date: Thu, 4 Aug 2022 12:22:43 +1000 Subject: [PATCH] Add - unit test coverage. --- tests/module_api/test_api.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/tests/module_api/test_api.py b/tests/module_api/test_api.py index 169e29b59001..68f0e158ade2 100644 --- a/tests/module_api/test_api.py +++ b/tests/module_api/test_api.py @@ -11,11 +11,12 @@ # 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 unittest.mock import Mock +from unittest.mock import Mock, patch from twisted.internet import defer from synapse.api.constants import EduTypes, EventTypes +from synapse.api.errors import NotFoundError from synapse.events import EventBase from synapse.federation.units import Transaction from synapse.handlers.presence import UserPresenceState @@ -409,7 +410,8 @@ def test_send_local_online_presence_to_federation(self): self.assertTrue(found_update) - def test_update_membership(self): + @patch("synapse.handlers.room_member.RoomMemberMasterHandler._remote_join") + def test_update_membership(self, mocked_remote_join): """Tests that the module API can update the membership of a user in a room.""" peter = self.register_user("peter", "hackme") lesley = self.register_user("lesley", "hackme") @@ -532,6 +534,32 @@ def test_update_membership(self): self.assertEqual(res["displayname"], "simone") self.assertIsNone(res["avatar_url"]) + # Check that no remote join attempts have occurred thus far. + self.assertFalse(mocked_remote_join.called) + + # Necessary to fake a remote join. + fake_stream_id = 1 + mocked_remote_join.return_value = "fake-event-id", fake_stream_id + fake_remote_host = f"{self.module_api.server_name}-remote" + + # Given that the join is to be faked, we expect the relevant join event not to + # be persisted and the module API method to raise that. + self.get_failure( + defer.ensureDeferred( + self.module_api.update_room_membership( + sender=peter, + target=peter, + room_id=f"!nonexistent:{fake_remote_host}", + new_membership="join", + remote_room_hosts=[fake_remote_host], + ) + ), + NotFoundError, + ) + + # Check that a remote join was attempted. + self.assertEqual(mocked_remote_join.call_count, 1) + def test_get_room_state(self): """Tests that a module can retrieve the state of a room through the module API.""" user_id = self.register_user("peter", "hackme")