Skip to content

Commit

Permalink
test: optimizing test
Browse files Browse the repository at this point in the history
Related #420
  • Loading branch information
tegefaulkes committed Jul 26, 2022
1 parent dd22047 commit a3fb46d
Show file tree
Hide file tree
Showing 25 changed files with 215 additions and 250 deletions.
42 changes: 34 additions & 8 deletions tests/PolykeyAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import path from 'path';
import fs from 'fs';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import PolykeyAgent from '@/PolykeyAgent';
import { utils as keysUtils } from '@/keys';
import { Status } from '@/status';
import { Schema } from '@/schema';
import * as errors from '@/errors';
import * as keysUtils from '@/keys/utils';
import config from '@/config';
import { promise } from '@/utils/index';
import * as testUtils from './utils';
import { globalRootKeyPems } from './globalRootKeyPems';

describe('PolykeyAgent', () => {
const password = 'password';
Expand All @@ -21,16 +21,15 @@ describe('PolykeyAgent', () => {
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
beforeAll(async () => {
const globalKeyPair = await testUtils.setupGlobalKeypair();
const privateKey = keysUtils.privateKeyFromPem(globalRootKeyPems[1]);
const publicKey = keysUtils.publicKeyFromPrivateKey(privateKey);
const keyPair = { privateKey, publicKey };
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValue(globalKeyPair);
.mockResolvedValue(keyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValue(globalKeyPair);
dataDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'polykey-test-'),
);
.mockResolvedValue(keyPair);
});
afterAll(async () => {
mockedGenerateKeyPair.mockRestore();
Expand All @@ -54,6 +53,9 @@ describe('PolykeyAgent', () => {
password,
nodePath,
logger,
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
});
await expect(pkAgent.destroy()).rejects.toThrow(
errors.ErrorPolykeyAgentRunning,
Expand All @@ -72,6 +74,9 @@ describe('PolykeyAgent', () => {
password,
nodePath,
logger,
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
});
let nodePathContents = await fs.promises.readdir(nodePath);
expect(nodePathContents).toContain(config.defaults.statusBase);
Expand Down Expand Up @@ -106,6 +111,9 @@ describe('PolykeyAgent', () => {
password,
nodePath,
logger,
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
});
const status = new Status({
statusPath,
Expand Down Expand Up @@ -136,6 +144,9 @@ describe('PolykeyAgent', () => {
password,
nodePath,
logger,
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
});
expect(await schema.readVersion()).toBe(config.stateVersion);
await pkAgent.stop();
Expand All @@ -158,6 +169,9 @@ describe('PolykeyAgent', () => {
password,
nodePath,
logger,
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
}),
).rejects.toThrow(errors.ErrorSchemaVersionTooNew);
// The 0 version will always be too old
Expand All @@ -174,6 +188,9 @@ describe('PolykeyAgent', () => {
password,
nodePath,
logger,
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
}),
).rejects.toThrow(errors.ErrorSchemaVersionTooOld);
});
Expand All @@ -185,6 +202,9 @@ describe('PolykeyAgent', () => {
password,
nodePath,
logger,
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
});
const prom = promise<KeyManagerChangeData>();
pkAgent.events.on(
Expand All @@ -209,6 +229,9 @@ describe('PolykeyAgent', () => {
password,
nodePath,
logger,
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
});
const prom = promise<KeyManagerChangeData>();
pkAgent.events.on(
Expand All @@ -233,6 +256,9 @@ describe('PolykeyAgent', () => {
password,
nodePath,
logger,
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
});
const prom = promise<KeyManagerChangeData>();
pkAgent.events.on(
Expand Down
21 changes: 6 additions & 15 deletions tests/PolykeyClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,18 @@ import fs from 'fs';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import { PolykeyClient, PolykeyAgent } from '@';
import { Session } from '@/sessions';
import { utils as keysUtils } from '@/keys';
import config from '@/config';
import * as testUtils from './utils';
import { globalRootKeyPems } from './globalRootKeyPems';

describe('PolykeyClient', () => {
const password = 'password';
const logger = new Logger('PolykeyClient Test', LogLevel.WARN, [
new StreamHandler(),
]);
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
let dataDir: string;
let nodePath: string;
let pkAgent: PolykeyAgent;
beforeAll(async () => {
const globalKeyPair = await testUtils.setupGlobalKeypair();
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValue(globalKeyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValue(globalKeyPair);
beforeEach(async () => {
dataDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'polykey-test-'),
);
Expand All @@ -35,16 +25,17 @@ describe('PolykeyClient', () => {
password,
nodePath,
logger,
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
});
});
afterAll(async () => {
afterEach(async () => {
await pkAgent.stop();
await fs.promises.rm(dataDir, {
force: true,
recursive: true,
});
mockedGenerateKeyPair.mockRestore();
mockedGenerateDeterministicKeyPair.mockRestore();
});
test('create PolykeyClient and connect to PolykeyAgent', async () => {
const pkClient = await PolykeyClient.createPolykeyClient({
Expand Down
2 changes: 1 addition & 1 deletion tests/bin/agent/start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ describe('start', () => {
},
global.defaultTimeout * 2,
);
runDescribeIfPlatforms('linux').only('start with global agent', () => {
runDescribeIfPlatforms('linux')('start with global agent', () => {
let agentDataDir;
let agent1Status: StatusLive;
let agent1Close: () => Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion tests/bin/bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('bootstrap', () => {
},
global.defaultTimeout * 2,
);
runTestIfPlatforms('linux', 'docker').only(
runTestIfPlatforms('linux', 'docker')(
'bootstraps node state from provided private key',
async () => {
const password = 'password';
Expand Down
2 changes: 1 addition & 1 deletion tests/bin/vaults/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ describe('CLI vaults', () => {
expect(result.stdout).not.toContain(writeF3Oid);
},
);
runTestIfPlatforms('linux', 'docker').todo('test formatting of the output');
test.todo('test formatting of the output');
});
describe('commandScanNode', () => {
runTestIfPlatforms('linux', 'docker')(
Expand Down
24 changes: 7 additions & 17 deletions tests/discovery/Discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import * as claimsUtils from '@/claims/utils';
import * as discoveryErrors from '@/discovery/errors';
import * as keysUtils from '@/keys/utils';
import * as testNodesUtils from '../nodes/utils';
import * as testUtils from '../utils';
import TestProvider from '../identities/TestProvider';
import { globalRootKeyPems } from '../globalRootKeyPems';

describe('Discovery', () => {
const password = 'password';
Expand All @@ -39,8 +39,6 @@ describe('Discovery', () => {
accessToken: 'abc123',
},
};
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
/**
* Shared GestaltGraph, IdentitiesManager, NodeManager for all tests
*/
Expand All @@ -60,21 +58,15 @@ describe('Discovery', () => {
let nodeA: PolykeyAgent;
let nodeB: PolykeyAgent;
let identityId: IdentityId;
beforeAll(async () => {
const globalKeyPair = await testUtils.setupGlobalKeypair();
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValueOnce(globalKeyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValueOnce(globalKeyPair);
beforeEach(async () => {
dataDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'polykey-test-'),
);
const keysPath = path.join(dataDir, 'keys');
keyManager = await KeyManager.createKeyManager({
password,
keysPath,
privateKeyPemOverride: globalRootKeyPems[0],
logger: logger.getChild('KeyManager'),
});
const dbPath = path.join(dataDir, 'db');
Expand Down Expand Up @@ -167,7 +159,7 @@ describe('Discovery', () => {
clientHost: '127.0.0.1' as Host,
},
keysConfig: {
rootKeyPairBits: 2048,
privateKeyPemOverride: globalRootKeyPems[1],
},
logger: logger.getChild('nodeA'),
});
Expand All @@ -181,7 +173,7 @@ describe('Discovery', () => {
clientHost: '127.0.0.1' as Host,
},
keysConfig: {
rootKeyPairBits: 2048,
privateKeyPemOverride: globalRootKeyPems[2],
},
logger: logger.getChild('nodeB'),
});
Expand All @@ -206,8 +198,8 @@ describe('Discovery', () => {
const [, claimEncoded] = await nodeB.sigchain.addClaim(identityClaim);
const claim = claimsUtils.decodeClaim(claimEncoded);
await testProvider.publishClaim(identityId, claim);
}, global.maxTimeout);
afterAll(async () => {
});
afterEach(async () => {
await nodeA.stop();
await nodeB.stop();
await nodeConnectionManager.stop();
Expand All @@ -225,8 +217,6 @@ describe('Discovery', () => {
force: true,
recursive: true,
});
mockedGenerateKeyPair.mockRestore();
mockedGenerateDeterministicKeyPair.mockRestore();
});
test('discovery readiness', async () => {
const discovery = await Discovery.createDiscovery({
Expand Down
18 changes: 4 additions & 14 deletions tests/grpc/GRPCServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,19 @@ import * as grpcUtils from '@/grpc/utils';
import * as keysUtils from '@/keys/utils';
import * as clientUtils from '@/client/utils';
import * as testGrpcUtils from './utils';
import * as testUtils from '../utils';
import { globalRootKeyPems } from '../globalRootKeyPems';

describe('GRPCServer', () => {
const logger = new Logger('GRPCServer Test', LogLevel.WARN, [
new StreamHandler(),
]);
const password = 'password';
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
let dataDir: string;
let keyManager: KeyManager;
let db: DB;
let sessionManager: SessionManager;
let authenticate: Authenticate;
beforeAll(async () => {
const globalKeyPair = await testUtils.setupGlobalKeypair();
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValue(globalKeyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValue(globalKeyPair);
beforeEach(async () => {
dataDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'polykey-test-'),
);
Expand All @@ -44,6 +35,7 @@ describe('GRPCServer', () => {
password,
keysPath,
logger,
privateKeyPemOverride: globalRootKeyPems[0],
});
const dbPath = path.join(dataDir, 'db');
db = await DB.createDB({
Expand All @@ -65,16 +57,14 @@ describe('GRPCServer', () => {
});
authenticate = clientUtils.authenticator(sessionManager, keyManager);
});
afterAll(async () => {
afterEach(async () => {
await sessionManager.stop();
await db.stop();
await keyManager.stop();
await fs.promises.rm(dataDir, {
force: true,
recursive: true,
});
mockedGenerateKeyPair.mockRestore();
mockedGenerateDeterministicKeyPair.mockRestore();
});
test('GRPCServer readiness', async () => {
const server = new GRPCServer({
Expand Down
9 changes: 2 additions & 7 deletions tests/nat/DMZ.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import config from '@/config';
import * as testNatUtils from './utils';
import { describeIf } from '../utils';
import * as testBinUtils from '../bin/utils';
import { globalRootKeyPems } from '../globalRootKeyPems';

describeIf(
process.platform === 'linux' &&
Expand Down Expand Up @@ -46,8 +47,6 @@ describeIf(
'start',
'--node-path',
path.join(dataDir, 'polykey'),
'--root-key-pair-bits',
'1024',
'--client-host',
'127.0.0.1',
'--proxy-host',
Expand All @@ -60,6 +59,7 @@ describeIf(
],
{
PK_PASSWORD: password,
PK_ROOT_KEY: globalRootKeyPems[0],
},
dataDir,
logger.getChild('agentProcess'),
Expand All @@ -81,12 +81,7 @@ describeIf(
forwardPort: expect.any(Number),
proxyHost: expect.any(String),
proxyPort: expect.any(Number),
recoveryCode: expect.any(String),
});
expect(
statusLiveData.recoveryCode.split(' ').length === 12 ||
statusLiveData.recoveryCode.split(' ').length === 24,
).toBe(true);
agentProcess.kill('SIGTERM');
let exitCode, signal;
[exitCode, signal] = await testBinUtils.processExit(agentProcess);
Expand Down
Loading

0 comments on commit a3fb46d

Please sign in to comment.