generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
CurrentIdentity.ts
207 lines (166 loc) · 6.92 KB
/
CurrentIdentity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import BigNumber from 'bignumber.js';
import sinon from 'sinon';
import { CurrentIdentity, Identity, Venue } from '~/api/entities';
import { createVenue, inviteAccount, removeSecondaryKeys } from '~/api/procedures';
import { Context, TransactionQueue } from '~/base';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { SecondaryKey, SubCallback, VenueType } from '~/types';
import { tuple } from '~/types/utils';
describe('CurrentIdentity class', () => {
let context: Context;
beforeAll(() => {
entityMockUtils.initMocks();
dsMockUtils.initMocks();
});
beforeEach(() => {
context = dsMockUtils.getContextInstance();
});
afterEach(() => {
entityMockUtils.reset();
dsMockUtils.reset();
});
afterAll(() => {
entityMockUtils.cleanup();
dsMockUtils.cleanup();
});
test('should extend Identity', () => {
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: [],
},
];
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';
const identity = new CurrentIdentity({ did }, context);
const signers = [entityMockUtils.getAccountInstance({ address: 'someAccount' })];
const expectedQueue = ('someQueue' as unknown) as TransactionQueue<void>;
sinon
.stub(removeSecondaryKeys, 'prepare')
.withArgs({ signers }, context)
.resolves(expectedQueue);
const queue = await identity.removeSecondaryKeys({ signers });
expect(queue).toBe(expectedQueue);
});
});
describe('method: inviteAccount', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const did = 'someDid';
const identity = new CurrentIdentity({ did }, context);
const args = {
targetAccount: 'someAccount',
};
const expectedQueue = ('someQueue' as unknown) as TransactionQueue<void>;
sinon
.stub(inviteAccount, 'prepare')
.withArgs(args, context)
.resolves(expectedQueue);
const queue = await identity.inviteAccount(args);
expect(queue).toBe(expectedQueue);
});
});
describe('method: createVenue', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const did = 'someDid';
const identity = new CurrentIdentity({ did }, context);
const args = {
details: 'details',
type: VenueType.Distribution,
};
const expectedQueue = ('someQueue' as unknown) as TransactionQueue<Venue>;
sinon
.stub(createVenue, 'prepare')
.withArgs(args, context)
.resolves(expectedQueue);
const queue = await identity.createVenue(args);
expect(queue).toBe(expectedQueue);
});
});
describe('method: getPendingInstructions', () => {
test('should return all pending instructions in which the identity is involved', async () => {
const id1 = new BigNumber(1);
const id2 = new BigNumber(2);
const id3 = new BigNumber(3);
const did = 'someDid';
const identity = new CurrentIdentity({ did }, context);
const rawPortfolio = dsMockUtils.createMockPortfolioId({
did: dsMockUtils.createMockIdentityId(did),
kind: dsMockUtils.createMockPortfolioKind('Default'),
});
dsMockUtils.createQueryStub('settlement', 'userAuths', {
entries: [
tuple(
[rawPortfolio, dsMockUtils.createMockU64(id1.toNumber())],
dsMockUtils.createMockAuthorizationStatus('Pending')
),
tuple(
[rawPortfolio, dsMockUtils.createMockU64(id2.toNumber())],
dsMockUtils.createMockAuthorizationStatus('Pending')
),
tuple(
[rawPortfolio, dsMockUtils.createMockU64(id3.toNumber())],
dsMockUtils.createMockAuthorizationStatus('Pending')
),
],
});
/* eslint-disable @typescript-eslint/camelcase */
dsMockUtils.createQueryStub('settlement', 'instructionDetails', {
multi: [
dsMockUtils.createMockInstruction({
instruction_id: dsMockUtils.createMockU64(id1.toNumber()),
venue_id: dsMockUtils.createMockU64(),
status: dsMockUtils.createMockInstructionStatus('Pending'),
settlement_type: dsMockUtils.createMockSettlementType('SettleOnAuthorization'),
created_at: dsMockUtils.createMockOption(),
valid_from: dsMockUtils.createMockOption(),
}),
dsMockUtils.createMockInstruction({
instruction_id: dsMockUtils.createMockU64(id2.toNumber()),
venue_id: dsMockUtils.createMockU64(),
status: dsMockUtils.createMockInstructionStatus('Pending'),
settlement_type: dsMockUtils.createMockSettlementType('SettleOnAuthorization'),
created_at: dsMockUtils.createMockOption(),
valid_from: dsMockUtils.createMockOption(),
}),
dsMockUtils.createMockInstruction({
instruction_id: dsMockUtils.createMockU64(id3.toNumber()),
venue_id: dsMockUtils.createMockU64(),
status: dsMockUtils.createMockInstructionStatus('Unknown'),
settlement_type: dsMockUtils.createMockSettlementType('SettleOnAuthorization'),
created_at: dsMockUtils.createMockOption(),
valid_from: dsMockUtils.createMockOption(),
}),
],
});
/* eslint-enable @typescript-eslint/camelcase */
const result = await identity.getPendingInstructions();
expect(result.length).toBe(2);
expect(result[0].id).toEqual(id1);
expect(result[1].id).toEqual(id2);
});
});
});