|
| 1 | +import { |
| 2 | + IExecDataProtectorCore, |
| 3 | + ProcessBulkRequestResponse, |
| 4 | + ProtectedDataWithSecretProps, |
| 5 | +} from '@iexec/dataprotector'; |
| 6 | +import { beforeAll, beforeEach, describe, expect, it } from '@jest/globals'; |
| 7 | +import { HDNodeWallet } from 'ethers'; |
| 8 | +import { |
| 9 | + DEFAULT_CHAIN_ID, |
| 10 | + getChainDefaultConfig, |
| 11 | +} from '../../src/config/config.js'; |
| 12 | +import { |
| 13 | + Contact, |
| 14 | + IExecWeb3mail, |
| 15 | + SendEmailSingleResponse, |
| 16 | +} from '../../src/index.js'; |
| 17 | +import { |
| 18 | + MAX_EXPECTED_BLOCKTIME, |
| 19 | + MAX_EXPECTED_WEB2_SERVICES_TIME, |
| 20 | + MAX_EXPECTED_SUBGRAPH_INDEXING_TIME, |
| 21 | + TEST_CHAIN, |
| 22 | + createAndPublishAppOrders, |
| 23 | + createAndPublishWorkerpoolOrder, |
| 24 | + ensureSufficientStake, |
| 25 | + getRandomWallet, |
| 26 | + getTestConfig, |
| 27 | + getTestIExecOption, |
| 28 | + getTestWeb3SignerProvider, |
| 29 | + waitSubgraphIndexing, |
| 30 | +} from '../test-utils.js'; |
| 31 | +import { IExec } from 'iexec'; |
| 32 | +import { NULL_ADDRESS } from 'iexec/utils'; |
| 33 | + |
| 34 | +describe('web3mail.sendEmail() - Bulk Processing', () => { |
| 35 | + let consumerWallet: HDNodeWallet; |
| 36 | + let providerWallet: HDNodeWallet; |
| 37 | + let web3mail: IExecWeb3mail; |
| 38 | + let dataProtector: IExecDataProtectorCore; |
| 39 | + let validProtectedData1: ProtectedDataWithSecretProps; |
| 40 | + let validProtectedData2: ProtectedDataWithSecretProps; |
| 41 | + let validProtectedData3: ProtectedDataWithSecretProps; |
| 42 | + let consumerIExecInstance: IExec; |
| 43 | + let consumerDataProtectorInstance: IExecDataProtectorCore; |
| 44 | + const iexecOptions = getTestIExecOption(); |
| 45 | + const prodWorkerpoolPublicPrice = 1000; |
| 46 | + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); |
| 47 | + |
| 48 | + beforeAll(async () => { |
| 49 | + // Create workerpool orders |
| 50 | + await createAndPublishWorkerpoolOrder( |
| 51 | + TEST_CHAIN.prodWorkerpool, |
| 52 | + TEST_CHAIN.prodWorkerpoolOwnerWallet, |
| 53 | + NULL_ADDRESS, |
| 54 | + 1_000, |
| 55 | + prodWorkerpoolPublicPrice |
| 56 | + ); |
| 57 | + |
| 58 | + // Create app orders |
| 59 | + providerWallet = getRandomWallet(); |
| 60 | + const ethProvider = getTestWeb3SignerProvider( |
| 61 | + TEST_CHAIN.appOwnerWallet.privateKey |
| 62 | + ); |
| 63 | + const resourceProvider = new IExec({ ethProvider }, iexecOptions); |
| 64 | + await createAndPublishAppOrders( |
| 65 | + resourceProvider, |
| 66 | + defaultConfig!.dappAddress |
| 67 | + ); |
| 68 | + |
| 69 | + dataProtector = new IExecDataProtectorCore( |
| 70 | + ...getTestConfig(providerWallet.privateKey) |
| 71 | + ); |
| 72 | + |
| 73 | + // create valid protected data |
| 74 | + validProtectedData1 = await dataProtector.protectData({ |
| 75 | + data: { email: '[email protected]' }, |
| 76 | + name: 'bulk test 1', |
| 77 | + }); |
| 78 | + |
| 79 | + validProtectedData2 = await dataProtector.protectData({ |
| 80 | + data: { email: '[email protected]' }, |
| 81 | + name: 'bulk test 2', |
| 82 | + }); |
| 83 | + |
| 84 | + validProtectedData3 = await dataProtector.protectData({ |
| 85 | + data: { email: '[email protected]' }, |
| 86 | + name: 'bulk test 3', |
| 87 | + }); |
| 88 | + |
| 89 | + await waitSubgraphIndexing(); |
| 90 | + }, 5 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME + 5_000); |
| 91 | + |
| 92 | + beforeEach(async () => { |
| 93 | + consumerWallet = getRandomWallet(); |
| 94 | + const consumerEthProvider = getTestWeb3SignerProvider( |
| 95 | + consumerWallet.privateKey |
| 96 | + ); |
| 97 | + consumerIExecInstance = new IExec( |
| 98 | + { ethProvider: consumerEthProvider }, |
| 99 | + iexecOptions |
| 100 | + ); |
| 101 | + |
| 102 | + // Grant access with allowBulk for bulk processing |
| 103 | + await dataProtector.grantAccess({ |
| 104 | + authorizedApp: defaultConfig.dappAddress, |
| 105 | + protectedData: validProtectedData1.address, |
| 106 | + authorizedUser: consumerWallet.address, |
| 107 | + allowBulk: true, |
| 108 | + }); |
| 109 | + |
| 110 | + await dataProtector.grantAccess({ |
| 111 | + authorizedApp: defaultConfig.dappAddress, |
| 112 | + protectedData: validProtectedData2.address, |
| 113 | + authorizedUser: consumerWallet.address, |
| 114 | + allowBulk: true, |
| 115 | + }); |
| 116 | + |
| 117 | + await dataProtector.grantAccess({ |
| 118 | + authorizedApp: defaultConfig.dappAddress, |
| 119 | + protectedData: validProtectedData3.address, |
| 120 | + authorizedUser: consumerWallet.address, |
| 121 | + allowBulk: true, |
| 122 | + }); |
| 123 | + |
| 124 | + await waitSubgraphIndexing(); |
| 125 | + |
| 126 | + web3mail = new IExecWeb3mail(...getTestConfig(consumerWallet.privateKey)); |
| 127 | + consumerDataProtectorInstance = new IExecDataProtectorCore( |
| 128 | + ...getTestConfig(consumerWallet.privateKey) |
| 129 | + ); |
| 130 | + }, MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_SUBGRAPH_INDEXING_TIME); |
| 131 | + |
| 132 | + describe('Bulk email sending', () => { |
| 133 | + it( |
| 134 | + 'should successfully process bulk request', |
| 135 | + async () => { |
| 136 | + // Fetch contacts with allowBulk access |
| 137 | + const contacts: Contact[] = await web3mail.fetchMyContacts(); |
| 138 | + expect(contacts.length).toBeGreaterThanOrEqual(3); |
| 139 | + |
| 140 | + // Ensure consumer has sufficient stake |
| 141 | + await ensureSufficientStake( |
| 142 | + consumerIExecInstance, |
| 143 | + prodWorkerpoolPublicPrice |
| 144 | + ); |
| 145 | + |
| 146 | + const bulkOrders = contacts.map((contact) => contact.grantedAccess); |
| 147 | + |
| 148 | + // Prepare email content encryption and upload (like sendEmail does for single) |
| 149 | + const emailSubject = 'Bulk test subject'; |
| 150 | + const emailContent = 'Bulk test message'; |
| 151 | + const iexec = consumerIExecInstance; |
| 152 | + const emailContentEncryptionKey = iexec.dataset.generateEncryptionKey(); |
| 153 | + const encryptedFile = await iexec.dataset.encrypt( |
| 154 | + Buffer.from(emailContent, 'utf8'), |
| 155 | + emailContentEncryptionKey |
| 156 | + ); |
| 157 | + |
| 158 | + // Upload to IPFS using local test configuration |
| 159 | + const { add } = await import('../../src/utils/ipfs-service.js'); |
| 160 | + const cid = await add(encryptedFile, { |
| 161 | + ipfsNode: TEST_CHAIN.ipfsNode, |
| 162 | + ipfsGateway: TEST_CHAIN.ipfsGateway, |
| 163 | + }); |
| 164 | + const multiaddr = `/ipfs/${cid}`; |
| 165 | + |
| 166 | + // Prepare secrets like sendEmail does |
| 167 | + const secrets = { |
| 168 | + 1: JSON.stringify({ |
| 169 | + senderName: 'Bulk Test Sender', |
| 170 | + emailSubject: emailSubject, |
| 171 | + emailContentMultiAddr: multiaddr, |
| 172 | + contentType: 'text/plain', |
| 173 | + emailContentEncryptionKey, |
| 174 | + useCallback: true, |
| 175 | + }), |
| 176 | + }; |
| 177 | + |
| 178 | + // Prepare the bulk request using the contacts |
| 179 | + await consumerDataProtectorInstance.prepareBulkRequest({ |
| 180 | + bulkOrders, |
| 181 | + app: defaultConfig.dappAddress, |
| 182 | + workerpool: TEST_CHAIN.prodWorkerpool, |
| 183 | + secrets, |
| 184 | + maxProtectedDataPerTask: 3, |
| 185 | + appMaxPrice: 1000, |
| 186 | + workerpoolMaxPrice: 1000, |
| 187 | + }); |
| 188 | + |
| 189 | + // Process the bulk request |
| 190 | + const result: ProcessBulkRequestResponse | SendEmailSingleResponse = |
| 191 | + await web3mail.sendEmail({ |
| 192 | + emailSubject: 'Bulk test subject', |
| 193 | + emailContent: 'Bulk test message', |
| 194 | + // protectedData is optional when grantedAccess is provided |
| 195 | + grantedAccess: bulkOrders, |
| 196 | + maxProtectedDataPerTask: 3, |
| 197 | + workerpoolMaxPrice: prodWorkerpoolPublicPrice, |
| 198 | + }); |
| 199 | + |
| 200 | + // Verify the result |
| 201 | + expect(result).toBeDefined(); |
| 202 | + expect('tasks' in result).toBe(true); |
| 203 | + const tasks = 'tasks' in result ? result.tasks : []; |
| 204 | + expect(Array.isArray(tasks)).toBe(true); |
| 205 | + expect(tasks.length).toBeGreaterThan(0); |
| 206 | + tasks.forEach((task) => { |
| 207 | + expect(task.taskId).toBeDefined(); |
| 208 | + expect(task.dealId).toBeDefined(); |
| 209 | + expect(task.bulkIndex).toBeDefined(); |
| 210 | + }); |
| 211 | + }, |
| 212 | + 30 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME + 60_000 |
| 213 | + ); |
| 214 | + }); |
| 215 | +}); |
0 commit comments