Skip to content

Commit

Permalink
review and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed Dec 6, 2023
1 parent 924443f commit b3620f8
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions tests/machine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1151,23 +1151,22 @@ describe(OlmMachine.name, () => {

describe("Request missing secrets", () => {
/**
*
* Creates an hypothetical response to a key query request for an account with a pre-existing device and identity.
*
* To be used in test when you want to create a setup where there is an existing device
* on the account with cross-signing setup.
* To be used in tests when you want to create a setup where there is an existing device
* on the account with cross-signing set up.
*
* This will create a valid response to a key query request with all needed signatures.
*
* @param {UserId} user_id - The user id
* @param {DeviceId} new_device - The device id
* @param {UserId} userId - The user id
* @param {DeviceId} deviceId - The device id
*
* @returns A valid response to a key query request that can be feed in a second login for that account.
*/
async function getKeyQueryResponseWithExistingDevice(user_id, device_id) {
let initialMachine = await OlmMachine.initialize(user_id, device_id);
const userId = initialMachine.userId.toString();
const deviceId = initialMachine.deviceId.toString();
async function getKeyQueryResponseWithExistingDevice(userId, deviceId) {
let initialMachine = await OlmMachine.initialize(userId, deviceId);
const userIdStr = initialMachine.userId.toString();
const deviceIdStr = initialMachine.deviceId.toString();

let deviceKeys;
let outgoingRequests = await initialMachine.outgoingRequests();
Expand All @@ -1185,38 +1184,38 @@ describe(OlmMachine.name, () => {
const newSignature = JSON.parse(bootstrapRequest.uploadSignaturesRequest.body);

const allSignatures = {
[userId]: {
[userIdStr]: {
...deviceKeys.device_keys.signatures[userId],
...newSignature[userId][deviceId].signatures[userId],
...newSignature[userIdStr][deviceIdStr].signatures[userId],
},
};

deviceKeys.device_keys.signatures = allSignatures;

return {
device_keys: {
[userId]: {
[deviceId]: deviceKeys,
[userIdStr]: {
[deviceIdStr]: deviceKeys,
},
},
...crossSigning,
};
}

/**
*
* This function is designed to work with `getKeyQueryResponseWithExistingDevice` to simulate a scenario where
* an existing device (and crossSigning identiy), is already associated with the account.
* an existing device (and cross-signing identity), is already associated with the account.
*
* It will create a new login and process a hypothetical response that includes the existing identity and devices.
* @param {UserId} user_id - the user id of the account
* @param {DeviceId} device_id - the id of the new device
* @param {*} hypothetical_response - the response to the key query request generated by `getKeyQueryResponseWithExistingDevice`
*
* @param {UserId} userId - the user id of the account
* @param {DeviceId} deviceId - the id of the new device
* @param {*} hypotheticalResponse - the response to the key query request generated by `getKeyQueryResponseWithExistingDevice`
*
* @returns an olm machine with the new device and the hypothetical response processed.
*/
async function getSecondMachine(user_id, device_id, hypothetical_response) {
const secondMachine = await OlmMachine.initialize(user_id, device_id);
async function getSecondMachine(userId, deviceId, hypotheticalResponse) {
const secondMachine = await OlmMachine.initialize(userId, deviceId);

const toDeviceEvents = JSON.stringify([]);
const changedDevices = new DeviceLists();
Expand All @@ -1237,20 +1236,20 @@ describe(OlmMachine.name, () => {
await secondMachine.markRequestAsSent(
request.id,
RequestType.KeysQuery,
JSON.stringify(hypothetical_response),
JSON.stringify(hypotheticalResponse),
);

return secondMachine;
}

test("Should request cross signing-keys if missing", async () => {
test("Should request cross-signing keys if missing", async () => {
const userId = new UserId("@alice:example.org");
const firstDevice = new DeviceId("ABCDEF");
const hypothetical_response = await getKeyQueryResponseWithExistingDevice(userId, firstDevice);
const hypotheticalResponse = await getKeyQueryResponseWithExistingDevice(userId, firstDevice);

const secondDeviceId = new DeviceId("GHIJKL");

const secondMachine = await getSecondMachine(userId, secondDeviceId, hypothetical_response);
const secondMachine = await getSecondMachine(userId, secondDeviceId, hypotheticalResponse);
const hasMissingSecrets = await secondMachine.requestMissingSecretsIfNeeded();

expect(hasMissingSecrets).toStrictEqual(true);
Expand Down Expand Up @@ -1284,11 +1283,11 @@ describe(OlmMachine.name, () => {
test("Should not request if there are requests already in flight", async () => {
const userId = new UserId("@alice:example.org");
const firstDevice = new DeviceId("ABCDEF");
const hypothetical_response = await getKeyQueryResponseWithExistingDevice(userId, firstDevice);
const hypotheticalResponse = await getKeyQueryResponseWithExistingDevice(userId, firstDevice);

const secondDeviceId = new DeviceId("GHIJKL");

const secondMachine = await getSecondMachine(userId, secondDeviceId, hypothetical_response);
const secondMachine = await getSecondMachine(userId, secondDeviceId, hypotheticalResponse);

const hasMissingSecrets = await secondMachine.requestMissingSecretsIfNeeded();
expect(hasMissingSecrets).toStrictEqual(true);
Expand Down

0 comments on commit b3620f8

Please sign in to comment.