Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/20136.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add missing tests for parse_stripped_state_event. Contributed by @guillemo12.
117 changes: 116 additions & 1 deletion tests/events/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

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,
Expand All @@ -35,6 +35,7 @@
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
Expand Down Expand Up @@ -1003,6 +1004,120 @@ def test_invalid_nesting_raises_type_error(self) -> None:
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.

Expand Down
Loading