@@ -4,6 +4,12 @@ import {
44} from '@iexec/dataprotector' ;
55import { beforeAll , describe , expect , it } from '@jest/globals' ;
66import { HDNodeWallet } from 'ethers' ;
7+ import { IExec } from 'iexec' ;
8+ import { NULL_ADDRESS } from 'iexec/utils' ;
9+ import {
10+ DEFAULT_CHAIN_ID ,
11+ getChainDefaultConfig ,
12+ } from '../../src/config/config.js' ;
713import { IExecWeb3mail , WorkflowError } from '../../src/index.js' ;
814import {
915 MAX_EXPECTED_BLOCKTIME ,
@@ -22,12 +28,6 @@ import {
2228 getTestWeb3SignerProvider ,
2329 waitSubgraphIndexing ,
2430} from '../test-utils.js' ;
25- import { IExec } from 'iexec' ;
26- import { NULL_ADDRESS } from 'iexec/utils' ;
27- import {
28- DEFAULT_CHAIN_ID ,
29- getChainDefaultConfig ,
30- } from '../../src/config/config.js' ;
3131
3232describe ( 'web3mail.sendEmail()' , ( ) => {
3333 let consumerWallet : HDNodeWallet ;
@@ -41,14 +41,14 @@ describe('web3mail.sendEmail()', () => {
4141 let learnProdWorkerpoolAddress : string ;
4242 const iexecOptions = getTestIExecOption ( ) ;
4343 const prodWorkerpoolPublicPrice = 1000 ;
44-
44+ const workerpoolprice = 1_000 ;
4545 beforeAll ( async ( ) => {
4646 // (default) prod workerpool (not free) always available
4747 await createAndPublishWorkerpoolOrder (
4848 TEST_CHAIN . prodWorkerpool ,
4949 TEST_CHAIN . prodWorkerpoolOwnerWallet ,
5050 NULL_ADDRESS ,
51- 1_000 ,
51+ workerpoolprice ,
5252 prodWorkerpoolPublicPrice
5353 ) ;
5454 // learn prod pool (free) assumed always available
@@ -657,4 +657,78 @@ describe('web3mail.sendEmail()', () => {
657657 } ) ;
658658 } ) ;
659659 } ) ;
660+
661+ describe ( 'allowDeposit' , ( ) => {
662+ let protectData : ProtectedDataWithSecretProps ;
663+ consumerWallet = getRandomWallet ( ) ;
664+ const dataPricePerAccess = 1000 ;
665+ let web3mailConsumerInstance : IExecWeb3mail ;
666+ beforeAll ( async ( ) => {
667+ protectData = await dataProtector . protectData ( {
668+ data :
{ email :
'[email protected] ' } , 669+ name : 'test do not use' ,
670+ } ) ;
671+ await dataProtector . grantAccess ( {
672+ authorizedApp : getChainDefaultConfig ( DEFAULT_CHAIN_ID ) . dappAddress ,
673+ protectedData : protectData . address ,
674+ authorizedUser : consumerWallet . address , // consumer wallet
675+ numberOfAccess : 1000 ,
676+ pricePerAccess : dataPricePerAccess ,
677+ } ) ;
678+ await waitSubgraphIndexing ( ) ;
679+ web3mailConsumerInstance = new IExecWeb3mail (
680+ ...getTestConfig ( consumerWallet . privateKey )
681+ ) ;
682+ } , 2 * MAX_EXPECTED_BLOCKTIME ) ;
683+ it (
684+ 'should throw error if insufficient total balance to cover task cost and allowDeposit is false' ,
685+ async ( ) => {
686+ let error ;
687+ try {
688+ await web3mailConsumerInstance . sendEmail ( {
689+ emailSubject : 'e2e mail object for test' ,
690+ emailContent : 'e2e mail content for test' ,
691+ protectedData : protectData . address ,
692+ dataMaxPrice : dataPricePerAccess ,
693+ workerpoolMaxPrice : workerpoolprice ,
694+ allowDeposit : false ,
695+ } ) ;
696+ } catch ( err ) {
697+ error = err ;
698+ }
699+ expect ( error ) . toBeInstanceOf ( WorkflowError ) ;
700+ expect ( error ) . toBeInstanceOf ( Error ) ;
701+ expect ( error . message ) . toBe ( 'Failed to sendEmail' ) ;
702+ const causeMsg =
703+ error . errorCause ?. message ||
704+ error . cause ?. message ||
705+ error . cause ||
706+ error . errorCause ;
707+ expect ( causeMsg ) . toBe (
708+ `Cost per task (${
709+ dataPricePerAccess + workerpoolprice
710+ } ) 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`
711+ ) ;
712+ } ,
713+ 3 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
714+ ) ;
715+
716+ it (
717+ 'should send email after depositing sufficient funds to cover task cost when allowDeposit is true' ,
718+ async ( ) => {
719+ const result = await web3mailConsumerInstance . sendEmail ( {
720+ emailSubject : 'e2e mail object for test' ,
721+ emailContent : 'e2e mail content for test' ,
722+ protectedData : protectData . address ,
723+ dataMaxPrice : dataPricePerAccess ,
724+ workerpoolMaxPrice : workerpoolprice ,
725+ allowDeposit : true ,
726+ } ) ;
727+ expect ( result ) . toBeDefined ( ) ;
728+ expect ( result ) . toHaveProperty ( 'taskId' ) ;
729+ expect ( result ) . toHaveProperty ( 'dealId' ) ;
730+ } ,
731+ 3 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
732+ ) ;
733+ } ) ;
660734} ) ;
0 commit comments