forked from dfinity/pic-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.spec.ts
More file actions
321 lines (271 loc) · 9.47 KB
/
todo.spec.ts
File metadata and controls
321 lines (271 loc) · 9.47 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import { resolve } from 'node:path';
import { Principal } from '@dfinity/principal';
import { AnonymousIdentity } from '@dfinity/agent';
import { Actor, PocketIc, createIdentity } from 'pic-js-mops';
import { _SERVICE, idlFactory } from '../../declarations/todo.did';
const WASM_PATH = resolve(
__dirname,
'..',
'..',
'..',
'..',
'.dfx',
'local',
'canisters',
'todo',
'todo.wasm.gz',
);
describe('Todo', () => {
let pic: PocketIc;
let actor: Actor<_SERVICE>;
let canisterId: Principal;
const alice = createIdentity('superSecretAlicePassword');
const bob = createIdentity('superSecretBobPassword');
beforeEach(async () => {
pic = await PocketIc.create(process.env.PIC_URL);
const fixture = await pic.setupCanister<_SERVICE>({
idlFactory,
wasm: WASM_PATH,
});
actor = fixture.actor;
canisterId = fixture.canisterId;
});
afterEach(async () => {
await pic.tearDown();
});
describe('with an anonymous user', () => {
const identity = new AnonymousIdentity();
const expectedError =
'Anonymous principals are not allowed to call this method';
beforeEach(() => {
actor.setIdentity(identity);
});
it('should prevent creating a todo', async () => {
await expect(
actor.create_todo({
text: 'Learn Rust',
}),
).rejects.toThrow(expectedError);
});
it('should prevent getting todos', async () => {
await expect(actor.get_todos()).rejects.toThrow(expectedError);
});
it('should prevent updating a todo', async () => {
await expect(
actor.update_todo(1n, {
text: ['Learn Rust'],
done: [],
}),
).rejects.toThrow(expectedError);
});
it('should prevent deleting a todo', async () => {
await expect(actor.delete_todo(1n)).rejects.toThrow(expectedError);
});
});
describe('with alice', () => {
beforeEach(() => {
actor.setIdentity(alice);
});
it('should allow creating a todo', async () => {
const initialGetResponse = await actor.get_todos();
const createResponse = await actor.create_todo({ text: 'Learn Rust' });
const getResponse = await actor.get_todos();
expect(initialGetResponse.todos).toHaveLength(0);
expect(getResponse.todos).toHaveLength(1);
expect(getResponse.todos).toContainEqual({
id: createResponse.id,
text: 'Learn Rust',
done: false,
});
});
it('should allow updating a todo', async () => {
const initialGetResponse = await actor.get_todos();
const createResponse = await actor.create_todo({
text: 'Learn Rust',
});
const afterCreateGetResponse = await actor.get_todos();
await actor.update_todo(createResponse.id, {
text: ['Learn Rust and WebAssembly'],
done: [],
});
const afterUpdateGetResponse = await actor.get_todos();
expect(initialGetResponse.todos).toHaveLength(0);
expect(afterCreateGetResponse.todos).toHaveLength(1);
expect(afterCreateGetResponse.todos).toContainEqual({
id: createResponse.id,
text: 'Learn Rust',
done: false,
});
expect(afterUpdateGetResponse.todos).toHaveLength(1);
expect(afterUpdateGetResponse.todos).toContainEqual({
id: createResponse.id,
text: 'Learn Rust and WebAssembly',
done: false,
});
});
it('should allow deleting a todo', async () => {
const initialGetResponse = await actor.get_todos();
const createResponse = await actor.create_todo({
text: 'Learn Rust',
});
const afterCreateGetResponse = await actor.get_todos();
await actor.delete_todo(createResponse.id);
const afterDeleteGetResponse = await actor.get_todos();
expect(initialGetResponse.todos).toHaveLength(0);
expect(afterCreateGetResponse.todos).toHaveLength(1);
expect(afterCreateGetResponse.todos).toContainEqual({
id: createResponse.id,
text: 'Learn Rust',
done: false,
});
expect(afterDeleteGetResponse.todos).toHaveLength(0);
});
});
describe('with alice and bob', () => {
it("should return alice's todos to alice and bob's todos to bob", async () => {
actor.setIdentity(alice);
const aliceCreateResponse = await actor.create_todo({
text: 'Learn Rust',
});
const aliceAfterCreateGetResponse = await actor.get_todos();
actor.setIdentity(bob);
const bobCreateResponse = await actor.create_todo({
text: 'Learn WebAssembly',
});
const bobAfterCreateGetResponse = await actor.get_todos();
expect(aliceAfterCreateGetResponse.todos).toHaveLength(1);
expect(aliceAfterCreateGetResponse.todos).toContainEqual({
id: aliceCreateResponse.id,
text: 'Learn Rust',
done: false,
});
expect(bobAfterCreateGetResponse.todos).toHaveLength(1);
expect(bobAfterCreateGetResponse.todos).toContainEqual({
id: bobCreateResponse.id,
text: 'Learn WebAssembly',
done: false,
});
});
it("should prevent bob from updating alice's todo", async () => {
actor.setIdentity(alice);
const aliceCreateResponse = await actor.create_todo({
text: 'Learn Rust',
});
actor.setIdentity(bob);
await expect(
actor.update_todo(aliceCreateResponse.id, {
text: ['Learn Rust and WebAssembly'],
done: [],
}),
).rejects.toThrow(
`Caller with principal ${bob
.getPrincipal()
.toText()} does not own todo with id ${aliceCreateResponse.id}`,
);
});
it('should prevent bob from deleting alices todo', async () => {
actor.setIdentity(alice);
const aliceCreateResponse = await actor.create_todo({
text: 'Learn Rust',
});
actor.setIdentity(bob);
await expect(actor.delete_todo(aliceCreateResponse.id)).rejects.toThrow(
`Caller with principal ${bob
.getPrincipal()
.toText()} does not own todo with id ${aliceCreateResponse.id}`,
);
});
it('should survive an upgrade', async () => {
actor.setIdentity(alice);
const aliceCreateResponse = await actor.create_todo({
text: 'Learn Rust',
});
actor.setIdentity(bob);
const bobCreateResponse = await actor.create_todo({
text: 'Learn WebAssembly',
});
await pic.upgradeCanister({ canisterId, wasm: WASM_PATH });
actor.setIdentity(alice);
const aliceAfterUpgradeGetResponse = await actor.get_todos();
actor.setIdentity(bob);
const bobAfterUpgradeGetResponse = await actor.get_todos();
expect(aliceAfterUpgradeGetResponse.todos).toHaveLength(1);
expect(aliceAfterUpgradeGetResponse.todos).toContainEqual({
id: aliceCreateResponse.id,
text: 'Learn Rust',
done: false,
});
expect(bobAfterUpgradeGetResponse.todos).toHaveLength(1);
expect(bobAfterUpgradeGetResponse.todos).toContainEqual({
id: bobCreateResponse.id,
text: 'Learn WebAssembly',
done: false,
});
});
it('should survive a reset and stable memory restore', async () => {
actor.setIdentity(alice);
const aliceCreateResponse = await actor.create_todo({
text: 'Learn Rust',
});
actor.setIdentity(bob);
const bobCreateResponse = await actor.create_todo({
text: 'Learn WebAssembly',
});
const stableMemory = await pic.getStableMemory(canisterId);
await pic.reinstallCode({ canisterId, wasm: WASM_PATH });
await pic.setStableMemory(canisterId, stableMemory);
actor.setIdentity(alice);
const aliceAfterUpgradeGetResponse = await actor.get_todos();
actor.setIdentity(bob);
const bobAfterUpgradeGetResponse = await actor.get_todos();
expect(aliceAfterUpgradeGetResponse.todos).toHaveLength(1);
expect(aliceAfterUpgradeGetResponse.todos).toContainEqual({
id: aliceCreateResponse.id,
text: 'Learn Rust',
done: false,
});
expect(bobAfterUpgradeGetResponse.todos).toHaveLength(1);
expect(bobAfterUpgradeGetResponse.todos).toContainEqual({
id: bobCreateResponse.id,
text: 'Learn WebAssembly',
done: false,
});
});
it('should stop and start the canister', async () => {
actor.setIdentity(alice);
const aliceCreateResponse = await actor.create_todo({
text: 'Learn Rust',
});
actor.setIdentity(bob);
const bobCreateResponse = await actor.create_todo({
text: 'Learn WebAssembly',
});
await pic.stopCanister({ canisterId });
actor.setIdentity(alice);
await expect(actor.get_todos()).rejects.toThrow(
`Canister ${canisterId} is stopped`,
);
actor.setIdentity(bob);
await expect(actor.get_todos()).rejects.toThrow(
`Canister ${canisterId} is stopped`,
);
await pic.startCanister({ canisterId });
actor.setIdentity(alice);
const aliceAfterStart = await actor.get_todos();
actor.setIdentity(bob);
const bobAfterStart = await actor.get_todos();
expect(aliceAfterStart.todos).toHaveLength(1);
expect(aliceAfterStart.todos).toContainEqual({
id: aliceCreateResponse.id,
text: 'Learn Rust',
done: false,
});
expect(bobAfterStart.todos).toHaveLength(1);
expect(bobAfterStart.todos).toContainEqual({
id: bobCreateResponse.id,
text: 'Learn WebAssembly',
done: false,
});
});
});
});