@peers-app/peers-sdk 0.27.3 → 0.27.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/contracts/contract-proxy.d.ts +7 -0
- package/dist/contracts/contract-proxy.js +19 -2
- package/dist/contracts/create-local-consumer.js +16 -1
- package/dist/contracts/index.d.ts +2 -0
- package/dist/contracts/index.js +2 -0
- package/dist/contracts/system/index.d.ts +2 -0
- package/dist/contracts/system/index.js +2 -0
- package/dist/contracts/system/invites-contract.js +2 -1
- package/dist/contracts/system/sync-check-contract.d.ts +97 -0
- package/dist/contracts/system/sync-check-contract.js +115 -0
- package/dist/contracts/tool-arguments.d.ts +15 -0
- package/dist/contracts/tool-arguments.js +41 -0
- package/dist/contracts/tool-catalog-service.js +2 -12
- package/dist/data/change-tracking.d.ts +40 -0
- package/dist/data/change-tracking.js +89 -0
- package/dist/data/orm/types.d.ts +24 -7
- package/dist/data/orm/types.js +97 -24
- package/dist/define/manifest.js +6 -34
- package/dist/package-loader/package-loader.js +27 -22
- package/dist/system-ids.d.ts +5 -0
- package/dist/system-ids.js +6 -1
- package/package.json +4 -2
- package/dist/context/user-context-identity.test.d.ts +0 -1
- package/dist/context/user-context-identity.test.js +0 -220
- package/dist/contracts/__tests__/action-dispatch.test.d.ts +0 -1
- package/dist/contracts/__tests__/action-dispatch.test.js +0 -319
- package/dist/contracts/__tests__/also-implements.test.d.ts +0 -1
- package/dist/contracts/__tests__/also-implements.test.js +0 -110
- package/dist/contracts/__tests__/builder.test.d.ts +0 -1
- package/dist/contracts/__tests__/builder.test.js +0 -669
- package/dist/contracts/__tests__/contract-events.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-events.test.js +0 -199
- package/dist/contracts/__tests__/contract-observables.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-observables.test.js +0 -290
- package/dist/contracts/__tests__/contract-provider-router.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-provider-router.test.js +0 -315
- package/dist/contracts/__tests__/contract-proxy.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-proxy.test.js +0 -657
- package/dist/contracts/__tests__/contract-resolvers.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-resolvers.test.js +0 -758
- package/dist/contracts/__tests__/contract-subscription-lifecycle.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-subscription-lifecycle.test.js +0 -353
- package/dist/contracts/__tests__/contract-table-events.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-table-events.test.js +0 -209
- package/dist/contracts/__tests__/contract-transport.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-transport.test.js +0 -163
- package/dist/contracts/__tests__/extract.test.d.ts +0 -1
- package/dist/contracts/__tests__/extract.test.js +0 -145
- package/dist/contracts/__tests__/frozen-contracts.test.d.ts +0 -1
- package/dist/contracts/__tests__/frozen-contracts.test.js +0 -191
- package/dist/contracts/__tests__/generic-tool-dispatch.test.d.ts +0 -1
- package/dist/contracts/__tests__/generic-tool-dispatch.test.js +0 -130
- package/dist/contracts/__tests__/host-contract-client.test.d.ts +0 -1
- package/dist/contracts/__tests__/host-contract-client.test.js +0 -793
- package/dist/contracts/__tests__/host-contract-consumer.test.d.ts +0 -1
- package/dist/contracts/__tests__/host-contract-consumer.test.js +0 -163
- package/dist/contracts/__tests__/host-contract-transport.test.d.ts +0 -1
- package/dist/contracts/__tests__/host-contract-transport.test.js +0 -66
- package/dist/contracts/__tests__/integration.test.d.ts +0 -1
- package/dist/contracts/__tests__/integration.test.js +0 -348
- package/dist/contracts/__tests__/legacy-removal.test.d.ts +0 -1
- package/dist/contracts/__tests__/legacy-removal.test.js +0 -128
- package/dist/contracts/__tests__/persistent-registry.test.d.ts +0 -1
- package/dist/contracts/__tests__/persistent-registry.test.js +0 -109
- package/dist/contracts/__tests__/playground.test.d.ts +0 -1
- package/dist/contracts/__tests__/playground.test.js +0 -216
- package/dist/contracts/__tests__/registry.test.d.ts +0 -1
- package/dist/contracts/__tests__/registry.test.js +0 -238
- package/dist/contracts/__tests__/remote-contract-transport.test.d.ts +0 -1
- package/dist/contracts/__tests__/remote-contract-transport.test.js +0 -119
- package/dist/contracts/__tests__/system-contracts.test.d.ts +0 -1
- package/dist/contracts/__tests__/system-contracts.test.js +0 -247
- package/dist/contracts/__tests__/target-dispatch.d.ts +0 -11
- package/dist/contracts/__tests__/target-dispatch.js +0 -24
- package/dist/contracts/__tests__/tool-catalog-inventory.test.d.ts +0 -1
- package/dist/contracts/__tests__/tool-catalog-inventory.test.js +0 -44
- package/dist/contracts/__tests__/tool-catalog-service.test.d.ts +0 -1
- package/dist/contracts/__tests__/tool-catalog-service.test.js +0 -482
- package/dist/contracts/__tests__/tool-catalog.test.d.ts +0 -1
- package/dist/contracts/__tests__/tool-catalog.test.js +0 -1199
- package/dist/contracts/__tests__/validate.test.d.ts +0 -1
- package/dist/contracts/__tests__/validate.test.js +0 -991
- package/dist/contracts/__tests__/workflow-context.test.d.ts +0 -1
- package/dist/contracts/__tests__/workflow-context.test.js +0 -50
- package/dist/data/access-level.test.d.ts +0 -1
- package/dist/data/access-level.test.js +0 -53
- package/dist/data/change-tracking.test.d.ts +0 -1
- package/dist/data/change-tracking.test.js +0 -62
- package/dist/data/files/files.test.d.ts +0 -1
- package/dist/data/files/files.test.js +0 -770
- package/dist/data/group-members.test.d.ts +0 -1
- package/dist/data/group-members.test.js +0 -336
- package/dist/data/group-share.test.d.ts +0 -1
- package/dist/data/group-share.test.js +0 -110
- package/dist/data/groups.test.d.ts +0 -1
- package/dist/data/groups.test.js +0 -153
- package/dist/data/orm/cursor.test.d.ts +0 -1
- package/dist/data/orm/cursor.test.js +0 -314
- package/dist/data/orm/data-query.mongo.test.d.ts +0 -1
- package/dist/data/orm/data-query.mongo.test.js +0 -367
- package/dist/data/orm/data-query.sqlite.test.d.ts +0 -1
- package/dist/data/orm/data-query.sqlite.test.js +0 -365
- package/dist/data/orm/data-query.test.d.ts +0 -1
- package/dist/data/orm/data-query.test.js +0 -545
- package/dist/data/orm/dependency-injection.test.d.ts +0 -1
- package/dist/data/orm/dependency-injection.test.js +0 -171
- package/dist/data/orm/event-registry.test.d.ts +0 -1
- package/dist/data/orm/event-registry.test.js +0 -44
- package/dist/data/orm/multi-cursors.test.d.ts +0 -1
- package/dist/data/orm/multi-cursors.test.js +0 -463
- package/dist/data/orm/sql.data-source.test.d.ts +0 -1
- package/dist/data/orm/sql.data-source.test.js +0 -731
- package/dist/data/orm/table-container-events.test.d.ts +0 -1
- package/dist/data/orm/table-container-events.test.js +0 -101
- package/dist/data/orm/table-container.test.d.ts +0 -1
- package/dist/data/orm/table-container.test.js +0 -273
- package/dist/data/orm/table.event-source.test.d.ts +0 -1
- package/dist/data/orm/table.event-source.test.js +0 -331
- package/dist/data/orm/types.test.d.ts +0 -1
- package/dist/data/orm/types.test.js +0 -409
- package/dist/data/package-table-identity.test.d.ts +0 -1
- package/dist/data/package-table-identity.test.js +0 -46
- package/dist/data/package-version-permissions.test.d.ts +0 -1
- package/dist/data/package-version-permissions.test.js +0 -107
- package/dist/data/package-version-resolver.test.d.ts +0 -4
- package/dist/data/package-version-resolver.test.js +0 -543
- package/dist/data/package-versions.test.d.ts +0 -1
- package/dist/data/package-versions.test.js +0 -227
- package/dist/data/persistent-vars.conflict.test.d.ts +0 -1
- package/dist/data/persistent-vars.conflict.test.js +0 -126
- package/dist/data/persistent-vars.migration.test.d.ts +0 -1
- package/dist/data/persistent-vars.migration.test.js +0 -133
- package/dist/data/persistent-vars.package-scope.test.d.ts +0 -1
- package/dist/data/persistent-vars.package-scope.test.js +0 -27
- package/dist/data/sync-recovery-queue.test.d.ts +0 -1
- package/dist/data/sync-recovery-queue.test.js +0 -215
- package/dist/data/table-signing.test.d.ts +0 -1
- package/dist/data/table-signing.test.js +0 -118
- package/dist/data/tool-access.test.d.ts +0 -1
- package/dist/data/tool-access.test.js +0 -166
- package/dist/data/user-permissions.test.d.ts +0 -1
- package/dist/data/user-permissions.test.js +0 -305
- package/dist/data/user-trust-levels.test.d.ts +0 -1
- package/dist/data/user-trust-levels.test.js +0 -37
- package/dist/define/also-implements.test.d.ts +0 -1
- package/dist/define/also-implements.test.js +0 -123
- package/dist/define/declare-contract.test.d.ts +0 -1
- package/dist/define/declare-contract.test.js +0 -573
- package/dist/define/define.test.d.ts +0 -1
- package/dist/define/define.test.js +0 -1010
- package/dist/define/empty-fields.test.d.ts +0 -1
- package/dist/define/empty-fields.test.js +0 -55
- package/dist/define/events-observables.test.d.ts +0 -1
- package/dist/define/events-observables.test.js +0 -1013
- package/dist/define/serve-members.test.d.ts +0 -1
- package/dist/define/serve-members.test.js +0 -630
- package/dist/define/vars.test.d.ts +0 -1
- package/dist/define/vars.test.js +0 -39
- package/dist/device/binary-peer-connection-v2.test.d.ts +0 -1
- package/dist/device/binary-peer-connection-v2.test.js +0 -730
- package/dist/device/binary-peer-connection.test.d.ts +0 -1
- package/dist/device/binary-peer-connection.test.js +0 -184
- package/dist/device/connection.test.d.ts +0 -1
- package/dist/device/connection.test.js +0 -613
- package/dist/device/device-election.test.d.ts +0 -1
- package/dist/device/device-election.test.js +0 -516
- package/dist/device/device.test.d.ts +0 -1
- package/dist/device/device.test.js +0 -243
- package/dist/device/frame-cipher.test.d.ts +0 -1
- package/dist/device/frame-cipher.test.js +0 -219
- package/dist/device/get-trust-level-fn.test.d.ts +0 -1
- package/dist/device/get-trust-level-fn.test.js +0 -685
- package/dist/device/streamed-socket.test.d.ts +0 -1
- package/dist/device/streamed-socket.test.js +0 -216
- package/dist/device/tx-encoding.test.d.ts +0 -1
- package/dist/device/tx-encoding.test.js +0 -295
- package/dist/device-pairing/cpace.test.d.ts +0 -1
- package/dist/device-pairing/cpace.test.js +0 -116
- package/dist/device-pairing/device-pairing.test.d.ts +0 -1
- package/dist/device-pairing/device-pairing.test.js +0 -211
- package/dist/hlc.test.d.ts +0 -1
- package/dist/hlc.test.js +0 -45
- package/dist/host-credentials.test.d.ts +0 -1
- package/dist/host-credentials.test.js +0 -88
- package/dist/identity/account-email.test.d.ts +0 -1
- package/dist/identity/account-email.test.js +0 -59
- package/dist/identity/anchor-document.test.d.ts +0 -1
- package/dist/identity/anchor-document.test.js +0 -16
- package/dist/identity/anchor-resolution.test.d.ts +0 -1
- package/dist/identity/anchor-resolution.test.js +0 -130
- package/dist/identity/key-change.test.d.ts +0 -1
- package/dist/identity/key-change.test.js +0 -811
- package/dist/invites/group-approval.test.d.ts +0 -1
- package/dist/invites/group-approval.test.js +0 -223
- package/dist/invites/invite-token.test.d.ts +0 -1
- package/dist/invites/invite-token.test.js +0 -123
- package/dist/keys.test.d.ts +0 -1
- package/dist/keys.test.js +0 -336
- package/dist/logging/console-logger.test.d.ts +0 -1
- package/dist/logging/console-logger.test.js +0 -300
- package/dist/logging/console-logs.table.test.d.ts +0 -1
- package/dist/logging/console-logs.table.test.js +0 -156
- package/dist/observable.test.d.ts +0 -1
- package/dist/observable.test.js +0 -309
- package/dist/package-build/build-package.test.d.ts +0 -1
- package/dist/package-build/build-package.test.js +0 -204
- package/dist/package-build/contract-add.test.d.ts +0 -1
- package/dist/package-build/contract-add.test.js +0 -467
- package/dist/package-build/freeze-contracts.test.d.ts +0 -1
- package/dist/package-build/freeze-contracts.test.js +0 -355
- package/dist/package-installer/package-author-signing.test.d.ts +0 -1
- package/dist/package-installer/package-author-signing.test.js +0 -200
- package/dist/package-installer/package-cloner.test.d.ts +0 -1
- package/dist/package-installer/package-cloner.test.js +0 -278
- package/dist/package-installer/package-creator.test.d.ts +0 -1
- package/dist/package-installer/package-creator.test.js +0 -908
- package/dist/package-installer/package-dev-tools.test.d.ts +0 -1
- package/dist/package-installer/package-dev-tools.test.js +0 -64
- package/dist/package-installer/package-installer.test.d.ts +0 -1
- package/dist/package-installer/package-installer.test.js +0 -882
- package/dist/package-installer/package-propagation.test.d.ts +0 -1
- package/dist/package-installer/package-propagation.test.js +0 -1269
- package/dist/package-installer/package-publisher.test.d.ts +0 -1
- package/dist/package-installer/package-publisher.test.js +0 -153
- package/dist/package-installer/package-remote-checker.test.d.ts +0 -1
- package/dist/package-installer/package-remote-checker.test.js +0 -334
- package/dist/package-installer/package-seed-installer.test.d.ts +0 -1
- package/dist/package-installer/package-seed-installer.test.js +0 -123
- package/dist/package-installer/package-tarball.test.d.ts +0 -1
- package/dist/package-installer/package-tarball.test.js +0 -75
- package/dist/package-installer/peers-core-installer.test.d.ts +0 -1
- package/dist/package-installer/peers-core-installer.test.js +0 -49
- package/dist/package-installer/peers-core-trust-anchor.test.d.ts +0 -1
- package/dist/package-installer/peers-core-trust-anchor.test.js +0 -155
- package/dist/package-loader/contract-package-loader.test.d.ts +0 -1
- package/dist/package-loader/contract-package-loader.test.js +0 -274
- package/dist/package-loader/host-authority-loading.test.d.ts +0 -1
- package/dist/package-loader/host-authority-loading.test.js +0 -104
- package/dist/package-loader/host-dispatch-policy.test.d.ts +0 -1
- package/dist/package-loader/host-dispatch-policy.test.js +0 -56
- package/dist/package-loader/isolated-artifact.test.d.ts +0 -1
- package/dist/package-loader/isolated-artifact.test.js +0 -688
- package/dist/package-loader/isolated-persistent-var-gateway.test.d.ts +0 -1
- package/dist/package-loader/isolated-persistent-var-gateway.test.js +0 -56
- package/dist/package-loader/isolated-table-gateway.test.d.ts +0 -1
- package/dist/package-loader/isolated-table-gateway.test.js +0 -633
- package/dist/package-loader/package-loader.test.d.ts +0 -1
- package/dist/package-loader/package-loader.test.js +0 -1764
- package/dist/package-loader/resolve-consumed-contracts.test.d.ts +0 -1
- package/dist/package-loader/resolve-consumed-contracts.test.js +0 -166
- package/dist/serial-json.compat.test.d.ts +0 -1
- package/dist/serial-json.compat.test.js +0 -369
- package/dist/serial-json.test.d.ts +0 -1
- package/dist/serial-json.test.js +0 -401
- package/dist/testing/mock-host-contract.test.d.ts +0 -1
- package/dist/testing/mock-host-contract.test.js +0 -782
- package/dist/utils.test.d.ts +0 -1
- package/dist/utils.test.js +0 -232
|
@@ -1,685 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const utils_1 = require("../utils");
|
|
4
|
-
const get_trust_level_fn_1 = require("./get-trust-level-fn");
|
|
5
|
-
const socket_type_1 = require("./socket.type");
|
|
6
|
-
jest.mock("../context", () => ({
|
|
7
|
-
getUserContext: jest.fn(),
|
|
8
|
-
}));
|
|
9
|
-
jest.mock("../data", () => ({
|
|
10
|
-
Devices: jest.fn(),
|
|
11
|
-
Users: jest.fn(),
|
|
12
|
-
UserTrustLevels: jest.fn(),
|
|
13
|
-
}));
|
|
14
|
-
jest.mock("../identity/key-change", () => ({
|
|
15
|
-
evaluatePresentedKey: jest.fn(async () => ({ accepted: false, state: "pending" })),
|
|
16
|
-
detectIdentityRotatedAway: jest.fn(async () => undefined),
|
|
17
|
-
}));
|
|
18
|
-
const context_1 = require("../context");
|
|
19
|
-
const data_1 = require("../data");
|
|
20
|
-
const key_change_1 = require("../identity/key-change");
|
|
21
|
-
const mockGetUserContext = context_1.getUserContext;
|
|
22
|
-
const mockDevices = data_1.Devices;
|
|
23
|
-
const mockUsers = data_1.Users;
|
|
24
|
-
const mockUserTrustLevels = data_1.UserTrustLevels;
|
|
25
|
-
function makeDeviceTable(deviceRecord = null) {
|
|
26
|
-
return {
|
|
27
|
-
get: jest.fn().mockResolvedValue(deviceRecord),
|
|
28
|
-
save: jest.fn().mockResolvedValue(undefined),
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
function makeUsersTable(userRecord = null) {
|
|
32
|
-
return {
|
|
33
|
-
get: jest.fn().mockResolvedValue(userRecord),
|
|
34
|
-
save: jest.fn().mockResolvedValue(undefined),
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
function makeTrustTable(trustRecord = null) {
|
|
38
|
-
return {
|
|
39
|
-
get: jest.fn().mockResolvedValue(trustRecord),
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
function makeTestIdentity() {
|
|
43
|
-
return {
|
|
44
|
-
userId: (0, utils_1.newid)(),
|
|
45
|
-
publicKey: `pk_${(0, utils_1.newid)()}`,
|
|
46
|
-
publicBoxKey: `pbk_${(0, utils_1.newid)()}`,
|
|
47
|
-
deviceId: (0, utils_1.newid)(),
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
function setupMocks(deviceRecord = null, userRecord = null, trustRecord = null) {
|
|
51
|
-
const mockUserDataContext = {};
|
|
52
|
-
mockGetUserContext.mockResolvedValue({
|
|
53
|
-
userDataContext: mockUserDataContext,
|
|
54
|
-
groupDataContexts: new Map(),
|
|
55
|
-
});
|
|
56
|
-
const devTable = makeDeviceTable(deviceRecord);
|
|
57
|
-
const usersTable = makeUsersTable(userRecord);
|
|
58
|
-
const trustTable = makeTrustTable(trustRecord);
|
|
59
|
-
mockDevices.mockReturnValue(devTable);
|
|
60
|
-
mockUsers.mockReturnValue(usersTable);
|
|
61
|
-
mockUserTrustLevels.mockReturnValue(trustTable);
|
|
62
|
-
return { devTable, usersTable, trustTable };
|
|
63
|
-
}
|
|
64
|
-
describe("getTrustLevelFn", () => {
|
|
65
|
-
beforeEach(() => {
|
|
66
|
-
jest.clearAllMocks();
|
|
67
|
-
});
|
|
68
|
-
// ─── Own-device fast path ───────────────────────────────────────────────────
|
|
69
|
-
describe("own device (userId + publicKey + publicBoxKey all match)", () => {
|
|
70
|
-
it("returns NewDevice when device is newly seen (< 2 days old)", async () => {
|
|
71
|
-
const me = makeTestIdentity();
|
|
72
|
-
const deviceInfo = { ...me };
|
|
73
|
-
const recentDate = new Date(Date.now() - 1000 * 60 * 60 * 24); // 1 day ago
|
|
74
|
-
const existingDevice = {
|
|
75
|
-
deviceId: me.deviceId,
|
|
76
|
-
userId: me.userId,
|
|
77
|
-
firstSeen: recentDate,
|
|
78
|
-
lastSeen: recentDate,
|
|
79
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
80
|
-
};
|
|
81
|
-
const { devTable } = setupMocks(existingDevice);
|
|
82
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
83
|
-
const result = await getTrustLevel(deviceInfo);
|
|
84
|
-
expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
|
|
85
|
-
expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({ trustLevel: socket_type_1.TrustLevel.NewDevice }), expect.objectContaining({ restoreIfDeleted: true, weakInsert: true }));
|
|
86
|
-
});
|
|
87
|
-
it("returns Trusted when own device has been seen for more than 2 days", async () => {
|
|
88
|
-
const me = makeTestIdentity();
|
|
89
|
-
const deviceInfo = { ...me };
|
|
90
|
-
const oldDate = new Date(Date.now() - 1000 * 60 * 60 * 24 * 3); // 3 days ago
|
|
91
|
-
const existingDevice = {
|
|
92
|
-
deviceId: me.deviceId,
|
|
93
|
-
userId: me.userId,
|
|
94
|
-
firstSeen: oldDate,
|
|
95
|
-
lastSeen: oldDate,
|
|
96
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
97
|
-
};
|
|
98
|
-
setupMocks(existingDevice);
|
|
99
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
100
|
-
const result = await getTrustLevel(deviceInfo);
|
|
101
|
-
expect(result).toBe(socket_type_1.TrustLevel.Trusted);
|
|
102
|
-
});
|
|
103
|
-
it("creates a new device record if own device has not been seen before", async () => {
|
|
104
|
-
const me = makeTestIdentity();
|
|
105
|
-
const deviceInfo = { ...me };
|
|
106
|
-
const { devTable } = setupMocks(null); // no existing device
|
|
107
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me, "https://myserver.example.com");
|
|
108
|
-
const result = await getTrustLevel(deviceInfo);
|
|
109
|
-
expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
|
|
110
|
-
expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({
|
|
111
|
-
deviceId: me.deviceId,
|
|
112
|
-
userId: me.userId,
|
|
113
|
-
serverUrl: "https://myserver.example.com",
|
|
114
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
115
|
-
}), expect.objectContaining({ restoreIfDeleted: true, weakInsert: true }));
|
|
116
|
-
});
|
|
117
|
-
it("does not look up Users or UserTrustLevels for own device", async () => {
|
|
118
|
-
const me = makeTestIdentity();
|
|
119
|
-
const deviceInfo = { ...me };
|
|
120
|
-
setupMocks(null);
|
|
121
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
122
|
-
await getTrustLevel(deviceInfo);
|
|
123
|
-
expect(mockUsers).not.toHaveBeenCalled();
|
|
124
|
-
expect(mockUserTrustLevels).not.toHaveBeenCalled();
|
|
125
|
-
});
|
|
126
|
-
it("updates lastSeen on every call for own device", async () => {
|
|
127
|
-
const me = makeTestIdentity();
|
|
128
|
-
const deviceInfo = { ...me };
|
|
129
|
-
const pastDate = new Date(Date.now() - 1000 * 60 * 5); // 5 minutes ago
|
|
130
|
-
const existingDevice = {
|
|
131
|
-
deviceId: me.deviceId,
|
|
132
|
-
userId: me.userId,
|
|
133
|
-
firstSeen: pastDate,
|
|
134
|
-
lastSeen: pastDate,
|
|
135
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
136
|
-
};
|
|
137
|
-
const { devTable } = setupMocks(existingDevice);
|
|
138
|
-
const before = Date.now();
|
|
139
|
-
await (0, get_trust_level_fn_1.getTrustLevelFn)(me)(deviceInfo);
|
|
140
|
-
const after = Date.now();
|
|
141
|
-
const savedDevice = devTable.save.mock.calls[0][0];
|
|
142
|
-
expect(savedDevice.lastSeen.getTime()).toBeGreaterThanOrEqual(before);
|
|
143
|
-
expect(savedDevice.lastSeen.getTime()).toBeLessThanOrEqual(after);
|
|
144
|
-
});
|
|
145
|
-
it("stores the owner-reported name without replacing a local device label", async () => {
|
|
146
|
-
const me = makeTestIdentity();
|
|
147
|
-
const deviceInfo = { ...me, deviceName: "Owner's laptop" };
|
|
148
|
-
const existingDevice = {
|
|
149
|
-
deviceId: me.deviceId,
|
|
150
|
-
userId: me.userId,
|
|
151
|
-
firstSeen: new Date(),
|
|
152
|
-
lastSeen: new Date(),
|
|
153
|
-
name: "My local label",
|
|
154
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
155
|
-
};
|
|
156
|
-
const { devTable } = setupMocks(existingDevice);
|
|
157
|
-
await (0, get_trust_level_fn_1.getTrustLevelFn)(me)(deviceInfo);
|
|
158
|
-
expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({
|
|
159
|
-
name: "My local label",
|
|
160
|
-
reportedName: "Owner's laptop",
|
|
161
|
-
}), expect.anything());
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
// ─── Blocked device early exit ──────────────────────────────────────────────
|
|
165
|
-
describe("explicitly blocked device (trustLevel < Unknown)", () => {
|
|
166
|
-
it("returns Untrusted immediately without saving any records", async () => {
|
|
167
|
-
const me = makeTestIdentity();
|
|
168
|
-
const other = makeTestIdentity();
|
|
169
|
-
const blockedDevice = {
|
|
170
|
-
deviceId: other.deviceId,
|
|
171
|
-
userId: other.userId,
|
|
172
|
-
firstSeen: new Date(),
|
|
173
|
-
lastSeen: new Date(),
|
|
174
|
-
trustLevel: socket_type_1.TrustLevel.Untrusted,
|
|
175
|
-
};
|
|
176
|
-
const { devTable } = setupMocks(blockedDevice);
|
|
177
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
178
|
-
const result = await getTrustLevel(other);
|
|
179
|
-
// All three tables are fetched in parallel before the block check
|
|
180
|
-
expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
|
|
181
|
-
// Should not save anything — returns early before any writes
|
|
182
|
-
expect(devTable.save).not.toHaveBeenCalled();
|
|
183
|
-
});
|
|
184
|
-
it("returns Malicious immediately for malicious-flagged devices", async () => {
|
|
185
|
-
const me = makeTestIdentity();
|
|
186
|
-
const other = makeTestIdentity();
|
|
187
|
-
const maliciousDevice = {
|
|
188
|
-
deviceId: other.deviceId,
|
|
189
|
-
userId: other.userId,
|
|
190
|
-
firstSeen: new Date(),
|
|
191
|
-
lastSeen: new Date(),
|
|
192
|
-
trustLevel: socket_type_1.TrustLevel.Malicious,
|
|
193
|
-
};
|
|
194
|
-
setupMocks(maliciousDevice);
|
|
195
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
196
|
-
const result = await getTrustLevel(other);
|
|
197
|
-
expect(result).toBe(socket_type_1.TrustLevel.Malicious);
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
// ─── Key-mismatch guard ─────────────────────────────────────────────────────
|
|
201
|
-
describe("key-mismatch guard (user AND device both exist)", () => {
|
|
202
|
-
it("returns Untrusted when publicKey does not match stored user record", async () => {
|
|
203
|
-
const me = makeTestIdentity();
|
|
204
|
-
const other = makeTestIdentity();
|
|
205
|
-
const existingUser = {
|
|
206
|
-
userId: other.userId,
|
|
207
|
-
publicKey: `different_pk_${(0, utils_1.newid)()}`,
|
|
208
|
-
publicBoxKey: other.publicBoxKey,
|
|
209
|
-
name: "Alice",
|
|
210
|
-
};
|
|
211
|
-
const existingDevice = {
|
|
212
|
-
deviceId: other.deviceId,
|
|
213
|
-
userId: other.userId,
|
|
214
|
-
firstSeen: new Date(),
|
|
215
|
-
lastSeen: new Date(),
|
|
216
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
217
|
-
};
|
|
218
|
-
setupMocks(existingDevice, existingUser);
|
|
219
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
220
|
-
const result = await getTrustLevel(other);
|
|
221
|
-
expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
|
|
222
|
-
});
|
|
223
|
-
it("returns Untrusted when publicBoxKey does not match stored user record", async () => {
|
|
224
|
-
const me = makeTestIdentity();
|
|
225
|
-
const other = makeTestIdentity();
|
|
226
|
-
const existingUser = {
|
|
227
|
-
userId: other.userId,
|
|
228
|
-
publicKey: other.publicKey,
|
|
229
|
-
publicBoxKey: `different_pbk_${(0, utils_1.newid)()}`,
|
|
230
|
-
name: "Alice",
|
|
231
|
-
};
|
|
232
|
-
const existingDevice = {
|
|
233
|
-
deviceId: other.deviceId,
|
|
234
|
-
userId: other.userId,
|
|
235
|
-
firstSeen: new Date(),
|
|
236
|
-
lastSeen: new Date(),
|
|
237
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
238
|
-
};
|
|
239
|
-
setupMocks(existingDevice, existingUser);
|
|
240
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
241
|
-
const result = await getTrustLevel(other);
|
|
242
|
-
expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
|
|
243
|
-
});
|
|
244
|
-
it("returns Untrusted when device.userId does not match deviceInfo.userId", async () => {
|
|
245
|
-
const me = makeTestIdentity();
|
|
246
|
-
const other = makeTestIdentity();
|
|
247
|
-
const existingUser = {
|
|
248
|
-
userId: other.userId,
|
|
249
|
-
publicKey: other.publicKey,
|
|
250
|
-
publicBoxKey: other.publicBoxKey,
|
|
251
|
-
name: "Alice",
|
|
252
|
-
};
|
|
253
|
-
const existingDevice = {
|
|
254
|
-
deviceId: other.deviceId,
|
|
255
|
-
userId: (0, utils_1.newid)(), // different userId stored on device record
|
|
256
|
-
firstSeen: new Date(),
|
|
257
|
-
lastSeen: new Date(),
|
|
258
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
259
|
-
};
|
|
260
|
-
setupMocks(existingDevice, existingUser);
|
|
261
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
262
|
-
const result = await getTrustLevel(other);
|
|
263
|
-
expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
|
|
264
|
-
});
|
|
265
|
-
it("does NOT return Untrusted when only device exists (no user record yet)", async () => {
|
|
266
|
-
const me = makeTestIdentity();
|
|
267
|
-
const other = makeTestIdentity();
|
|
268
|
-
const existingDevice = {
|
|
269
|
-
deviceId: other.deviceId,
|
|
270
|
-
userId: other.userId,
|
|
271
|
-
firstSeen: new Date(),
|
|
272
|
-
lastSeen: new Date(),
|
|
273
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
274
|
-
};
|
|
275
|
-
// user record is null — mismatch check is skipped
|
|
276
|
-
setupMocks(existingDevice, null);
|
|
277
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
278
|
-
const result = await getTrustLevel(other);
|
|
279
|
-
expect(result).not.toBe(socket_type_1.TrustLevel.Untrusted);
|
|
280
|
-
});
|
|
281
|
-
it("returns Untrusted for a known user's signing-key mismatch on a new device", async () => {
|
|
282
|
-
const me = makeTestIdentity();
|
|
283
|
-
const other = makeTestIdentity();
|
|
284
|
-
const existingUser = {
|
|
285
|
-
userId: other.userId,
|
|
286
|
-
publicKey: `different_pk_${(0, utils_1.newid)()}`, // key mismatch present but device is missing
|
|
287
|
-
publicBoxKey: other.publicBoxKey,
|
|
288
|
-
name: "Alice",
|
|
289
|
-
};
|
|
290
|
-
// device record is null — mismatch check is skipped
|
|
291
|
-
setupMocks(null, existingUser);
|
|
292
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
293
|
-
const result = await getTrustLevel(other);
|
|
294
|
-
expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
|
|
295
|
-
});
|
|
296
|
-
it("returns Untrusted for a known user's box-key mismatch on a new device", async () => {
|
|
297
|
-
const me = makeTestIdentity();
|
|
298
|
-
const other = makeTestIdentity();
|
|
299
|
-
const existingUser = {
|
|
300
|
-
userId: other.userId,
|
|
301
|
-
publicKey: other.publicKey,
|
|
302
|
-
publicBoxKey: `different_pbk_${(0, utils_1.newid)()}`,
|
|
303
|
-
name: "Alice",
|
|
304
|
-
};
|
|
305
|
-
setupMocks(null, existingUser);
|
|
306
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
307
|
-
const result = await getTrustLevel(other);
|
|
308
|
-
expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
|
|
309
|
-
});
|
|
310
|
-
});
|
|
311
|
-
// ─── Trusted fast path ──────────────────────────────────────────────────────
|
|
312
|
-
describe("trusted fast path", () => {
|
|
313
|
-
it("returns Trusted immediately when device and userTrustLevel are both >= Trusted", async () => {
|
|
314
|
-
const me = makeTestIdentity();
|
|
315
|
-
const other = makeTestIdentity();
|
|
316
|
-
const existingUser = {
|
|
317
|
-
userId: other.userId,
|
|
318
|
-
publicKey: other.publicKey,
|
|
319
|
-
publicBoxKey: other.publicBoxKey,
|
|
320
|
-
name: "Alice",
|
|
321
|
-
};
|
|
322
|
-
const existingDevice = {
|
|
323
|
-
deviceId: other.deviceId,
|
|
324
|
-
userId: other.userId,
|
|
325
|
-
firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 30),
|
|
326
|
-
lastSeen: new Date(),
|
|
327
|
-
trustLevel: socket_type_1.TrustLevel.Trusted,
|
|
328
|
-
};
|
|
329
|
-
const trustRecord = {
|
|
330
|
-
userId: other.userId,
|
|
331
|
-
trustLevel: socket_type_1.TrustLevel.Trusted,
|
|
332
|
-
};
|
|
333
|
-
const { devTable } = setupMocks(existingDevice, existingUser, trustRecord);
|
|
334
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
335
|
-
const result = await getTrustLevel(other);
|
|
336
|
-
expect(result).toBe(socket_type_1.TrustLevel.Trusted);
|
|
337
|
-
expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({ trustLevel: socket_type_1.TrustLevel.Trusted }), expect.objectContaining({ restoreIfDeleted: true }));
|
|
338
|
-
});
|
|
339
|
-
it("does NOT trigger fast path when device is Trusted but userTrustLevel is not", async () => {
|
|
340
|
-
const me = makeTestIdentity();
|
|
341
|
-
const other = makeTestIdentity();
|
|
342
|
-
const existingUser = {
|
|
343
|
-
userId: other.userId,
|
|
344
|
-
publicKey: other.publicKey,
|
|
345
|
-
publicBoxKey: other.publicBoxKey,
|
|
346
|
-
name: "Alice",
|
|
347
|
-
};
|
|
348
|
-
const existingDevice = {
|
|
349
|
-
deviceId: other.deviceId,
|
|
350
|
-
userId: other.userId,
|
|
351
|
-
firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 10),
|
|
352
|
-
lastSeen: new Date(),
|
|
353
|
-
trustLevel: socket_type_1.TrustLevel.Trusted,
|
|
354
|
-
};
|
|
355
|
-
const trustRecord = {
|
|
356
|
-
userId: other.userId,
|
|
357
|
-
trustLevel: socket_type_1.TrustLevel.Known, // below Trusted
|
|
358
|
-
};
|
|
359
|
-
const { devTable } = setupMocks(existingDevice, existingUser, trustRecord);
|
|
360
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
361
|
-
await getTrustLevel(other);
|
|
362
|
-
// save was called via the normal path (weakInsert), not the fast path
|
|
363
|
-
expect(devTable.save).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ weakInsert: true }));
|
|
364
|
-
});
|
|
365
|
-
it("does NOT trigger fast path when userTrustLevel is Trusted but device is not", async () => {
|
|
366
|
-
const me = makeTestIdentity();
|
|
367
|
-
const other = makeTestIdentity();
|
|
368
|
-
const existingUser = {
|
|
369
|
-
userId: other.userId,
|
|
370
|
-
publicKey: other.publicKey,
|
|
371
|
-
publicBoxKey: other.publicBoxKey,
|
|
372
|
-
name: "Alice",
|
|
373
|
-
};
|
|
374
|
-
const existingDevice = {
|
|
375
|
-
deviceId: other.deviceId,
|
|
376
|
-
userId: other.userId,
|
|
377
|
-
firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 10),
|
|
378
|
-
lastSeen: new Date(),
|
|
379
|
-
trustLevel: socket_type_1.TrustLevel.Known, // below Trusted
|
|
380
|
-
};
|
|
381
|
-
const trustRecord = {
|
|
382
|
-
userId: other.userId,
|
|
383
|
-
trustLevel: socket_type_1.TrustLevel.Trusted,
|
|
384
|
-
};
|
|
385
|
-
setupMocks(existingDevice, existingUser, trustRecord);
|
|
386
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
387
|
-
const result = await getTrustLevel(other);
|
|
388
|
-
expect(result).not.toBe(socket_type_1.TrustLevel.Trusted);
|
|
389
|
-
});
|
|
390
|
-
});
|
|
391
|
-
// ─── New user / new device resolution ───────────────────────────────────────
|
|
392
|
-
describe("new user and new device", () => {
|
|
393
|
-
it("returns NewDevice and saves a user stub when both user and device are unseen", async () => {
|
|
394
|
-
const me = makeTestIdentity();
|
|
395
|
-
const other = {
|
|
396
|
-
...makeTestIdentity(),
|
|
397
|
-
userName: "Alice",
|
|
398
|
-
deviceName: "Alice's phone",
|
|
399
|
-
};
|
|
400
|
-
const { devTable, usersTable } = setupMocks(null, null, null);
|
|
401
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me, "https://server.example.com");
|
|
402
|
-
const result = await getTrustLevel(other);
|
|
403
|
-
// The local `trustLevel` variable is set to NewUser (10), but the function returns
|
|
404
|
-
// `device.trustLevel` which remains NewDevice (20). The NewUser assignment was
|
|
405
|
-
// intended for the now-commented-out `device.trustLevel = remoteTrustLevel || trustLevel`.
|
|
406
|
-
expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
|
|
407
|
-
expect(usersTable.save).toHaveBeenCalledWith(expect.objectContaining({
|
|
408
|
-
userId: other.userId,
|
|
409
|
-
publicKey: other.publicKey,
|
|
410
|
-
publicBoxKey: other.publicBoxKey,
|
|
411
|
-
name: "Alice",
|
|
412
|
-
}), expect.objectContaining({ restoreIfDeleted: true, weakInsert: true }));
|
|
413
|
-
expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({
|
|
414
|
-
deviceId: other.deviceId,
|
|
415
|
-
userId: other.userId,
|
|
416
|
-
reportedName: "Alice's phone",
|
|
417
|
-
serverUrl: "https://server.example.com",
|
|
418
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
419
|
-
}), expect.objectContaining({ restoreIfDeleted: true, weakInsert: true }));
|
|
420
|
-
});
|
|
421
|
-
it("uses a fallback name from userId suffix when serverUrl is not provided", async () => {
|
|
422
|
-
const me = makeTestIdentity();
|
|
423
|
-
const other = makeTestIdentity();
|
|
424
|
-
const { usersTable } = setupMocks(null, null, null);
|
|
425
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me); // no serverUrl
|
|
426
|
-
await getTrustLevel(other);
|
|
427
|
-
const savedUser = usersTable.save.mock.calls[0][0];
|
|
428
|
-
expect(savedUser.name).toBe(`Unnamed_${other.userId.substring(20)}`);
|
|
429
|
-
});
|
|
430
|
-
it("does not save a user stub when user already exists", async () => {
|
|
431
|
-
const me = makeTestIdentity();
|
|
432
|
-
const other = makeTestIdentity();
|
|
433
|
-
const existingUser = {
|
|
434
|
-
userId: other.userId,
|
|
435
|
-
publicKey: other.publicKey,
|
|
436
|
-
publicBoxKey: other.publicBoxKey,
|
|
437
|
-
name: "Alice",
|
|
438
|
-
};
|
|
439
|
-
const { usersTable } = setupMocks(null, existingUser, null);
|
|
440
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
441
|
-
await getTrustLevel(other);
|
|
442
|
-
expect(usersTable.save).not.toHaveBeenCalled();
|
|
443
|
-
});
|
|
444
|
-
});
|
|
445
|
-
// ─── Device aging ────────────────────────────────────────────────────────────
|
|
446
|
-
describe("device aging", () => {
|
|
447
|
-
it("returns NewDevice for a device first seen < 7 days ago", async () => {
|
|
448
|
-
const me = makeTestIdentity();
|
|
449
|
-
const other = makeTestIdentity();
|
|
450
|
-
const existingUser = {
|
|
451
|
-
userId: other.userId,
|
|
452
|
-
publicKey: other.publicKey,
|
|
453
|
-
publicBoxKey: other.publicBoxKey,
|
|
454
|
-
name: "Alice",
|
|
455
|
-
};
|
|
456
|
-
const existingDevice = {
|
|
457
|
-
deviceId: other.deviceId,
|
|
458
|
-
userId: other.userId,
|
|
459
|
-
firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 5), // 5 days ago
|
|
460
|
-
lastSeen: new Date(),
|
|
461
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
462
|
-
};
|
|
463
|
-
setupMocks(existingDevice, existingUser, null);
|
|
464
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
465
|
-
const result = await getTrustLevel(other);
|
|
466
|
-
expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
|
|
467
|
-
});
|
|
468
|
-
it("promotes device to Known when first seen > 7 days ago", async () => {
|
|
469
|
-
const me = makeTestIdentity();
|
|
470
|
-
const other = makeTestIdentity();
|
|
471
|
-
const existingUser = {
|
|
472
|
-
userId: other.userId,
|
|
473
|
-
publicKey: other.publicKey,
|
|
474
|
-
publicBoxKey: other.publicBoxKey,
|
|
475
|
-
name: "Alice",
|
|
476
|
-
};
|
|
477
|
-
const existingDevice = {
|
|
478
|
-
deviceId: other.deviceId,
|
|
479
|
-
userId: other.userId,
|
|
480
|
-
firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 8), // 8 days ago
|
|
481
|
-
lastSeen: new Date(),
|
|
482
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
483
|
-
};
|
|
484
|
-
const { devTable } = setupMocks(existingDevice, existingUser, null);
|
|
485
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
486
|
-
const result = await getTrustLevel(other);
|
|
487
|
-
expect(result).toBe(socket_type_1.TrustLevel.Known);
|
|
488
|
-
expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({ trustLevel: socket_type_1.TrustLevel.Known }), expect.anything());
|
|
489
|
-
});
|
|
490
|
-
it("bumps existing device with Unknown trust level up to NewDevice", async () => {
|
|
491
|
-
const me = makeTestIdentity();
|
|
492
|
-
const other = makeTestIdentity();
|
|
493
|
-
const existingUser = {
|
|
494
|
-
userId: other.userId,
|
|
495
|
-
publicKey: other.publicKey,
|
|
496
|
-
publicBoxKey: other.publicBoxKey,
|
|
497
|
-
name: "Alice",
|
|
498
|
-
};
|
|
499
|
-
const existingDevice = {
|
|
500
|
-
deviceId: other.deviceId,
|
|
501
|
-
userId: other.userId,
|
|
502
|
-
firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 3), // 3 days, under 7-day threshold
|
|
503
|
-
lastSeen: new Date(),
|
|
504
|
-
trustLevel: socket_type_1.TrustLevel.Unknown,
|
|
505
|
-
};
|
|
506
|
-
const { devTable } = setupMocks(existingDevice, existingUser, null);
|
|
507
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
508
|
-
const result = await getTrustLevel(other);
|
|
509
|
-
expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
|
|
510
|
-
expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({ trustLevel: socket_type_1.TrustLevel.NewDevice }), expect.anything());
|
|
511
|
-
});
|
|
512
|
-
it("does not downgrade a device already at Known when re-evaluated before 7 days", async () => {
|
|
513
|
-
const me = makeTestIdentity();
|
|
514
|
-
const other = makeTestIdentity();
|
|
515
|
-
const existingUser = {
|
|
516
|
-
userId: other.userId,
|
|
517
|
-
publicKey: other.publicKey,
|
|
518
|
-
publicBoxKey: other.publicBoxKey,
|
|
519
|
-
name: "Alice",
|
|
520
|
-
};
|
|
521
|
-
const existingDevice = {
|
|
522
|
-
deviceId: other.deviceId,
|
|
523
|
-
userId: other.userId,
|
|
524
|
-
firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 5), // 5 days — under 7-day threshold
|
|
525
|
-
lastSeen: new Date(),
|
|
526
|
-
trustLevel: socket_type_1.TrustLevel.Known, // manually promoted already
|
|
527
|
-
};
|
|
528
|
-
setupMocks(existingDevice, existingUser, null);
|
|
529
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
530
|
-
const result = await getTrustLevel(other);
|
|
531
|
-
expect(result).toBe(socket_type_1.TrustLevel.Known);
|
|
532
|
-
});
|
|
533
|
-
it("updates lastSeen on the device record each time", async () => {
|
|
534
|
-
const me = makeTestIdentity();
|
|
535
|
-
const other = makeTestIdentity();
|
|
536
|
-
const existingUser = {
|
|
537
|
-
userId: other.userId,
|
|
538
|
-
publicKey: other.publicKey,
|
|
539
|
-
publicBoxKey: other.publicBoxKey,
|
|
540
|
-
name: "Alice",
|
|
541
|
-
};
|
|
542
|
-
const pastDate = new Date(Date.now() - 1000 * 60 * 60); // 1 hour ago
|
|
543
|
-
const existingDevice = {
|
|
544
|
-
deviceId: other.deviceId,
|
|
545
|
-
userId: other.userId,
|
|
546
|
-
firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 2),
|
|
547
|
-
lastSeen: pastDate,
|
|
548
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
549
|
-
};
|
|
550
|
-
const { devTable } = setupMocks(existingDevice, existingUser, null);
|
|
551
|
-
const before = Date.now();
|
|
552
|
-
await (0, get_trust_level_fn_1.getTrustLevelFn)(me)(other);
|
|
553
|
-
const after = Date.now();
|
|
554
|
-
const savedDevice = devTable.save.mock.calls[0][0];
|
|
555
|
-
expect(savedDevice.lastSeen.getTime()).toBeGreaterThanOrEqual(before);
|
|
556
|
-
expect(savedDevice.lastSeen.getTime()).toBeLessThanOrEqual(after);
|
|
557
|
-
});
|
|
558
|
-
});
|
|
559
|
-
// ─── Existing user, no device ────────────────────────────────────────────────
|
|
560
|
-
describe("existing user but no device record", () => {
|
|
561
|
-
it("creates a new device record with NewDevice trust level", async () => {
|
|
562
|
-
const me = makeTestIdentity();
|
|
563
|
-
const other = makeTestIdentity();
|
|
564
|
-
const existingUser = {
|
|
565
|
-
userId: other.userId,
|
|
566
|
-
publicKey: other.publicKey,
|
|
567
|
-
publicBoxKey: other.publicBoxKey,
|
|
568
|
-
name: "Alice",
|
|
569
|
-
};
|
|
570
|
-
const { devTable } = setupMocks(null, existingUser, null);
|
|
571
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me, "https://relay.example.com");
|
|
572
|
-
const result = await getTrustLevel(other);
|
|
573
|
-
expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
|
|
574
|
-
expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({
|
|
575
|
-
deviceId: other.deviceId,
|
|
576
|
-
userId: other.userId,
|
|
577
|
-
serverUrl: "https://relay.example.com",
|
|
578
|
-
trustLevel: socket_type_1.TrustLevel.NewDevice,
|
|
579
|
-
}), expect.anything());
|
|
580
|
-
});
|
|
581
|
-
});
|
|
582
|
-
// ─── Parallel DB lookups ────────────────────────────────────────────────────
|
|
583
|
-
describe("parallel DB lookups for non-self devices", () => {
|
|
584
|
-
it("queries Users, Devices and UserTrustLevels in parallel", async () => {
|
|
585
|
-
const me = makeTestIdentity();
|
|
586
|
-
const other = makeTestIdentity();
|
|
587
|
-
setupMocks(null, null, null);
|
|
588
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
589
|
-
await getTrustLevel(other);
|
|
590
|
-
expect(mockDevices).toHaveBeenCalled();
|
|
591
|
-
expect(mockUsers).toHaveBeenCalled();
|
|
592
|
-
expect(mockUserTrustLevels).toHaveBeenCalled();
|
|
593
|
-
});
|
|
594
|
-
});
|
|
595
|
-
describe("anchor confirmation", () => {
|
|
596
|
-
it("accepts a different key when the anchors confirm a rotation", async () => {
|
|
597
|
-
const me = makeTestIdentity();
|
|
598
|
-
const other = makeTestIdentity();
|
|
599
|
-
const existingUser = {
|
|
600
|
-
userId: other.userId,
|
|
601
|
-
publicKey: `stored_${(0, utils_1.newid)()}`,
|
|
602
|
-
publicBoxKey: `stored_box_${(0, utils_1.newid)()}`,
|
|
603
|
-
name: "Alice",
|
|
604
|
-
};
|
|
605
|
-
const { usersTable } = setupMocks(null, existingUser, null);
|
|
606
|
-
usersTable.get.mockResolvedValueOnce(existingUser).mockResolvedValueOnce({
|
|
607
|
-
...existingUser,
|
|
608
|
-
publicKey: other.publicKey,
|
|
609
|
-
publicBoxKey: other.publicBoxKey,
|
|
610
|
-
});
|
|
611
|
-
key_change_1.evaluatePresentedKey.mockResolvedValueOnce({
|
|
612
|
-
accepted: true,
|
|
613
|
-
state: "confirmed-rotation",
|
|
614
|
-
});
|
|
615
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
616
|
-
const result = await getTrustLevel(other);
|
|
617
|
-
expect(key_change_1.evaluatePresentedKey).toHaveBeenCalledWith(expect.objectContaining({
|
|
618
|
-
userId: other.userId,
|
|
619
|
-
presentedKey: other.publicKey,
|
|
620
|
-
}));
|
|
621
|
-
expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
|
|
622
|
-
});
|
|
623
|
-
it("writes nothing for a contact's key the anchors have not confirmed", async () => {
|
|
624
|
-
// Pending, vetoed, and a recovery still in its delay all land here: the handshake is
|
|
625
|
-
// refused and neither a device record nor a user stub is created for the presenter.
|
|
626
|
-
const me = makeTestIdentity();
|
|
627
|
-
const other = makeTestIdentity();
|
|
628
|
-
const existingUser = {
|
|
629
|
-
userId: other.userId,
|
|
630
|
-
publicKey: `stored_${(0, utils_1.newid)()}`,
|
|
631
|
-
publicBoxKey: `stored_box_${(0, utils_1.newid)()}`,
|
|
632
|
-
name: "Alice",
|
|
633
|
-
};
|
|
634
|
-
for (const state of ["pending", "vetoed", "recovery-pending"]) {
|
|
635
|
-
const { usersTable, devTable } = setupMocks(null, existingUser, null);
|
|
636
|
-
key_change_1.evaluatePresentedKey.mockResolvedValueOnce({ accepted: false, state });
|
|
637
|
-
const result = await (0, get_trust_level_fn_1.getTrustLevelFn)(me)(other);
|
|
638
|
-
expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
|
|
639
|
-
expect(usersTable.save).not.toHaveBeenCalled();
|
|
640
|
-
expect(devTable.save).not.toHaveBeenCalled();
|
|
641
|
-
}
|
|
642
|
-
});
|
|
643
|
-
it("judges a group-only contact's new key against the group row and copies nothing into the personal table", async () => {
|
|
644
|
-
const me = makeTestIdentity();
|
|
645
|
-
const other = makeTestIdentity();
|
|
646
|
-
const groupUser = {
|
|
647
|
-
userId: other.userId,
|
|
648
|
-
publicKey: `stored_${(0, utils_1.newid)()}`,
|
|
649
|
-
publicBoxKey: `stored_box_${(0, utils_1.newid)()}`,
|
|
650
|
-
name: "Alice",
|
|
651
|
-
};
|
|
652
|
-
const { usersTable, devTable } = setupMocks(null, null, null);
|
|
653
|
-
const groupUsersTable = makeUsersTable(groupUser);
|
|
654
|
-
const groupTrustTable = makeTrustTable({ trustLevel: socket_type_1.TrustLevel.Known });
|
|
655
|
-
const groupContext = { dataContextId: "group" };
|
|
656
|
-
mockGetUserContext.mockResolvedValue({
|
|
657
|
-
userDataContext: {},
|
|
658
|
-
groupDataContexts: new Map([["group", groupContext]]),
|
|
659
|
-
});
|
|
660
|
-
mockUsers.mockImplementation(((context) => context === groupContext ? groupUsersTable : usersTable));
|
|
661
|
-
mockUserTrustLevels.mockImplementation(((context) => context === groupContext ? groupTrustTable : makeTrustTable(null)));
|
|
662
|
-
key_change_1.evaluatePresentedKey.mockResolvedValueOnce({
|
|
663
|
-
accepted: true,
|
|
664
|
-
state: "confirmed-rotation",
|
|
665
|
-
});
|
|
666
|
-
const result = await (0, get_trust_level_fn_1.getTrustLevelFn)(me)(other);
|
|
667
|
-
expect(key_change_1.evaluatePresentedKey).toHaveBeenCalledWith(expect.objectContaining({ userId: other.userId, record: groupUser }));
|
|
668
|
-
// The group row is the only record; it is not copied into the personal table.
|
|
669
|
-
expect(usersTable.save).not.toHaveBeenCalled();
|
|
670
|
-
expect(groupUsersTable.save).not.toHaveBeenCalled();
|
|
671
|
-
expect(devTable.save).toHaveBeenCalledTimes(1);
|
|
672
|
-
expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
|
|
673
|
-
});
|
|
674
|
-
it("treats another device of this user on a different key as untrusted", async () => {
|
|
675
|
-
const me = makeTestIdentity();
|
|
676
|
-
const otherDevice = { ...me, deviceId: (0, utils_1.newid)(), publicKey: `other_${(0, utils_1.newid)()}` };
|
|
677
|
-
setupMocks();
|
|
678
|
-
const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
|
|
679
|
-
const result = await getTrustLevel(otherDevice);
|
|
680
|
-
expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
|
|
681
|
-
expect(key_change_1.detectIdentityRotatedAway).toHaveBeenCalledWith(me, expect.objectContaining({ userId: me.userId, publicKey: otherDevice.publicKey }));
|
|
682
|
-
expect(mockUsers).not.toHaveBeenCalled();
|
|
683
|
-
});
|
|
684
|
-
});
|
|
685
|
-
});
|