diff --git a/spec/unit/rust-crypto/rust-crypto.spec.ts b/spec/unit/rust-crypto/rust-crypto.spec.ts index 52916b43642..7485065d245 100644 --- a/spec/unit/rust-crypto/rust-crypto.spec.ts +++ b/spec/unit/rust-crypto/rust-crypto.spec.ts @@ -696,6 +696,58 @@ describe("RustCrypto", () => { await awaitCallToMakeOutgoingRequest(); expect(olmMachine.outgoingRequests).toHaveBeenCalledTimes(2); }); + + it("should encode outgoing requests properly", async () => { + // we need a real OlmMachine, so replace the one created by beforeEach + rustCrypto = await makeTestRustCrypto(); + const olmMachine: OlmMachine = rustCrypto["olmMachine"]; + + const outgoingRequestProcessor = {} as unknown as OutgoingRequestProcessor; + rustCrypto["outgoingRequestProcessor"] = outgoingRequestProcessor; + const outgoingRequestsManager = new OutgoingRequestsManager(logger, olmMachine, outgoingRequestProcessor); + rustCrypto["outgoingRequestsManager"] = outgoingRequestsManager; + + // The second time we do a /keys/upload, the `device_keys` property + // should be absent from the request body + // cf. https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/issues/57 + // + // On the first upload, we pretend that there are no OTKs, so it will + // try to upload more keys + let keysUploadCount = 0; + let deviceKeys: object; + let deviceKeysAbsent = false; + outgoingRequestProcessor.makeOutgoingRequest = jest.fn(async (request, uiaCallback?) => { + let resp: any = {}; + if (request instanceof RustSdkCryptoJs.KeysUploadRequest) { + if (keysUploadCount == 0) { + deviceKeys = JSON.parse(request.body).device_keys; + resp = { one_time_key_counts: { signed_curve25519: 0 } }; + } else { + deviceKeysAbsent = !("device_keys" in JSON.parse(request.body)); + resp = { one_time_key_counts: { signed_curve25519: 50 } }; + } + keysUploadCount++; + } else if (request instanceof RustSdkCryptoJs.KeysQueryRequest) { + resp = { + device_keys: { + [TEST_USER]: { + [TEST_DEVICE_ID]: deviceKeys, + }, + }, + }; + } else if (request instanceof RustSdkCryptoJs.UploadSigningKeysRequest) { + // SigningKeysUploadRequest does not implement OutgoingRequest and does not need to be marked as sent. + return; + } + if (request.id) { + olmMachine.markRequestAsSent(request.id, request.type, JSON.stringify(resp)); + } + }); + await outgoingRequestsManager.doProcessOutgoingRequests(); + await outgoingRequestsManager.doProcessOutgoingRequests(); + + expect(deviceKeysAbsent).toBe(true); + }); }); describe(".getEventEncryptionInfo", () => {