async def test_room_key_on_client_sync_stream(self, client): await client.receive_response(self.login_response) await client.receive_response( SyncResponse.from_dict(self.initial_sync_response)) await client.receive_response( KeysUploadResponse.from_dict(self.keys_upload_response)) await client.receive_response( KeysQueryResponse.from_dict(self.keys_query_response)) BobId = "@bob:example.org" Bob_device = "BOBDEVICE" bob_olm = Olm(BobId, Bob_device, SqliteMemoryStore("ephemeral", "DEVICEID")) alice_device = OlmDevice(client.user_id, client.device_id, client.olm.account.identity_keys) bob_device = OlmDevice(bob_olm.user_id, bob_olm.device_id, bob_olm.account.identity_keys) client.olm.device_store.add(bob_device) bob_olm.device_store.add(alice_device) bob_olm.store.save_device_keys( {client.user_id: { client.device_id: alice_device }}) client.olm.account.generate_one_time_keys(1) one_time = list( client.olm.account.one_time_keys["curve25519"].values())[0] client.olm.account.mark_keys_as_published() bob_olm.create_session(one_time, alice_device.curve25519) _, to_device = bob_olm.share_group_session( TEST_ROOM_ID, [client.user_id], ignore_unverified_devices=True) outbound_session = bob_olm.outbound_group_sessions[TEST_ROOM_ID] olm_content = to_device["messages"][client.user_id][client.device_id] payload = { "sender": bob_olm.user_id, "type": "m.room.encrypted", "content": olm_content, } sync_response = self.empty_sync sync_response["to_device"]["events"].append(payload) session = client.olm.inbound_group_store.get(TEST_ROOM_ID, bob_device.curve25519, outbound_session.id) assert not session client.handle_to_device_from_sync_body(sync_response) session = client.olm.inbound_group_store.get(TEST_ROOM_ID, bob_device.curve25519, outbound_session.id) assert session
def sync_response(self): timeline = Timeline([ RoomMemberEvent( { "event_id": "event_id_1", "sender": ALICE_ID, "origin_server_ts": 1516809890615 }, ALICE_ID, "join", None, {"membership": "join"}), RoomEncryptionEvent({ "event_id": "event_id_2", "sender": ALICE_ID, "origin_server_ts": 1516809890615 }) ], False, "prev_batch_token") test_room_info = RoomInfo(timeline, [], [TypingNoticeEvent([ALICE_ID])], [], RoomSummary(1, 2, [])) rooms = Rooms({}, {TEST_ROOM_ID: test_room_info}, {}) return SyncResponse("token123", rooms, DeviceOneTimeKeyCount( 49, 50), DeviceList([ALICE_ID], []), [ RoomEncryptionEvent({ "event_id": "event_id_2", "sender": ALICE_ID, "origin_server_ts": 1516809890615 }) ])
def sync_invite_response(self): state = [ InviteMemberEvent( {}, "@BOB:example.org", ALICE_ID, "invite", None, { "membership": "invite", "display_name": None, } ) ] test_room_info = InviteInfo(state) rooms = Rooms( { TEST_ROOM_ID: test_room_info }, {}, {} ) return SyncResponse( "token123", rooms, DeviceOneTimeKeyCount(49, 50), DeviceList([ALICE_ID], []), [], [] )
def downgrade_sync(self): timeline = Timeline([ RoomMemberEvent("event_id_1", ALICE_ID, 1516809890615, ALICE_ID, {"membership": "join"}), ], False, "prev_batch_token") test_room_info = RoomInfo(timeline, [], [], [], RoomSummary(1, 2, [])) rooms = Rooms({}, {TEST_ROOM_ID: test_room_info}, {}) return SyncResponse("token123", rooms, DeviceOneTimeKeyCount(49, 50), DeviceList([ALICE_ID], []), [])
def test_soft_logout(self, client): client.receive_response(self.login_response) assert client.logged_in error_response = SyncResponse.from_dict({ "errcode": "M_UNKNOWN_TOKEN", "error": "Access token has expired", "soft_logout": True }) client.receive_response(error_response) assert not client.logged_in
def sync_response(self): timeline = Timeline([ RoomMemberEvent( { "event_id": "event_id_1", "sender": ALICE_ID, "origin_server_ts": 1516809890615 }, ALICE_ID, "join", None, {"membership": "join"}), RoomMemberEvent( { "event_id": "event_id_2", "sender": ALICE_ID, "origin_server_ts": 1516809890615 }, CAROL_ID, "invite", None, {"membership": "invite"}, ), RoomEncryptionEvent({ "event_id": "event_id_3", "sender": ALICE_ID, "origin_server_ts": 1516809890615 }) ], False, "prev_batch_token") test_room_info = RoomInfo( timeline, [], [ TypingNoticeEvent([ALICE_ID]), ReceiptEvent([ Receipt(event_id="event_id_3", receipt_type="m.read", user_id=ALICE_ID, timestamp=1516809890615) ]) ], [], RoomSummary(invited_member_count=1, joined_member_count=2), ) rooms = Rooms({}, {TEST_ROOM_ID: test_room_info}, {}) return SyncResponse("token123", rooms, DeviceOneTimeKeyCount( 49, 50), DeviceList([ALICE_ID], []), [ RoomEncryptionEvent({ "event_id": "event_id_2", "sender": ALICE_ID, "origin_server_ts": 1516809890615 }) ])
def test_matrix_nio_backend_serve_once_logged_in_has_not_synced(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") # Needed for ensuring that backend.client.logged_in = True backend.client.access_token = True # Needed since path may be tricky to get with open(os.path.join(os.path.dirname(__file__), "sync.json")) as json_file: data = json.loads(json_file.read()) sync_mock = mock.Mock( return_value=aiounittest.futurized(SyncResponse.from_dict(data))) backend.client.sync = sync_mock backend.serve_once() self.assertTrue(backend.has_synced) self.assertEqual(backend.client.next_batch, data["next_batch"]) sync_mock.assert_called_once_with(full_state=True)
def sync_response(self): timeline = Timeline([ RoomMemberEvent( { "event_id": "event_id_1", "sender": ALICE_ID, "origin_server_ts": 1516809890615 }, ALICE_ID, "join", None, {"membership": "join"}), RoomMemberEvent( { "event_id": "event_id_2", "sender": ALICE_ID, "origin_server_ts": 1516809890615 }, CAROL_ID, "invite", None, {"membership": "invite"}, ), RoomEncryptionEvent({ "event_id": "event_id_3", "sender": ALICE_ID, "origin_server_ts": 1516809890615 }) ], False, "prev_batch_token") test_room_info = RoomInfo( timeline=timeline, state=[], ephemeral=[ TypingNoticeEvent([ALICE_ID]), ReceiptEvent([ Receipt(event_id="event_id_3", receipt_type="m.read", user_id=ALICE_ID, timestamp=1516809890615) ]) ], account_data=[ FullyReadEvent(event_id="event_id_2"), TagEvent(tags={"u.test": { "order": 1 }}), ], summary=RoomSummary( invited_member_count=1, joined_member_count=2, ), ) rooms = Rooms(invite={}, join={TEST_ROOM_ID: test_room_info}, leave={}) return SyncResponse(next_batch="token123", rooms=rooms, device_key_count=DeviceOneTimeKeyCount(49, 50), device_list=DeviceList([ALICE_ID], []), to_device_events=[ RoomEncryptionEvent({ "event_id": "event_id_2", "sender": ALICE_ID, "origin_server_ts": 1516809890615 }) ], presence_events=[ PresenceEvent(ALICE_ID, "online", 1337, True, "I am here.") ])