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_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