Skip to content

Commit

Permalink
matrix-mock-request to 2.0.1 (#2416)
Browse files Browse the repository at this point in the history
* matrix-mock-request to 2.0.0

Signed-off-by: Kerry Archibald <kerrya@element.io>

* track and destroy timeouts from test client

Signed-off-by: Kerry Archibald <kerrya@element.io>

* remove debug

Signed-off-by: Kerry Archibald <kerrya@element.io>

* fix bad property refernce caught by ts TestClient

Signed-off-by: Kerry Archibald <kerrya@element.io>

* Revert "fix bad property refernce caught by ts TestClient"

This reverts commit 92c9f6c.

* update yarn lock

Signed-off-by: Kerry Archibald <kerrya@element.io>

* correct IUploadKeysRequest type

* fix types in TestClient for typed matrix-mock-request

Signed-off-by: Kerry Archibald <kerrya@element.io>

* update to matrix-mock-request 2.0.1

Signed-off-by: Kerry Archibald <kerrya@element.io>
  • Loading branch information
Kerry authored Jun 3, 2022
1 parent 012f6c5 commit 518e16e
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 217 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"jest-localstorage-mock": "^2.4.6",
"jest-sonar-reporter": "^2.0.0",
"jsdoc": "^3.6.6",
"matrix-mock-request": "^1.2.3",
"matrix-mock-request": "^2.0.1",
"rimraf": "^3.0.2",
"terser": "^5.5.1",
"tsify": "^5.0.2",
Expand Down
35 changes: 19 additions & 16 deletions spec/TestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import { MockStorageApi } from "./MockStorageApi";
import { encodeUri } from "../src/utils";
import { IDeviceKeys, IOneTimeKey } from "../src/crypto/dehydration";
import { IKeyBackupSession } from "../src/crypto/keybackup";
import { IHttpOpts } from "../src/http-api";
import { IKeysUploadResponse, IUploadKeysRequest } from '../src/client';

/**
* Wrapper for a MockStorageApi, MockHttpBackend and MatrixClient
Expand Down Expand Up @@ -61,7 +63,7 @@ export class TestClient {
accessToken: accessToken,
deviceId: deviceId,
sessionStore: sessionStore,
request: this.httpBackend.requestFn,
request: this.httpBackend.requestFn as IHttpOpts["request"],
...options,
};
if (!fullOptions.cryptoStore) {
Expand Down Expand Up @@ -109,26 +111,27 @@ export class TestClient {
* stop the client
* @return {Promise} Resolves once the mock http backend has finished all pending flushes
*/
public stop(): Promise<void> {
public async stop(): Promise<void> {
this.client.stopClient();
return this.httpBackend.stop();
await this.httpBackend.stop();
}

/**
* Set up expectations that the client will upload device keys.
*/
public expectDeviceKeyUpload() {
this.httpBackend.when("POST", "/keys/upload").respond(200, (path, content) => {
expect(content.one_time_keys).toBe(undefined);
expect(content.device_keys).toBeTruthy();
this.httpBackend.when("POST", "/keys/upload")
.respond<IKeysUploadResponse, IUploadKeysRequest>(200, (_path, content) => {
expect(content.one_time_keys).toBe(undefined);
expect(content.device_keys).toBeTruthy();

logger.log(this + ': received device keys');
// we expect this to happen before any one-time keys are uploaded.
expect(Object.keys(this.oneTimeKeys).length).toEqual(0);
logger.log(this + ': received device keys');
// we expect this to happen before any one-time keys are uploaded.
expect(Object.keys(this.oneTimeKeys).length).toEqual(0);

this.deviceKeys = content.device_keys;
return { one_time_key_counts: { signed_curve25519: 0 } };
});
this.deviceKeys = content.device_keys;
return { one_time_key_counts: { signed_curve25519: 0 } };
});
}

/**
Expand All @@ -145,7 +148,7 @@ export class TestClient {
}

this.httpBackend.when("POST", "/keys/upload")
.respond(200, (path, content) => {
.respond<IKeysUploadResponse, IUploadKeysRequest>(200, (_path, content: IUploadKeysRequest) => {
expect(content.device_keys).toBe(undefined);
expect(content.one_time_keys).toBe(undefined);
return { one_time_key_counts: {
Expand All @@ -154,7 +157,7 @@ export class TestClient {
});

this.httpBackend.when("POST", "/keys/upload")
.respond(200, (path, content) => {
.respond<IKeysUploadResponse, IUploadKeysRequest>(200, (_path, content: IUploadKeysRequest) => {
expect(content.device_keys).toBe(undefined);
expect(content.one_time_keys).toBeTruthy();
expect(content.one_time_keys).not.toEqual({});
Expand All @@ -181,8 +184,8 @@ export class TestClient {
* @param {Object} response response to the query.
*/
public expectKeyQuery(response: IDownloadKeyResult) {
this.httpBackend.when('POST', '/keys/query').respond(
200, (path, content) => {
this.httpBackend.when('POST', '/keys/query').respond<IDownloadKeyResult>(
200, (_path, content) => {
Object.keys(response.device_keys).forEach((userId) => {
expect(content.device_keys[userId]).toEqual([]);
});
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/crypto/secrets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe("Secrets", function() {
});

it("should request secrets from other clients", async function() {
const [osborne2, vax] = await makeTestClients(
const [[osborne2, vax], clearTestClientTimeouts] = await makeTestClients(
[
{ userId: "@alice:example.com", deviceId: "Osborne2" },
{ userId: "@alice:example.com", deviceId: "VAX" },
Expand Down Expand Up @@ -280,6 +280,7 @@ describe("Secrets", function() {
expect(secret).toBe("bar");
osborne2.stop();
vax.stop();
clearTestClientTimeouts();
});

describe("bootstrap", function() {
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/crypto/verification/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("verification request integration tests with crypto layer", function()
});

it("should request and accept a verification", async function() {
const [alice, bob] = await makeTestClients(
const [[alice, bob], clearTestClientTimeouts] = await makeTestClients(
[
{ userId: "@alice:example.com", deviceId: "Osborne2" },
{ userId: "@bob:example.com", deviceId: "Dynabook" },
Expand Down Expand Up @@ -81,5 +81,6 @@ describe("verification request integration tests with crypto layer", function()

alice.stop();
bob.stop();
clearTestClientTimeouts();
});
});
13 changes: 10 additions & 3 deletions spec/unit/crypto/verification/sas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ describe("SAS verification", function() {
let bobSasEvent;
let aliceVerifier;
let bobPromise;
let clearTestClientTimeouts;

beforeEach(async () => {
[alice, bob] = await makeTestClients(
[[alice, bob], clearTestClientTimeouts] = await makeTestClients(
[
{ userId: "@alice:example.com", deviceId: "Osborne2" },
{ userId: "@bob:example.com", deviceId: "Dynabook" },
Expand Down Expand Up @@ -178,6 +179,8 @@ describe("SAS verification", function() {
alice.stop(),
bob.stop(),
]);

clearTestClientTimeouts();
});

it("should verify a key", async () => {
Expand Down Expand Up @@ -334,7 +337,7 @@ describe("SAS verification", function() {
});

it("should send a cancellation message on error", async function() {
const [alice, bob] = await makeTestClients(
const [[alice, bob], clearTestClientTimeouts] = await makeTestClients(
[
{ userId: "@alice:example.com", deviceId: "Osborne2" },
{ userId: "@bob:example.com", deviceId: "Dynabook" },
Expand Down Expand Up @@ -380,6 +383,7 @@ describe("SAS verification", function() {

alice.stop();
bob.stop();
clearTestClientTimeouts();
});

describe("verification in DM", function() {
Expand All @@ -389,9 +393,10 @@ describe("SAS verification", function() {
let bobSasEvent;
let aliceVerifier;
let bobPromise;
let clearTestClientTimeouts;

beforeEach(async function() {
[alice, bob] = await makeTestClients(
[[alice, bob], clearTestClientTimeouts] = await makeTestClients(
[
{ userId: "@alice:example.com", deviceId: "Osborne2" },
{ userId: "@bob:example.com", deviceId: "Dynabook" },
Expand Down Expand Up @@ -491,6 +496,8 @@ describe("SAS verification", function() {
alice.stop(),
bob.stop(),
]);

clearTestClientTimeouts();
});

it("should verify a key", async function() {
Expand Down
11 changes: 9 additions & 2 deletions spec/unit/crypto/verification/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { logger } from '../../../../src/logger';

export async function makeTestClients(userInfos, options) {
const clients = [];
const timeouts = [];
const clientMap = {};
const sendToDevice = function(type, map) {
// logger.log(this.getUserId(), "sends", type, map);
Expand Down Expand Up @@ -66,7 +67,7 @@ export async function makeTestClients(userInfos, options) {
},
}));

setImmediate(() => {
const timeout = setTimeout(() => {
for (const tc of clients) {
if (tc.client === this) { // eslint-disable-line @babel/no-invalid-this
logger.log("sending remote echo!!");
Expand All @@ -77,6 +78,8 @@ export async function makeTestClients(userInfos, options) {
}
});

timeouts.push(timeout);

return Promise.resolve({ event_id: eventId });
};

Expand All @@ -103,7 +106,11 @@ export async function makeTestClients(userInfos, options) {

await Promise.all(clients.map((testClient) => testClient.client.initCrypto()));

return clients;
const destroy = () => {
timeouts.forEach((t) => clearTimeout(t));
};

return [clients, destroy];
}

export function setupWebcrypto() {
Expand Down
8 changes: 2 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,9 @@ export interface IRequestMsisdnTokenResponse extends IRequestTokenResponse {
intl_fmt: string;
}

interface IUploadKeysRequest {
export interface IUploadKeysRequest {
device_keys?: Required<IDeviceKeys>;
one_time_keys?: {
[userId: string]: {
[deviceId: string]: number;
};
};
one_time_keys?: Record<string, IOneTimeKey>;
"org.matrix.msc2732.fallback_keys"?: Record<string, IOneTimeKey>;
}

Expand Down
Loading

0 comments on commit 518e16e

Please sign in to comment.