Skip to content

Commit

Permalink
feat(identity): move getSecondaryKeys from CurrentIdentity to Identity
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorVicente committed Jun 28, 2021
1 parent 1a00869 commit 0165d02
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 67 deletions.
23 changes: 1 addition & 22 deletions src/api/entities/CurrentIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
toggleFreezeSecondaryKeys,
Venue,
} from '~/internal';
import { ProcedureMethod, SecondaryKey, Signer, SubCallback, UnsubCallback } from '~/types';
import { ProcedureMethod, Signer } from '~/types';
import { createProcedureMethod } from '~/utils/internal';

/**
Expand Down Expand Up @@ -71,27 +71,6 @@ export class CurrentIdentity extends Identity {
);
}

/**
* Get the list of secondary keys related to the Identity
*
* @note can be subscribed to
*/
public async getSecondaryKeys(): Promise<SecondaryKey[]>;
public async getSecondaryKeys(callback: SubCallback<SecondaryKey[]>): Promise<UnsubCallback>;

// eslint-disable-next-line require-jsdoc
public async getSecondaryKeys(
callback?: SubCallback<SecondaryKey[]>
): Promise<SecondaryKey[] | UnsubCallback> {
const { context } = this;

if (callback) {
return context.getSecondaryKeys(callback);
}

return context.getSecondaryKeys();
}

/**
* Remove a list of secondary keys associated with the Identity
*/
Expand Down
44 changes: 44 additions & 0 deletions src/api/entities/Identity/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
PortfolioCustodianRole,
Role,
RoleType,
SecondaryKey,
SubCallback,
TickerOwnerRole,
TokenCaaRole,
TokenOwnerRole,
Expand Down Expand Up @@ -940,4 +942,46 @@ describe('Identity class', () => {
expect(result).toEqual([expectedDistribution]);
});
});

describe('method: getSecondaryKeys', () => {
test('should return a list of Secondaries', async () => {
const fakeResult = [
{
signer: entityMockUtils.getAccountInstance({ address: 'someAddress' }),
permissions: {
tokens: null,
transactions: null,
transactionGroups: [],
portfolios: null,
},
},
];

dsMockUtils.configureMocks({ contextOptions: { secondaryKeys: fakeResult } });

const did = 'someDid';

const identity = new Identity({ did }, context);

const result = await identity.getSecondaryKeys();
expect(result).toEqual(fakeResult);
});

test('should allow subscription', async () => {
const unsubCallback = 'unsubCallBack';

const getSecondaryKeysStub = dsMockUtils
.getContextInstance()
.getSecondaryKeys.resolves(unsubCallback);

const did = 'someDid';

const identity = new Identity({ did }, context);

const callback = (() => [] as unknown) as SubCallback<SecondaryKey[]>;
const result = await identity.getSecondaryKeys(callback);
expect(result).toEqual(unsubCallback);
sinon.assert.calledWithExactly(getSecondaryKeysStub, callback);
});
});
});
22 changes: 22 additions & 0 deletions src/api/entities/Identity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
Order,
ResultSet,
Role,
SecondaryKey,
SubCallback,
UnsubCallback,
} from '~/types';
Expand Down Expand Up @@ -562,4 +563,25 @@ export class Identity extends Entity<UniqueIdentifiers> {
}
);
}

/**
* Get the list of secondary keys related to the Identity
*
* @note can be subscribed to
*/
public async getSecondaryKeys(): Promise<SecondaryKey[]>;
public async getSecondaryKeys(callback: SubCallback<SecondaryKey[]>): Promise<UnsubCallback>;

// eslint-disable-next-line require-jsdoc
public async getSecondaryKeys(
callback?: SubCallback<SecondaryKey[]>
): Promise<SecondaryKey[] | UnsubCallback> {
const { context } = this;

if (callback) {
return context.getSecondaryKeys(callback);
}

return context.getSecondaryKeys();
}
}
46 changes: 1 addition & 45 deletions src/api/entities/__tests__/CurrentIdentity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import sinon from 'sinon';

import { Context, CurrentIdentity, Identity, TransactionQueue, Venue } from '~/internal';
import { dsMockUtils, entityMockUtils, procedureMockUtils } from '~/testUtils/mocks';
import { SecondaryKey, SubCallback, VenueType } from '~/types';
import { VenueType } from '~/types';

jest.mock(
'~/base/Procedure',
Expand Down Expand Up @@ -38,48 +36,6 @@ describe('CurrentIdentity class', () => {
expect(CurrentIdentity.prototype instanceof Identity).toBe(true);
});

describe('method: getSecondaryKeys', () => {
test('should return a list of Secondaries', async () => {
const fakeResult = [
{
signer: entityMockUtils.getAccountInstance({ address: 'someAddress' }),
permissions: {
tokens: null,
transactions: null,
transactionGroups: [],
portfolios: null,
},
},
];

dsMockUtils.configureMocks({ contextOptions: { secondaryKeys: fakeResult } });

const did = 'someDid';

const identity = new CurrentIdentity({ did }, context);

const result = await identity.getSecondaryKeys();
expect(result).toEqual(fakeResult);
});

test('should allow subscription', async () => {
const unsubCallback = 'unsubCallBack';

const getSecondaryKeysStub = dsMockUtils
.getContextInstance()
.getSecondaryKeys.resolves(unsubCallback);

const did = 'someDid';

const identity = new CurrentIdentity({ did }, context);

const callback = (() => [] as unknown) as SubCallback<SecondaryKey[]>;
const result = await identity.getSecondaryKeys(callback);
expect(result).toEqual(unsubCallback);
sinon.assert.calledWithExactly(getSecondaryKeysStub, callback);
});
});

describe('method: removeSecondaryKeys', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const did = 'someDid';
Expand Down

0 comments on commit 0165d02

Please sign in to comment.