Skip to content

Commit

Permalink
test: removing global agent usage
Browse files Browse the repository at this point in the history
I've also used the key override where needed for `createPolykeyAgent`.

Related #404
  • Loading branch information
tegefaulkes committed Jul 27, 2022
1 parent 7a4cdde commit cb17957
Show file tree
Hide file tree
Showing 27 changed files with 423 additions and 531 deletions.
54 changes: 29 additions & 25 deletions tests/bin/agent/lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,87 @@ import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import Session from '@/sessions/Session';
import config from '@/config';
import * as testBinUtils from '../utils';
import * as testUtils from '../../utils';
import { runTestIfPlatforms } from '../../utils';
import { globalRootKeyPems } from '../../globalRootKeyPems';

jest.mock('prompts');
const mockedPrompts = mocked(prompts.prompt);

describe('lock', () => {
const logger = new Logger('lock test', LogLevel.WARN, [new StreamHandler()]);
let globalAgentDir;
let globalAgentPassword;
let globalAgentClose;
beforeAll(async () => {
({ globalAgentDir, globalAgentPassword, globalAgentClose } =
await testUtils.setupGlobalAgent(logger));
}, globalThis.maxTimeout);
afterAll(async () => {
await globalAgentClose();
let agentDir: string;
let agentPassword: string;
let agentClose: () => Promise<void>;
beforeEach(async () => {
({ agentDir, agentPassword, agentClose } =
await testBinUtils.setupTestAgent(
global.testCmd,
globalRootKeyPems[0],
logger,
));
});
afterEach(async () => {
await agentClose();
});
runTestIfPlatforms('linux', 'docker')(
'lock deletes the session token',
async () => {
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'unlock'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
const { exitCode } = await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'lock'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
},
globalAgentDir,
agentDir,
);
expect(exitCode).toBe(0);
const session = await Session.createSession({
sessionTokenPath: path.join(globalAgentDir, config.defaults.tokenBase),
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
fs,
logger,
});
expect(await session.readToken()).toBeUndefined();
await session.stop();
},
);
runTestIfPlatforms('linux')(
runTestIfPlatforms('linux', 'docker')(
'lock ensures re-authentication is required',
async () => {
const password = globalAgentPassword;
const password = agentPassword;
mockedPrompts.mockClear();
mockedPrompts.mockImplementation(async (_opts: any) => {
return { password };
});
await testBinUtils.pkStdio(
['agent', 'unlock'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
// Session token is deleted
await testBinUtils.pkStdio(
['agent', 'lock'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
},
globalAgentDir,
agentDir,
);
// Will prompt to reauthenticate
await testBinUtils.pkStdio(
['agent', 'status'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
},
globalAgentDir,
agentDir,
);
// Prompted for password 1 time
expect(mockedPrompts.mock.calls.length).toBe(1);
Expand Down
70 changes: 37 additions & 33 deletions tests/bin/agent/lockall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Session from '@/sessions/Session';
import config from '@/config';
import * as errors from '@/errors';
import * as testBinUtils from '../utils';
import * as testUtils from '../../utils';
import { runTestIfPlatforms } from '../../utils';
import { globalRootKeyPems } from '../../globalRootKeyPems';

/**
* Mock prompts module which is used prompt for password
Expand All @@ -20,37 +20,41 @@ describe('lockall', () => {
const logger = new Logger('lockall test', LogLevel.WARN, [
new StreamHandler(),
]);
let globalAgentDir;
let globalAgentPassword;
let globalAgentClose;
beforeAll(async () => {
({ globalAgentDir, globalAgentPassword, globalAgentClose } =
await testUtils.setupGlobalAgent(logger));
}, globalThis.maxTimeout);
afterAll(async () => {
await globalAgentClose();
let agentDir;
let agentPassword;
let agentClose;
beforeEach(async () => {
({ agentDir, agentPassword, agentClose } =
await testBinUtils.setupTestAgent(
global.testCmd,
globalRootKeyPems[0],
logger,
));
});
afterEach(async () => {
await agentClose();
});
runTestIfPlatforms('linux', 'docker')(
'lockall deletes the session token',
async () => {
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'unlock'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
const { exitCode } = await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'lockall'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
},
globalAgentDir,
agentDir,
);
expect(exitCode).toBe(0);
const session = await Session.createSession({
sessionTokenPath: path.join(globalAgentDir, config.defaults.tokenBase),
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
fs,
logger,
});
Expand All @@ -61,21 +65,21 @@ describe('lockall', () => {
runTestIfPlatforms('linux', 'docker')(
'lockall ensures reauthentication is required',
async () => {
const password = globalAgentPassword;
const password = agentPassword;
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'unlock'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'lockall'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
},
globalAgentDir,
agentDir,
);
// Token is deleted, reauthentication is required
mockedPrompts.mockClear();
Expand All @@ -85,9 +89,9 @@ describe('lockall', () => {
await testBinUtils.pkStdio(
['agent', 'status'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
},
globalAgentDir,
agentDir,
);
// Prompted for password 1 time
expect(mockedPrompts.mock.calls.length).toBe(1);
Expand All @@ -100,13 +104,13 @@ describe('lockall', () => {
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'unlock'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
const session = await Session.createSession({
sessionTokenPath: path.join(globalAgentDir, config.defaults.tokenBase),
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
fs,
logger,
});
Expand All @@ -115,21 +119,21 @@ describe('lockall', () => {
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'lockall'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
// Old token is invalid
const { exitCode, stderr } = await testBinUtils.pkStdioSwitch(
global.testCmd,
)(
['agent', 'status', '--format', 'json'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
PK_TOKEN: token,
},
globalAgentDir,
agentDir,
);
testBinUtils.expectProcessError(exitCode, stderr, [
new errors.ErrorClientAuthDenied(),
Expand Down
20 changes: 7 additions & 13 deletions tests/bin/agent/start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,9 +832,9 @@ describe('start', () => {
runDescribeIfPlatforms('linux').only('start with global agent', () => {
let agentDataDir;
let agent1Status: StatusLive;
let agent1Stop: () => void;
let agent1Close: () => Promise<void>;
let agent2Status: StatusLive;
let agent2Stop: () => void;
let agent2Close: () => Promise<void>;
let seedNodeId1: NodeId;
let seedNodeHost1: Host;
let seedNodePort1: Port;
Expand All @@ -846,21 +846,15 @@ describe('start', () => {
agentDataDir = await fs.promises.mkdtemp(
path.join(global.tmpDir, 'polykey-test-'),
);
const agent1Path = path.join(agentDataDir, 'agent1');
await fs.promises.mkdir(agent1Path);
({ agentStatus: agent1Status, agentStop: agent1Stop } =
({ agentStatus: agent1Status, agentClose: agent1Close } =
await testBinUtils.setupTestAgent(
undefined,
agent1Path,
globalRootKeyPems[0],
logger,
));
const agent2Path = path.join(agentDataDir, 'agent2');
await fs.promises.mkdir(agent2Path);
({ agentStatus: agent2Status, agentStop: agent2Stop } =
({ agentStatus: agent2Status, agentClose: agent2Close } =
await testBinUtils.setupTestAgent(
undefined,
agent2Path,
globalRootKeyPems[1],
logger,
));
Expand All @@ -870,10 +864,10 @@ describe('start', () => {
seedNodeId2 = agent2Status.data.nodeId;
seedNodeHost2 = agent2Status.data.proxyHost;
seedNodePort2 = agent2Status.data.proxyPort;
}, globalThis.maxTimeout);
});
afterEach(async () => {
agent1Stop();
agent2Stop();
await agent1Close();
await agent2Close();
await fs.promises.rm(agentDataDir, {
force: true,
recursive: true,
Expand Down
Loading

0 comments on commit cb17957

Please sign in to comment.