def test_summary_update(self): room = self.test_room assert not room.summary room.update_summary(RoomSummary(1, 2, [])) assert room.member_count == 2 assert room.summary room.update_summary(RoomSummary(1, 3, ["@alice:example.org"])) assert room.member_count == 3 assert room.summary.heroes == ["@alice:example.org"]
def test_summary_update(self): room = self.test_room room.summary = None room.update_summary(RoomSummary(1, 2, [])) assert room.invited_count == 1 assert room.joined_count == 2 assert room.member_count == 3 assert room.summary room.update_summary(RoomSummary(1, 3, ["@alice:example.org"])) assert room.invited_count == 1 assert room.joined_count == 3 assert room.member_count == 4 assert room.summary.heroes == ["@alice:example.org"]
def test_summary_details(self): room = self.test_room room.summary = None with pytest.raises(ValueError): assert room._summary_details() room.summary = RoomSummary(None, None, []) with pytest.raises(ValueError): assert room._summary_details() room.summary = RoomSummary(0, None, []) with pytest.raises(ValueError): assert room._summary_details() room.summary = RoomSummary(None, 0, []) with pytest.raises(ValueError): assert room._summary_details() room.summary = RoomSummary(0, 0, []) assert room._summary_details() == ([], 0, 0)
def test_name_calculation_when_unnamed_no_summary(self): room = self.test_room room.summary = RoomSummary() assert room.named_room_name() is None assert room.display_name == "Empty Room" # Members join room.add_member(BOB_ID, "Bob", None) # us assert room.display_name == "Empty Room" room.add_member("@alice:example.org", "Alice", None) assert room.display_name == "Alice" room.add_member("@malory:example.org", "Alice", None) assert (room.display_name == "Alice (@alice:example.org) and Alice (@malory:example.org)") room.add_member("@steve:example.org", "Steve", None) room.add_member("@carol:example.org", "Carol", None) room.add_member("@dave:example.org", "Dave", None) assert (room.display_name == "Alice (@alice:example.org), Alice (@malory:example.org), " "Carol, Dave and Steve") room.add_member("@erin:example.org", "Eirin", None) assert (room.display_name == "Alice (@alice:example.org), Alice (@malory:example.org), " "Carol, Dave, Eirin and 1 other") room.add_member("@frank:example.org", "Frank", None) assert (room.display_name == "Alice (@alice:example.org), Alice (@malory:example.org), " "Carol, Dave, Eirin and 2 others") room.add_member("@gregor:example.org", "Gregor", None) assert (room.display_name == "Alice (@alice:example.org), Alice (@malory:example.org), " "Carol, Dave, Eirin and 3 others") # Members leave for member in room.users.copy(): room.remove_member(member) assert room.display_name == "Empty Room"
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_room(self): room = MatrixRoom(TEST_ROOM, BOB_ID) room.update_summary(RoomSummary(0, 0, [])) return room