Skip to content

Commit

Permalink
tests: updating share.test.ts tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Oct 16, 2024
1 parent 34cbe96 commit 73cb13b
Showing 1 changed file with 130 additions and 49 deletions.
179 changes: 130 additions & 49 deletions tests/vaults/share.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import PolykeyAgent from 'polykey/dist/PolykeyAgent';
import * as ids from 'polykey/dist/ids';
import * as nodesUtils from 'polykey/dist/nodes/utils';
import * as vaultsUtils from 'polykey/dist/vaults/utils';
import NotificationsManager from 'polykey/dist/notifications/NotificationsManager';
import * as keysUtils from 'polykey/dist/keys/utils';
import * as testUtils from '../utils';

describe('commandShare', () => {
const password = 'password';
const logger = new Logger('CLI Test', LogLevel.WARN, [new StreamHandler()]);
let dataDir: string;
let polykeyAgent: PolykeyAgent;
let nodePathLocal: string;
let nodePathPeer: string;
let polykeyAgentLocal: PolykeyAgent;
let polykeyAgentPeer: PolykeyAgent;
let command: Array<string>;
let vaultNumber: number;
let vaultName: VaultName;
Expand All @@ -41,10 +43,12 @@ describe('commandShare', () => {
dataDir = await fs.promises.mkdtemp(
path.join(globalThis.tmpDir, 'polykey-test-'),
);
polykeyAgent = await PolykeyAgent.createPolykeyAgent({
nodePathLocal = path.join(dataDir, 'nodeLocal');
nodePathPeer = path.join(dataDir, 'nodePeer');
polykeyAgentLocal = await PolykeyAgent.createPolykeyAgent({
password,
options: {
nodePath: dataDir,
nodePath: nodePathLocal,
agentServiceHost: '127.0.0.1',
clientServiceHost: '127.0.0.1',
keys: {
Expand All @@ -55,66 +59,143 @@ describe('commandShare', () => {
},
logger: logger,
});
await polykeyAgent.gestaltGraph.setNode(node1);
await polykeyAgent.gestaltGraph.setNode(node2);
await polykeyAgent.gestaltGraph.setNode(node3);
polykeyAgentPeer = await PolykeyAgent.createPolykeyAgent({
password,
options: {
nodePath: nodePathPeer,
agentServiceHost: '127.0.0.1',
clientServiceHost: '127.0.0.1',
keys: {
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
passwordMemLimit: keysUtils.passwordMemLimits.min,
strictMemoryLock: false,
},
},
logger: logger,
});
await polykeyAgentLocal.gestaltGraph.setNode(node1);
await polykeyAgentLocal.gestaltGraph.setNode(node2);
await polykeyAgentLocal.gestaltGraph.setNode(node3);

vaultNumber = 0;
vaultName = genVaultName();
command = [];
});
afterEach(async () => {
await polykeyAgent.stop();
await polykeyAgentLocal.stop();
await polykeyAgentPeer.stop();
await fs.promises.rm(dataDir, {
force: true,
recursive: true,
});
});

test('Should share a vault', async () => {
const mockedSendNotification = jest.spyOn(
NotificationsManager.prototype,
'sendNotification',
const vaultId = await polykeyAgentLocal.vaultManager.createVault(vaultName);
const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId);
const targetNodeId = polykeyAgentPeer.keyRing.getNodeId();
const targetNodeIdEncoded = nodesUtils.encodeNodeId(targetNodeId);
await polykeyAgentLocal.gestaltGraph.setNode({
nodeId: targetNodeId,
});
await polykeyAgentPeer.gestaltGraph.setNode({
nodeId: polykeyAgentLocal.keyRing.getNodeId(),
});
await polykeyAgentPeer.gestaltGraph.setGestaltAction(
['node', polykeyAgentLocal.keyRing.getNodeId()],
'notify',
);
try {
// We don't want to actually send a notification
mockedSendNotification.mockResolvedValue({
notificationId: ids.generateNotificationIdFromTimestamp(Date.now()),
sendP: Promise.resolve(),
});
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId);
const targetNodeId = nodeIdGenerator();
const targetNodeIdEncoded = nodesUtils.encodeNodeId(targetNodeId);
await polykeyAgent.gestaltGraph.setNode({
nodeId: targetNodeId,
});
expect(
(await polykeyAgent.acl.getNodePerm(targetNodeId))?.vaults[vaultId],
).toBeUndefined();
expect(
(await polykeyAgentLocal.acl.getNodePerm(targetNodeId))?.vaults[vaultId],
).toBeUndefined();

command = [
'vaults',
'share',
'-np',
nodePathLocal,
vaultIdEncoded,
targetNodeIdEncoded,
];
const result = await testUtils.pkStdio([...command], {
env: { PK_PASSWORD: password },
cwd: nodePathLocal,
});
expect(result.exitCode).toBe(0);

// Check permission
const permissions1 = (await polykeyAgentLocal.acl.getNodePerm(targetNodeId))
?.vaults[vaultId];
expect(permissions1).toBeDefined();
expect(permissions1.pull).toBeDefined();
expect(permissions1.clone).toBeDefined();
});
test('sharing vault handles failure to send notification due to trust', async () => {
const vaultId = await polykeyAgentLocal.vaultManager.createVault(vaultName);
const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId);
const targetNodeId = polykeyAgentPeer.keyRing.getNodeId();
const targetNodeIdEncoded = nodesUtils.encodeNodeId(targetNodeId);
await polykeyAgentLocal.gestaltGraph.setNode({
nodeId: targetNodeId,
});
expect(
(await polykeyAgentLocal.acl.getNodePerm(targetNodeId))?.vaults[vaultId],
).toBeUndefined();

command = [
'vaults',
'share',
'-np',
dataDir,
vaultIdEncoded,
targetNodeIdEncoded,
];
const result = await testUtils.pkStdio([...command], {
env: { PK_PASSWORD: password },
cwd: dataDir,
});
expect(result.exitCode).toBe(0);
command = [
'vaults',
'share',
'-np',
nodePathLocal,
vaultIdEncoded,
targetNodeIdEncoded,
];
const result = await testUtils.pkStdio([...command], {
env: { PK_PASSWORD: password },
cwd: nodePathLocal,
});
// While the notification should fail the sharing of a vault should still succeed
expect(result.exitCode).toBe(0);

// Check permission
const permissions1 = (await polykeyAgentLocal.acl.getNodePerm(targetNodeId))
?.vaults[vaultId];
expect(permissions1).toBeDefined();
expect(permissions1.pull).toBeDefined();
expect(permissions1.clone).toBeDefined();
});
test('sharing vault handles failure to send notification due connection failure', async () => {
const vaultId = await polykeyAgentLocal.vaultManager.createVault(vaultName);
const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId);
const targetNodeId = nodeIdGenerator();
const targetNodeIdEncoded = nodesUtils.encodeNodeId(targetNodeId);
await polykeyAgentLocal.gestaltGraph.setNode({
nodeId: targetNodeId,
});
expect(
(await polykeyAgentLocal.acl.getNodePerm(targetNodeId))?.vaults[vaultId],
).toBeUndefined();

command = [
'vaults',
'share',
'-np',
nodePathLocal,
vaultIdEncoded,
targetNodeIdEncoded,
];
const result = await testUtils.pkStdio([...command], {
env: { PK_PASSWORD: password },
cwd: nodePathLocal,
});
// While the notification should fail the sharing of a vault should still succeed
expect(result.exitCode).toBe(0);

// Check permission
const permissions1 = (await polykeyAgent.acl.getNodePerm(targetNodeId))
?.vaults[vaultId];
expect(permissions1).toBeDefined();
expect(permissions1.pull).toBeDefined();
expect(permissions1.clone).toBeDefined();
} finally {
mockedSendNotification.mockRestore();
}
// Check permission
const permissions1 = (await polykeyAgentLocal.acl.getNodePerm(targetNodeId))
?.vaults[vaultId];
expect(permissions1).toBeDefined();
expect(permissions1.pull).toBeDefined();
expect(permissions1.clone).toBeDefined();
});
});

0 comments on commit 73cb13b

Please sign in to comment.