-
Notifications
You must be signed in to change notification settings - Fork 542
/
transaction-insight.test.ts
180 lines (162 loc) · 4.86 KB
/
transaction-insight.test.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
import { PermissionConstraint, PermissionType } from '@metamask/controllers';
import { SnapCaveatType } from '@metamask/snaps-utils';
import {
getTransactionInsightCaveatMapper,
getTransactionOriginCaveat,
transactionInsightCaveatSpecifications,
transactionInsightEndowmentBuilder,
} from './transaction-insight';
import { SnapEndowments } from '.';
describe('endowment:transaction-insight', () => {
const specification = transactionInsightEndowmentBuilder.specificationBuilder(
{},
);
it('builds the expected permission specification', () => {
expect(specification).toStrictEqual({
permissionType: PermissionType.Endowment,
targetKey: SnapEndowments.TransactionInsight,
allowedCaveats: [SnapCaveatType.TransactionOrigin],
endowmentGetter: expect.any(Function),
validator: expect.any(Function),
});
expect(specification.endowmentGetter()).toBeUndefined();
});
describe('validator', () => {
it('allows no caveats', () => {
expect(() =>
// @ts-expect-error Missing required permission types.
specification.validator({}),
).not.toThrow();
});
it('throws if the caveat is not a single "transactionOrigin"', () => {
expect(() =>
// @ts-expect-error Missing other required permission types.
specification.validator({
caveats: [{ type: 'foo', value: 'bar' }],
}),
).toThrow('Expected a single "transactionOrigin" caveat.');
expect(() =>
// @ts-expect-error Missing other required permission types.
specification.validator({
caveats: [
{ type: 'transactionOrigin', value: [] },
{ type: 'transactionOrigin', value: [] },
],
}),
).toThrow('Expected a single "transactionOrigin" caveat.');
});
});
});
describe('getTransactionOriginCaveat', () => {
it('returns the value from a transaction insight permission', () => {
const permission: PermissionConstraint = {
date: 0,
parentCapability: 'foo',
invoker: 'bar',
id: 'baz',
caveats: [
{
type: SnapCaveatType.TransactionOrigin,
value: true,
},
],
};
expect(getTransactionOriginCaveat(permission)).toStrictEqual(true);
});
it('returns null if the input is undefined', () => {
expect(getTransactionOriginCaveat(undefined)).toBeNull();
});
it('returns null if the permission does not have caveats', () => {
const permission: PermissionConstraint = {
date: 0,
parentCapability: 'foo',
invoker: 'bar',
id: 'baz',
caveats: null,
};
expect(getTransactionOriginCaveat(permission)).toBeNull();
});
it('throws if the permission does not have exactly one caveat', () => {
const permission: PermissionConstraint = {
date: 0,
parentCapability: 'foo',
invoker: 'bar',
id: 'baz',
caveats: [
{
type: SnapCaveatType.TransactionOrigin,
value: true,
},
{
type: SnapCaveatType.TransactionOrigin,
value: true,
},
],
};
expect(() => getTransactionOriginCaveat(permission)).toThrow(
'Assertion failed',
);
});
it('throws if the first caveat is not a "snapKeyring" caveat', () => {
const permission: PermissionConstraint = {
date: 0,
parentCapability: 'foo',
invoker: 'bar',
id: 'baz',
caveats: [
{
type: SnapCaveatType.PermittedCoinTypes,
value: 'foo',
},
],
};
expect(() => getTransactionOriginCaveat(permission)).toThrow(
'Assertion failed',
);
});
});
describe('getTransactionInsightCaveatMapper', () => {
it('maps input to a caveat', () => {
expect(
getTransactionInsightCaveatMapper({
allowTransactionOrigin: true,
}),
).toStrictEqual({
caveats: [
{
type: 'transactionOrigin',
value: true,
},
],
});
});
it('does not include caveat if input is empty object', () => {
expect(getTransactionInsightCaveatMapper({})).toStrictEqual({
caveats: null,
});
});
});
describe('transactionInsightCaveatSpecifications', () => {
describe('validator', () => {
it('throws if the caveat values are invalid', () => {
expect(() =>
transactionInsightCaveatSpecifications[
SnapCaveatType.TransactionOrigin
].validator?.(
// @ts-expect-error Missing value type.
{
type: SnapCaveatType.TransactionOrigin,
},
),
).toThrow('Expected a plain object.');
expect(() =>
transactionInsightCaveatSpecifications[
SnapCaveatType.TransactionOrigin
].validator?.({
type: SnapCaveatType.TransactionOrigin,
value: undefined,
}),
).toThrow('Expected caveat value to have type "boolean"');
});
});
});