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

Commit 4afc0a4

Browse files
committed
Add a couple unit tests
1 parent 2b00758 commit 4afc0a4

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

tests/rest/client/test_third_party_rules.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,3 +931,124 @@ def test_on_threepid_bind(self) -> None:
931931

932932
# Check that the mock was called with the right parameters
933933
self.assertEqual(args, (user_id, "email", "foo@example.com"))
934+
935+
def test_on_add_and_remove_user_third_party_identifier(self) -> None:
936+
"""Tests that the on_add_user_third_party_identifier and
937+
on_remove_user_third_party_identifier module callbacks are called
938+
just before associating and removing a 3PID to/from an account.
939+
"""
940+
# Pretend to be a Synapse module and register both callbacks as mocks.
941+
third_party_rules = self.hs.get_third_party_event_rules()
942+
on_add_user_third_party_identifier_callback_mock = Mock(
943+
return_value=make_awaitable(None)
944+
)
945+
on_remove_user_third_party_identifier_callback_mock = Mock(
946+
return_value=make_awaitable(None)
947+
)
948+
third_party_rules._on_threepid_bind_callbacks.append(
949+
on_add_user_third_party_identifier_callback_mock
950+
)
951+
third_party_rules._on_threepid_bind_callbacks.append(
952+
on_remove_user_third_party_identifier_callback_mock
953+
)
954+
955+
# Register an admin user.
956+
self.register_user("admin", "password", admin=True)
957+
admin_tok = self.login("admin", "password")
958+
959+
# Also register a normal user we can modify.
960+
user_id = self.register_user("user", "password")
961+
962+
# Add a 3PID to the user.
963+
channel = self.make_request(
964+
"PUT",
965+
"/_synapse/admin/v2/users/%s" % user_id,
966+
{
967+
"threepids": [
968+
{
969+
"medium": "email",
970+
"address": "foo@example.com",
971+
},
972+
],
973+
},
974+
access_token=admin_tok,
975+
)
976+
977+
# Check that the mocked add callback was called with the appropriate
978+
# 3PID details.
979+
self.assertEqual(channel.code, 200, channel.json_body)
980+
on_add_user_third_party_identifier_callback_mock.assert_called_once()
981+
args = on_add_user_third_party_identifier_callback_mock.call_args[0]
982+
self.assertEqual(args, (user_id, "email", "foo@example.com"))
983+
984+
# Now remove the 3PID from the user
985+
channel = self.make_request(
986+
"PUT",
987+
"/_synapse/admin/v2/users/%s" % user_id,
988+
{
989+
"threepids": [],
990+
},
991+
access_token=admin_tok,
992+
)
993+
994+
# Check that the mocked remove callback was called with the appropriate
995+
# 3PID details.
996+
self.assertEqual(channel.code, 200, channel.json_body)
997+
on_remove_user_third_party_identifier_callback_mock.assert_called_once()
998+
args = on_remove_user_third_party_identifier_callback_mock.call_args[0]
999+
self.assertEqual(args, (user_id, "email", "foo@example.com"))
1000+
1001+
def test_on_remove_user_third_party_identifier_is_called_on_deactivate(
1002+
self,
1003+
) -> None:
1004+
"""Tests that the on_remove_user_third_party_identifier module callback is called
1005+
when a user is deactivated and their third-party ID associations are deleted.
1006+
"""
1007+
# Pretend to be a Synapse module and register both callbacks as mocks.
1008+
third_party_rules = self.hs.get_third_party_event_rules()
1009+
on_remove_user_third_party_identifier_callback_mock = Mock(
1010+
return_value=make_awaitable(None)
1011+
)
1012+
third_party_rules._on_threepid_bind_callbacks.append(
1013+
on_remove_user_third_party_identifier_callback_mock
1014+
)
1015+
1016+
# Register an admin user.
1017+
self.register_user("admin", "password", admin=True)
1018+
admin_tok = self.login("admin", "password")
1019+
1020+
# Also register a normal user we can modify.
1021+
user_id = self.register_user("user", "password")
1022+
1023+
# Add a 3PID to the user.
1024+
channel = self.make_request(
1025+
"PUT",
1026+
"/_synapse/admin/v2/users/%s" % user_id,
1027+
{
1028+
"threepids": [
1029+
{
1030+
"medium": "email",
1031+
"address": "foo@example.com",
1032+
},
1033+
],
1034+
},
1035+
access_token=admin_tok,
1036+
)
1037+
self.assertEqual(channel.code, 200, channel.json_body)
1038+
1039+
# Now deactivate the user.
1040+
channel = self.make_request(
1041+
"PUT",
1042+
"/_synapse/admin/v2/users/%s" % user_id,
1043+
{
1044+
"deactivated": True,
1045+
},
1046+
access_token=admin_tok,
1047+
)
1048+
1049+
# Check that the mocked remove callback was called with the appropriate
1050+
# 3PID details.
1051+
self.assertEqual(channel.code, 200, channel.json_body)
1052+
on_remove_user_third_party_identifier_callback_mock.assert_called_once()
1053+
args = on_remove_user_third_party_identifier_callback_mock.call_args[0]
1054+
self.assertEqual(args, (user_id, "email", "foo@example.com"))

0 commit comments

Comments
 (0)