-
Notifications
You must be signed in to change notification settings - Fork 86
/
actor.test.ts
208 lines (185 loc) · 5.96 KB
/
actor.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
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
208
import { IDL } from '@dfinity/candid';
import { Principal } from '@dfinity/principal';
import { Actor } from './actor';
import { HttpAgent, Nonce } from './agent';
import { Expiry, makeNonceTransform } from './agent/http/transforms';
import { CallRequest, SubmitRequestType, UnSigned } from './agent/http/types';
import * as cbor from './cbor';
import { requestIdOf } from './request_id';
const originalDateNowFn = global.Date.now;
beforeEach(() => {
global.Date.now = jest.fn(() => new Date(1000000).getTime());
});
afterEach(() => {
global.Date.now = originalDateNowFn;
});
describe('makeActor', () => {
// TODO: update tests to be compatible with changes to Certificate
it.skip('should encode calls', async () => {
const actorInterface = () => {
return IDL.Service({
greet: IDL.Func([IDL.Text], [IDL.Text]),
});
};
const expectedReplyArg = IDL.encode([IDL.Text], ['Hello, World!']);
const mockFetch: jest.Mock = jest
.fn()
.mockImplementationOnce((/*resource, init*/) => {
return Promise.resolve(
new Response(null, {
status: 202,
}),
);
})
.mockImplementationOnce((resource, init) => {
const body = cbor.encode({ status: 'received' });
return Promise.resolve(
new Response(body, {
status: 200,
}),
);
})
.mockImplementationOnce((resource, init) => {
const body = cbor.encode({ status: 'processing' });
return Promise.resolve(
new Response(body, {
status: 200,
}),
);
})
.mockImplementationOnce((resource, init) => {
const body = cbor.encode({
status: 'replied',
reply: {
arg: expectedReplyArg,
},
});
return Promise.resolve(
new Response(body, {
status: 200,
}),
);
});
const methodName = 'greet';
const argValue = 'Name';
const arg = IDL.encode([IDL.Text], [argValue]);
const canisterId = Principal.fromText('2chl6-4hpzw-vqaaa-aaaaa-c');
const principal = await Principal.anonymous();
const sender = principal.toUint8Array();
const nonces = [
new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]) as Nonce,
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]) as Nonce,
new Uint8Array([2, 3, 4, 5, 6, 7, 8, 9]) as Nonce,
new Uint8Array([3, 4, 5, 6, 7, 8, 9, 0]) as Nonce,
new Uint8Array([4, 5, 6, 7, 8, 9, 0, 1]) as Nonce,
];
const expectedCallRequest = {
content: {
request_type: SubmitRequestType.Call,
canister_id: canisterId,
method_name: methodName,
arg,
nonce: nonces[0],
sender,
ingress_expiry: new Expiry(300000),
},
} as UnSigned<CallRequest>;
const expectedCallRequestId = await requestIdOf(expectedCallRequest.content);
let nonceCount = 0;
const httpAgent = new HttpAgent({ fetch: mockFetch, disableNonce: true });
httpAgent.addTransform(makeNonceTransform(() => nonces[nonceCount++]));
const actor = Actor.createActor(actorInterface, { canisterId, agent: httpAgent });
const reply = await actor.greet(argValue);
expect(reply).toEqual(IDL.decode([IDL.Text], expectedReplyArg)[0]);
const { calls } = mockFetch.mock;
expect(calls.length).toBe(5);
expect(calls[0]).toEqual([
`http://localhost/api/v2/canister/${canisterId.toText()}/call`,
{
method: 'POST',
headers: {
'Content-Type': 'application/cbor',
},
body: cbor.encode(expectedCallRequest),
},
]);
expect(calls[1]).toEqual([
`http://localhost/api/v2/canister/${canisterId.toText()}/read_state`,
{
method: 'POST',
headers: {
'Content-Type': 'application/cbor',
},
body: cbor.encode({
content: {
request_type: 'request_status',
request_id: expectedCallRequestId,
ingress_expiry: new Expiry(300000),
},
}),
},
]);
expect(calls[2][0]).toBe('http://localhost/api/v1/read');
expect(calls[2][1]).toEqual({
method: 'POST',
headers: {
'Content-Type': 'application/cbor',
},
body: cbor.encode({
content: {
request_type: 'request_status',
request_id: expectedCallRequestId,
ingress_expiry: new Expiry(300000),
},
}),
});
expect(calls[3][0]).toBe('http://localhost/api/v1/read');
expect(calls[3][1]).toEqual({
method: 'POST',
headers: {
'Content-Type': 'application/cbor',
},
body: cbor.encode({
content: {
request_type: 'request_status',
request_id: expectedCallRequestId,
ingress_expiry: new Expiry(300000),
},
}),
});
expect(calls[4][0]).toBe('http://localhost/api/v1/read');
expect(calls[4][1]).toEqual({
method: 'POST',
headers: {
'Content-Type': 'application/cbor',
},
body: cbor.encode({
content: {
request_type: 'request_status',
request_id: expectedCallRequestId,
ingress_expiry: new Expiry(300000),
},
}),
});
});
it('should allow its agent to be invalidated', async () => {
const mockFetch = jest.fn();
const actorInterface = () => {
return IDL.Service({
greet: IDL.Func([IDL.Text], [IDL.Text]),
});
};
const httpAgent = new HttpAgent({ fetch: mockFetch, host: 'http://localhost' });
const canisterId = Principal.fromText('2chl6-4hpzw-vqaaa-aaaaa-c');
const actor = Actor.createActor(actorInterface, { canisterId, agent: httpAgent });
Actor.agentOf(actor).invalidateIdentity();
try {
await actor.greet('test');
} catch (error) {
expect(error.message).toBe(
"This identity has expired due this application's security policy. Please refresh your authentication.",
);
}
});
});
// TODO: tests for rejected, unknown time out