Skip to content

Commit

Permalink
add tests and fix documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
uhoreg committed Dec 13, 2023
1 parent 6a58a51 commit 160beb5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
45 changes: 43 additions & 2 deletions tests/machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down Expand Up @@ -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();
Expand All @@ -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);
});
});

Expand Down

0 comments on commit 160beb5

Please sign in to comment.