Skip to content

Commit 5967c67

Browse files
authored
Element R: Add test that requests are encoded properly (#4033)
* add test that requests are encoded properly * fix variable name
1 parent 2fe35fe commit 5967c67

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

spec/unit/rust-crypto/rust-crypto.spec.ts

+52
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,58 @@ describe("RustCrypto", () => {
696696
await awaitCallToMakeOutgoingRequest();
697697
expect(olmMachine.outgoingRequests).toHaveBeenCalledTimes(2);
698698
});
699+
700+
it("should encode outgoing requests properly", async () => {
701+
// we need a real OlmMachine, so replace the one created by beforeEach
702+
rustCrypto = await makeTestRustCrypto();
703+
const olmMachine: OlmMachine = rustCrypto["olmMachine"];
704+
705+
const outgoingRequestProcessor = {} as unknown as OutgoingRequestProcessor;
706+
rustCrypto["outgoingRequestProcessor"] = outgoingRequestProcessor;
707+
const outgoingRequestsManager = new OutgoingRequestsManager(logger, olmMachine, outgoingRequestProcessor);
708+
rustCrypto["outgoingRequestsManager"] = outgoingRequestsManager;
709+
710+
// The second time we do a /keys/upload, the `device_keys` property
711+
// should be absent from the request body
712+
// cf. https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/issues/57
713+
//
714+
// On the first upload, we pretend that there are no OTKs, so it will
715+
// try to upload more keys
716+
let keysUploadCount = 0;
717+
let deviceKeys: object;
718+
let deviceKeysAbsent = false;
719+
outgoingRequestProcessor.makeOutgoingRequest = jest.fn(async (request, uiaCallback?) => {
720+
let resp: any = {};
721+
if (request instanceof RustSdkCryptoJs.KeysUploadRequest) {
722+
if (keysUploadCount == 0) {
723+
deviceKeys = JSON.parse(request.body).device_keys;
724+
resp = { one_time_key_counts: { signed_curve25519: 0 } };
725+
} else {
726+
deviceKeysAbsent = !("device_keys" in JSON.parse(request.body));
727+
resp = { one_time_key_counts: { signed_curve25519: 50 } };
728+
}
729+
keysUploadCount++;
730+
} else if (request instanceof RustSdkCryptoJs.KeysQueryRequest) {
731+
resp = {
732+
device_keys: {
733+
[TEST_USER]: {
734+
[TEST_DEVICE_ID]: deviceKeys,
735+
},
736+
},
737+
};
738+
} else if (request instanceof RustSdkCryptoJs.UploadSigningKeysRequest) {
739+
// SigningKeysUploadRequest does not implement OutgoingRequest and does not need to be marked as sent.
740+
return;
741+
}
742+
if (request.id) {
743+
olmMachine.markRequestAsSent(request.id, request.type, JSON.stringify(resp));
744+
}
745+
});
746+
await outgoingRequestsManager.doProcessOutgoingRequests();
747+
await outgoingRequestsManager.doProcessOutgoingRequests();
748+
749+
expect(deviceKeysAbsent).toBe(true);
750+
});
699751
});
700752

701753
describe(".getEventEncryptionInfo", () => {

0 commit comments

Comments
 (0)