Skip to content

Commit 41ab333

Browse files
author
Kerry
authored
test typescriptification - spec/unit/crypto/verification (#2673)
* renamed: spec/unit/crypto/verification/request.spec.js -> spec/unit/crypto/verification/request.spec.ts * renamed: spec/unit/crypto/verification/qr_code.spec.js -> spec/unit/crypto/verification/qr_code.spec.ts * renamed: spec/unit/crypto/verification/InRoomChannel.spec.js -> spec/unit/crypto/verification/InRoomChannel.spec.ts * fix ts issues in InRoomChannel.spec * renamed: spec/unit/crypto/verification/util.js -> spec/unit/crypto/verification/util.ts * fix ts issues in util.t * fix strict errors in util.ts * js lint
1 parent 1432e09 commit 41ab333

File tree

5 files changed

+41
-35
lines changed

5 files changed

+41
-35
lines changed

spec/unit/crypto/secrets.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ describe("Secrets", function() {
250250

251251
osborne2.client.crypto.deviceList.storeDevicesForUser("@alice:example.com", {
252252
"VAX": {
253-
user_id: "@alice:example.com",
254-
device_id: "VAX",
253+
verified: 0,
254+
known: false,
255255
algorithms: [olmlib.OLM_ALGORITHM, olmlib.MEGOLM_ALGORITHM],
256256
keys: {
257257
"ed25519:VAX": vaxDevice.deviceEd25519Key,
@@ -261,9 +261,9 @@ describe("Secrets", function() {
261261
});
262262
vax.client.crypto.deviceList.storeDevicesForUser("@alice:example.com", {
263263
"Osborne2": {
264-
user_id: "@alice:example.com",
265-
device_id: "Osborne2",
266264
algorithms: [olmlib.OLM_ALGORITHM, olmlib.MEGOLM_ALGORITHM],
265+
verified: 0,
266+
known: false,
267267
keys: {
268268
"ed25519:Osborne2": osborne2Device.deviceEd25519Key,
269269
"curve25519:Osborne2": osborne2Device.deviceCurve25519Key,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
import { MatrixClient } from "../../../../src/client";
1617
import { InRoomChannel } from "../../../../src/crypto/verification/request/InRoomChannel";
1718
import { MatrixEvent } from "../../../../src/models/event";
18-
"../../../../src/crypto/verification/request/ToDeviceChannel";
1919

2020
describe("InRoomChannel tests", function() {
2121
const ALICE = "@alice:hs.tld";
2222
const BOB = "@bob:hs.tld";
2323
const MALORY = "@malory:hs.tld";
2424
const client = {
2525
getUserId() { return ALICE; },
26-
};
26+
} as unknown as MatrixClient;
2727

2828
it("getEventType only returns .request for a message with a msgtype", function() {
2929
const invalidEvent = new MatrixEvent({

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
1717
import "../../../olm-loader";
18-
import { verificationMethods } from "../../../../src/crypto";
18+
import { CryptoEvent, verificationMethods } from "../../../../src/crypto";
1919
import { logger } from "../../../../src/logger";
2020
import { SAS } from "../../../../src/crypto/verification/SAS";
2121
import { makeTestClients, setupWebcrypto, teardownWebcrypto } from './util';
@@ -52,31 +52,30 @@ describe("verification request integration tests with crypto layer", function()
5252
alice.client.crypto.deviceList.getRawStoredDevicesForUser = function() {
5353
return {
5454
Dynabook: {
55+
algorithms: [],
56+
verified: 0,
57+
known: false,
5558
keys: {
5659
"ed25519:Dynabook": "bob+base64+ed25519+key",
5760
},
5861
},
5962
};
6063
};
61-
alice.client.downloadKeys = () => {
62-
return Promise.resolve();
63-
};
64-
bob.client.downloadKeys = () => {
65-
return Promise.resolve();
66-
};
67-
bob.client.on("crypto.verification.request", (request) => {
64+
alice.client.downloadKeys = jest.fn().mockResolvedValue({});
65+
bob.client.downloadKeys = jest.fn().mockResolvedValue({});
66+
bob.client.on(CryptoEvent.VerificationRequest, (request) => {
6867
const bobVerifier = request.beginKeyVerification(verificationMethods.SAS);
6968
bobVerifier.verify();
7069

71-
// XXX: Private function access (but it's a test, so we're okay)
70+
// @ts-ignore Private function access (but it's a test, so we're okay)
7271
bobVerifier.endTimer();
7372
});
7473
const aliceRequest = await alice.client.requestVerification("@bob:example.com");
7574
await aliceRequest.waitFor(r => r.started);
7675
const aliceVerifier = aliceRequest.verifier;
7776
expect(aliceVerifier).toBeInstanceOf(SAS);
7877

79-
// XXX: Private function access (but it's a test, so we're okay)
78+
// @ts-ignore Private function access (but it's a test, so we're okay)
8079
aliceVerifier.endTimer();
8180

8281
alice.stop();

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@ import nodeCrypto from "crypto";
1919

2020
import { TestClient } from '../../../TestClient';
2121
import { MatrixEvent } from "../../../../src/models/event";
22+
import { IRoomTimelineData } from "../../../../src/models/event-timeline-set";
23+
import { Room, RoomEvent } from "../../../../src/models/room";
2224
import { logger } from '../../../../src/logger';
25+
import { MatrixClient, ClientEvent } from '../../../../src/client';
2326

24-
export async function makeTestClients(userInfos, options) {
25-
const clients = [];
26-
const timeouts = [];
27-
const clientMap = {};
28-
const sendToDevice = function(type, map) {
27+
export async function makeTestClients(userInfos, options): Promise<[TestClient[], () => void]> {
28+
const clients: TestClient[] = [];
29+
const timeouts: ReturnType<typeof setTimeout>[] = [];
30+
const clientMap: Record<string, Record<string, MatrixClient>> = {};
31+
const makeSendToDevice = (matrixClient: MatrixClient): MatrixClient['sendToDevice'] => async (type, map) => {
2932
// logger.log(this.getUserId(), "sends", type, map);
3033
for (const [userId, devMap] of Object.entries(map)) {
3134
if (userId in clientMap) {
3235
for (const [deviceId, msg] of Object.entries(devMap)) {
3336
if (deviceId in clientMap[userId]) {
3437
const event = new MatrixEvent({
35-
sender: this.getUserId(), // eslint-disable-line @babel/no-invalid-this
38+
sender: matrixClient.getUserId()!,
3639
type: type,
3740
content: msg,
3841
});
@@ -42,18 +45,19 @@ export async function makeTestClients(userInfos, options) {
4245
Promise.resolve();
4346

4447
decryptionPromise.then(
45-
() => client.emit("toDeviceEvent", event),
48+
() => client.emit(ClientEvent.ToDeviceEvent, event),
4649
);
4750
}
4851
}
4952
}
5053
}
54+
return {};
5155
};
52-
const sendEvent = function(room, type, content) {
56+
const makeSendEvent = (matrixClient: MatrixClient) => (room, type, content) => {
5357
// make up a unique ID as the event ID
54-
const eventId = "$" + this.makeTxnId(); // eslint-disable-line @babel/no-invalid-this
58+
const eventId = "$" + matrixClient.makeTxnId();
5559
const rawEvent = {
56-
sender: this.getUserId(), // eslint-disable-line @babel/no-invalid-this
60+
sender: matrixClient.getUserId()!,
5761
type: type,
5862
content: content,
5963
room_id: room,
@@ -63,22 +67,24 @@ export async function makeTestClients(userInfos, options) {
6367
const event = new MatrixEvent(rawEvent);
6468
const remoteEcho = new MatrixEvent(Object.assign({}, rawEvent, {
6569
unsigned: {
66-
transaction_id: this.makeTxnId(), // eslint-disable-line @babel/no-invalid-this
70+
transaction_id: matrixClient.makeTxnId(),
6771
},
6872
}));
6973

7074
const timeout = setTimeout(() => {
7175
for (const tc of clients) {
72-
if (tc.client === this) { // eslint-disable-line @babel/no-invalid-this
76+
const room = new Room('test', tc.client, tc.client.getUserId()!);
77+
const roomTimelineData = {} as unknown as IRoomTimelineData;
78+
if (tc.client === matrixClient) {
7379
logger.log("sending remote echo!!");
74-
tc.client.emit("Room.timeline", remoteEcho);
80+
tc.client.emit(RoomEvent.Timeline, remoteEcho, room, false, false, roomTimelineData);
7581
} else {
76-
tc.client.emit("Room.timeline", event);
82+
tc.client.emit(RoomEvent.Timeline, event, room, false, false, roomTimelineData);
7783
}
7884
}
7985
});
8086

81-
timeouts.push(timeout);
87+
timeouts.push(timeout as unknown as ReturnType<typeof setTimeout>);
8288

8389
return Promise.resolve({ event_id: eventId });
8490
};
@@ -99,8 +105,8 @@ export async function makeTestClients(userInfos, options) {
99105
clientMap[userInfo.userId] = {};
100106
}
101107
clientMap[userInfo.userId][userInfo.deviceId] = testClient.client;
102-
testClient.client.sendToDevice = sendToDevice;
103-
testClient.client.sendEvent = sendEvent;
108+
testClient.client.sendToDevice = makeSendToDevice(testClient.client);
109+
testClient.client.sendEvent = makeSendEvent(testClient.client);
104110
clients.push(testClient);
105111
}
106112

@@ -116,11 +122,12 @@ export async function makeTestClients(userInfos, options) {
116122
export function setupWebcrypto() {
117123
global.crypto = {
118124
getRandomValues: (buf) => {
119-
return nodeCrypto.randomFillSync(buf);
125+
return nodeCrypto.randomFillSync(buf as any);
120126
},
121-
};
127+
} as unknown as Crypto;
122128
}
123129

124130
export function teardownWebcrypto() {
131+
// @ts-ignore undefined != Crypto
125132
global.crypto = undefined;
126133
}

0 commit comments

Comments
 (0)