Skip to content

Commit

Permalink
test: importing test utils as testUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Aug 8, 2022
1 parent cfb0f99 commit 8ae818b
Show file tree
Hide file tree
Showing 36 changed files with 2,220 additions and 2,139 deletions.
63 changes: 29 additions & 34 deletions tests/bin/agent/lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import Session from '@/sessions/Session';
import config from '@/config';
import * as execUtils from '../../utils/exec';
import { testIf } from '../../utils';
import * as testUtils from '../../utils';
import { globalRootKeyPems } from '../../fixtures/globalRootKeyPems';
import {
isTestPlatformEmpty,
isTestPlatformDocker,
} from '../../utils/platform';

jest.mock('prompts');
const mockedPrompts = mocked(prompts.prompt);
Expand All @@ -30,35 +26,34 @@ describe('lock', () => {
afterEach(async () => {
await agentClose();
});
testIf(isTestPlatformEmpty || isTestPlatformDocker)(
'lock deletes the session token',
async () => {
await execUtils.pkStdio(
['agent', 'unlock'],
{
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
agentDir,
);
const { exitCode } = await execUtils.pkStdio(
['agent', 'lock'],
{
PK_NODE_PATH: agentDir,
},
agentDir,
);
expect(exitCode).toBe(0);
const session = await Session.createSession({
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
fs,
logger,
});
expect(await session.readToken()).toBeUndefined();
await session.stop();
},
);
testIf(isTestPlatformEmpty)(
testUtils.testIf(
testUtils.isTestPlatformEmpty || testUtils.isTestPlatformDocker,
)('lock deletes the session token', async () => {
await execUtils.pkStdio(
['agent', 'unlock'],
{
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
agentDir,
);
const { exitCode } = await execUtils.pkStdio(
['agent', 'lock'],
{
PK_NODE_PATH: agentDir,
},
agentDir,
);
expect(exitCode).toBe(0);
const session = await Session.createSession({
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
fs,
logger,
});
expect(await session.readToken()).toBeUndefined();
await session.stop();
});
testUtils.testIf(testUtils.isTestPlatformEmpty)(
'lock ensures re-authentication is required',
async () => {
const password = agentPassword;
Expand Down
142 changes: 68 additions & 74 deletions tests/bin/agent/lockall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ import Session from '@/sessions/Session';
import config from '@/config';
import * as errors from '@/errors';
import * as execUtils from '../../utils/exec';
import { testIf } from '../../utils';
import * as testUtils from '../../utils';
import { globalRootKeyPems } from '../../fixtures/globalRootKeyPems';
import {
isTestPlatformEmpty,
isTestPlatformDocker,
} from '../../utils/platform';

/**
* Mock prompts module which is used prompt for password
Expand All @@ -36,35 +32,34 @@ describe('lockall', () => {
afterEach(async () => {
await agentClose();
});
testIf(isTestPlatformEmpty || isTestPlatformDocker)(
'lockall deletes the session token',
async () => {
await execUtils.pkStdio(
['agent', 'unlock'],
{
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
agentDir,
);
const { exitCode } = await execUtils.pkStdio(
['agent', 'lockall'],
{
PK_NODE_PATH: agentDir,
},
agentDir,
);
expect(exitCode).toBe(0);
const session = await Session.createSession({
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
fs,
logger,
});
expect(await session.readToken()).toBeUndefined();
await session.stop();
},
);
testIf(isTestPlatformEmpty)(
testUtils.testIf(
testUtils.isTestPlatformEmpty || testUtils.isTestPlatformDocker,
)('lockall deletes the session token', async () => {
await execUtils.pkStdio(
['agent', 'unlock'],
{
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
agentDir,
);
const { exitCode } = await execUtils.pkStdio(
['agent', 'lockall'],
{
PK_NODE_PATH: agentDir,
},
agentDir,
);
expect(exitCode).toBe(0);
const session = await Session.createSession({
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
fs,
logger,
});
expect(await session.readToken()).toBeUndefined();
await session.stop();
});
testUtils.testIf(testUtils.isTestPlatformEmpty)(
'lockall ensures reauthentication is required',
async () => {
const password = agentPassword;
Expand Down Expand Up @@ -100,44 +95,43 @@ describe('lockall', () => {
mockedPrompts.mockClear();
},
);
testIf(isTestPlatformEmpty || isTestPlatformDocker)(
'lockall causes old session tokens to fail',
async () => {
await execUtils.pkStdio(
['agent', 'unlock'],
{
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
agentDir,
);
const session = await Session.createSession({
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
fs,
logger,
});
const token = await session.readToken();
await session.stop();
await execUtils.pkStdio(
['agent', 'lockall'],
{
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
agentDir,
);
// Old token is invalid
const { exitCode, stderr } = await execUtils.pkStdio(
['agent', 'status', '--format', 'json'],
{
PK_NODE_PATH: agentDir,
PK_TOKEN: token,
},
agentDir,
);
execUtils.expectProcessError(exitCode, stderr, [
new errors.ErrorClientAuthDenied(),
]);
},
);
testUtils.testIf(
testUtils.isTestPlatformEmpty || testUtils.isTestPlatformDocker,
)('lockall causes old session tokens to fail', async () => {
await execUtils.pkStdio(
['agent', 'unlock'],
{
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
agentDir,
);
const session = await Session.createSession({
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
fs,
logger,
});
const token = await session.readToken();
await session.stop();
await execUtils.pkStdio(
['agent', 'lockall'],
{
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
agentDir,
);
// Old token is invalid
const { exitCode, stderr } = await execUtils.pkStdio(
['agent', 'status', '--format', 'json'],
{
PK_NODE_PATH: agentDir,
PK_TOKEN: token,
},
agentDir,
);
execUtils.expectProcessError(exitCode, stderr, [
new errors.ErrorClientAuthDenied(),
]);
});
});
Loading

0 comments on commit 8ae818b

Please sign in to comment.