summary refs log tree commit diff
path: root/tests/handlers/test_presencelike.py
blob: 1f2e66ac11e8c586590ba54155fcf1cea415fdf6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# -*- coding: utf-8 -*-
# Copyright 2014 OpenMarket Ltd
#
# 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.

"""This file contains tests of the "presence-like" data that is shared between
presence and profiles; namely, the displayname and avatar_url."""

from tests import unittest
from twisted.internet import defer

from mock import Mock, call, ANY, NonCallableMock

from ..utils import MockClock, setup_test_homeserver

from synapse.api.constants import PresenceState
from synapse.handlers.presence import PresenceHandler
from synapse.handlers.profile import ProfileHandler
from synapse.types import UserID


OFFLINE = PresenceState.OFFLINE
UNAVAILABLE = PresenceState.UNAVAILABLE
ONLINE = PresenceState.ONLINE


class MockReplication(object):
    def __init__(self):
        self.edu_handlers = {}

    def register_edu_handler(self, edu_type, handler):
        self.edu_handlers[edu_type] = handler

    def register_query_handler(self, query_type, handler):
        pass

    def received_edu(self, origin, edu_type, content):
        self.edu_handlers[edu_type](origin, content)


class PresenceAndProfileHandlers(object):
    def __init__(self, hs):
        self.presence_handler = PresenceHandler(hs)
        self.profile_handler = ProfileHandler(hs)


class PresenceProfilelikeDataTestCase(unittest.TestCase):

    @defer.inlineCallbacks
    def setUp(self):
        hs = yield setup_test_homeserver(
            clock=MockClock(),
            datastore=Mock(spec=[
                "set_presence_state",
                "is_presence_visible",
                "set_profile_displayname",
                "get_rooms_for_user",
            ]),
            handlers=None,
            resource_for_federation=Mock(),
            http_client=None,
            replication_layer=MockReplication(),
            ratelimiter=NonCallableMock(spec_set=[
                "send_message",
            ]),
        )
        self.ratelimiter = hs.get_ratelimiter()
        self.ratelimiter.send_message.return_value = (True, 0)
        hs.handlers = PresenceAndProfileHandlers(hs)

        self.datastore = hs.get_datastore()

        self.replication = hs.get_replication_layer()
        self.replication.send_edu = Mock()

        def send_edu(*args, **kwargs):
            # print "send_edu: %s, %s" % (args, kwargs)
            return defer.succeed((200, "OK"))
        self.replication.send_edu.side_effect = send_edu

        def get_profile_displayname(user_localpart):
            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

        self.presence_list = [
            {"observed_user_id": "@banana:test"},
            {"observed_user_id": "@clementine:test"},
        ]
        def get_presence_list(user_localpart, accepted=None):
            return defer.succeed(self.presence_list)
        self.datastore.get_presence_list = get_presence_list

        def user_rooms_intersect(userlist):
            return defer.succeed(False)
        self.datastore.user_rooms_intersect = user_rooms_intersect

        self.handlers = hs.get_handlers()

        self.mock_update_client = Mock()
        def update(*args, **kwargs):
            # print "mock_update_client: %s, %s" %(args, kwargs)
            return defer.succeed(None)
        self.mock_update_client.side_effect = update

        self.handlers.presence_handler.push_update_to_clients = (
                self.mock_update_client)

        hs.handlers.room_member_handler = Mock(spec=[
            "get_joined_rooms_for_user",
        ])
        hs.handlers.room_member_handler.get_joined_rooms_for_user = (
                lambda u: defer.succeed([]))

        # Some local users to test with
        self.u_apple = UserID.from_string("@apple:test")
        self.u_banana = UserID.from_string("@banana:test")
        self.u_clementine = UserID.from_string("@clementine:test")

        # Remote user
        self.u_potato = UserID.from_string("@potato:remote")

        self.mock_get_joined = (
            self.datastore.get_rooms_for_user
        )

    @defer.inlineCallbacks
    def test_set_my_state(self):
        self.presence_list = [
            {"observed_user_id": "@banana:test"},
            {"observed_user_id": "@clementine:test"},
        ]

        mocked_set = self.datastore.set_presence_state
        mocked_set.return_value = defer.succeed({"state": OFFLINE})

        yield self.handlers.presence_handler.set_state(
                target_user=self.u_apple, auth_user=self.u_apple,
                state={"presence": UNAVAILABLE, "status_msg": "Away"})

        mocked_set.assert_called_with("apple",
            {"state": UNAVAILABLE, "status_msg": "Away"}
        )

    @defer.inlineCallbacks
    def test_push_local(self):
        def get_joined(*args):
            return defer.succeed([])

        self.mock_get_joined.side_effect = get_joined

        self.presence_list = [
            {"observed_user_id": "@banana:test"},
            {"observed_user_id": "@clementine:test"},
        ]

        self.datastore.set_presence_state.return_value = defer.succeed(
            {"state": ONLINE}
        )

        # TODO(paul): Gut-wrenching
        from synapse.handlers.presence import UserPresenceCache
        self.handlers.presence_handler._user_cachemap[self.u_apple] = (
            UserPresenceCache()
        )
        self.handlers.presence_handler._user_cachemap[self.u_apple].update(
            {"presence": OFFLINE}, serial=0
        )
        apple_set = self.handlers.presence_handler._local_pushmap.setdefault(
                "apple", set())
        apple_set.add(self.u_banana)
        apple_set.add(self.u_clementine)

        yield self.handlers.presence_handler.set_state(self.u_apple,
            self.u_apple, {"presence": ONLINE}
        )
        yield self.handlers.presence_handler.set_state(self.u_banana,
            self.u_banana, {"presence": ONLINE}
        )

        presence = yield self.handlers.presence_handler.get_presence_list(
                observer_user=self.u_apple, accepted=True)

        self.assertEquals([
            {"observed_user": self.u_banana,
                "presence": ONLINE,
                "last_active_ago": 0,
                "displayname": "Frank",
                "avatar_url": "http://foo"},
            {"observed_user": self.u_clementine,
                "presence": OFFLINE}
        ], presence)

        self.mock_update_client.assert_has_calls([
            call(
                users_to_push={self.u_apple, self.u_banana, self.u_clementine},
                room_ids=[]
            ),
        ], any_order=True)

        self.mock_update_client.reset_mock()

        self.datastore.set_profile_displayname.return_value = defer.succeed(
                None)

        yield self.handlers.profile_handler.set_displayname(self.u_apple,
                self.u_apple, "I am an Apple")

        self.mock_update_client.assert_has_calls([
            call(
                users_to_push={self.u_apple, self.u_banana, self.u_clementine},
                room_ids=[],
            ),
        ], any_order=True)

    @defer.inlineCallbacks
    def test_push_remote(self):
        self.presence_list = [
            {"observed_user_id": "@potato:remote"},
        ]

        self.datastore.set_presence_state.return_value = defer.succeed(
            {"state": ONLINE}
        )

        # TODO(paul): Gut-wrenching
        from synapse.handlers.presence import UserPresenceCache
        self.handlers.presence_handler._user_cachemap[self.u_apple] = (
            UserPresenceCache()
        )
        self.handlers.presence_handler._user_cachemap[self.u_apple].update(
            {"presence": OFFLINE}, serial=0
        )
        apple_set = self.handlers.presence_handler._remote_sendmap.setdefault(
                "apple", set())
        apple_set.add(self.u_potato.domain)

        yield self.handlers.presence_handler.set_state(self.u_apple,
            self.u_apple, {"presence": ONLINE}
        )

        self.replication.send_edu.assert_called_with(
                destination="remote",
                edu_type="m.presence",
                content={
                    "push": [
                        {"user_id": "@apple:test",
                         "presence": "online",
                         "last_active_ago": 0,
                         "displayname": "Frank",
                         "avatar_url": "http://foo"},
                    ],
                },
        )

    @defer.inlineCallbacks
    def test_recv_remote(self):
        self.presence_list = [
            {"observed_user_id": "@banana:test"},
            {"observed_user_id": "@clementine:test"},
        ]

        # TODO(paul): Gut-wrenching
        potato_set = self.handlers.presence_handler._remote_recvmap.setdefault(
            self.u_potato, set()
        )
        potato_set.add(self.u_apple)

        yield self.replication.received_edu(
            "remote", "m.presence", {
                "push": [
                    {"user_id": "@potato:remote",
                     "presence": "online",
                     "displayname": "Frank",
                     "avatar_url": "http://foo"},
                ],
            }
        )

        self.mock_update_client.assert_called_with(
            users_to_push=set([self.u_apple]),
            room_ids=[],
        )

        state = yield self.handlers.presence_handler.get_state(self.u_potato,
                self.u_apple)

        self.assertEquals(
                {"presence": ONLINE,
                 "displayname": "Frank",
                 "avatar_url": "http://foo"},
            state)