Add unit tests for parse_stripped_state_event (#20136)

This PR adds a suite of tests for Synapse state events. It covers key
scenarios around creation, updates and state consistency to prevent
regressions in event processing and serialization.
The primary goal is to increase test coverage.
This commit is contained in:
guillermo
2026-09-02 16:49:44 +01:00
committed by GitHub
parent dea059a94d
commit 8f27ac006f
2 changed files with 117 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
Add missing tests for parse_stripped_state_event. Contributed by @guillemo12.
+116 -1
View File
@@ -24,7 +24,7 @@ from typing import TYPE_CHECKING, Any, Mapping
from synapse.api.constants import EventContentFields
from synapse.api.room_versions import RoomVersions
from synapse.events import EventBase
from synapse.events import EventBase, StrippedStateEvent
from synapse.events.utils import (
FilteredEvent,
PowerLevelsContent,
@@ -35,6 +35,7 @@ from synapse.events.utils import (
format_event_for_client_v2_without_room_id,
format_event_raw,
maybe_upsert_event_field,
parse_stripped_state_event,
prune_event,
)
from synapse.types import JsonDict
@@ -1003,6 +1004,120 @@ class CopyPowerLevelsContentTestCase(stdlib_unittest.TestCase):
copy_and_fixup_power_levels_contents({"a": {"b": {"c": 1}}}) # type: ignore[dict-item]
class TestParseStrippedStateEvent(stdlib_unittest.TestCase):
def test_valid_dict(self) -> None:
"""A valid dict should be parsed into a StrippedStateEvent."""
raw = {
"type": "m.room.member",
"state_key": "@alice:example.com",
"sender": "@alice:example.com",
"content": {"membership": "join"},
}
result = parse_stripped_state_event(raw)
self.assertEqual(
result,
StrippedStateEvent(
type="m.room.member",
state_key="@alice:example.com",
sender="@alice:example.com",
content={"membership": "join"},
),
)
def test_invalid_type(self) -> None:
"""Non-dict inputs should return None."""
self.assertIsNone(parse_stripped_state_event("string"))
self.assertIsNone(parse_stripped_state_event(123))
self.assertIsNone(parse_stripped_state_event([]))
self.assertIsNone(parse_stripped_state_event(None))
def test_missing_fields(self) -> None:
"""Dicts with missing required fields should return None."""
self.assertIsNone(
parse_stripped_state_event(
{
"state_key": "@alice:example.com",
"sender": "@alice:example.com",
"content": {"membership": "join"},
}
)
)
self.assertIsNone(
parse_stripped_state_event(
{
"type": "m.room.member",
"sender": "@alice:example.com",
"content": {"membership": "join"},
}
)
)
self.assertIsNone(
parse_stripped_state_event(
{
"type": "m.room.member",
"state_key": "@alice:example.com",
"content": {"membership": "join"},
}
)
)
self.assertIsNone(
parse_stripped_state_event(
{
"type": "m.room.member",
"state_key": "@alice:example.com",
"sender": "@alice:example.com",
}
)
)
def test_invalid_field_types(self) -> None:
"""Dicts with invalid field types should return None."""
# Type must be string
self.assertIsNone(
parse_stripped_state_event(
{
"type": 123,
"state_key": "@alice:example.com",
"sender": "@alice:example.com",
"content": {"membership": "join"},
}
)
)
# State key must be string
self.assertIsNone(
parse_stripped_state_event(
{
"type": "m.room.member",
"state_key": 123,
"sender": "@alice:example.com",
"content": {"membership": "join"},
}
)
)
# Sender must be string
self.assertIsNone(
parse_stripped_state_event(
{
"type": "m.room.member",
"state_key": "@alice:example.com",
"sender": 123,
"content": {"membership": "join"},
}
)
)
# Content must be dict
self.assertIsNone(
parse_stripped_state_event(
{
"type": "m.room.member",
"state_key": "@alice:example.com",
"sender": "@alice:example.com",
"content": "membership_join",
}
)
)
class FormatEventForClientTestCase(stdlib_unittest.TestCase):
"""Tests for the standalone `format_event_*` transforms.