Skip to content

Commit

Permalink
wip: optimizing client tests
Browse files Browse the repository at this point in the history
Related #420
  • Loading branch information
tegefaulkes committed Jul 26, 2022
1 parent 9015a80 commit dd22047
Show file tree
Hide file tree
Showing 30 changed files with 69 additions and 453 deletions.
17 changes: 4 additions & 13 deletions tests/client/GRPCClientClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@ import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import GRPCClientClient from '@/client/GRPCClientClient';
import PolykeyAgent from '@/PolykeyAgent';
import Session from '@/sessions/Session';
import * as keysUtils from '@/keys/utils';
import * as clientErrors from '@/client/errors';
import * as utilsPB from '@/proto/js/polykey/v1/utils/utils_pb';
import { timerStart } from '@/utils';
import * as testClientUtils from './utils';
import * as testUtils from '../utils';
import { globalRootKeyPems } from '../globalRootKeyPems';

describe(GRPCClientClient.name, () => {
const password = 'password';
const logger = new Logger(`${GRPCClientClient.name} test`, LogLevel.WARN, [
new StreamHandler(),
]);
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
let client: GRPCClientClient;
let server: grpc.Server;
let port: number;
Expand All @@ -31,13 +28,6 @@ describe(GRPCClientClient.name, () => {
let nodeId: NodeId;
let session: Session;
beforeAll(async () => {
const globalKeyPair = await testUtils.setupGlobalKeypair();
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValue(globalKeyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValue(globalKeyPair);
dataDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'polykey-test-'),
);
Expand All @@ -46,6 +36,9 @@ describe(GRPCClientClient.name, () => {
password,
nodePath,
logger: logger,
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
});
nodeId = pkAgent.keyManager.getNodeId();
[server, port] = await testClientUtils.openTestClientServer({
Expand All @@ -67,8 +60,6 @@ describe(GRPCClientClient.name, () => {
force: true,
recursive: true,
});
mockedGenerateKeyPair.mockRestore();
mockedGenerateDeterministicKeyPair.mockRestore();
});
test('cannot be called when destroyed', async () => {
client = await GRPCClientClient.createGRPCClientClient({
Expand Down
25 changes: 6 additions & 19 deletions tests/client/service/gestaltsGestaltTrustByNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ import * as claimsUtils from '@/claims/utils';
import * as keysUtils from '@/keys/utils';
import * as clientUtils from '@/client/utils/utils';
import * as nodesUtils from '@/nodes/utils';
import * as testUtils from '../../utils';
import TestProvider from '../../identities/TestProvider';
import { globalRootKeyPems } from '../../globalRootKeyPems';

describe('gestaltsGestaltTrustByNode', () => {
const logger = new Logger('gestaltsGestaltTrustByNode test', LogLevel.WARN, [
const logger = new Logger('gestaltsGestaltTrustByNode test', LogLevel.INFO, [
new StreamHandler(),
]);
const password = 'helloworld';
Expand All @@ -52,22 +52,7 @@ describe('gestaltsGestaltTrustByNode', () => {
let nodeId: NodeIdEncoded;
const nodeChainData: ChainData = {};
let mockedRequestChainData: jest.SpyInstance;
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
beforeAll(async () => {
const globalKeyPair = await testUtils.setupGlobalKeypair();
const nodeKeyPair = await keysUtils.generateKeyPair(2048);
mockedRequestChainData = jest
.spyOn(NodeManager.prototype, 'requestChainData')
.mockResolvedValue(nodeChainData);
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValueOnce(nodeKeyPair)
.mockResolvedValue(globalKeyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValueOnce(nodeKeyPair)
.mockResolvedValue(globalKeyPair);
nodeDataDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'trusted-node-'),
);
Expand All @@ -81,6 +66,9 @@ describe('gestaltsGestaltTrustByNode', () => {
agentHost: '127.0.0.1' as Host,
clientHost: '127.0.0.1' as Host,
},
keysConfig: {
privateKeyPemOverride: globalRootKeyPems[0],
},
logger,
});
nodeId = nodesUtils.encodeNodeId(node.keyManager.getNodeId());
Expand All @@ -106,8 +94,6 @@ describe('gestaltsGestaltTrustByNode', () => {
force: true,
recursive: true,
});
mockedGenerateKeyPair.mockRestore();
mockedGenerateDeterministicKeyPair.mockRestore();
mockedRequestChainData.mockRestore();
});
const authToken = 'abc123';
Expand Down Expand Up @@ -136,6 +122,7 @@ describe('gestaltsGestaltTrustByNode', () => {
password,
keysPath,
logger,
privateKeyPemOverride: globalRootKeyPems[1],
});
const dbPath = path.join(dataDir, 'db');
db = await DB.createDB({
Expand Down
18 changes: 4 additions & 14 deletions tests/client/service/identitiesClaim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import identitiesClaim from '@/client/service/identitiesClaim';
import { ClientServiceService } from '@/proto/js/polykey/v1/client_service_grpc_pb';
import * as identitiesPB from '@/proto/js/polykey/v1/identities/identities_pb';
import * as clientUtils from '@/client/utils/utils';
import * as keysUtils from '@/keys/utils';
import * as claimsUtils from '@/claims/utils';
import * as nodesUtils from '@/nodes/utils';
import * as validationErrors from '@/validation/errors';
import * as testUtils from '../../utils';
import TestProvider from '../../identities/TestProvider';
import { expectRemoteError } from '../../utils';
import { globalRootKeyPems } from '../../globalRootKeyPems';

describe('identitiesClaim', () => {
const logger = new Logger('identitiesClaim test', LogLevel.WARN, [
Expand All @@ -54,32 +53,22 @@ describe('identitiesClaim', () => {
const claimId = claimsUtils.createClaimIdGenerator(
nodesUtils.decodeNodeId(claimData.node)!,
)();
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
let mockedAddClaim: jest.SpyInstance;
const dummyNodeManager = { setNode: jest.fn() } as unknown as NodeManager;
beforeAll(async () => {
const globalKeyPair = await testUtils.setupGlobalKeypair();
const privateKey = globalRootKeyPems[0];
const claim = await claimsUtils.createClaim({
privateKey: keysUtils.keyPairToPem(globalKeyPair).privateKey,
privateKey: privateKey,
hPrev: null,
seq: 0,
data: claimData,
kid: claimData.node,
});
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValue(globalKeyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValue(globalKeyPair);
mockedAddClaim = jest
.spyOn(Sigchain.prototype, 'addClaim')
.mockResolvedValue([claimId, claim]);
});
afterAll(async () => {
mockedGenerateKeyPair.mockRestore();
mockedGenerateDeterministicKeyPair.mockRestore();
mockedAddClaim.mockRestore();
});
const authToken = 'abc123';
Expand All @@ -105,6 +94,7 @@ describe('identitiesClaim', () => {
password,
keysPath,
logger,
privateKeyPemOverride: globalRootKeyPems[0],
});
const dbPath = path.join(dataDir, 'db');
db = await DB.createDB({
Expand Down
15 changes: 2 additions & 13 deletions tests/client/service/keysCertsChainGet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { ClientServiceService } from '@/proto/js/polykey/v1/client_service_grpc_
import * as utilsPB from '@/proto/js/polykey/v1/utils/utils_pb';
import * as keysPB from '@/proto/js/polykey/v1/keys/keys_pb';
import * as clientUtils from '@/client/utils/utils';
import * as keysUtils from '@/keys/utils';
import * as testUtils from '../../utils';
import { globalRootKeyPems } from '../../globalRootKeyPems';

describe('keysCertsChainGet', () => {
const logger = new Logger('keysCertsChainGet test', LogLevel.WARN, [
Expand All @@ -24,24 +23,13 @@ describe('keysCertsChainGet', () => {
metaServer;
const certs = ['cert1', 'cert2', 'cert3'];
let mockedGetRootCertChainPems: jest.SpyInstance;
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
beforeAll(async () => {
const globalKeyPair = await testUtils.setupGlobalKeypair();
mockedGetRootCertChainPems = jest
.spyOn(KeyManager.prototype, 'getRootCertChainPems')
.mockResolvedValue(certs);
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValue(globalKeyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValue(globalKeyPair);
});
afterAll(async () => {
mockedGetRootCertChainPems.mockRestore();
mockedGenerateKeyPair.mockRestore();
mockedGenerateDeterministicKeyPair.mockRestore();
});
let dataDir: string;
let keyManager: KeyManager;
Expand All @@ -56,6 +44,7 @@ describe('keysCertsChainGet', () => {
password,
keysPath,
logger,
privateKeyPemOverride: globalRootKeyPems[0],
});
const clientService = {
keysCertsChainGet: keysCertsChainGet({
Expand Down
15 changes: 2 additions & 13 deletions tests/client/service/keysCertsGet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { ClientServiceService } from '@/proto/js/polykey/v1/client_service_grpc_
import * as utilsPB from '@/proto/js/polykey/v1/utils/utils_pb';
import * as keysPB from '@/proto/js/polykey/v1/keys/keys_pb';
import * as clientUtils from '@/client/utils/utils';
import * as keysUtils from '@/keys/utils';
import * as testUtils from '../../utils';
import { globalRootKeyPems } from '../../globalRootKeyPems';

describe('keysCertsGet', () => {
const logger = new Logger('keysCertsGet test', LogLevel.WARN, [
Expand All @@ -23,24 +22,13 @@ describe('keysCertsGet', () => {
const authenticate = async (metaClient, metaServer = new Metadata()) =>
metaServer;
let mockedGetRootCertPem: jest.SpyInstance;
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
beforeAll(async () => {
const globalKeyPair = await testUtils.setupGlobalKeypair();
mockedGetRootCertPem = jest
.spyOn(KeyManager.prototype, 'getRootCertPem')
.mockReturnValue('rootCertPem');
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValue(globalKeyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValue(globalKeyPair);
});
afterAll(async () => {
mockedGetRootCertPem.mockRestore();
mockedGenerateKeyPair.mockRestore();
mockedGenerateDeterministicKeyPair.mockRestore();
});
let dataDir: string;
let keyManager: KeyManager;
Expand All @@ -55,6 +43,7 @@ describe('keysCertsGet', () => {
password,
keysPath,
logger,
privateKeyPemOverride: globalRootKeyPems[0],
});
const clientService = {
keysCertsGet: keysCertsGet({
Expand Down
19 changes: 2 additions & 17 deletions tests/client/service/keysEncryptDecrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import keysDecrypt from '@/client/service/keysDecrypt';
import { ClientServiceService } from '@/proto/js/polykey/v1/client_service_grpc_pb';
import * as keysPB from '@/proto/js/polykey/v1/keys/keys_pb';
import * as clientUtils from '@/client/utils/utils';
import * as keysUtils from '@/keys/utils';
import * as testUtils from '../../utils';
import { globalRootKeyPems } from '../../globalRootKeyPems';

describe('keysEncryptDecrypt', () => {
const logger = new Logger('keysEncryptDecrypt test', LogLevel.WARN, [
Expand All @@ -22,21 +21,6 @@ describe('keysEncryptDecrypt', () => {
const password = 'helloworld';
const authenticate = async (metaClient, metaServer = new Metadata()) =>
metaServer;
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
beforeAll(async () => {
const globalKeyPair = await testUtils.setupGlobalKeypair();
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValue(globalKeyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValue(globalKeyPair);
});
afterAll(async () => {
mockedGenerateKeyPair.mockRestore();
mockedGenerateDeterministicKeyPair.mockRestore();
});
let dataDir: string;
let keyManager: KeyManager;
let grpcServer: GRPCServer;
Expand All @@ -50,6 +34,7 @@ describe('keysEncryptDecrypt', () => {
password,
keysPath,
logger,
privateKeyPemOverride: globalRootKeyPems[0],
});
const clientService = {
keysEncrypt: keysEncrypt({
Expand Down
29 changes: 9 additions & 20 deletions tests/client/service/keysKeyPairRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as keysPB from '@/proto/js/polykey/v1/keys/keys_pb';
import * as utilsPB from '@/proto/js/polykey/v1/utils/utils_pb';
import * as clientUtils from '@/client/utils/utils';
import * as keysUtils from '@/keys/utils';
import * as testUtils from '../../utils';
import { globalRootKeyPems } from '../../globalRootKeyPems';

describe('keysKeyPairRoot', () => {
const logger = new Logger('keysKeyPairRoot test', LogLevel.WARN, [
Expand All @@ -22,22 +22,6 @@ describe('keysKeyPairRoot', () => {
const password = 'helloworld';
const authenticate = async (metaClient, metaServer = new Metadata()) =>
metaServer;
let globalKeyPair;
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
beforeAll(async () => {
globalKeyPair = await testUtils.setupGlobalKeypair();
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValue(globalKeyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValue(globalKeyPair);
});
afterAll(async () => {
mockedGenerateKeyPair.mockRestore();
mockedGenerateDeterministicKeyPair.mockRestore();
});
let dataDir: string;
let keyManager: KeyManager;
let grpcServer: GRPCServer;
Expand All @@ -51,6 +35,7 @@ describe('keysKeyPairRoot', () => {
password,
keysPath,
logger,
privateKeyPemOverride: globalRootKeyPems[0],
});
const clientService = {
keysKeyPairRoot: keysKeyPairRoot({
Expand Down Expand Up @@ -88,8 +73,12 @@ describe('keysKeyPairRoot', () => {
clientUtils.encodeAuthFromPassword(password),
);
expect(response).toBeInstanceOf(keysPB.KeyPair);
const keyPairPem = keysUtils.keyPairToPem(globalKeyPair);
expect(response.getPublic()).toBe(keyPairPem.publicKey);
expect(response.getPrivate()).toBe(keyPairPem.privateKey);
const publicKey = keysUtils.publicKeyToPem(
keysUtils.publicKeyFromPrivateKey(
keysUtils.privateKeyFromPem(globalRootKeyPems[0]),
),
);
expect(response.getPublic()).toBe(publicKey);
expect(response.getPrivate()).toBe(globalRootKeyPems[0]);
});
});
Loading

0 comments on commit dd22047

Please sign in to comment.