-
Notifications
You must be signed in to change notification settings - Fork 119
/
PrivacyTab.spec.js
484 lines (427 loc) · 15 KB
/
PrivacyTab.spec.js
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import PrivacyPage from "components/views/PrivacyPage";
import PrivacyTab from "components/views/PrivacyPage/PrivacyTab";
import { render } from "test-utils.js";
import { screen, waitFor } from "@testing-library/react";
import { act } from "react-dom/test-utils";
import { MIXED_ACCOUNT, CHANGE_ACCOUNT, DCR } from "constants";
import * as sel from "selectors";
import * as actM from "actions/AccountMixerActions";
import * as wl from "wallet";
import * as ca from "actions/ControlActions";
import * as sa from "actions/SnackbarActions";
const selectors = sel;
const wallet = wl;
const controlActions = ca;
const accountMixerActions = actM;
const snackbarActions = sa;
const mockDefaultAccount = {
hidden: false,
label: "default: 19 DCR",
name: "default",
accountName: "default",
spendable: 1900000000,
spendableAndUnit: "19 DCR",
total: 1900000000,
value: 0,
accountNumber: 0
};
const mockEmptyUnMixedAccount = {
hidden: false,
label: "unmixed: 0.0000000 DCR",
name: "unmixed",
accountName: "unmixed",
spendable: 0,
spendableAndUnit: "0.00000 DCR",
total: 0,
value: 1,
accountNumber: 1
};
const mockUnMixedAccount = {
hidden: false,
label: "unmixed: 10.0000000 DCR",
name: "unmixed",
accountName: "unmixed",
spendable: 1000000000,
spendableAndUnit: "10.00000 DCR",
total: 0,
value: 1,
accountNumber: 1
};
const mockMixedAccount = {
hidden: false,
label: "mixed: 249.79547928 DCR",
name: "mixed",
accountName: "mixed",
spendable: 24979547928,
spendableAndUnit: "249.79547928 DCR",
total: 24979547928,
value: 2,
accountNumber: 2
};
const mockSpendingAccounts = [
mockDefaultAccount,
mockMixedAccount,
mockUnMixedAccount
];
const mockVisibleAccounts = [
mockDefaultAccount,
mockMixedAccount,
mockUnMixedAccount
];
const mockCsppServer = "mockCsppServer.decred.org";
const mockCsppPort = "1234";
const mockMixedAccountBranch = 0;
const mockNextAddress = "TsiTfsjizPgzBrPxovheccayb4UbLRmQAqY";
const mockLogLine = "mock-log-line";
let mockCreateNeededAccounts;
let mockRunAccountMixer;
let mockStopAccountMixer;
let mockToggleAllowSendFromUnmixed;
let mockConstructTransactionAttempt;
let mockGetPrivacyLogs;
let mockDispatchSingleMessage;
beforeEach(() => {
selectors.currencyDisplay = jest.fn(() => DCR);
selectors.defaultSpendingAccount = jest.fn(() => mockMixedAccount);
selectors.visibleAccounts = jest.fn(() => mockVisibleAccounts);
selectors.getPrivacyEnabled = jest.fn(() => true);
selectors.isWatchingOnly = jest.fn(() => false);
selectors.getMixedAccountName = jest.fn(() => mockMixedAccount.name);
selectors.getChangeAccountName = jest.fn(() => mockUnMixedAccount.name);
selectors.getAccountMixerRunning = jest.fn(() => false);
selectors.walletService = jest.fn(() => {
return {};
});
selectors.nextAddress = jest.fn(() => mockNextAddress);
selectors.getRunningIndicator = jest.fn(() => false);
selectors.getMixedAccount = jest.fn(() => mockMixedAccount.value);
selectors.getChangeAccount = jest.fn(() => mockUnMixedAccount.value);
wallet.getBalance = jest.fn((_, acctId) =>
mockSpendingAccounts.find((acc) => acc.value == acctId)
);
selectors.balances = jest.fn(() => mockSpendingAccounts);
selectors.getMixedAccountSpendableBalance = jest.fn(
() => mockMixedAccount?.spendable
);
selectors.getChangeAccountSpendableBalance = jest.fn(
() => mockUnMixedAccount?.spendable
);
selectors.defaultSpendingAccountDisregardMixedAccount = jest.fn(
() => mockDefaultAccount
);
mockGetPrivacyLogs = wallet.getPrivacyLogs = jest.fn(() => Promise.reject());
mockCreateNeededAccounts = accountMixerActions.createNeededAccounts = jest.fn(
() => () => {}
);
mockRunAccountMixer = accountMixerActions.runAccountMixer = jest.fn(
() => () => Promise.resolve()
);
mockStopAccountMixer = accountMixerActions.stopAccountMixer = jest.fn(
() => () => {}
);
selectors.getCsppServer = jest.fn(() => mockCsppServer);
selectors.getCsppPort = jest.fn(() => mockCsppPort);
selectors.getMixedAccountBranch = jest.fn(() => mockMixedAccountBranch);
selectors.getAllowSendFromUnmixed = jest.fn(() => false);
mockToggleAllowSendFromUnmixed =
accountMixerActions.toggleAllowSendFromUnmixed = jest.fn(() => () => {});
controlActions.getNextAddressAttempt = jest.fn(() => () => {});
mockConstructTransactionAttempt = controlActions.constructTransactionAttempt =
jest.fn(() => () => {});
mockDispatchSingleMessage = snackbarActions.dispatchSingleMessage = jest.fn(
() => () => {}
);
});
const getCreateDefaultAccountsButton = () =>
screen.getByRole("button", { name: "Create default Accounts" });
const getCreateNeededAccountsButton = () =>
screen.getByRole("button", { name: "Create Needed Accounts" });
const getPrivatePassphraseInput = () =>
screen.getByLabelText("Private Passphrase");
const getCancelButton = () => screen.getByRole("button", { name: "Cancel" });
const getContinueButton = () =>
screen.getByRole("button", { name: "Continue" });
const getStartMixerButton = () =>
screen.getByRole("button", { name: "Start Mixer" });
const getStopMixerButton = () =>
screen.getByRole("button", { name: "Stop Mixer" });
const getEnableSendingFromUnmixedAccountButton = () =>
screen.getByRole("button", { name: "Enable sending from unmixed accounts" });
const getConfirmInput = () => screen.getByTestId("confirm-input");
const getPrivacyCheckbox = () => screen.getByTestId("privacyCheckbox");
const getSendToSelfButton = () =>
screen.getByRole("button", { name: "Send to Self" });
test("create default mixer accounts on `Privacy Configuration` view", async () => {
selectors.getMixedAccount = jest.fn(() => null);
selectors.getChangeAccount = jest.fn(() => null);
selectors.balances = jest.fn(() => []);
const { user } = render(<PrivacyPage />);
await user.click(screen.getByText("Privacy"));
expect(screen.getByText("Privacy Configuration")).toBeInTheDocument();
const testPassphrase = "test-passphrase";
await user.click(getCreateDefaultAccountsButton());
await user.type(getPrivatePassphraseInput(), testPassphrase);
// cancel first
await user.click(getCancelButton());
await user.click(getCreateDefaultAccountsButton());
await user.type(getPrivatePassphraseInput(), testPassphrase);
await user.click(getContinueButton());
expect(mockCreateNeededAccounts).toHaveBeenCalledWith(
testPassphrase,
MIXED_ACCOUNT,
CHANGE_ACCOUNT
);
});
test("create needed mixer accounts on `Privacy Configuration` view", async () => {
selectors.getMixedAccount = jest.fn(() => null);
selectors.getChangeAccount = jest.fn(() => null);
const { user } = render(<PrivacyTab />);
expect(
screen.getByText(
/It looks like you already have one of the default accounts/i
).textContent
).toMatchInlineSnapshot(`
"It looks like you already have one of the default accounts: mixed and unmixed.
You will need to create 2 new accounts for using the mixer. This action can not be undone"
`);
expect(screen.getByText("Privacy Configuration")).toBeInTheDocument();
const testPassphrase = "test-passphrase";
const testMixedAccountName = "test-mixed-account-name";
const testUnMixedAccountName = "test-unmixed-account-name";
await user.click(getCreateNeededAccountsButton());
await user.type(getPrivatePassphraseInput(), testPassphrase);
// cancel first
await user.click(getCancelButton());
await user.click(getCreateNeededAccountsButton());
await user.type(getPrivatePassphraseInput(), testPassphrase);
await user.type(
screen.getByLabelText("Mixed Account Name"),
testMixedAccountName
);
await user.type(
screen.getByLabelText("Unmixed Account Name"),
testUnMixedAccountName
);
await user.click(getContinueButton());
expect(mockCreateNeededAccounts).toHaveBeenCalledWith(
testPassphrase,
testMixedAccountName,
testUnMixedAccountName
);
});
test("test insufficient unmixed account balance error message", async () => {
selectors.balances = jest.fn(() => [
mockDefaultAccount,
mockMixedAccount,
mockEmptyUnMixedAccount
]);
selectors.getChangeAccountSpendableBalance = jest.fn(() => 0);
wallet.getBalance = jest.fn((_, acctId) =>
[mockDefaultAccount, mockMixedAccount, mockEmptyUnMixedAccount].find(
(acc) => acc.value == acctId
)
);
render(<PrivacyTab />, {
initialState: {
control: {
getNextAddressResponse: {
accountNumber: mockUnMixedAccount.value
}
}
}
});
await waitFor(() =>
expect(screen.getByText("Unmixed Balance").parentNode.className).toMatch(
/balanceError/i
)
);
expect(
screen.getByText("Insufficient unmixed account balance")
).toBeInTheDocument();
expect(getStartMixerButton().disabled).toBe(true);
});
test("start coin mixer", async () => {
const { user } = render(<PrivacyTab />, {
initialState: {
control: {
getNextAddressResponse: {
accountNumber: mockDefaultAccount.value
}
}
}
});
await waitFor(() =>
expect(
screen.getByText("Unmixed Balance").parentNode.className
).not.toMatch(/balanceError/i)
);
expect(
screen.queryByText("Insufficient unmixed account balance")
).not.toBeInTheDocument();
const startMixerButton = getStartMixerButton();
expect(startMixerButton.disabled).toBe(false);
expect(screen.getByText("Mixer is not running")).toBeInTheDocument();
expect(
screen.getByText("Unmixed Balance").parentNode.textContent
).toMatchInlineSnapshot('"10.00000 DCRUnmixed Balance"');
expect(
screen.getByText("Mixed Balance").parentNode.textContent
).toMatchInlineSnapshot('"249.79547928 DCRMixed Balance"');
const testPassphrase = "test-passphrase";
await user.click(startMixerButton);
// cancel first
await user.click(getCancelButton());
await user.click(startMixerButton);
await user.type(getPrivatePassphraseInput(), testPassphrase);
await user.click(getContinueButton());
expect(mockRunAccountMixer).toHaveBeenCalledWith({
changeAccount: mockUnMixedAccount.accountNumber,
mixedAccount: mockMixedAccount.accountNumber,
passphrase: testPassphrase,
csppServer: `${mockCsppServer}:${mockCsppPort}`,
mixedAccountBranch: mockMixedAccountBranch
});
});
test("stop coin mixer", async () => {
selectors.getAccountMixerRunning = jest.fn(() => true);
const { user } = render(<PrivacyTab />, {
initialState: {
control: {
getNextAddressResponse: {
accountNumber: mockDefaultAccount.value
}
}
}
});
expect(screen.getByText("Mixer is running")).toBeInTheDocument();
await user.click(getStopMixerButton());
expect(mockStopAccountMixer).toHaveBeenCalled();
});
test("mixer is disabled (Autobuyer running)", () => {
selectors.getRunningIndicator = jest.fn(() => true);
render(<PrivacyTab />, {
initialState: {
control: {
getNextAddressResponse: {
accountNumber: mockDefaultAccount.value
}
}
}
});
expect(getSendToSelfButton().disabled).toBe(true);
expect(getStartMixerButton().disabled).toBe(true);
});
test("allow sending from unmixed accounts", async () => {
const { user } = render(<PrivacyTab />, {
initialState: {
control: {
getNextAddressResponse: {
accountNumber: mockDefaultAccount.value
}
}
}
});
const checkbox = getPrivacyCheckbox();
expect(checkbox.checked).toBe(false);
await user.click(checkbox);
expect(screen.getByText("Sending from Unmixed Accounts")).toBeInTheDocument();
expect(getEnableSendingFromUnmixedAccountButton().disabled).toBe(true);
// cancel first
await user.click(getCancelButton());
await user.click(checkbox);
// type random text, enable button should stay disabled
await user.type(getConfirmInput(), "random text");
expect(getEnableSendingFromUnmixedAccountButton().disabled).toBe(true);
await user.clear(getConfirmInput());
await user.type(getConfirmInput(), "I understand the risks");
await user.click(getEnableSendingFromUnmixedAccountButton());
expect(mockToggleAllowSendFromUnmixed).toHaveBeenCalledWith(true);
});
test("sending from unmixed accounts is allowed already", async () => {
selectors.getAllowSendFromUnmixed = jest.fn(() => true);
const { user } = render(<PrivacyTab />, {
initialState: {
control: {
getNextAddressResponse: {
accountNumber: mockDefaultAccount.value
}
}
}
});
const checkbox = getPrivacyCheckbox();
expect(checkbox.checked).toBe(true);
await user.click(checkbox);
expect(mockToggleAllowSendFromUnmixed).toHaveBeenCalledWith(false);
});
test("Send to Unmixed Account form", async () => {
const { user } = render(<PrivacyTab />, {
initialState: {
control: {
getNextAddressResponse: {
accountNumber: mockDefaultAccount.value
}
}
}
});
const sendToSelfBtn = getSendToSelfButton();
const amountInput = screen.getByLabelText("Amount:");
const testAmount = "12";
expect(sendToSelfBtn.disabled).toBe(true);
await user.type(amountInput, testAmount);
await waitFor(() =>
expect(mockConstructTransactionAttempt).toHaveBeenCalled()
);
await user.click(
screen.getByText("Send all funds from selected account").nextElementSibling
);
expect(screen.getByText("Amount:").nextElementSibling.textContent).toBe(
"19.00000 DCR"
);
await user.click(
screen.getByText("Cancel sending all funds").nextElementSibling
);
expect(screen.getByLabelText("Amount:").value).toBe("");
});
test("check logs", async () => {
jest.useFakeTimers();
mockGetPrivacyLogs = wallet.getPrivacyLogs = jest.fn(() =>
Promise.resolve(mockLogLine)
);
const { user } = render(<PrivacyTab />, {
initialState: {
control: {
getNextAddressResponse: {
accountNumber: mockDefaultAccount.value
}
}
}
});
const logsLabel = screen.getByText("Logs");
await user.click(logsLabel);
act(() => {
jest.advanceTimersByTime(2001);
});
await waitFor(() => expect(mockGetPrivacyLogs).toHaveBeenCalledTimes(2));
await waitFor(() => screen.getByText(mockLogLine));
await user.click(logsLabel);
await waitFor(() =>
expect(screen.queryByText(mockLogLine)).not.toBeInTheDocument()
);
});
test("privacy configuration have to be disabled in watching only (already have mixed or unmixed account)", async () => {
selectors.getMixedAccount = jest.fn(() => null);
selectors.getChangeAccount = jest.fn(() => null);
selectors.isWatchingOnly = jest.fn(() => true);
const { user } = render(<PrivacyTab />);
await user.click(getCreateNeededAccountsButton());
expect(mockDispatchSingleMessage).toHaveBeenCalledTimes(1);
});
test("privacy configuration have to be disabled in watching only ", async () => {
selectors.getMixedAccount = jest.fn(() => null);
selectors.getChangeAccount = jest.fn(() => null);
selectors.isWatchingOnly = jest.fn(() => true);
selectors.balances = jest.fn(() => []);
const { user } = render(<PrivacyTab />);
await user.click(getCreateDefaultAccountsButton());
expect(mockDispatchSingleMessage).toHaveBeenCalledTimes(1);
});