Skip to content

Commit 2f71fb6

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

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed

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

Lines changed: 180 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,183 @@ 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: deployedWorkerpoolAddress } =
414+
await iexecWorkerpoolOwner.workerpool.deployWorkerpool({
415+
description: 'payable test workerpool',
416+
owner: await iexecWorkerpoolOwner.wallet.getAddress(),
417+
});
418+
payableWorkerpoolAddress = deployedWorkerpoolAddress;
419+
420+
await iexecWorkerpoolOwner.order
421+
.createWorkerpoolorder({
422+
workerpool: deployedWorkerpoolAddress,
423+
category: 0,
424+
workerpoolprice,
425+
volume: 1000,
426+
tag: ['tee', 'scone'],
427+
})
428+
.then(iexecWorkerpoolOwner.order.signWorkerpoolorder)
429+
.then(iexecWorkerpoolOwner.order.publishWorkerpoolorder);
430+
});
431+
it(
432+
'should throw error when insufficient funds and allowDeposit is false',
433+
async () => {
434+
const { processProtectedData } = await import(
435+
'../../../src/lib/dataProtectorCore/processProtectedData.js'
436+
);
437+
438+
// wallet has enough nRLC
439+
await setNRlcBalance(wallet.address, workerpoolprice * 10e9);
440+
// but account has no enough funds to process the data (less than workerpoolprice)
441+
await iexec.account.deposit(1 * 10e9);
442+
443+
let caughtError: Error | undefined;
444+
try {
445+
await processProtectedData({
446+
iexec,
447+
protectedData: protectedData.address,
448+
app: appAddress,
449+
defaultWorkerpool: payableWorkerpoolAddress,
450+
workerpool: payableWorkerpoolAddress,
451+
workerpoolMaxPrice: 100000,
452+
secrets: {
453+
1: 'ProcessProtectedData test subject',
454+
2: 'email content for test processData',
455+
},
456+
args: '_args_test_process_data_',
457+
path: 'computed.json',
458+
waitForResult: false,
459+
});
460+
} catch (firstError) {
461+
try {
462+
await processProtectedData({
463+
iexec,
464+
protectedData: protectedData.address,
465+
app: appAddress,
466+
defaultWorkerpool: payableWorkerpoolAddress,
467+
workerpool: payableWorkerpoolAddress,
468+
workerpoolMaxPrice: 100000,
469+
secrets: {
470+
1: 'ProcessProtectedData test subject',
471+
2: 'email content for test processData',
472+
},
473+
args: '_args_test_process_data_',
474+
path: 'computed.json',
475+
waitForResult: false,
476+
});
477+
} catch (secondError) {
478+
caughtError = secondError as Error;
479+
}
480+
}
481+
482+
expect(caughtError).toBeDefined();
483+
expect(caughtError).toBeInstanceOf(Error);
484+
expect(caughtError?.message).toBe('Failed to process protected data');
485+
const causeMsg =
486+
caughtError?.errorCause?.message ||
487+
caughtError?.cause?.message ||
488+
caughtError?.cause ||
489+
caughtError?.errorCause;
490+
expect(causeMsg).toBe(
491+
`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`
492+
);
493+
},
494+
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
495+
);
496+
497+
it(
498+
'should process protected data when no funds are deposited and allowDeposit is true',
499+
async () => {
500+
const { processProtectedData } = await import(
501+
'../../../src/lib/dataProtectorCore/processProtectedData.js'
502+
);
503+
// wallet has enough nRLC but account has no funds
504+
await setNRlcBalance(wallet.address, workerpoolprice * 10e9);
505+
506+
const walletBefore = await iexec.wallet.checkBalances(
507+
await iexec.wallet.getAddress()
508+
);
509+
const res = await processProtectedData({
510+
iexec,
511+
protectedData: protectedData.address,
512+
513+
app: appAddress,
514+
defaultWorkerpool: workerpoolAddress,
515+
workerpool: workerpoolAddress,
516+
secrets: {
517+
1: 'ProcessProtectedData test subject',
518+
2: 'email content for test processData',
519+
},
520+
args: '_args_test_process_data_',
521+
path: 'computed.json',
522+
waitForResult: false,
523+
allowDeposit: true,
524+
});
525+
const walletAfter = await iexec.wallet.checkBalances(
526+
await iexec.wallet.getAddress()
527+
);
528+
expect(walletAfter.nRLC.lt(walletBefore.nRLC)).toBe(true);
529+
expect(res.dealId).toEqual(expect.any(String));
530+
expect(res.taskId).toEqual(expect.any(String));
531+
expect(res.txHash).toEqual(expect.any(String));
532+
},
533+
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
534+
);
535+
536+
it(
537+
'should process protected data when insufficient funds are deposited and allowDeposit is true',
538+
async () => {
539+
const { processProtectedData } = await import(
540+
'../../../src/lib/dataProtectorCore/processProtectedData.js'
541+
);
542+
// wallet has enough nRLC but account has insufficient funds
543+
await setNRlcBalance(wallet.address, workerpoolprice * 10e9);
544+
await iexec.account.deposit(10 * 10e9);
545+
546+
const walletBefore = await iexec.wallet.checkBalances(
547+
await iexec.wallet.getAddress()
548+
);
549+
const res = await processProtectedData({
550+
iexec,
551+
protectedData: protectedData.address,
552+
app: appAddress,
553+
defaultWorkerpool: workerpoolAddress,
554+
workerpool: workerpoolAddress,
555+
secrets: {
556+
1: 'ProcessProtectedData test subject',
557+
2: 'email content for test processData',
558+
},
559+
args: '_args_test_process_data_',
560+
path: 'computed.json',
561+
waitForResult: false,
562+
allowDeposit: true,
563+
});
564+
const walletAfter = await iexec.wallet.checkBalances(
565+
await iexec.wallet.getAddress()
566+
);
567+
expect(walletAfter.nRLC.lt(walletBefore.nRLC)).toBe(true);
568+
expect(res.dealId).toEqual(expect.any(String));
569+
expect(res.taskId).toEqual(expect.any(String));
570+
expect(res.txHash).toEqual(expect.any(String));
571+
},
572+
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
573+
);
574+
});
395575
});

0 commit comments

Comments
 (0)