Replace uses of checkDeviceTrust with getDeviceVerificationStatus (#10663)

matrix-org/matrix-js-sdk#3287 and matrix-org/matrix-js-sdk#3303 added a new API called getDeviceVerificationStatus. Let's use it.
This commit is contained in:
Richard van der Hoff 2023-04-24 14:19:46 +01:00 committed by GitHub
parent aa8c0f5cc7
commit d7bb8043ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 286 additions and 161 deletions

View file

@ -22,21 +22,25 @@ import DMRoomMap from "../../src/utils/DMRoomMap";
function mkClient(selfTrust = false) {
return {
getUserId: () => "@self:localhost",
getCrypto: () => ({
getDeviceVerificationStatus: (userId: string, deviceId: string) =>
Promise.resolve({
isVerified: () => (userId === "@self:localhost" ? selfTrust : userId[2] == "T"),
}),
}),
checkUserTrust: (userId: string) => ({
isCrossSigningVerified: () => userId[1] == "T",
wasCrossSigningVerified: () => userId[1] == "T" || userId[1] == "W",
}),
checkDeviceTrust: (userId: string, deviceId: string) => ({
isVerified: () => (userId === "@self:localhost" ? selfTrust : userId[2] == "T"),
}),
getStoredDevicesForUser: (userId: string) => ["DEVICE"],
} as unknown as MatrixClient;
}
describe("mkClient self-test", function () {
test.each([true, false])("behaves well for self-trust=%s", (v) => {
test.each([true, false])("behaves well for self-trust=%s", async (v) => {
const client = mkClient(v);
expect(client.checkDeviceTrust("@self:localhost", "DEVICE").isVerified()).toBe(v);
const status = await client.getCrypto()!.getDeviceVerificationStatus("@self:localhost", "DEVICE");
expect(status?.isVerified()).toBe(v);
});
test.each([
@ -53,8 +57,9 @@ describe("mkClient self-test", function () {
["@TF:h", false],
["@FT:h", true],
["@FF:h", false],
])("behaves well for device trust %s", (userId, trust) => {
expect(mkClient().checkDeviceTrust(userId, "device").isVerified()).toBe(trust);
])("behaves well for device trust %s", async (userId, trust) => {
const status = await mkClient().getCrypto()!.getDeviceVerificationStatus(userId, "device");
expect(status?.isVerified()).toBe(trust);
});
});

View file

@ -30,6 +30,7 @@ import {
GroupedArray,
concat,
asyncEvery,
asyncSome,
} from "../../src/utils/arrays";
type TestParams = { input: number[]; output: number[] };
@ -444,4 +445,27 @@ describe("arrays", () => {
expect(predicate).toHaveBeenCalledWith(2);
});
});
describe("asyncSome", () => {
it("when called with an empty array, it should return false", async () => {
expect(await asyncSome([], jest.fn().mockResolvedValue(true))).toBe(false);
});
it("when called with some items and the predicate resolves to false for all of them, it should return false", async () => {
const predicate = jest.fn().mockResolvedValue(false);
expect(await asyncSome([1, 2, 3], predicate)).toBe(false);
expect(predicate).toHaveBeenCalledTimes(3);
expect(predicate).toHaveBeenCalledWith(1);
expect(predicate).toHaveBeenCalledWith(2);
expect(predicate).toHaveBeenCalledWith(3);
});
it("when called with some items and the predicate resolves to true, it should short-circuit and return true", async () => {
const predicate = jest.fn().mockResolvedValueOnce(false).mockResolvedValueOnce(true);
expect(await asyncSome([1, 2, 3], predicate)).toBe(true);
expect(predicate).toHaveBeenCalledTimes(2);
expect(predicate).toHaveBeenCalledWith(1);
expect(predicate).toHaveBeenCalledWith(2);
});
});
});