summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/federation/test_federation.py12
-rw-r--r--tests/handlers/test_directory.py112
-rw-r--r--tests/handlers/test_presence.py36
-rw-r--r--tests/handlers/test_presencelike.py48
-rw-r--r--tests/rest/test_presence.py12
-rw-r--r--tests/test_types.py6
-rw-r--r--tests/utils.py14
7 files changed, 200 insertions, 40 deletions
diff --git a/tests/federation/test_federation.py b/tests/federation/test_federation.py
index a3bcb5ede8..ec39c7ee33 100644
--- a/tests/federation/test_federation.py
+++ b/tests/federation/test_federation.py
@@ -20,7 +20,7 @@ from twisted.trial import unittest
 from mock import Mock
 import logging
 
-from ..utils import MockHttpServer
+from ..utils import MockHttpServer, MockClock
 
 from synapse.server import HomeServer
 from synapse.federation import initialize_http_replication
@@ -48,16 +48,6 @@ def make_pdu(prev_pdus=[], **kwargs):
     return PduTuple(PduEntry(**pdu_fields), prev_pdus)
 
 
-class MockClock(object):
-    now = 1000
-
-    def time(self):
-        return self.now
-
-    def time_msec(self):
-        return self.time() * 1000
-
-
 class FederationTestCase(unittest.TestCase):
     def setUp(self):
         self.mock_http_server = MockHttpServer()
diff --git a/tests/handlers/test_directory.py b/tests/handlers/test_directory.py
new file mode 100644
index 0000000000..0ace2d0c9a
--- /dev/null
+++ b/tests/handlers/test_directory.py
@@ -0,0 +1,112 @@
+# -*- coding: utf-8 -*-
+# Copyright 2014 matrix.org
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# 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 twisted.trial import unittest
+from twisted.internet import defer
+
+from mock import Mock
+import logging
+
+from synapse.server import HomeServer
+from synapse.handlers.directory import DirectoryHandler
+from synapse.storage.directory import RoomAliasMapping
+
+
+logging.getLogger().addHandler(logging.NullHandler())
+
+
+class DirectoryHandlers(object):
+    def __init__(self, hs):
+        self.directory_handler = DirectoryHandler(hs)
+
+
+class DirectoryTestCase(unittest.TestCase):
+    """ Tests the directory service. """
+
+    def setUp(self):
+        self.mock_federation = Mock(spec=[
+            "make_query",
+        ])
+
+        self.query_handlers = {}
+        def register_query_handler(query_type, handler):
+            self.query_handlers[query_type] = handler
+        self.mock_federation.register_query_handler = register_query_handler
+
+        hs = HomeServer("test",
+            datastore=Mock(spec=[
+                "get_association_from_room_alias",
+            ]),
+            http_client=None,
+            http_server=Mock(),
+            replication_layer=self.mock_federation,
+        )
+        hs.handlers = DirectoryHandlers(hs)
+
+        self.handler = hs.get_handlers().directory_handler
+
+        self.datastore = hs.get_datastore()
+
+        self.my_room = hs.parse_roomalias("#my-room:test")
+        self.remote_room = hs.parse_roomalias("#another:remote")
+
+    @defer.inlineCallbacks
+    def test_get_local_association(self):
+        mocked_get = self.datastore.get_association_from_room_alias
+        mocked_get.return_value = defer.succeed(
+            RoomAliasMapping("!8765qwer:test", "#my-room:test", ["test"])
+        )
+
+        result = yield self.handler.get_association(self.my_room)
+
+        self.assertEquals({
+            "room_id": "!8765qwer:test",
+            "servers": ["test"],
+        }, result)
+
+    @defer.inlineCallbacks
+    def test_get_remote_association(self):
+        self.mock_federation.make_query.return_value = defer.succeed(
+            {"room_id": "!8765qwer:test", "servers": ["test", "remote"]}
+        )
+
+        result = yield self.handler.get_association(self.remote_room)
+
+        self.assertEquals({
+            "room_id": "!8765qwer:test",
+            "servers": ["test", "remote"],
+        }, result)
+        self.mock_federation.make_query.assert_called_with(
+            destination="remote",
+            query_type="directory",
+            args={"room_alias": "#another:remote"}
+        )
+
+    @defer.inlineCallbacks
+    def test_incoming_fed_query(self):
+        mocked_get = self.datastore.get_association_from_room_alias
+        mocked_get.return_value = defer.succeed(
+            RoomAliasMapping("!8765asdf:test", "#your-room:test", ["test"])
+        )
+
+        response = yield self.query_handlers["directory"](
+            {"room_alias": "#your-room:test"}
+        )
+
+        self.assertEquals({
+            "room_id": "!8765asdf:test",
+            "servers": ["test"],
+        }, response)
diff --git a/tests/handlers/test_presence.py b/tests/handlers/test_presence.py
index 2299a2a7ba..b365741d99 100644
--- a/tests/handlers/test_presence.py
+++ b/tests/handlers/test_presence.py
@@ -20,6 +20,8 @@ from twisted.internet import defer
 from mock import Mock, call, ANY
 import logging
 
+from ..utils import MockClock
+
 from synapse.server import HomeServer
 from synapse.api.constants import PresenceState
 from synapse.api.errors import SynapseError
@@ -55,6 +57,7 @@ class PresenceStateTestCase(unittest.TestCase):
 
     def setUp(self):
         hs = HomeServer("test",
+                clock=MockClock(),
                 db_pool=None,
                 datastore=Mock(spec=[
                     "get_presence_state",
@@ -154,7 +157,11 @@ class PresenceStateTestCase(unittest.TestCase):
         mocked_set.assert_called_with("apple",
                 {"state": UNAVAILABLE, "status_msg": "Away"})
         self.mock_start.assert_called_with(self.u_apple,
-                state={"state": UNAVAILABLE, "status_msg": "Away"})
+                state={
+                    "state": UNAVAILABLE,
+                    "status_msg": "Away",
+                    "mtime": 1000000, # MockClock
+                })
 
         yield self.handler.set_state(
                 target_user=self.u_apple, auth_user=self.u_apple,
@@ -386,7 +393,10 @@ class PresencePushTestCase(unittest.TestCase):
         self.replication.send_edu = Mock()
         self.replication.send_edu.return_value = defer.succeed((200, "OK"))
 
+        self.clock = MockClock()
+
         hs = HomeServer("test",
+                clock=self.clock,
                 db_pool=None,
                 datastore=Mock(spec=[
                     "set_presence_state",
@@ -519,13 +529,18 @@ class PresencePushTestCase(unittest.TestCase):
         yield self.handler.set_state(self.u_banana, self.u_banana,
                 {"state": ONLINE})
 
+        self.clock.advance_time(2)
+
         presence = yield self.handler.get_presence_list(
                 observer_user=self.u_apple, accepted=True)
 
         self.assertEquals([
-                {"observed_user": self.u_banana, "state": ONLINE},
-                {"observed_user": self.u_clementine, "state": OFFLINE}],
-            presence)
+                {"observed_user": self.u_banana,
+                 "state": ONLINE,
+                 "mtime_age": 2000},
+                {"observed_user": self.u_clementine,
+                 "state": OFFLINE},
+        ], presence)
 
         self.mock_update_client.assert_has_calls([
                 call(observer_user=self.u_banana,
@@ -555,7 +570,8 @@ class PresencePushTestCase(unittest.TestCase):
                     content={
                         "push": [
                             {"user_id": "@apple:test",
-                            "state": "online"},
+                             "state": "online",
+                             "mtime_age": 0},
                         ],
                     }),
                 call(
@@ -564,7 +580,8 @@ class PresencePushTestCase(unittest.TestCase):
                     content={
                         "push": [
                             {"user_id": "@apple:test",
-                             "state": "online"},
+                             "state": "online",
+                             "mtime_age": 0},
                         ],
                     })
         ], any_order=True)
@@ -582,7 +599,8 @@ class PresencePushTestCase(unittest.TestCase):
                 "remote", "m.presence", {
                     "push": [
                         {"user_id": "@potato:remote",
-                         "state": "online"},
+                         "state": "online",
+                         "mtime_age": 1000},
                     ],
                 }
         )
@@ -596,9 +614,11 @@ class PresencePushTestCase(unittest.TestCase):
                     statuscache=ANY),
         ], any_order=True)
 
+        self.clock.advance_time(2)
+
         state = yield self.handler.get_state(self.u_potato, self.u_apple)
 
-        self.assertEquals({"state": ONLINE}, state)
+        self.assertEquals({"state": ONLINE, "mtime_age": 3000}, state)
 
     @defer.inlineCallbacks
     def test_join_room_local(self):
diff --git a/tests/handlers/test_presencelike.py b/tests/handlers/test_presencelike.py
index f244ab6007..6eeb1bb522 100644
--- a/tests/handlers/test_presencelike.py
+++ b/tests/handlers/test_presencelike.py
@@ -22,6 +22,8 @@ from twisted.internet import defer
 from mock import Mock, call, ANY
 import logging
 
+from ..utils import MockClock
+
 from synapse.server import HomeServer
 from synapse.api.constants import PresenceState
 from synapse.handlers.presence import PresenceHandler
@@ -60,9 +62,11 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
 
     def setUp(self):
         hs = HomeServer("test",
+                clock=MockClock(),
                 db_pool=None,
                 datastore=Mock(spec=[
                     "set_presence_state",
+                    "is_presence_visible",
 
                     "set_profile_displayname",
                 ]),
@@ -83,6 +87,10 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
             return defer.succeed("Frank")
         self.datastore.get_profile_displayname = get_profile_displayname
 
+        def is_presence_visible(*args, **kwargs):
+            return defer.succeed(False)
+        self.datastore.is_presence_visible = is_presence_visible
+
         def get_profile_avatar_url(user_localpart):
             return defer.succeed("http://foo")
         self.datastore.get_profile_avatar_url = get_profile_avatar_url
@@ -96,14 +104,9 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
 
         self.handlers = hs.get_handlers()
 
-        self.mock_start = Mock()
-        self.mock_stop = Mock()
-
         self.mock_update_client = Mock()
         self.mock_update_client.return_value = defer.succeed(None)
 
-        self.handlers.presence_handler.start_polling_presence = self.mock_start
-        self.handlers.presence_handler.stop_polling_presence = self.mock_stop
         self.handlers.presence_handler.push_update_to_clients = (
                 self.mock_update_client)
 
@@ -132,10 +135,6 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
 
         mocked_set.assert_called_with("apple",
                 {"state": UNAVAILABLE, "status_msg": "Away"})
-        self.mock_start.assert_called_with(self.u_apple,
-                state={"state": UNAVAILABLE, "status_msg": "Away",
-                       "displayname": "Frank",
-                       "avatar_url": "http://foo"})
 
     @defer.inlineCallbacks
     def test_push_local(self):
@@ -160,10 +159,14 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
                 observer_user=self.u_apple, accepted=True)
 
         self.assertEquals([
-                {"observed_user": self.u_banana, "state": ONLINE,
-                    "displayname": "Frank", "avatar_url": "http://foo"},
-                {"observed_user": self.u_clementine, "state": OFFLINE}],
-            presence)
+            {"observed_user": self.u_banana,
+                "state": ONLINE,
+                "mtime_age": 0,
+                "displayname": "Frank",
+                "avatar_url": "http://foo"},
+            {"observed_user": self.u_clementine,
+                "state": OFFLINE}],
+        presence)
 
         self.mock_update_client.assert_has_calls([
             call(observer_user=self.u_apple,
@@ -175,9 +178,12 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
         ], any_order=True)
 
         statuscache = self.mock_update_client.call_args[1]["statuscache"]
-        self.assertEquals({"state": ONLINE,
-                           "displayname": "Frank",
-                           "avatar_url": "http://foo"}, statuscache.state)
+        self.assertEquals({
+            "state": ONLINE,
+            "mtime": 1000000, # MockClock
+            "displayname": "Frank",
+            "avatar_url": "http://foo",
+        }, statuscache.state)
 
         self.mock_update_client.reset_mock()
 
@@ -197,9 +203,12 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
         ], any_order=True)
 
         statuscache = self.mock_update_client.call_args[1]["statuscache"]
-        self.assertEquals({"state": ONLINE,
-                           "displayname": "I am an Apple",
-                           "avatar_url": "http://foo"}, statuscache.state)
+        self.assertEquals({
+            "state": ONLINE,
+            "mtime": 1000000, # MockClock
+            "displayname": "I am an Apple",
+            "avatar_url": "http://foo",
+        }, statuscache.state)
 
     @defer.inlineCallbacks
     def test_push_remote(self):
@@ -224,6 +233,7 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
                     "push": [
                         {"user_id": "@apple:test",
                          "state": "online",
+                         "mtime_age": 0,
                          "displayname": "Frank",
                          "avatar_url": "http://foo"},
                     ],
diff --git a/tests/rest/test_presence.py b/tests/rest/test_presence.py
index 7c54e067c9..f013abbee4 100644
--- a/tests/rest/test_presence.py
+++ b/tests/rest/test_presence.py
@@ -234,7 +234,11 @@ class PresenceEventStreamTestCase(unittest.TestCase):
         # I'll already get my own presence state change
         self.assertEquals({"start": "0", "end": "1", "chunk": [
             {"type": "m.presence",
-             "content": {"user_id": "@apple:test", "state": ONLINE}},
+             "content": {
+                 "user_id": "@apple:test",
+                 "state": ONLINE,
+                 "mtime_age": 0,
+            }},
         ]}, response)
 
         self.mock_datastore.set_presence_state.return_value = defer.succeed(
@@ -251,5 +255,9 @@ class PresenceEventStreamTestCase(unittest.TestCase):
         self.assertEquals(200, code)
         self.assertEquals({"start": "1", "end": "2", "chunk": [
             {"type": "m.presence",
-             "content": {"user_id": "@banana:test", "state": ONLINE}},
+             "content": {
+                 "user_id": "@banana:test",
+                 "state": ONLINE,
+                 "mtime_age": 0,
+            }},
         ]}, response)
diff --git a/tests/test_types.py b/tests/test_types.py
index 522d52363d..d2ccbcfa55 100644
--- a/tests/test_types.py
+++ b/tests/test_types.py
@@ -62,3 +62,9 @@ class RoomAliasTestCase(unittest.TestCase):
         room = RoomAlias("channel", "my.domain", True)
 
         self.assertEquals(room.to_string(), "#channel:my.domain")
+
+    def test_via_homeserver(self):
+        room = mock_homeserver.parse_roomalias("#elsewhere:my.domain")
+
+        self.assertEquals("elsewhere", room.localpart)
+        self.assertEquals("my.domain", room.domain)
diff --git a/tests/utils.py b/tests/utils.py
index 578866b4f4..20a63316fd 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -95,6 +95,20 @@ class MockHttpServer(HttpServer):
         self.callbacks.append((method, path_pattern, callback))
 
 
+class MockClock(object):
+    now = 1000
+
+    def time(self):
+        return self.now
+
+    def time_msec(self):
+        return self.time() * 1000
+
+    # For unit testing
+    def advance_time(self, secs):
+        self.now += secs
+
+
 class MemoryDataStore(object):
 
     class RoomMember(namedtuple(