|
21 | 21 |
|
22 | 22 | import synapse
|
23 | 23 | from synapse.api.constants import LoginType
|
| 24 | +from synapse.api.errors import Codes |
24 | 25 | from synapse.handlers.auth import load_legacy_password_auth_providers
|
25 | 26 | from synapse.module_api import ModuleApi
|
26 |
| -from synapse.rest.client import devices, login, logout, register |
| 27 | +from synapse.rest.client import account, devices, login, logout, register |
27 | 28 | from synapse.types import JsonDict, UserID
|
28 | 29 |
|
29 | 30 | from tests import unittest
|
30 | 31 | from tests.server import FakeChannel
|
| 32 | +from tests.test_utils import make_awaitable |
31 | 33 | from tests.unittest import override_config
|
32 | 34 |
|
33 | 35 | # (possibly experimental) login flows we expect to appear in the list after the normal
|
@@ -158,6 +160,7 @@ class PasswordAuthProviderTests(unittest.HomeserverTestCase):
|
158 | 160 | devices.register_servlets,
|
159 | 161 | logout.register_servlets,
|
160 | 162 | register.register_servlets,
|
| 163 | + account.register_servlets, |
161 | 164 | ]
|
162 | 165 |
|
163 | 166 | def setUp(self):
|
@@ -803,6 +806,77 @@ def test_username_uia(self):
|
803 | 806 | # Check that the callback has been called.
|
804 | 807 | m.assert_called_once()
|
805 | 808 |
|
| 809 | + # Set some email configuration so the test doesn't fail because of its absence. |
| 810 | + @override_config({"email": {"notif_from": "noreply@test"}}) |
| 811 | + def test_3pid_allowed(self): |
| 812 | + """Tests that an is_3pid_allowed_callbacks forbidding a 3PID makes Synapse refuse |
| 813 | + to bind the new 3PID, and that one allowing a 3PID makes Synapse accept to bind |
| 814 | + the 3PID. Also checks that the module is passed a boolean indicating whether the |
| 815 | + user to bind this 3PID to is currently registering. |
| 816 | + """ |
| 817 | + self._test_3pid_allowed("rin", False) |
| 818 | + self._test_3pid_allowed("kitay", True) |
| 819 | + |
| 820 | + def _test_3pid_allowed(self, username: str, registration: bool): |
| 821 | + """Tests that the "is_3pid_allowed" module callback is called correctly, using |
| 822 | + either /register or /account URLs depending on the arguments. |
| 823 | +
|
| 824 | + Args: |
| 825 | + username: The username to use for the test. |
| 826 | + registration: Whether to test with registration URLs. |
| 827 | + """ |
| 828 | + self.hs.get_identity_handler().send_threepid_validation = Mock( |
| 829 | + return_value=make_awaitable(0), |
| 830 | + ) |
| 831 | + |
| 832 | + m = Mock(return_value=make_awaitable(False)) |
| 833 | + self.hs.get_password_auth_provider().is_3pid_allowed_callbacks = [m] |
| 834 | + |
| 835 | + self.register_user(username, "password") |
| 836 | + tok = self.login(username, "password") |
| 837 | + |
| 838 | + if registration: |
| 839 | + url = "/register/email/requestToken" |
| 840 | + else: |
| 841 | + url = "/account/3pid/email/requestToken" |
| 842 | + |
| 843 | + channel = self.make_request( |
| 844 | + "POST", |
| 845 | + url, |
| 846 | + { |
| 847 | + "client_secret": "foo", |
| 848 | + |
| 849 | + "send_attempt": 0, |
| 850 | + }, |
| 851 | + access_token=tok, |
| 852 | + ) |
| 853 | + self.assertEqual(channel.code, 403, channel.result) |
| 854 | + self.assertEqual( |
| 855 | + channel.json_body["errcode"], |
| 856 | + Codes.THREEPID_DENIED, |
| 857 | + channel.json_body, |
| 858 | + ) |
| 859 | + |
| 860 | + m. assert_called_once_with( "email", "[email protected]", registration) |
| 861 | + |
| 862 | + m = Mock(return_value=make_awaitable(True)) |
| 863 | + self.hs.get_password_auth_provider().is_3pid_allowed_callbacks = [m] |
| 864 | + |
| 865 | + channel = self.make_request( |
| 866 | + "POST", |
| 867 | + url, |
| 868 | + { |
| 869 | + "client_secret": "foo", |
| 870 | + |
| 871 | + "send_attempt": 0, |
| 872 | + }, |
| 873 | + access_token=tok, |
| 874 | + ) |
| 875 | + self.assertEqual(channel.code, 200, channel.result) |
| 876 | + self.assertIn("sid", channel.json_body) |
| 877 | + |
| 878 | + m. assert_called_once_with( "email", "[email protected]", registration) |
| 879 | + |
806 | 880 | def _setup_get_username_for_registration(self) -> Mock:
|
807 | 881 | """Registers a get_username_for_registration callback that appends "-foo" to the
|
808 | 882 | username the client is trying to register.
|
|
0 commit comments