2018-07-17 13:43:18 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2018 New Vector
|
|
|
|
#
|
|
|
|
# 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.
|
2019-10-30 19:47:37 +03:00
|
|
|
import json
|
2019-10-30 21:01:56 +03:00
|
|
|
|
2018-08-17 18:08:45 +03:00
|
|
|
from mock import Mock
|
|
|
|
|
2019-05-01 17:32:38 +03:00
|
|
|
import synapse.rest.admin
|
2019-11-01 13:30:51 +03:00
|
|
|
from synapse.api.constants import EventContentFields, EventTypes
|
2019-05-01 17:32:38 +03:00
|
|
|
from synapse.rest.client.v1 import login, room
|
2018-07-17 13:43:18 +03:00
|
|
|
from synapse.rest.client.v2_alpha import sync
|
|
|
|
|
|
|
|
from tests import unittest
|
2018-11-02 16:19:23 +03:00
|
|
|
from tests.server import TimedOutException
|
2018-07-17 13:43:18 +03:00
|
|
|
|
|
|
|
|
2018-08-17 18:08:45 +03:00
|
|
|
class FilterTestCase(unittest.HomeserverTestCase):
|
2018-07-17 13:43:18 +03:00
|
|
|
|
2018-08-17 18:08:45 +03:00
|
|
|
user_id = "@apple:test"
|
2019-10-30 19:47:37 +03:00
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
2018-07-17 13:43:18 +03:00
|
|
|
|
2018-08-17 18:08:45 +03:00
|
|
|
def make_homeserver(self, reactor, clock):
|
2018-07-17 13:43:18 +03:00
|
|
|
|
2018-08-17 18:08:45 +03:00
|
|
|
hs = self.setup_test_homeserver(
|
|
|
|
"red", http_client=None, federation_client=Mock()
|
2018-07-17 13:43:18 +03:00
|
|
|
)
|
2018-08-17 18:08:45 +03:00
|
|
|
return hs
|
2018-07-17 13:43:18 +03:00
|
|
|
|
2018-08-17 18:08:45 +03:00
|
|
|
def test_sync_argless(self):
|
|
|
|
request, channel = self.make_request("GET", "/sync")
|
|
|
|
self.render(request)
|
2018-07-17 13:43:18 +03:00
|
|
|
|
2018-08-17 18:08:45 +03:00
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
self.assertTrue(
|
|
|
|
set(
|
|
|
|
[
|
|
|
|
"next_batch",
|
|
|
|
"rooms",
|
|
|
|
"presence",
|
|
|
|
"account_data",
|
|
|
|
"to_device",
|
|
|
|
"device_lists",
|
|
|
|
]
|
|
|
|
).issubset(set(channel.json_body.keys()))
|
|
|
|
)
|
2018-07-17 13:43:18 +03:00
|
|
|
|
2018-08-17 18:08:45 +03:00
|
|
|
def test_sync_presence_disabled(self):
|
|
|
|
"""
|
|
|
|
When presence is disabled, the key does not appear in /sync.
|
|
|
|
"""
|
|
|
|
self.hs.config.use_presence = False
|
2018-07-17 13:43:18 +03:00
|
|
|
|
2018-08-17 18:08:45 +03:00
|
|
|
request, channel = self.make_request("GET", "/sync")
|
|
|
|
self.render(request)
|
2018-07-17 13:43:18 +03:00
|
|
|
|
2018-08-17 18:08:45 +03:00
|
|
|
self.assertEqual(channel.code, 200)
|
2018-07-17 13:43:18 +03:00
|
|
|
self.assertTrue(
|
|
|
|
set(
|
2018-09-06 19:58:18 +03:00
|
|
|
["next_batch", "rooms", "account_data", "to_device", "device_lists"]
|
2018-07-17 13:43:18 +03:00
|
|
|
).issubset(set(channel.json_body.keys()))
|
|
|
|
)
|
2018-11-02 16:19:23 +03:00
|
|
|
|
|
|
|
|
2019-10-30 19:47:37 +03:00
|
|
|
class SyncFilterTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_sync_filter_labels(self):
|
2019-10-30 20:28:41 +03:00
|
|
|
"""Test that we can filter by a label."""
|
2019-10-30 19:47:37 +03:00
|
|
|
sync_filter = json.dumps(
|
|
|
|
{
|
|
|
|
"room": {
|
|
|
|
"timeline": {
|
|
|
|
"types": [EventTypes.Message],
|
|
|
|
"org.matrix.labels": ["#fun"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
events = self._test_sync_filter_labels(sync_filter)
|
|
|
|
|
2019-10-30 20:28:41 +03:00
|
|
|
self.assertEqual(len(events), 2, [event["content"] for event in events])
|
|
|
|
self.assertEqual(events[0]["content"]["body"], "with right label", events[0])
|
|
|
|
self.assertEqual(events[1]["content"]["body"], "with right label", events[1])
|
2019-10-30 19:47:37 +03:00
|
|
|
|
|
|
|
def test_sync_filter_not_labels(self):
|
2019-10-30 20:28:41 +03:00
|
|
|
"""Test that we can filter by the absence of a label."""
|
2019-10-30 19:47:37 +03:00
|
|
|
sync_filter = json.dumps(
|
|
|
|
{
|
|
|
|
"room": {
|
|
|
|
"timeline": {
|
|
|
|
"types": [EventTypes.Message],
|
|
|
|
"org.matrix.not_labels": ["#fun"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
events = self._test_sync_filter_labels(sync_filter)
|
|
|
|
|
2019-10-30 20:28:41 +03:00
|
|
|
self.assertEqual(len(events), 3, [event["content"] for event in events])
|
2019-10-30 19:47:37 +03:00
|
|
|
self.assertEqual(events[0]["content"]["body"], "without label", events[0])
|
|
|
|
self.assertEqual(events[1]["content"]["body"], "with wrong label", events[1])
|
2019-10-30 21:01:56 +03:00
|
|
|
self.assertEqual(
|
|
|
|
events[2]["content"]["body"], "with two wrong labels", events[2]
|
|
|
|
)
|
2019-10-30 20:28:41 +03:00
|
|
|
|
|
|
|
def test_sync_filter_labels_not_labels(self):
|
|
|
|
"""Test that we can filter by both a label and the absence of another label."""
|
|
|
|
sync_filter = json.dumps(
|
|
|
|
{
|
|
|
|
"room": {
|
|
|
|
"timeline": {
|
|
|
|
"types": [EventTypes.Message],
|
|
|
|
"org.matrix.labels": ["#work"],
|
|
|
|
"org.matrix.not_labels": ["#notfun"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
events = self._test_sync_filter_labels(sync_filter)
|
|
|
|
|
|
|
|
self.assertEqual(len(events), 1, [event["content"] for event in events])
|
|
|
|
self.assertEqual(events[0]["content"]["body"], "with wrong label", events[0])
|
2019-10-30 19:47:37 +03:00
|
|
|
|
|
|
|
def _test_sync_filter_labels(self, sync_filter):
|
|
|
|
user_id = self.register_user("kermit", "test")
|
|
|
|
tok = self.login("kermit", "test")
|
|
|
|
|
|
|
|
room_id = self.helper.create_room_as(user_id, tok=tok)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
2019-10-30 20:28:41 +03:00
|
|
|
"body": "with right label",
|
2019-11-01 19:22:44 +03:00
|
|
|
EventContentFields.LABELS: ["#fun"],
|
2019-10-30 19:47:37 +03:00
|
|
|
},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
2019-10-30 21:01:56 +03:00
|
|
|
content={"msgtype": "m.text", "body": "without label"},
|
2019-10-30 19:47:37 +03:00
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "with wrong label",
|
2019-11-01 19:22:44 +03:00
|
|
|
EventContentFields.LABELS: ["#work"],
|
2019-10-30 19:47:37 +03:00
|
|
|
},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
2019-10-30 20:28:41 +03:00
|
|
|
"body": "with two wrong labels",
|
2019-11-01 19:22:44 +03:00
|
|
|
EventContentFields.LABELS: ["#work", "#notfun"],
|
2019-10-30 20:28:41 +03:00
|
|
|
},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "with right label",
|
2019-11-01 19:22:44 +03:00
|
|
|
EventContentFields.LABELS: ["#fun"],
|
2019-10-30 19:47:37 +03:00
|
|
|
},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
request, channel = self.make_request(
|
|
|
|
"GET", "/sync?filter=%s" % sync_filter, access_token=tok
|
|
|
|
)
|
|
|
|
self.render(request)
|
|
|
|
self.assertEqual(channel.code, 200, channel.result)
|
|
|
|
|
|
|
|
return channel.json_body["rooms"]["join"][room_id]["timeline"]["events"]
|
|
|
|
|
|
|
|
|
2018-11-02 16:19:23 +03:00
|
|
|
class SyncTypingTests(unittest.HomeserverTestCase):
|
|
|
|
|
|
|
|
servlets = [
|
2019-05-02 13:59:16 +03:00
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
2018-11-02 16:19:23 +03:00
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
|
|
|
user_id = True
|
|
|
|
hijack_auth = False
|
|
|
|
|
|
|
|
def test_sync_backwards_typing(self):
|
|
|
|
"""
|
|
|
|
If the typing serial goes backwards and the typing handler is then reset
|
|
|
|
(such as when the master restarts and sets the typing serial to 0), we
|
|
|
|
do not incorrectly return typing information that had a serial greater
|
|
|
|
than the now-reset serial.
|
|
|
|
"""
|
|
|
|
typing_url = "/rooms/%s/typing/%s?access_token=%s"
|
|
|
|
sync_url = "/sync?timeout=3000000&access_token=%s&since=%s"
|
|
|
|
|
|
|
|
# Register the user who gets notified
|
|
|
|
user_id = self.register_user("user", "pass")
|
|
|
|
access_token = self.login("user", "pass")
|
|
|
|
|
|
|
|
# Register the user who sends the message
|
|
|
|
other_user_id = self.register_user("otheruser", "pass")
|
|
|
|
other_access_token = self.login("otheruser", "pass")
|
|
|
|
|
|
|
|
# Create a room
|
|
|
|
room = self.helper.create_room_as(user_id, tok=access_token)
|
|
|
|
|
|
|
|
# Invite the other person
|
|
|
|
self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
|
|
|
|
|
|
|
|
# The other user joins
|
|
|
|
self.helper.join(room=room, user=other_user_id, tok=other_access_token)
|
|
|
|
|
|
|
|
# The other user sends some messages
|
|
|
|
self.helper.send(room, body="Hi!", tok=other_access_token)
|
|
|
|
self.helper.send(room, body="There!", tok=other_access_token)
|
|
|
|
|
|
|
|
# Start typing.
|
|
|
|
request, channel = self.make_request(
|
|
|
|
"PUT",
|
|
|
|
typing_url % (room, other_user_id, other_access_token),
|
|
|
|
b'{"typing": true, "timeout": 30000}',
|
|
|
|
)
|
|
|
|
self.render(request)
|
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
|
|
|
|
request, channel = self.make_request(
|
|
|
|
"GET", "/sync?access_token=%s" % (access_token,)
|
|
|
|
)
|
|
|
|
self.render(request)
|
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# Stop typing.
|
|
|
|
request, channel = self.make_request(
|
|
|
|
"PUT",
|
|
|
|
typing_url % (room, other_user_id, other_access_token),
|
|
|
|
b'{"typing": false}',
|
|
|
|
)
|
|
|
|
self.render(request)
|
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
|
|
|
|
# Start typing.
|
|
|
|
request, channel = self.make_request(
|
|
|
|
"PUT",
|
|
|
|
typing_url % (room, other_user_id, other_access_token),
|
|
|
|
b'{"typing": true, "timeout": 30000}',
|
|
|
|
)
|
|
|
|
self.render(request)
|
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
|
|
|
|
# Should return immediately
|
|
|
|
request, channel = self.make_request(
|
|
|
|
"GET", sync_url % (access_token, next_batch)
|
|
|
|
)
|
|
|
|
self.render(request)
|
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# Reset typing serial back to 0, as if the master had.
|
|
|
|
typing = self.hs.get_typing_handler()
|
|
|
|
typing._latest_room_serial = 0
|
|
|
|
|
|
|
|
# Since it checks the state token, we need some state to update to
|
|
|
|
# invalidate the stream token.
|
|
|
|
self.helper.send(room, body="There!", tok=other_access_token)
|
|
|
|
|
|
|
|
request, channel = self.make_request(
|
|
|
|
"GET", sync_url % (access_token, next_batch)
|
|
|
|
)
|
|
|
|
self.render(request)
|
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# This should time out! But it does not, because our stream token is
|
|
|
|
# ahead, and therefore it's saying the typing (that we've actually
|
|
|
|
# already seen) is new, since it's got a token above our new, now-reset
|
|
|
|
# stream token.
|
|
|
|
request, channel = self.make_request(
|
|
|
|
"GET", sync_url % (access_token, next_batch)
|
|
|
|
)
|
|
|
|
self.render(request)
|
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# Clear the typing information, so that it doesn't think everything is
|
|
|
|
# in the future.
|
|
|
|
typing._reset()
|
|
|
|
|
|
|
|
# Now it SHOULD fail as it never completes!
|
|
|
|
request, channel = self.make_request(
|
|
|
|
"GET", sync_url % (access_token, next_batch)
|
|
|
|
)
|
|
|
|
self.assertRaises(TimedOutException, self.render, request)
|