From 160beb5be849bb87feb7212a0112e26b64135c09 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Wed, 13 Dec 2023 17:23:00 -0500 Subject: [PATCH] add tests and fix documentation --- src/verification.rs | 7 ++++--- tests/machine.test.ts | 45 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/verification.rs b/src/verification.rs index 156caac64..f3575bacd 100644 --- a/src/verification.rs +++ b/src/verification.rs @@ -288,7 +288,7 @@ impl Sas { /// Cancel the verification. /// - /// This cancels the verification with given code. + /// This cancels the verification with given code (e.g. `m.user`). /// /// Returns either an `OutgoingRequest` which should be sent out, or /// `undefined` if the verification is already cancelled. @@ -569,7 +569,7 @@ impl Qr { /// Cancel the verification. /// - /// This cancels the verification with given code. + /// This cancels the verification with given code (e.g. `m.user`). /// /// Returns either an `OutgoingRequest` which should be sent out, or /// `undefined` if the verification is already cancelled. @@ -617,7 +617,8 @@ impl CancelInfo { self.inner.reason().into() } - /// Get the `CancelCode` that cancelled this verification. + /// Get the `code` (e.g. `m.user`) that was used to cancel the + /// verification. #[wasm_bindgen(js_name = "cancelCode")] pub fn cancel_code(&self) -> String { self.inner.cancel_code().to_string() diff --git a/tests/machine.test.ts b/tests/machine.test.ts index dbd083724..751016226 100644 --- a/tests/machine.test.ts +++ b/tests/machine.test.ts @@ -934,7 +934,7 @@ describe(OlmMachine.name, () => { } }); - test("can start an in-room SAS verification", async () => { + test("can start and cancel an in-room SAS verification", async () => { let _ = m.bootstrapCrossSigning(true); const identity = await m.getIdentity(new UserId("@example:morpheus.localhost")); @@ -965,7 +965,7 @@ describe(OlmMachine.name, () => { expect(verificationRequest.roomId.toString()).toStrictEqual(room.toString()); - const [_sas, outgoingVerificationRequest] = await verificationRequest.startSas(); + const [sas, outgoingVerificationRequest] = await verificationRequest.startSas(); expect(outgoingVerificationRequest).toBeInstanceOf(RoomMessageRequest); expect(outgoingVerificationRequest.id).toBeDefined(); @@ -987,6 +987,47 @@ describe(OlmMachine.name, () => { event_id: eventId.toString(), }, }); + + const outgoingCancellationRequest = sas.cancelWithCode("org.matrix.custom"); + + const cancellationBody = JSON.parse(outgoingCancellationRequest.body); + expect(cancellationBody.code).toEqual("org.matrix.custom"); + + let cancelInfo = verificationRequest.cancelInfo; + expect(cancelInfo).toBeTruthy(); + expect(cancelInfo.cancelCode()).toEqual("org.matrix.custom"); + expect(cancelInfo.cancelledbyUs()).toBe(true); + }); + + test("can handle a cancelled in-room verification", async () => { + let _ = m.bootstrapCrossSigning(true); + const identity = await m.getIdentity(new UserId("@example:morpheus.localhost")); + + const eventId = new EventId("$qnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5ZRg"); + const verificationRequest = await identity.requestVerification(room, eventId); + + await m.receiveVerificationEvent( + JSON.stringify({ + sender: "@example:morpheus.localhost", + type: "m.key.verification.cancel", + event_id: "$gQWuamMe6taH7oaEX6DHnIrvn8kden7vt9a9e2xBzl0", + origin_server_ts: 1674037264827, + content: { + "reason": "Cancelled by user", + "code": "m.user", + "m.relates_to": { + rel_type: "m.reference", + event_id: eventId.toString(), + }, + }, + }), + room, + ); + + let cancelInfo = verificationRequest.cancelInfo; + expect(cancelInfo).toBeTruthy(); + expect(cancelInfo.cancelCode()).toEqual("m.user"); + expect(cancelInfo.cancelledbyUs()).toBe(false); }); });