summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorBrendan Abolivier <babolivier@matrix.org>2019-11-05 13:47:47 +0000
committerBrendan Abolivier <babolivier@matrix.org>2019-11-05 13:47:47 +0000
commitc9e4748cb75271a2178d0cae05d551829249ada3 (patch)
tree6dea158331db34298a2d8a09cee53f9d6b267cf6 /tests
parentAdd test case (diff)
downloadsynapse-c9e4748cb75271a2178d0cae05d551829249ada3.tar.xz
Merge labels tests for /context and /messages
Diffstat (limited to 'tests')
-rw-r--r--tests/rest/client/v1/test_rooms.py276
1 files changed, 130 insertions, 146 deletions
diff --git a/tests/rest/client/v1/test_rooms.py b/tests/rest/client/v1/test_rooms.py
index 621c894e35..fe327d1bf8 100644
--- a/tests/rest/client/v1/test_rooms.py
+++ b/tests/rest/client/v1/test_rooms.py
@@ -811,105 +811,6 @@ class RoomMessageListTestCase(RoomBase):
         self.assertTrue("chunk" 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):
     servlets = [
@@ -1108,7 +1009,7 @@ class PerRoomProfilesForbiddenTestCase(unittest.HomeserverTestCase):
         self.assertEqual(res_displayname, self.displayname, channel.result)
 
 
-class ContextTestCase(unittest.HomeserverTestCase):
+class LabelsTestCase(unittest.HomeserverTestCase):
     servlets = [
         synapse.rest.admin.register_servlets_for_client_rest_resource,
         room.register_servlets,
@@ -1116,8 +1017,13 @@ class ContextTestCase(unittest.HomeserverTestCase):
         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):
-        """Test that we can filter by a label."""
+        """Test that we can filter by a label on a /context request."""
         context_filter = json.dumps(
             {
                 "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(
-            res["event"]["content"]["body"], "with right label", res["event"]
+        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 = res["events_before"]
+        events_before = channel.json_body["events_before"]
 
         self.assertEqual(
             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_after = res["events_before"]
+        events_after = channel.json_body["events_before"]
 
         self.assertEqual(
             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):
-        """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(
             {
                 "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()
+
+        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 = res["events_before"]
+        events_before = channel.json_body["events_before"]
 
         self.assertEqual(
             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_after = res["events_after"]
+        events_after = channel.json_body["events_after"]
 
         self.assertEqual(
             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):
-        """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(
             {
                 "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(
             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(
             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]
         )
 
-    def _test_context_filter_labels(self, context_filter):
-        user_id = self.register_user("kermit", "test")
-        tok = self.login("kermit", "test")
+    def test_messages_filter_labels(self):
+        """Test that we can filter by a label on a /messages request."""
+        message_filter = json.dumps(
+            {"types": [EventTypes.Message], "org.matrix.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), 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"]
 
-        room_id = self.helper.create_room_as(user_id, tok=tok)
+        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(
-            room_id=room_id,
+            room_id=self.room_id,
             type=EventTypes.Message,
             content={
                 "msgtype": "m.text",
                 "body": "with right label",
                 EventContentFields.LABELS: ["#fun"],
             },
-            tok=tok,
+            tok=self.tok,
         )
 
         self.helper.send_event(
-            room_id=room_id,
+            room_id=self.room_id,
             type=EventTypes.Message,
             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(
-            room_id=room_id,
+            room_id=self.room_id,
             type=EventTypes.Message,
-            content={
-                "msgtype": "m.text",
-                "body": "with right label",
-                EventContentFields.LABELS: ["#fun"],
-            },
-            tok=tok,
+            content={"msgtype": "m.text", "body": "without label"},
+            tok=self.tok,
         )
         event_id = res["event_id"]
 
         self.helper.send_event(
-            room_id=room_id,
+            room_id=self.room_id,
             type=EventTypes.Message,
             content={
                 "msgtype": "m.text",
                 "body": "with wrong label",
                 EventContentFields.LABELS: ["#work"],
             },
-            tok=tok,
+            tok=self.tok,
         )
 
         self.helper.send_event(
-            room_id=room_id,
+            room_id=self.room_id,
             type=EventTypes.Message,
             content={
                 "msgtype": "m.text",
                 "body": "with two wrong labels",
                 EventContentFields.LABELS: ["#work", "#notfun"],
             },
-            tok=tok,
+            tok=self.tok,
         )
 
         self.helper.send_event(
-            room_id=room_id,
+            room_id=self.room_id,
             type=EventTypes.Message,
             content={
                 "msgtype": "m.text",
                 "body": "with right label",
                 EventContentFields.LABELS: ["#fun"],
             },
-            tok=tok,
+            tok=self.tok,
         )
 
-        request, channel = self.make_request(
-            "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
-
+        return event_id