Skip to content
Snippets Groups Projects
test_admin.py 38.2 KiB
Newer Older
  • Learn to ignore specific revisions
  •         _order_test("alphabetical", [room_id_3, room_id_2, room_id_1], reverse=True)
    
            _order_test("size", [room_id_3, room_id_2, room_id_1])
            _order_test("size", [room_id_1, room_id_2, room_id_3], reverse=True)
    
        def test_search_term(self):
            """Test that searching for a room works correctly"""
            # Create two test rooms
            room_id_1 = self.helper.create_room_as(self.admin_user, tok=self.admin_user_tok)
            room_id_2 = self.helper.create_room_as(self.admin_user, tok=self.admin_user_tok)
    
            room_name_1 = "something"
            room_name_2 = "else"
    
            # Set the name for each room
            self.helper.send_state(
                room_id_1, "m.room.name", {"name": room_name_1}, tok=self.admin_user_tok,
            )
            self.helper.send_state(
                room_id_2, "m.room.name", {"name": room_name_2}, tok=self.admin_user_tok,
            )
    
            def _search_test(
                expected_room_id: Optional[str],
                search_term: str,
                expected_http_code: int = 200,
            ):
                """Search for a room and check that the returned room's id is a match
    
                Args:
                    expected_room_id: The room_id expected to be returned by the API. Set
                        to None to expect zero results for the search
                    search_term: The term to search for room names with
                    expected_http_code: The expected http code for the request
                """
                url = "/_synapse/admin/v1/rooms?search_term=%s" % (search_term,)
                request, channel = self.make_request(
                    "GET", url.encode("ascii"), access_token=self.admin_user_tok,
                )
                self.render(request)
                self.assertEqual(expected_http_code, channel.code, msg=channel.json_body)
    
                if expected_http_code != 200:
                    return
    
                # Check that rooms were returned
                self.assertTrue("rooms" in channel.json_body)
                rooms = channel.json_body["rooms"]
    
                # Check that the expected number of rooms were returned
                expected_room_count = 1 if expected_room_id else 0
                self.assertEqual(len(rooms), expected_room_count)
                self.assertEqual(channel.json_body["total_rooms"], expected_room_count)
    
                # Check that the offset is correct
                # We're not paginating, so should be 0
                self.assertEqual(channel.json_body["offset"], 0)
    
                # Check that there is no `prev_batch`
                self.assertNotIn("prev_batch", channel.json_body)
    
                # Check that there is no `next_batch`
                self.assertNotIn("next_batch", channel.json_body)
    
                if expected_room_id:
                    # Check that the first returned room id is correct
                    r = rooms[0]
                    self.assertEqual(expected_room_id, r["room_id"])
    
            # Perform search tests
            _search_test(room_id_1, "something")
            _search_test(room_id_1, "thing")
    
            _search_test(room_id_2, "else")
            _search_test(room_id_2, "se")
    
            _search_test(None, "foo")
            _search_test(None, "bar")
            _search_test(None, "", expected_http_code=400)