def test_session_sharing(self, alice_client, async_client, aioresponse): loop = asyncio.get_event_loop() async_client.receive_response( LoginResponse.from_dict(self.login_response)) assert async_client.logged_in async_client.receive_response(self.encryption_sync_response) alice_client.load_store() alice_device = OlmDevice( ALICE_ID, ALICE_DEVICE_ID, alice_client.olm.account.identity_keys["ed25519"], alice_client.olm.account.identity_keys["curve25519"], ) async_client.device_store.add(alice_device) async_client.verify_device(alice_device) missing = async_client.get_missing_sessions(TEST_ROOM_ID) assert ALICE_ID in missing assert ALICE_DEVICE_ID in missing[ALICE_ID] to_share = alice_client.olm.share_keys() one_time_key = list(to_share["one_time_keys"].items())[0] key_claim_dict = { "one_time_keys": { ALICE_ID: { ALICE_DEVICE_ID: { one_time_key[0]: one_time_key[1] }, }, }, "failures": {}, } aioresponse.post( "https://example.org/_matrix/client/r0/keys/claim?access_token=abc123", status=200, payload=key_claim_dict) aioresponse.put( "https://example.org/_matrix/client/r0/sendToDevice/m.room.encrypted/1?access_token=abc123", status=200, payload={}) with pytest.raises(KeyError): session = async_client.olm.outbound_group_sessions[TEST_ROOM_ID] response = loop.run_until_complete( async_client.share_group_session(TEST_ROOM_ID, "1")) session = async_client.olm.outbound_group_sessions[TEST_ROOM_ID] assert session.shared assert isinstance(response, ShareGroupSessionResponse) assert not async_client.get_missing_sessions(TEST_ROOM_ID) assert async_client.olm.session_store.get(alice_device.curve25519)
def test_matrix_nio_backend_serve_once_not_logged_in_has_not_synced_error_sync( self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") backend.client.access_token = True user_id = "@example:localhost" login_response = LoginResponse.from_dict({ "user_id": user_id, "device_id": "device_id", "access_token": "12345", }) login_response_mock = mock.Mock( return_value=aiounittest.futurized(login_response)) backend.client.login_raw = login_response_mock sync_mock = mock.Mock(return_value=aiounittest.futurized( ErrorResponse.from_dict({ "errcode": "ERROR_SYNCING", "error": "Error syncing", "retry_after_ms": 10000 }))) backend.client.sync = sync_mock with self.assertRaises(ValueError): backend.serve_once() sync_mock.assert_called_once_with(full_state=True)
def login_response(self): return LoginResponse.from_dict({ "access_token": "abc123", "device_id": "DEVICEID", "home_server": "example.org", "user_id": "@example:example.org", })
def test_matrix_nio_backend_serve_once_not_logged_in_has_synced(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") backend.has_synced = True user_id = "@example:localhost" login_response = LoginResponse.from_dict({ "user_id": user_id, "device_id": "device_id", "access_token": "12345", }) login_response_mock = mock.Mock( return_value=aiounittest.futurized(login_response)) backend.client.login_raw = login_response_mock test_name = "Test Name" backend.client.get_profile = mock.Mock( return_value=aiounittest.futurized( ProfileGetResponse.from_dict( { "displayname": test_name, "avatar_url": "http://test.org/avatar.png" }))) sync_forever_mock = mock.Mock(return_value=aiounittest.futurized(True)) backend.client.sync_forever = sync_forever_mock backend.serve_once() sync_forever_mock.assert_called_once_with(30000, full_state=True) backend.client.get_profile.assert_called_once_with(user_id) login_response_mock.assert_called_once_with( self.bot_config.BOT_IDENTITY["auth_dict"])
def test_key_claiming(self, alice_client, async_client, aioresponse): loop = asyncio.get_event_loop() async_client.receive_response( LoginResponse.from_dict(self.login_response)) assert async_client.logged_in async_client.receive_response(self.encryption_sync_response) alice_client.load_store() alice_device = OlmDevice( ALICE_ID, ALICE_DEVICE_ID, alice_client.olm.account.identity_keys["ed25519"], alice_client.olm.account.identity_keys["curve25519"], ) async_client.device_store.add(alice_device) missing = async_client.get_missing_sessions(TEST_ROOM_ID) assert ALICE_ID in missing assert ALICE_DEVICE_ID in missing[ALICE_ID] to_share = alice_client.olm.share_keys() one_time_key = list(to_share["one_time_keys"].items())[0] key_claim_dict = { "one_time_keys": { ALICE_ID: { ALICE_DEVICE_ID: { one_time_key[0]: one_time_key[1] }, }, }, "failures": {}, } aioresponse.post( "https://example.org/_matrix/client/r0/keys/claim?access_token=abc123", status=200, payload=key_claim_dict) response = loop.run_until_complete(async_client.keys_claim(missing)) assert isinstance(response, KeysClaimResponse) assert not async_client.get_missing_sessions(TEST_ROOM_ID) assert async_client.olm.session_store.get(alice_device.curve25519)