Merge labels tests for /context and /messages

This commit is contained in:
Brendan Abolivier 2019-11-05 13:47:47 +00:00
parent a7c818c79b
commit c9e4748cb7
No known key found for this signature in database
GPG Key ID: 1E015C145F1916CD
1 changed files with 130 additions and 146 deletions

View File

@ -811,105 +811,6 @@ class RoomMessageListTestCase(RoomBase):
self.assertTrue("chunk" in channel.json_body) self.assertTrue("chunk" in channel.json_body)
self.assertTrue("end" in channel.json_body) self.assertTrue("end" in channel.json_body)
def test_filter_labels(self):
"""Test that we can filter by a label."""
message_filter = json.dumps(
{"types": [EventTypes.Message], "org.matrix.labels": ["#fun"]}
)
events = self._test_filter_labels(message_filter)
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])
def test_filter_not_labels(self):
"""Test that we can filter by the absence of a label."""
message_filter = json.dumps(
{"types": [EventTypes.Message], "org.matrix.not_labels": ["#fun"]}
)
events = self._test_filter_labels(message_filter)
self.assertEqual(len(events), 3, [event["content"] for event in events])
self.assertEqual(events[0]["content"]["body"], "without label", events[0])
self.assertEqual(events[1]["content"]["body"], "with wrong label", events[1])
self.assertEqual(
events[2]["content"]["body"], "with two wrong labels", events[2]
)
def test_filter_labels_not_labels(self):
"""Test that we can filter by both a label and the absence of another label."""
sync_filter = json.dumps(
{
"types": [EventTypes.Message],
"org.matrix.labels": ["#work"],
"org.matrix.not_labels": ["#notfun"],
}
)
events = self._test_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])
def _test_filter_labels(self, message_filter):
self.helper.send_event(
room_id=self.room_id,
type=EventTypes.Message,
content={
"msgtype": "m.text",
"body": "with right label",
EventContentFields.LABELS: ["#fun"],
},
)
self.helper.send_event(
room_id=self.room_id,
type=EventTypes.Message,
content={"msgtype": "m.text", "body": "without label"},
)
self.helper.send_event(
room_id=self.room_id,
type=EventTypes.Message,
content={
"msgtype": "m.text",
"body": "with wrong label",
EventContentFields.LABELS: ["#work"],
},
)
self.helper.send_event(
room_id=self.room_id,
type=EventTypes.Message,
content={
"msgtype": "m.text",
"body": "with two wrong labels",
EventContentFields.LABELS: ["#work", "#notfun"],
},
)
self.helper.send_event(
room_id=self.room_id,
type=EventTypes.Message,
content={
"msgtype": "m.text",
"body": "with right label",
EventContentFields.LABELS: ["#fun"],
},
)
token = "s0_0_0_0_0_0_0_0_0"
request, channel = self.make_request(
"GET",
"/rooms/%s/messages?access_token=x&from=%s&filter=%s"
% (self.room_id, token, message_filter),
)
self.render(request)
return channel.json_body["chunk"]
class RoomSearchTestCase(unittest.HomeserverTestCase): class RoomSearchTestCase(unittest.HomeserverTestCase):
servlets = [ servlets = [
@ -1108,7 +1009,7 @@ class PerRoomProfilesForbiddenTestCase(unittest.HomeserverTestCase):
self.assertEqual(res_displayname, self.displayname, channel.result) self.assertEqual(res_displayname, self.displayname, channel.result)
class ContextTestCase(unittest.HomeserverTestCase): class LabelsTestCase(unittest.HomeserverTestCase):
servlets = [ servlets = [
synapse.rest.admin.register_servlets_for_client_rest_resource, synapse.rest.admin.register_servlets_for_client_rest_resource,
room.register_servlets, room.register_servlets,
@ -1116,8 +1017,13 @@ class ContextTestCase(unittest.HomeserverTestCase):
profile.register_servlets, profile.register_servlets,
] ]
def prepare(self, reactor, clock, homeserver):
self.user_id = self.register_user("test", "test")
self.tok = self.login("test", "test")
self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
def test_context_filter_labels(self): def test_context_filter_labels(self):
"""Test that we can filter by a label.""" """Test that we can filter by a label on a /context request."""
context_filter = json.dumps( context_filter = json.dumps(
{ {
"types": [EventTypes.Message], "types": [EventTypes.Message],
@ -1125,13 +1031,17 @@ class ContextTestCase(unittest.HomeserverTestCase):
} }
) )
res = self._test_context_filter_labels(context_filter) event_id = self._send_labelled_messages_in_room()
self.assertEqual( request, channel = self.make_request(
res["event"]["content"]["body"], "with right label", res["event"] "GET",
"/rooms/%s/context/%s?filter=%s" % (self.room_id, event_id, context_filter),
access_token=self.tok,
) )
self.render(request)
self.assertEqual(channel.code, 200, channel.result)
events_before = res["events_before"] events_before = channel.json_body["events_before"]
self.assertEqual( self.assertEqual(
len(events_before), 1, [event["content"] for event in events_before] len(events_before), 1, [event["content"] for event in events_before]
@ -1140,7 +1050,7 @@ class ContextTestCase(unittest.HomeserverTestCase):
events_before[0]["content"]["body"], "with right label", events_before[0] events_before[0]["content"]["body"], "with right label", events_before[0]
) )
events_after = res["events_before"] events_after = channel.json_body["events_before"]
self.assertEqual( self.assertEqual(
len(events_after), 1, [event["content"] for event in events_after] len(events_after), 1, [event["content"] for event in events_after]
@ -1150,7 +1060,7 @@ class ContextTestCase(unittest.HomeserverTestCase):
) )
def test_context_filter_not_labels(self): def test_context_filter_not_labels(self):
"""Test that we can filter by the absence of a label.""" """Test that we can filter by the absence of a label on a /context request."""
context_filter = json.dumps( context_filter = json.dumps(
{ {
"types": [EventTypes.Message], "types": [EventTypes.Message],
@ -1158,9 +1068,17 @@ class ContextTestCase(unittest.HomeserverTestCase):
} }
) )
res = self._test_context_filter_labels(context_filter) event_id = self._send_labelled_messages_in_room()
events_before = res["events_before"] request, channel = self.make_request(
"GET",
"/rooms/%s/context/%s?filter=%s" % (self.room_id, event_id, context_filter),
access_token=self.tok,
)
self.render(request)
self.assertEqual(channel.code, 200, channel.result)
events_before = channel.json_body["events_before"]
self.assertEqual( self.assertEqual(
len(events_before), 1, [event["content"] for event in events_before] len(events_before), 1, [event["content"] for event in events_before]
@ -1169,7 +1087,7 @@ class ContextTestCase(unittest.HomeserverTestCase):
events_before[0]["content"]["body"], "without label", events_before[0] events_before[0]["content"]["body"], "without label", events_before[0]
) )
events_after = res["events_after"] events_after = channel.json_body["events_after"]
self.assertEqual( self.assertEqual(
len(events_after), 2, [event["content"] for event in events_after] len(events_after), 2, [event["content"] for event in events_after]
@ -1182,7 +1100,9 @@ class ContextTestCase(unittest.HomeserverTestCase):
) )
def test_context_filter_labels_not_labels(self): def test_context_filter_labels_not_labels(self):
"""Test that we can filter by both a label and the absence of another label.""" """Test that we can filter by both a label and the absence of another label on a
/context request.
"""
context_filter = json.dumps( context_filter = json.dumps(
{ {
"types": [EventTypes.Message], "types": [EventTypes.Message],
@ -1191,15 +1111,23 @@ class ContextTestCase(unittest.HomeserverTestCase):
} }
) )
res = self._test_context_filter_labels(context_filter) event_id = self._send_labelled_messages_in_room()
events_before = res["events_before"] request, channel = self.make_request(
"GET",
"/rooms/%s/context/%s?filter=%s" % (self.room_id, event_id, context_filter),
access_token=self.tok,
)
self.render(request)
self.assertEqual(channel.code, 200, channel.result)
events_before = channel.json_body["events_before"]
self.assertEqual( self.assertEqual(
len(events_before), 0, [event["content"] for event in events_before] len(events_before), 0, [event["content"] for event in events_before]
) )
events_after = res["events_after"] events_after = channel.json_body["events_after"]
self.assertEqual( self.assertEqual(
len(events_after), 1, [event["content"] for event in events_after] len(events_after), 1, [event["content"] for event in events_after]
@ -1208,83 +1136,139 @@ class ContextTestCase(unittest.HomeserverTestCase):
events_after[0]["content"]["body"], "with wrong label", events_after[0] events_after[0]["content"]["body"], "with wrong label", events_after[0]
) )
def _test_context_filter_labels(self, context_filter): def test_messages_filter_labels(self):
user_id = self.register_user("kermit", "test") """Test that we can filter by a label on a /messages request."""
tok = self.login("kermit", "test") message_filter = json.dumps(
{"types": [EventTypes.Message], "org.matrix.labels": ["#fun"]}
)
room_id = self.helper.create_room_as(user_id, tok=tok) self._send_labelled_messages_in_room()
token = "s0_0_0_0_0_0_0_0_0"
request, channel = self.make_request(
"GET",
"/rooms/%s/messages?access_token=%s&from=%s&filter=%s"
% (self.room_id, self.tok, token, message_filter),
)
self.render(request)
events = channel.json_body["chunk"]
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])
def test_messages_filter_not_labels(self):
"""Test that we can filter by the absence of a label on a /messages request."""
message_filter = json.dumps(
{"types": [EventTypes.Message], "org.matrix.not_labels": ["#fun"]}
)
self._send_labelled_messages_in_room()
token = "s0_0_0_0_0_0_0_0_0"
request, channel = self.make_request(
"GET",
"/rooms/%s/messages?access_token=%s&from=%s&filter=%s"
% (self.room_id, self.tok, token, message_filter),
)
self.render(request)
events = channel.json_body["chunk"]
self.assertEqual(len(events), 4, [event["content"] for event in events])
self.assertEqual(events[0]["content"]["body"], "without label", events[0])
self.assertEqual(events[1]["content"]["body"], "without label", events[1])
self.assertEqual(events[2]["content"]["body"], "with wrong label", events[2])
self.assertEqual(
events[3]["content"]["body"], "with two wrong labels", events[3]
)
def test_messages_filter_labels_not_labels(self):
"""Test that we can filter by both a label and the absence of another label on a
/messages request.
"""
message_filter = json.dumps(
{
"types": [EventTypes.Message],
"org.matrix.labels": ["#work"],
"org.matrix.not_labels": ["#notfun"],
}
)
self._send_labelled_messages_in_room()
token = "s0_0_0_0_0_0_0_0_0"
request, channel = self.make_request(
"GET",
"/rooms/%s/messages?access_token=%s&from=%s&filter=%s"
% (self.room_id, self.tok, token, message_filter),
)
self.render(request)
events = channel.json_body["chunk"]
self.assertEqual(len(events), 1, [event["content"] for event in events])
self.assertEqual(events[0]["content"]["body"], "with wrong label", events[0])
def _send_labelled_messages_in_room(self):
self.helper.send_event( self.helper.send_event(
room_id=room_id, room_id=self.room_id,
type=EventTypes.Message, type=EventTypes.Message,
content={ content={
"msgtype": "m.text", "msgtype": "m.text",
"body": "with right label", "body": "with right label",
EventContentFields.LABELS: ["#fun"], EventContentFields.LABELS: ["#fun"],
}, },
tok=tok, tok=self.tok,
) )
self.helper.send_event( self.helper.send_event(
room_id=room_id, room_id=self.room_id,
type=EventTypes.Message, type=EventTypes.Message,
content={"msgtype": "m.text", "body": "without label"}, content={"msgtype": "m.text", "body": "without label"},
tok=tok, tok=self.tok,
) )
# The event we'll look up the context for.
res = self.helper.send_event( res = self.helper.send_event(
room_id=room_id, room_id=self.room_id,
type=EventTypes.Message, type=EventTypes.Message,
content={ content={"msgtype": "m.text", "body": "without label"},
"msgtype": "m.text", tok=self.tok,
"body": "with right label",
EventContentFields.LABELS: ["#fun"],
},
tok=tok,
) )
event_id = res["event_id"] event_id = res["event_id"]
self.helper.send_event( self.helper.send_event(
room_id=room_id, room_id=self.room_id,
type=EventTypes.Message, type=EventTypes.Message,
content={ content={
"msgtype": "m.text", "msgtype": "m.text",
"body": "with wrong label", "body": "with wrong label",
EventContentFields.LABELS: ["#work"], EventContentFields.LABELS: ["#work"],
}, },
tok=tok, tok=self.tok,
) )
self.helper.send_event( self.helper.send_event(
room_id=room_id, room_id=self.room_id,
type=EventTypes.Message, type=EventTypes.Message,
content={ content={
"msgtype": "m.text", "msgtype": "m.text",
"body": "with two wrong labels", "body": "with two wrong labels",
EventContentFields.LABELS: ["#work", "#notfun"], EventContentFields.LABELS: ["#work", "#notfun"],
}, },
tok=tok, tok=self.tok,
) )
self.helper.send_event( self.helper.send_event(
room_id=room_id, room_id=self.room_id,
type=EventTypes.Message, type=EventTypes.Message,
content={ content={
"msgtype": "m.text", "msgtype": "m.text",
"body": "with right label", "body": "with right label",
EventContentFields.LABELS: ["#fun"], EventContentFields.LABELS: ["#fun"],
}, },
tok=tok, tok=self.tok,
) )
request, channel = self.make_request( return event_id
"GET",
"/rooms/%s/context/%s?filter=%s" % (room_id, event_id, context_filter),
access_token=tok,
)
self.render(request)
self.assertEqual(channel.code, 200, channel.result)
return channel.json_body