def test_changing_other_required_power_levels(): client = MatrixClient(HOSTNAME) room_id = "!UcYsUzyxTGDxLBEvLz:matrix.org" room = client._mkroom(room_id) PL_state_path = HOSTNAME + MATRIX_V2_API_PATH + \ "/rooms/" + quote(room_id) + "/state/m.room.power_levels" # Code should first get current power_levels and then modify them responses.add(responses.GET, PL_state_path, json=response_examples.example_pl_event["content"]) responses.add(responses.PUT, PL_state_path, json=response_examples.example_event_response) # Remove event from events and adds new controlled event assert room.modify_required_power_levels(kick=53, redact=2, state_default=None) expected_request = deepcopy(response_examples.example_pl_event["content"]) expected_request["kick"] = 53 expected_request["redact"] = 2 del expected_request["state_default"] assert json.loads(responses.calls[1].request.body) == expected_request
def test_bad_state_events(): client = MatrixClient("http://example.com") room = client._mkroom("!abc:matrix.org") ev = {"tomato": False} room._process_state_event(ev)
def test_detect_encryption_state(): client = MatrixClient(HOSTNAME, encryption=True) room_id = "!UcYsUzyxTGDxLBEvLz:matrix.org" encryption_state_path = HOSTNAME + MATRIX_V2_API_PATH + \ "/rooms/" + quote(room_id) + "/state/m.room.encryption" responses.add(responses.GET, encryption_state_path, json={"algorithm": "m.megolm.v1.aes-sha2"}) responses.add(responses.GET, encryption_state_path, json={}, status=404) room = client._mkroom(room_id) assert room.encrypted room = client._mkroom(room_id) assert not room.encrypted
def test_changing_user_power_levels(): client = MatrixClient(HOSTNAME) room_id = "!UcYsUzyxTGDxLBEvLz:matrix.org" room = client._mkroom(room_id) PL_state_path = HOSTNAME + MATRIX_V2_API_PATH + \ "/rooms/" + quote(room_id) + "/state/m.room.power_levels" # Code should first get current power_levels and then modify them responses.add(responses.GET, PL_state_path, json=response_examples.example_pl_event["content"]) responses.add(responses.PUT, PL_state_path, json=response_examples.example_event_response) # Removes user from user and adds user to to users list assert room.modify_user_power_levels(users={ "@example:localhost": None, "@foobar:example.com": 49 }) expected_request = deepcopy(response_examples.example_pl_event["content"]) del expected_request["users"]["@example:localhost"] expected_request["users"]["@foobar:example.com"] = 49 assert json.loads(responses.calls[1].request.body) == expected_request
def test_get_rooms_display_name(): def add_members(api, room, num): for i in range(num): room._add_member('@frho%s:matrix.org' % i, 'ho%s' % i) client = MatrixClient("http://example.com") client.user_id = "@frho0:matrix.org" room1 = client._mkroom("!abc:matrix.org") add_members(client.api, room1, 1) room2 = client._mkroom("!def:matrix.org") add_members(client.api, room2, 2) room3 = client._mkroom("!ghi:matrix.org") add_members(client.api, room3, 3) room4 = client._mkroom("!rfi:matrix.org") add_members(client.api, room4, 30) rooms = client.get_rooms() assert len(rooms) == 4 assert room1.display_name == "Empty room" assert room2.display_name == "ho1" assert room3.display_name == "ho1 and ho2" assert room4.display_name == "ho1 and 28 others"
def test_enable_encryption_in_room(): client = MatrixClient(HOSTNAME) room_id = "!UcYsUzyxTGDxLBEvLz:matrix.org" room = client._mkroom(room_id) assert not room.encrypted encryption_state_path = HOSTNAME + MATRIX_V2_API_PATH + \ "/rooms/" + quote(room_id) + "/state/m.room.encryption" responses.add(responses.PUT, encryption_state_path, json=response_examples.example_event_response) assert room.enable_encryption() assert room.encrypted room = client._mkroom(room_id) assert room.enable_encryption(rotation_period_msgs=1, rotation_period_ms=1) assert room.rotation_period_msgs == 1 assert room.rotation_period_ms == 1 assert room.enable_encryption(rotation_period_msgs=2) # The room was already encrypted, we should not have changed its attribute assert room.rotation_period_msgs == 1
def test_room_guest_access(): client = MatrixClient(HOSTNAME) room_id = "!UcYsUzyxTGDxLBEvLz:matrix.org" room = client._mkroom(room_id) assert room.guest_access is None guest_access_state_path = HOSTNAME + MATRIX_V2_API_PATH + \ "/rooms/" + quote(room_id) + "/state/m.room.guest_access" responses.add(responses.PUT, guest_access_state_path, json=response_examples.example_event_response) assert room.set_guest_access(True) assert room.guest_access
def test_room_join_rules(): client = MatrixClient(HOSTNAME) room_id = "!UcYsUzyxTGDxLBEvLz:matrix.org" room = client._mkroom(room_id) assert room.invite_only is None join_rules_state_path = HOSTNAME + MATRIX_V2_API_PATH + \ "/rooms/" + quote(room_id) + "/state/m.room.join_rules" responses.add(responses.PUT, join_rules_state_path, json=response_examples.example_event_response) assert room.set_invite_only(True) assert room.invite_only
def test__mkroom(): client = MatrixClient("http://example.com") roomId = "!UcYsUzyxTGDxLBEvLz:matrix.org" goodRoom = client._mkroom(roomId) assert isinstance(goodRoom, Room) assert goodRoom.room_id is roomId with pytest.raises(ValueError): client._mkroom("BAD_ROOM:matrix.org") client._mkroom("!BAD_ROOMmatrix.org") client._mkroom("!BAD_ROOM::matrix.org")
def test_changing_default_power_level(): client = MatrixClient(HOSTNAME) room_id = "!UcYsUzyxTGDxLBEvLz:matrix.org" room = client._mkroom(room_id) PL_state_path = HOSTNAME + MATRIX_V2_API_PATH + \ "/rooms/" + quote(room_id) + "/state/m.room.power_levels" # Code should first get current power_levels and then modify them responses.add(responses.GET, PL_state_path, json=response_examples.example_pl_event["content"]) responses.add(responses.PUT, PL_state_path, json=response_examples.example_event_response) assert room.modify_user_power_levels(users_default=23) expected_request = deepcopy(response_examples.example_pl_event["content"]) expected_request["users_default"] = 23 assert json.loads(responses.calls[1].request.body) == expected_request
def test_get_rooms(): client = MatrixClient("http://example.com") rooms = client.get_rooms() assert isinstance(rooms, dict) assert len(rooms) == 0 client = MatrixClient("http://example.com") client._mkroom("!abc:matrix.org") client._mkroom("!def:matrix.org") client._mkroom("!ghi:matrix.org") rooms = client.get_rooms() assert isinstance(rooms, dict) assert len(rooms) == 3
def test_state_event(): client = MatrixClient("http://example.com") room = client._mkroom("!abc:matrix.org") room.name = False room.topic = False room.aliases = False ev = {"type": "m.room.name", "content": {}} room._process_state_event(ev) assert room.name is None ev["content"]["name"] = "TestName" room._process_state_event(ev) assert room.name is "TestName" ev["type"] = "m.room.topic" room._process_state_event(ev) assert room.topic is None ev["content"]["topic"] = "TestTopic" room._process_state_event(ev) assert room.topic is "TestTopic" ev["type"] = "m.room.aliases" room._process_state_event(ev) assert room.aliases is None aliases = ["#foo:matrix.org", "#bar:matrix.org"] ev["content"]["aliases"] = aliases room._process_state_event(ev) assert room.aliases is aliases # test member join event ev["type"] = "m.room.member" ev["content"] = {'membership': 'join', 'displayname': 'stereo'} ev["state_key"] = "@stereo:xxx.org" room._process_state_event(ev) assert len(room._members) == 1 assert room._members["@stereo:xxx.org"] # test member leave event ev["content"]['membership'] = 'leave' room._process_state_event(ev) assert len(room._members) == 0 # test join_rules room.invite_only = False ev["type"] = "m.room.join_rules" ev["content"] = {"join_rule": "invite"} room._process_state_event(ev) assert room.invite_only # test guest_access room.guest_access = False ev["type"] = "m.room.guest_access" ev["content"] = {"guest_access": "can_join"} room._process_state_event(ev) assert room.guest_access # test encryption room.encrypted = False ev["type"] = "m.room.encryption" ev["content"] = { "algorithm": "m.megolm.v1.aes-sha2", "rotation_period_msgs": 50, "rotation_period_ms": 100000, } room._process_state_event(ev) assert room.encrypted assert room.rotation_period_ms == 100000 assert room.rotation_period_msgs == 50 # encrypted flag must not be cleared on configuration change ev["content"] = {"algorithm": None} room._process_state_event(ev) assert room.encrypted assert room.rotation_period_ms == 100000 assert room.rotation_period_msgs == 50 # nor should the session parameters be changed ev["content"] = { "algorithm": "m.megolm.v1.aes-sha2", "rotation_period_msgs": 5, "rotation_period_ms": 10000, } room._process_state_event(ev) assert room.rotation_period_ms == 100000 assert room.rotation_period_msgs == 50