summary refs log tree commit diff
diff options
context:
space:
mode:
authorNeil Johnson <neil@fragile.org.uk>2018-11-07 22:29:17 +0000
committerNeil Johnson <neil@fragile.org.uk>2018-11-07 22:29:17 +0000
commitf217b5fd7a74a19b0c1dae3ac64c70fa7e5aa65a (patch)
tree5bd0d99fc03f062a72bb33c2203c5c0dfeeaa3a0
parentreplace filter with list comp to aid py2 py3 compat, make test more paranoid (diff)
downloadsynapse-f217b5fd7a74a19b0c1dae3ac64c70fa7e5aa65a.tar.xz
fix case where auto creation of rooms should never auto create for support user
-rw-r--r--synapse/handlers/register.py12
-rw-r--r--tests/handlers/test_register.py29
2 files changed, 32 insertions, 9 deletions
diff --git a/synapse/handlers/register.py b/synapse/handlers/register.py
index d2beb275cf..093da8d3ff 100644
--- a/synapse/handlers/register.py
+++ b/synapse/handlers/register.py
@@ -221,11 +221,17 @@ class RegistrationHandler(BaseHandler):
         # auto-join the user to any rooms we're supposed to dump them into
         fake_requester = create_requester(user_id)
 
-        # try to create the room if we're the first user on the server
+        # try to create the room if we're the first real user on the server. Note
+        # there maybe a support user present that should autojoin rooms
         should_auto_create_rooms = False
-        if self.hs.config.autocreate_auto_join_rooms:
+        if (self.hs.config.autocreate_auto_join_rooms and
+                self.hs.config.support_user_id != user_id):
             count = yield self.store.count_all_users()
-            should_auto_create_rooms = count == 1
+            if self.hs.config.support_user_id is None:
+                should_auto_create_rooms = count == 1
+            elif self.hs.config.support_user_id != user_id:
+                # assume that support user has been created first
+                should_auto_create_rooms = count == 2
 
         for r in self.hs.config.auto_join_rooms:
             try:
diff --git a/tests/handlers/test_register.py b/tests/handlers/test_register.py
index 3e9a190727..701bcb7f35 100644
--- a/tests/handlers/test_register.py
+++ b/tests/handlers/test_register.py
@@ -43,10 +43,7 @@ class RegistrationTestCase(unittest.TestCase):
             self.addCleanup,
             expire_access_token=True,
         )
-        self.macaroon_generator = Mock(
-            generate_access_token=Mock(return_value='secret')
-        )
-        self.hs.get_macaroon_generator = Mock(return_value=self.macaroon_generator)
+
         self.handler = self.hs.get_handlers().registration_handler
         self.store = self.hs.get_datastore()
         self.hs.config.max_mau_value = 50
@@ -64,7 +61,7 @@ class RegistrationTestCase(unittest.TestCase):
             requester, frank.localpart, "Frankie"
         )
         self.assertEquals(result_user_id, user_id)
-        self.assertEquals(result_token, 'secret')
+        self.assertTrue(result_token is not None)
 
     @defer.inlineCallbacks
     def test_if_user_exists(self):
@@ -82,7 +79,8 @@ class RegistrationTestCase(unittest.TestCase):
             requester, local_part, None
         )
         self.assertEquals(result_user_id, user_id)
-        self.assertEquals(result_token, 'secret')
+        print("result token is %s" % result_token)
+        self.assertTrue(result_token is not None)
 
     @defer.inlineCallbacks
     def test_mau_limits_when_disabled(self):
@@ -184,3 +182,22 @@ class RegistrationTestCase(unittest.TestCase):
         res = yield self.handler.register(localpart='jeff')
         rooms = yield self.store.get_rooms_for_user(res[0])
         self.assertEqual(len(rooms), 0)
+
+    @defer.inlineCallbacks
+    def test_auto_create_auto_join_rooms_when_support_user_exists(self):
+        room_alias_str = "#room:test"
+        self.hs.config.auto_join_rooms = [room_alias_str]
+        self.hs.config.support_user_id = "@support:test"
+        res_support = yield self.handler.register(localpart='support')
+        rooms = yield self.store.get_rooms_for_user(res_support[0])
+        self.assertTrue(len(rooms) == 0)
+
+        res = yield self.handler.register(localpart='jeff')
+
+        rooms = yield self.store.get_rooms_for_user(res[0])
+        directory_handler = self.hs.get_handlers().directory_handler
+        room_alias = RoomAlias.from_string(room_alias_str)
+        room_id = yield directory_handler.get_association(room_alias)
+
+        self.assertTrue(room_id['room_id'] in rooms)
+        self.assertEqual(len(rooms), 1)