Skip to content

Commit 21865ee

Browse files
test(e2e): add and update tests for allowDeposit in processProtectedData
1 parent a0ce781 commit 21865ee

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

packages/sdk/tests/e2e/dataProtectorCore/processProtectedData.test.ts

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
MAX_EXPECTED_WEB2_SERVICES_TIME,
1111
deployRandomApp,
1212
getTestConfig,
13+
setNRlcBalance,
1314
} from '../../test-utils.js';
1415

1516
describe('dataProtectorCore.processProtectedData() (waitForResult: false)', () => {
@@ -392,4 +393,178 @@ describe('dataProtectorCore.processProtectedData() (waitForResult: false)', () =
392393
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
393394
);
394395
});
396+
397+
describe('allowDeposit', () => {
398+
let payableWorkerpoolAddress: string;
399+
const workerpoolprice = 1000;
400+
beforeAll(async () => {
401+
const workerpoolOwnerWallet = Wallet.createRandom();
402+
const [ethProvider, options] = getTestConfig(
403+
workerpoolOwnerWallet.privateKey
404+
);
405+
406+
const iexecWorkerpoolOwner = new IExec(
407+
{ ethProvider },
408+
options.iexecOptions
409+
);
410+
411+
await setNRlcBalance(workerpoolOwnerWallet.address, 100 * 10e9);
412+
await iexecWorkerpoolOwner.account.deposit(100 * 10e9);
413+
const { address: payableWorkerpoolAddress } =
414+
await iexecWorkerpoolOwner.workerpool.deployWorkerpool({
415+
description: 'payable test workerpool',
416+
owner: await iexecWorkerpoolOwner.wallet.getAddress(),
417+
});
418+
419+
await iexecWorkerpoolOwner.order
420+
.createWorkerpoolorder({
421+
workerpool: payableWorkerpoolAddress,
422+
category: 0,
423+
workerpoolprice,
424+
volume: 1000,
425+
tag: ['tee', 'scone'],
426+
})
427+
.then(iexecWorkerpoolOwner.order.signWorkerpoolorder)
428+
.then(iexecWorkerpoolOwner.order.publishWorkerpoolorder);
429+
});
430+
it(
431+
'should throw error when insufficient funds and allowDeposit is false',
432+
async () => {
433+
const { processProtectedData } = await import(
434+
'../../../src/lib/dataProtectorCore/processProtectedData.js'
435+
);
436+
437+
// wallet has enough nRLC
438+
await setNRlcBalance(wallet.address, workerpoolprice * 10e9);
439+
// but account has no enough funds to process the data (less than workerpoolprice)
440+
await iexec.account.deposit(1 * 10e9);
441+
442+
try {
443+
await processProtectedData({
444+
iexec,
445+
protectedData: protectedData.address,
446+
app: appAddress,
447+
defaultWorkerpool: payableWorkerpoolAddress,
448+
workerpool: payableWorkerpoolAddress,
449+
workerpoolMaxPrice: 100000,
450+
secrets: {
451+
1: 'ProcessProtectedData test subject',
452+
2: 'email content for test processData',
453+
},
454+
args: '_args_test_process_data_',
455+
path: 'computed.json',
456+
waitForResult: false,
457+
});
458+
} catch (error) {
459+
try {
460+
await processProtectedData({
461+
iexec,
462+
protectedData: protectedData.address,
463+
app: appAddress,
464+
defaultWorkerpool: payableWorkerpoolAddress,
465+
workerpool: payableWorkerpoolAddress,
466+
workerpoolMaxPrice: 100000,
467+
secrets: {
468+
1: 'ProcessProtectedData test subject',
469+
2: 'email content for test processData',
470+
},
471+
args: '_args_test_process_data_',
472+
path: 'computed.json',
473+
waitForResult: false,
474+
});
475+
} catch (error) {
476+
expect(error).toBeInstanceOf(Error);
477+
expect(error.message).toBe('Failed to process protected data');
478+
const causeMsg =
479+
error.errorCause?.message ||
480+
error.cause?.message ||
481+
error.cause ||
482+
error.errorCause;
483+
expect(causeMsg).toBe(
484+
`Cost per task (${workerpoolprice} nRlc) is greater than requester account stake (0). Orders can't be matched. If you are the requester, you should deposit to top up your account`
485+
);
486+
}
487+
}
488+
},
489+
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
490+
);
491+
492+
it(
493+
'should process protected data when no funds are deposited and allowDeposit is true',
494+
async () => {
495+
const { processProtectedData } = await import(
496+
'../../../src/lib/dataProtectorCore/processProtectedData.js'
497+
);
498+
// wallet has enough nRLC but account has no funds
499+
await setNRlcBalance(wallet.address, workerpoolprice * 10e9);
500+
501+
const walletBefore = await iexec.wallet.checkBalances(
502+
await iexec.wallet.getAddress()
503+
);
504+
const res = await processProtectedData({
505+
iexec,
506+
protectedData: protectedData.address,
507+
508+
app: appAddress,
509+
defaultWorkerpool: workerpoolAddress,
510+
workerpool: workerpoolAddress,
511+
secrets: {
512+
1: 'ProcessProtectedData test subject',
513+
2: 'email content for test processData',
514+
},
515+
args: '_args_test_process_data_',
516+
path: 'computed.json',
517+
waitForResult: false,
518+
allowDeposit: true,
519+
});
520+
const walletAfter = await iexec.wallet.checkBalances(
521+
await iexec.wallet.getAddress()
522+
);
523+
expect(walletAfter.nRLC.lt(walletBefore.nRLC)).toBe(true);
524+
expect(res.dealId).toEqual(expect.any(String));
525+
expect(res.taskId).toEqual(expect.any(String));
526+
expect(res.txHash).toEqual(expect.any(String));
527+
},
528+
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
529+
);
530+
531+
it(
532+
'should process protected data when insufficient funds are deposited and allowDeposit is true',
533+
async () => {
534+
const { processProtectedData } = await import(
535+
'../../../src/lib/dataProtectorCore/processProtectedData.js'
536+
);
537+
// wallet has enough nRLC but account has insufficient funds
538+
await setNRlcBalance(wallet.address, workerpoolprice * 10e9);
539+
await iexec.account.deposit(10 * 10e9);
540+
541+
const walletBefore = await iexec.wallet.checkBalances(
542+
await iexec.wallet.getAddress()
543+
);
544+
const res = await processProtectedData({
545+
iexec,
546+
protectedData: protectedData.address,
547+
app: appAddress,
548+
defaultWorkerpool: workerpoolAddress,
549+
workerpool: workerpoolAddress,
550+
secrets: {
551+
1: 'ProcessProtectedData test subject',
552+
2: 'email content for test processData',
553+
},
554+
args: '_args_test_process_data_',
555+
path: 'computed.json',
556+
waitForResult: false,
557+
allowDeposit: true,
558+
});
559+
const walletAfter = await iexec.wallet.checkBalances(
560+
await iexec.wallet.getAddress()
561+
);
562+
expect(walletAfter.nRLC.lt(walletBefore.nRLC)).toBe(true);
563+
expect(res.dealId).toEqual(expect.any(String));
564+
expect(res.taskId).toEqual(expect.any(String));
565+
expect(res.txHash).toEqual(expect.any(String));
566+
},
567+
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
568+
);
569+
});
395570
});

0 commit comments

Comments
 (0)