def test_megolm_event(self): parsed_dict = TestClass._load_response("tests/data/events/megolm.json") event = Event.parse_event(parsed_dict) assert isinstance(event, MegolmEvent) parsed_dict["content"]["algorithm"] = "m.megolm.unknown" event = Event.parse_event(parsed_dict) assert isinstance(event, UnknownEncryptedEvent)
def test_encrypted_media_thumbnails(self): parsed_dict = TestClass._load_response( "tests/data/events/room_encrypted_image.json") event = Event.parse_decrypted_event(parsed_dict) assert isinstance(event, RoomEncryptedImage) assert event.thumbnail_url assert event.thumbnail_key assert event.thumbnail_hashes assert event.thumbnail_iv
def test_event_flattening(self): parsed_dict = TestClass._load_response( "tests/data/events/to_flatten.json", ) event = Event.from_dict(parsed_dict) assert event.flattened() == { "content.body": "foo", "content.m.dotted.key": "bar", "event_id": "!test:example.org", "origin_server_ts": 0, "sender": "@alice:example.org", "type": "m.flatten_test", }
def test_invalid_state_event(self): for event_type, event_file in [ ("m.room.create", "create.json"), ("m.room.guest_access", "guest_access.json"), ("m.room.join_rules", "join_rules.json"), ("m.room.history_visibility", "history_visibility.json"), ("m.room.member", "member.json"), ("m.room.canonical_alias", "alias.json"), ("m.room.name", "name.json"), ("m.room.topic", "topic.json"), ("m.room.avatar", "room_avatar.json"), ("m.room.power_levels", "power_levels.json"), ("m.room.encryption", "room_encryption.json"), ]: parsed_dict = TestClass._load_response( "tests/data/events/{}".format(event_file)) parsed_dict.pop("state_key") event = Event.parse_event(parsed_dict) assert isinstance(event, BadEvent) assert event.source["type"] == event_type
def test_pushrules_matching(self): room = MatrixRoom("!test:example.org", "@alice:example.com") name = "Alice" event = Event.from_dict({ "event_id": "!test:example.org", "room_id": room.room_id, "origin_server_ts": 0, "sender": "@alice:example.org", "type": "m.test", "words": "foo bar", "int": 0, "content": { "body": "a,here c" }, }) args = (event, room, name) # PushEventMatch must_succeed = [ ("type", "m.test"), ("type", "M*T"), # glob + ignoring case ("content.body", "heRe"), # word boundaries + ignoring case ("content.body", "a"), # word at the start of the string ("content.body", "c"), # word at the end of the string ("content.body", "[a-z]*c"), # more glob patterns ] must_fail = [ ("int", "0"), # only match string values ("words", "foo"), # match words only for content.body ("content.body", "her"), # not a full word match ] for key, pattern in must_succeed: assert PushEventMatch(key, pattern).matches(*args) for key, pattern in must_fail: assert not PushEventMatch(key, pattern).matches(*args) # PushContainsDisplayName assert not PushContainsDisplayName().matches(*args) del event.source["content"]["body"] assert not PushContainsDisplayName().matches(*args) event.source["content"]["body"] = "alice!" assert PushContainsDisplayName().matches(*args) # PushRoomMemberCount room.summary = RoomSummary(100, 5) # invited members don't matter tests = [(5, "=="), (6, "<"), (4, ">"), (5, "<="), (4, ">=")] for count, operator in tests: assert PushRoomMemberCount(count, operator).matches(*args) # PushSenderNotificationPermission assert not PushSenderNotificationPermission("room").matches(*args) room.power_levels.users[event.sender] = 50 assert PushSenderNotificationPermission("room").matches(*args) # PushUnknownCondition assert not PushUnknownCondition({}).matches(*args) # PushRule rule = PushRule(PushRuleKind.override, "all", False) assert rule.matches(*args) rule.enabled = False assert not rule.matches(*args) cnds = [PushEventMatch("type", "m.test")] rule = PushRule(PushRuleKind.override, "test", False, conditions=cnds) assert rule.matches(*args) cnds.append(PushUnknownCondition({})) assert not rule.matches(*args) rule = PushRule(PushRuleKind.room, room.room_id, False) assert rule.matches(*args) rule.id += "blah" assert not rule.matches(*args) rule = PushRule(PushRuleKind.sender, event.sender, False) assert rule.matches(*args) rule.id += "blah" assert not rule.matches(*args) event.source["content"]["body"] = "a here! b c" rule = PushRule(PushRuleKind.content, "here", False, pattern="here") assert rule.matches(*args) rule.pattern = "her" assert not rule.matches(*args) # PushRuleset ruleset = PushRuleset( room=[ PushRule(PushRuleKind.room, "blah", False), PushRule(PushRuleKind.room, room.room_id, False), ], sender=[PushRule(PushRuleKind.sender, event.sender, False)], ) assert ruleset.matching_rule(*args) is ruleset.room[1] del ruleset.room[1] del ruleset.sender[0] assert ruleset.matching_rule(*args) is None
def test_redacted_state_event(self): parsed_dict = TestClass._load_response("tests/data/events/redacted_state.json") event = Event.parse_event(parsed_dict) assert isinstance(event, RedactedEvent)
def test_invalid_room_event(self): event = Event.parse_event({"type": "m.unknown"}) assert isinstance(event, UnknownBadEvent)
def test_room_encryption(self): parsed_dict = TestClass._load_response("tests/data/events/room_encryption.json") event = Event.parse_event(parsed_dict) assert isinstance(event, RoomEncryptionEvent)