Skip to content
Snippets Groups Projects
test_presence.py 42.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • matrix.org's avatar
    matrix.org committed
                )
            hs.handlers = JustPresenceHandlers(hs)
    
            self.datastore = hs.get_datastore()
    
    
            def get_received_txn_response(*args):
                return defer.succeed(None)
            self.datastore.get_received_txn_response = get_received_txn_response
    
    
    matrix.org's avatar
    matrix.org committed
            self.mock_update_client = Mock()
    
    
            def update(*args,**kwargs):
                # print "mock_update_client: Args=%s, kwargs=%s" %(args, kwargs,)
                return defer.succeed(None)
    
            self.mock_update_client.side_effect = update
    
    matrix.org's avatar
    matrix.org committed
    
            self.handler = hs.get_handlers().presence_handler
            self.handler.push_update_to_clients = self.mock_update_client
    
            hs.handlers.room_member_handler = Mock(spec=[
                "get_rooms_for_user",
            ])
            # For this test no users are ever in rooms
            def get_rooms_for_user(user):
                return defer.succeed([])
            hs.handlers.room_member_handler.get_rooms_for_user = get_rooms_for_user
    
            # Mocked database state
            # Local users always start offline
            self.current_user_state = {
    
                "apple": OFFLINE,
                "banana": OFFLINE,
                "clementine": OFFLINE,
                "fig": OFFLINE,
    
    matrix.org's avatar
    matrix.org committed
            }
    
            def get_presence_state(user_localpart):
                return defer.succeed(
                        {"state": self.current_user_state[user_localpart],
    
    matrix.org's avatar
    matrix.org committed
                )
            self.datastore.get_presence_state = get_presence_state
    
            def set_presence_state(user_localpart, new_state):
                was = self.current_user_state[user_localpart]
                self.current_user_state[user_localpart] = new_state["state"]
                return defer.succeed({"state": was})
            self.datastore.set_presence_state = set_presence_state
    
            def get_presence_list(user_localpart, accepted):
                return defer.succeed([
                    {"observed_user_id": u} for u in
                    self.PRESENCE_LIST[user_localpart]])
            self.datastore.get_presence_list = get_presence_list
    
            def is_presence_visible(observed_localpart, observer_userid):
                return True
            self.datastore.is_presence_visible = is_presence_visible
    
            # Local users
            self.u_apple = hs.parse_userid("@apple:test")
            self.u_banana = hs.parse_userid("@banana:test")
            self.u_clementine = hs.parse_userid("@clementine:test")
    
            self.u_fig = hs.parse_userid("@fig:test")
    
    matrix.org's avatar
    matrix.org committed
    
            # Remote users
            self.u_potato = hs.parse_userid("@potato:remote")
    
        @defer.inlineCallbacks
        def test_push_local(self):
            # apple goes online
            yield self.handler.set_state(
    
    matrix.org's avatar
    matrix.org committed
    
            # apple should see both banana and clementine currently offline
            self.mock_update_client.assert_has_calls([
    
    matrix.org's avatar
    matrix.org committed
                        observed_user=self.u_banana,
                        statuscache=ANY),
    
    matrix.org's avatar
    matrix.org committed
                        observed_user=self.u_clementine,
                        statuscache=ANY),
            ], any_order=True)
    
            # Gut-wrenching tests
            self.assertTrue("banana" in self.handler._local_pushmap)
            self.assertTrue(self.u_apple in self.handler._local_pushmap["banana"])
            self.assertTrue("clementine" in self.handler._local_pushmap)
            self.assertTrue(self.u_apple in self.handler._local_pushmap["clementine"])
    
            self.mock_update_client.reset_mock()
    
            # banana goes online
            yield self.handler.set_state(
    
                target_user=self.u_banana, auth_user=self.u_banana,
                state={"presence": ONLINE}
            )
    
    matrix.org's avatar
    matrix.org committed
    
            # apple and banana should now both see each other online
            self.mock_update_client.assert_has_calls([
    
                    call(users_to_push=set([self.u_apple]),
    
    matrix.org's avatar
    matrix.org committed
                        observed_user=self.u_banana,
    
    matrix.org's avatar
    matrix.org committed
                        statuscache=ANY),
    
    matrix.org's avatar
    matrix.org committed
                        observed_user=self.u_apple,
                        statuscache=ANY),
            ], any_order=True)
    
            self.assertTrue("apple" in self.handler._local_pushmap)
            self.assertTrue(self.u_banana in self.handler._local_pushmap["apple"])
    
            self.mock_update_client.reset_mock()
    
            # apple goes offline
            yield self.handler.set_state(
    
                target_user=self.u_apple, auth_user=self.u_apple,
                state={"presence": OFFLINE}
            )
    
    matrix.org's avatar
    matrix.org committed
    
            # banana should now be told apple is offline
            self.mock_update_client.assert_has_calls([
    
                    call(users_to_push=set([self.u_banana, self.u_apple]),
    
    matrix.org's avatar
    matrix.org committed
                        observed_user=self.u_apple,
    
    matrix.org's avatar
    matrix.org committed
                        statuscache=ANY),
            ], any_order=True)
    
            self.assertFalse("banana" in self.handler._local_pushmap)
            self.assertFalse("clementine" in self.handler._local_pushmap)
    
    matrix.org's avatar
    matrix.org committed
        @defer.inlineCallbacks
        def test_remote_poll_send(self):
    
            put_json = self.mock_http_client.put_json
            put_json.expect_call_and_return(
                call("remote",
    
                    data=_expect_edu("remote", "m.presence",
                        content={
                            "poll": [ "@potato:remote" ],
                        },
                    ),
                ),
                defer.succeed((200, "OK"))
            )
    
    
            put_json.expect_call_and_return(
                call("remote",
                    path=ANY,
                    data=_expect_edu("remote", "m.presence",
                        content={
                            "push": [ {"user_id": "@clementine:test" }],
                        },
                    ),
                ),
                defer.succeed((200, "OK"))
            )
    
    
    matrix.org's avatar
    matrix.org committed
            # clementine goes online
            yield self.handler.set_state(
    
                target_user=self.u_clementine, auth_user=self.u_clementine,
                state={"presence": ONLINE}
            )
    
    matrix.org's avatar
    matrix.org committed
    
            # Gut-wrenching tests
    
            self.assertTrue(self.u_potato in self.handler._remote_recvmap,
                msg="expected potato to be in _remote_recvmap"
            )
    
    matrix.org's avatar
    matrix.org committed
            self.assertTrue(self.u_clementine in
                    self.handler._remote_recvmap[self.u_potato])
    
    
    
            put_json.expect_call_and_return(
                call("remote",
                    path=ANY,
                    data=_expect_edu("remote", "m.presence",
                        content={
                            "push": [ {"user_id": "@fig:test" }],
                        },
                    ),
                ),
                defer.succeed((200, "OK"))
            )
    
    
            # fig goes online; shouldn't send a second poll
            yield self.handler.set_state(
                target_user=self.u_fig, auth_user=self.u_fig,
    
    
            # fig goes offline
            yield self.handler.set_state(
                target_user=self.u_fig, auth_user=self.u_fig,
    
                    data=_expect_edu("remote", "m.presence",
                        content={
                            "unpoll": [ "@potato:remote" ],
                        },
                    ),
                ),
                defer.succeed((200, "OK"))
            )
    
    matrix.org's avatar
    matrix.org committed
    
            # clementine goes offline
            yield self.handler.set_state(
    
                target_user=self.u_clementine, auth_user=self.u_clementine,
                state={"presence": OFFLINE}
            )
    
            self.assertFalse(self.u_potato in self.handler._remote_recvmap,
                msg="expected potato not to be in _remote_recvmap"
            )
    
    matrix.org's avatar
    matrix.org committed
    
        @defer.inlineCallbacks
        def test_remote_poll_receive(self):
    
            put_json = self.mock_http_client.put_json
            put_json.expect_call_and_return(
                call("remote",
    
                    path="/_matrix/federation/v1/send/1000000/",
    
                    data=_expect_edu("remote", "m.presence",
                        content={
                            "push": [
                                {"user_id": "@banana:test",
    
                                 "status_msg": None},
                            ],
                        },
                    ),
                ),
                defer.succeed((200, "OK"))
            )
    
            yield self.mock_federation_resource.trigger("PUT",
    
    matrix.org's avatar
    matrix.org committed
                        "poll": [ "@banana:test" ],
    
    matrix.org's avatar
    matrix.org committed
            # Gut-wrenching tests
            self.assertTrue(self.u_banana in self.handler._remote_sendmap)
    
    
    matrix.org's avatar
    matrix.org committed
                    content={
                        "unpoll": [ "@banana:test" ],
                    }
    
    matrix.org's avatar
    matrix.org committed
            )
    
            # Gut-wrenching tests
            self.assertFalse(self.u_banana in self.handler._remote_sendmap)