Skip to content

Commit 4a42418

Browse files
author
Kerry
authored
test typescriptification - autodiscovery / crypto specs (#2550)
* spec/unit/autodiscovery.spec.js -> spec/unit/autodiscovery.spec.ts * fix ts in autodiscovery.spec * renamed: spec/unit/crypto.spec.js -> spec/unit/crypto.spec.ts * fix ts in crypto.spec * fix some strict errors
1 parent 3824f65 commit 4a42418

File tree

4 files changed

+151
-112
lines changed

4 files changed

+151
-112
lines changed

spec/unit/autodiscovery.spec.js renamed to spec/unit/autodiscovery.spec.ts

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Copyright 2018 New Vector Ltd
3-
Copyright 2019 The Matrix.org Foundation C.I.C.
3+
Copyright 2019, 2022 The Matrix.org Foundation C.I.C.
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -17,19 +17,20 @@ limitations under the License.
1717

1818
import MockHttpBackend from "matrix-mock-request";
1919

20-
import * as sdk from "../../src";
20+
import { request } from "../../src/matrix";
2121
import { AutoDiscovery } from "../../src/autodiscovery";
2222

2323
describe("AutoDiscovery", function() {
24-
let httpBackend = null;
25-
26-
beforeEach(function() {
27-
httpBackend = new MockHttpBackend();
28-
sdk.request(httpBackend.requestFn);
29-
});
24+
const getHttpBackend = (): MockHttpBackend => {
25+
const httpBackend = new MockHttpBackend();
26+
request(httpBackend.requestFn);
27+
return httpBackend;
28+
};
3029

3130
it("should throw an error when no domain is specified", function() {
31+
getHttpBackend();
3232
return Promise.all([
33+
// @ts-ignore testing no args
3334
AutoDiscovery.findClientConfig(/* no args */).then(() => {
3435
throw new Error("Expected a failure, not success with no args");
3536
}, () => {
@@ -42,13 +43,13 @@ describe("AutoDiscovery", function() {
4243
return true;
4344
}),
4445

45-
AutoDiscovery.findClientConfig(null).then(() => {
46+
AutoDiscovery.findClientConfig(null as any).then(() => {
4647
throw new Error("Expected a failure, not success with null");
4748
}, () => {
4849
return true;
4950
}),
5051

51-
AutoDiscovery.findClientConfig(true).then(() => {
52+
AutoDiscovery.findClientConfig(true as any).then(() => {
5253
throw new Error("Expected a failure, not success with a non-string");
5354
}, () => {
5455
return true;
@@ -57,6 +58,7 @@ describe("AutoDiscovery", function() {
5758
});
5859

5960
it("should return PROMPT when .well-known 404s", function() {
61+
const httpBackend = getHttpBackend();
6062
httpBackend.when("GET", "/.well-known/matrix/client").respond(404, {});
6163
return Promise.all([
6264
httpBackend.flushAllExpected(),
@@ -80,6 +82,7 @@ describe("AutoDiscovery", function() {
8082
});
8183

8284
it("should return FAIL_PROMPT when .well-known returns a 500 error", function() {
85+
const httpBackend = getHttpBackend();
8386
httpBackend.when("GET", "/.well-known/matrix/client").respond(500, {});
8487
return Promise.all([
8588
httpBackend.flushAllExpected(),
@@ -103,6 +106,7 @@ describe("AutoDiscovery", function() {
103106
});
104107

105108
it("should return FAIL_PROMPT when .well-known returns a 400 error", function() {
109+
const httpBackend = getHttpBackend();
106110
httpBackend.when("GET", "/.well-known/matrix/client").respond(400, {});
107111
return Promise.all([
108112
httpBackend.flushAllExpected(),
@@ -126,6 +130,7 @@ describe("AutoDiscovery", function() {
126130
});
127131

128132
it("should return FAIL_PROMPT when .well-known returns an empty body", function() {
133+
const httpBackend = getHttpBackend();
129134
httpBackend.when("GET", "/.well-known/matrix/client").respond(200, "");
130135
return Promise.all([
131136
httpBackend.flushAllExpected(),
@@ -149,6 +154,7 @@ describe("AutoDiscovery", function() {
149154
});
150155

151156
it("should return FAIL_PROMPT when .well-known returns not-JSON", function() {
157+
const httpBackend = getHttpBackend();
152158
httpBackend.when("GET", "/.well-known/matrix/client").respond(200, "abc");
153159
return Promise.all([
154160
httpBackend.flushAllExpected(),
@@ -173,6 +179,7 @@ describe("AutoDiscovery", function() {
173179

174180
it("should return FAIL_PROMPT when .well-known does not have a base_url for " +
175181
"m.homeserver (empty string)", function() {
182+
const httpBackend = getHttpBackend();
176183
httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
177184
"m.homeserver": {
178185
base_url: "",
@@ -201,6 +208,7 @@ describe("AutoDiscovery", function() {
201208

202209
it("should return FAIL_PROMPT when .well-known does not have a base_url for " +
203210
"m.homeserver (no property)", function() {
211+
const httpBackend = getHttpBackend();
204212
httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
205213
"m.homeserver": {},
206214
});
@@ -227,6 +235,7 @@ describe("AutoDiscovery", function() {
227235

228236
it("should return FAIL_ERROR when .well-known has an invalid base_url for " +
229237
"m.homeserver (disallowed scheme)", function() {
238+
const httpBackend = getHttpBackend();
230239
httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
231240
"m.homeserver": {
232241
base_url: "mxc://example.org",
@@ -255,6 +264,7 @@ describe("AutoDiscovery", function() {
255264

256265
it("should return FAIL_ERROR when .well-known has an invalid base_url for " +
257266
"m.homeserver (verification failure: 404)", function() {
267+
const httpBackend = getHttpBackend();
258268
httpBackend.when("GET", "/_matrix/client/versions").respond(404, {});
259269
httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
260270
"m.homeserver": {
@@ -284,6 +294,7 @@ describe("AutoDiscovery", function() {
284294

285295
it("should return FAIL_ERROR when .well-known has an invalid base_url for " +
286296
"m.homeserver (verification failure: 500)", function() {
297+
const httpBackend = getHttpBackend();
287298
httpBackend.when("GET", "/_matrix/client/versions").respond(500, {});
288299
httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
289300
"m.homeserver": {
@@ -313,6 +324,7 @@ describe("AutoDiscovery", function() {
313324

314325
it("should return FAIL_ERROR when .well-known has an invalid base_url for " +
315326
"m.homeserver (verification failure: 200 but wrong content)", function() {
327+
const httpBackend = getHttpBackend();
316328
httpBackend.when("GET", "/_matrix/client/versions").respond(200, {
317329
not_matrix_versions: ["r0.0.1"],
318330
});
@@ -344,8 +356,9 @@ describe("AutoDiscovery", function() {
344356

345357
it("should return SUCCESS when .well-known has a verifiably accurate base_url for " +
346358
"m.homeserver", function() {
359+
const httpBackend = getHttpBackend();
347360
httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
348-
expect(req.opts.uri).toEqual("https://example.org/_matrix/client/versions");
361+
expect(req.path).toEqual("https://example.org/_matrix/client/versions");
349362
}).respond(200, {
350363
versions: ["r0.0.1"],
351364
});
@@ -376,8 +389,9 @@ describe("AutoDiscovery", function() {
376389
});
377390

378391
it("should return SUCCESS with the right homeserver URL", function() {
392+
const httpBackend = getHttpBackend();
379393
httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
380-
expect(req.opts.uri)
394+
expect(req.path)
381395
.toEqual("https://chat.example.org/_matrix/client/versions");
382396
}).respond(200, {
383397
versions: ["r0.0.1"],
@@ -411,8 +425,9 @@ describe("AutoDiscovery", function() {
411425

412426
it("should return SUCCESS / FAIL_PROMPT when the identity server configuration " +
413427
"is wrong (missing base_url)", function() {
428+
const httpBackend = getHttpBackend();
414429
httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
415-
expect(req.opts.uri)
430+
expect(req.path)
416431
.toEqual("https://chat.example.org/_matrix/client/versions");
417432
}).respond(200, {
418433
versions: ["r0.0.1"],
@@ -451,8 +466,9 @@ describe("AutoDiscovery", function() {
451466

452467
it("should return SUCCESS / FAIL_PROMPT when the identity server configuration " +
453468
"is wrong (empty base_url)", function() {
469+
const httpBackend = getHttpBackend();
454470
httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
455-
expect(req.opts.uri)
471+
expect(req.path)
456472
.toEqual("https://chat.example.org/_matrix/client/versions");
457473
}).respond(200, {
458474
versions: ["r0.0.1"],
@@ -491,8 +507,9 @@ describe("AutoDiscovery", function() {
491507

492508
it("should return SUCCESS / FAIL_PROMPT when the identity server configuration " +
493509
"is wrong (validation error: 404)", function() {
510+
const httpBackend = getHttpBackend();
494511
httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
495-
expect(req.opts.uri)
512+
expect(req.path)
496513
.toEqual("https://chat.example.org/_matrix/client/versions");
497514
}).respond(200, {
498515
versions: ["r0.0.1"],
@@ -532,8 +549,9 @@ describe("AutoDiscovery", function() {
532549

533550
it("should return SUCCESS / FAIL_PROMPT when the identity server configuration " +
534551
"is wrong (validation error: 500)", function() {
552+
const httpBackend = getHttpBackend();
535553
httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
536-
expect(req.opts.uri)
554+
expect(req.path)
537555
.toEqual("https://chat.example.org/_matrix/client/versions");
538556
}).respond(200, {
539557
versions: ["r0.0.1"],
@@ -573,14 +591,15 @@ describe("AutoDiscovery", function() {
573591

574592
it("should return SUCCESS when the identity server configuration is " +
575593
"verifiably accurate", function() {
594+
const httpBackend = getHttpBackend();
576595
httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
577-
expect(req.opts.uri)
596+
expect(req.path)
578597
.toEqual("https://chat.example.org/_matrix/client/versions");
579598
}).respond(200, {
580599
versions: ["r0.0.1"],
581600
});
582601
httpBackend.when("GET", "/_matrix/identity/api/v1").check((req) => {
583-
expect(req.opts.uri)
602+
expect(req.path)
584603
.toEqual("https://identity.example.org/_matrix/identity/api/v1");
585604
}).respond(200, {});
586605
httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
@@ -615,14 +634,15 @@ describe("AutoDiscovery", function() {
615634

616635
it("should return SUCCESS and preserve non-standard keys from the " +
617636
".well-known response", function() {
637+
const httpBackend = getHttpBackend();
618638
httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
619-
expect(req.opts.uri)
639+
expect(req.path)
620640
.toEqual("https://chat.example.org/_matrix/client/versions");
621641
}).respond(200, {
622642
versions: ["r0.0.1"],
623643
});
624644
httpBackend.when("GET", "/_matrix/identity/api/v1").check((req) => {
625-
expect(req.opts.uri)
645+
expect(req.path)
626646
.toEqual("https://identity.example.org/_matrix/identity/api/v1");
627647
}).respond(200, {});
628648
httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {

spec/unit/content-repo.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("ContentRepo", function() {
2727
});
2828

2929
it("should return the empty string for null input", function() {
30-
expect(getHttpUriForMxc(null, null)).toEqual("");
30+
expect(getHttpUriForMxc(null as any, '')).toEqual("");
3131
});
3232

3333
it("should return a thumbnail URL if a width/height/resize is specified",

spec/unit/crypto.spec.js renamed to spec/unit/crypto.spec.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function awaitEvent(emitter, event) {
2525
});
2626
}
2727

28-
async function keyshareEventForEvent(client, event, index) {
28+
async function keyshareEventForEvent(client, event, index): Promise<MatrixEvent> {
2929
const roomId = event.getRoomId();
3030
const eventContent = event.getWireContent();
3131
const key = await client.crypto.olmDevice.getInboundGroupSessionKey(
@@ -50,6 +50,7 @@ async function keyshareEventForEvent(client, event, index) {
5050
},
5151
});
5252
// make onRoomKeyEvent think this was an encrypted event
53+
// @ts-ignore private property
5354
ksEvent.senderCurve25519Key = "akey";
5455
return ksEvent;
5556
}
@@ -79,7 +80,7 @@ describe("Crypto", function() {
7980
getId: () => "$event_id",
8081
getSenderKey: () => null,
8182
getWireContent: () => {return {};},
82-
};
83+
} as unknown as MatrixEvent;
8384

8485
let encryptionInfo = client.getEventEncryptionInfo(event);
8586
expect(encryptionInfo.encrypted).toBeFalsy();
@@ -154,20 +155,23 @@ describe("Crypto", function() {
154155
beforeEach(async function() {
155156
const mockStorage = new MockStorageApi();
156157
const clientStore = new MemoryStore({ localStorage: mockStorage });
157-
const cryptoStore = new MemoryCryptoStore(mockStorage);
158+
const cryptoStore = new MemoryCryptoStore();
158159

159160
cryptoStore.storeEndToEndDeviceData({
160161
devices: {
161162
'@bob:home.server': {
162163
'BOBDEVICE': {
164+
algorithms: [],
165+
verified: 1,
166+
known: false,
163167
keys: {
164168
'curve25519:BOBDEVICE': 'this is a key',
165169
},
166170
},
167171
},
168172
},
169173
trackingStatus: {},
170-
});
174+
}, {});
171175

172176
mockBaseApis = {
173177
sendToDevice: jest.fn(),
@@ -185,6 +189,7 @@ describe("Crypto", function() {
185189
clientStore,
186190
cryptoStore,
187191
mockRoomList,
192+
[],
188193
);
189194
crypto.registerEventHandlers(fakeEmitter);
190195
await crypto.init();
@@ -195,7 +200,7 @@ describe("Crypto", function() {
195200
});
196201

197202
it("restarts wedged Olm sessions", async function() {
198-
const prom = new Promise((resolve) => {
203+
const prom = new Promise<void>((resolve) => {
199204
mockBaseApis.claimOneTimeKeys = function() {
200205
resolve();
201206
return otkResponse;
@@ -276,8 +281,12 @@ describe("Crypto", function() {
276281
// alice encrypts each event, and then bob tries to decrypt
277282
// them without any keys, so that they'll be in pending
278283
await aliceClient.crypto.encryptEvent(event, aliceRoom);
284+
// remove keys from the event
285+
// @ts-ignore private properties
279286
event.clearEvent = undefined;
287+
// @ts-ignore private properties
280288
event.senderCurve25519Key = null;
289+
// @ts-ignore private properties
281290
event.claimedEd25519Key = null;
282291
try {
283292
await bobClient.crypto.decryptEvent(event);
@@ -291,7 +300,7 @@ describe("Crypto", function() {
291300
roomId, olmlib.MEGOLM_ALGORITHM,
292301
);
293302

294-
let eventPromise = Promise.all(events.map((ev) => {
303+
const decryptEventsPromise = Promise.all(events.map((ev) => {
295304
return awaitEvent(ev, "Event.decrypted");
296305
}));
297306

@@ -300,7 +309,7 @@ describe("Crypto", function() {
300309
// can
301310
let ksEvent = await keyshareEventForEvent(aliceClient, events[1], 1);
302311
await bobDecryptor.onRoomKeyEvent(ksEvent);
303-
await eventPromise;
312+
await decryptEventsPromise;
304313
expect(events[0].getContent().msgtype).toBe("m.bad.encrypted");
305314
expect(events[1].getContent().msgtype).not.toBe("m.bad.encrypted");
306315

@@ -320,10 +329,10 @@ describe("Crypto", function() {
320329

321330
// keyshare the session key starting at the first message, so
322331
// that it can now be decrypted
323-
eventPromise = awaitEvent(events[0], "Event.decrypted");
332+
const decryptEventPromise = awaitEvent(events[0], "Event.decrypted");
324333
ksEvent = await keyshareEventForEvent(aliceClient, events[0], 0);
325334
await bobDecryptor.onRoomKeyEvent(ksEvent);
326-
await eventPromise;
335+
await decryptEventPromise;
327336
expect(events[0].getContent().msgtype).not.toBe("m.bad.encrypted");
328337
await sleep(1);
329338
// the room key request should be gone since we've now decrypted everything
@@ -354,8 +363,12 @@ describe("Crypto", function() {
354363
// alice encrypts each event, and then bob tries to decrypt
355364
// them without any keys, so that they'll be in pending
356365
await aliceClient.crypto.encryptEvent(event, aliceRoom);
366+
// remove keys from the event
367+
// @ts-ignore private property
357368
event.clearEvent = undefined;
369+
// @ts-ignore private property
358370
event.senderCurve25519Key = null;
371+
// @ts-ignore private property
359372
event.claimedEd25519Key = null;
360373
try {
361374
await bobClient.crypto.decryptEvent(event);
@@ -451,7 +464,7 @@ describe("Crypto", function() {
451464
await client.initCrypto();
452465
client.crypto.getSecretStorageKey = async () => null;
453466
client.crypto.isCrossSigningReady = async () => false;
454-
client.crypto.baseApis.uploadDeviceSigningKeys = () => null;
467+
client.crypto.baseApis.uploadDeviceSigningKeys = jest.fn().mockResolvedValue(null);
455468
client.crypto.baseApis.setAccountData = () => null;
456469
client.crypto.baseApis.uploadKeySignatures = () => null;
457470
client.crypto.baseApis.http.authedRequest = () => null;

0 commit comments

Comments
 (0)