diff --git a/src/utils/subgraphQuery.ts b/src/utils/subgraphQuery.ts index 4f973e2..f3d02b9 100644 --- a/src/utils/subgraphQuery.ts +++ b/src/utils/subgraphQuery.ts @@ -29,6 +29,9 @@ export const getValidContact = async ( graphQLClient: GraphQLClient, contacts: Contact[] ): Promise => { + if (contacts.length === 0) { + return []; + } try { // Contacts addresses const contactsAddresses = contacts.map((contact) => contact.address); diff --git a/tests/e2e/fetchUserContacts.test.ts b/tests/e2e/fetchUserContacts.test.ts index dd95d92..2ef8b51 100644 --- a/tests/e2e/fetchUserContacts.test.ts +++ b/tests/e2e/fetchUserContacts.test.ts @@ -42,115 +42,179 @@ describe('web3telegram.fetchMyContacts()', () => { await waitSubgraphIndexing(); }, 4 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME); - it( - 'Tow different user should have different contacts', - async () => { - const user1 = Wallet.createRandom().address; - const user2 = Wallet.createRandom().address; - const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); - await dataProtector.grantAccess({ - authorizedApp: defaultConfig.dappAddress, - protectedData: protectedData1.address, - authorizedUser: user1, - }); - - await dataProtector.grantAccess({ - authorizedApp: defaultConfig.dappAddress, - protectedData: protectedData2.address, - authorizedUser: user2, - }); - - const contactUser1 = await web3telegram.fetchUserContacts({ - userAddress: user1, - }); - const contactUser2 = await web3telegram.fetchUserContacts({ - userAddress: user2, - }); - expect(contactUser1).not.toEqual(contactUser2); - }, - MAX_EXPECTED_WEB2_SERVICES_TIME - ); - - it( - 'Test that the protected data can be accessed by authorized user', - async () => { - const userWithAccess = Wallet.createRandom().address; - const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); - await dataProtector.grantAccess({ - authorizedApp: defaultConfig.dappAddress, - protectedData: protectedData1.address, - authorizedUser: userWithAccess, - }); - - const contacts = await web3telegram.fetchUserContacts({ - userAddress: userWithAccess, - }); - expect(contacts.length).toBeGreaterThan(0); - }, - MAX_EXPECTED_WEB2_SERVICES_TIME - ); - - it( - 'should throw a protocol error', - async () => { - // Call getTestConfig to get the default configuration - const [ethProvider, defaultOptions] = getTestConfig(wallet.privateKey); - const user1 = Wallet.createRandom().address; - - const options = { - ...defaultOptions, - iexecOptions: { - ...defaultOptions.iexecOptions, - iexecGatewayURL: 'https://test', - }, - }; - - // Pass the modified options to IExecWeb3telegram - const invalidWeb3telegram = new IExecWeb3telegram(ethProvider, options); - let error: WorkflowError | undefined; - - try { - await invalidWeb3telegram.fetchUserContacts({ + describe('when no access is granted', () => { + it( + 'should return an empty contact array', + async () => { + const noAccessUser = Wallet.createRandom().address; + + const contacts = await web3telegram.fetchUserContacts({ + userAddress: noAccessUser, + isUserStrict: true, + }); + console.log('🚀 ~ contacts:', contacts); + + expect(contacts.length).toBe(0); + }, + 2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME + ); + }); + + describe('when access is granted', () => { + it( + 'should return the user contacts for both app and whitelist', + async () => { + const userWithAccess = Wallet.createRandom().address; + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); + expect(defaultConfig).not.toBeNull(); + const authorizedApp = defaultConfig!.dappAddress; + const authorizedWhitelist = defaultConfig!.whitelistSmartContract; + + await dataProtector.grantAccess({ + authorizedApp: authorizedApp, + protectedData: protectedData1.address, + authorizedUser: userWithAccess, + }); + + await dataProtector.grantAccess({ + authorizedApp: authorizedWhitelist, + protectedData: protectedData2.address, + authorizedUser: userWithAccess, + }); + + const contacts = await web3telegram.fetchUserContacts({ + userAddress: userWithAccess, + isUserStrict: true, + }); + expect(contacts.length).toBe(2); + }, + MAX_EXPECTED_WEB2_SERVICES_TIME + ); + + it( + 'Two different user should have different contacts', + async () => { + const user1 = Wallet.createRandom().address; + const user2 = Wallet.createRandom().address; + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); + await dataProtector.grantAccess({ + authorizedApp: defaultConfig.dappAddress, + protectedData: protectedData1.address, + authorizedUser: user1, + }); + + await dataProtector.grantAccess({ + authorizedApp: defaultConfig.dappAddress, + protectedData: protectedData2.address, + authorizedUser: user2, + }); + + const contactUser1 = await web3telegram.fetchUserContacts({ userAddress: user1, }); - } catch (err) { - error = err as WorkflowError; - } - - expect(error).toBeInstanceOf(WorkflowError); - expect(error?.message).toBe( - "A service in the iExec protocol appears to be unavailable. You can retry later or contact iExec's technical support for help." - ); - expect(error?.isProtocolError).toBe(true); - }, - 2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME - ); - - it( - 'should throw a fetchUserContacts error', - async () => { - // Call getTestConfig to get the default configuration - const [ethProvider, defaultOptions] = getTestConfig(wallet.privateKey); - - const options = { - ...defaultOptions, - dataProtectorSubgraph: 'https://test', - }; - - // Pass the modified options to IExecWeb3telegram - const invalidWeb3telegram = new IExecWeb3telegram(ethProvider, options); - let error: WorkflowError | undefined; - - try { - await invalidWeb3telegram.fetchMyContacts(); - } catch (err) { - error = err as WorkflowError; - } - - expect(error).toBeInstanceOf(WorkflowError); - expect(error?.message).toBe('Failed to fetch user contacts'); - expect(error?.isProtocolError).toBe(false); - }, - 2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME - ); + const contactUser2 = await web3telegram.fetchUserContacts({ + userAddress: user2, + }); + expect(contactUser1).not.toEqual(contactUser2); + }, + MAX_EXPECTED_WEB2_SERVICES_TIME + ); + + it( + 'Test that the protected data can be accessed by authorized user', + async () => { + const userWithAccess = Wallet.createRandom().address; + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); + await dataProtector.grantAccess({ + authorizedApp: defaultConfig.dappAddress, + protectedData: protectedData1.address, + authorizedUser: userWithAccess, + }); + + const contacts = await web3telegram.fetchUserContacts({ + userAddress: userWithAccess, + }); + expect(contacts.length).toBeGreaterThan(0); + }, + MAX_EXPECTED_WEB2_SERVICES_TIME + ); + }); + + describe('when iexec market API is not reachable', () => { + it( + 'should throw a protocol error', + async () => { + // Call getTestConfig to get the default configuration + const [ethProvider, defaultOptions] = getTestConfig(wallet.privateKey); + const user1 = Wallet.createRandom().address; + + const options = { + ...defaultOptions, + iexecOptions: { + ...defaultOptions.iexecOptions, + iexecGatewayURL: 'https://test', + }, + }; + + // Pass the modified options to IExecWeb3telegram + const invalidWeb3telegram = new IExecWeb3telegram(ethProvider, options); + let error: WorkflowError | undefined; + + try { + await invalidWeb3telegram.fetchUserContacts({ + userAddress: user1, + }); + } catch (err) { + error = err as WorkflowError; + } + + expect(error).toBeInstanceOf(WorkflowError); + expect(error?.message).toBe( + "A service in the iExec protocol appears to be unavailable. You can retry later or contact iExec's technical support for help." + ); + expect(error?.isProtocolError).toBe(true); + }, + 2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME + ); + }); + + describe('when subgraph is not reachable', () => { + it( + 'should throw a fetchUserContacts error', + async () => { + // Call getTestConfig to get the default configuration + const [ethProvider, defaultOptions] = getTestConfig(wallet.privateKey); + + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); + expect(defaultConfig).not.toBeNull(); + const authorizedApp = defaultConfig!.dappAddress; + + await dataProtector.grantAccess({ + authorizedApp: authorizedApp, + protectedData: protectedData1.address, + authorizedUser: ethProvider.address, + }); + + const options = { + ...defaultOptions, + dataProtectorSubgraph: 'https://test', + }; + + // Pass the modified options to IExecWeb3telegram + const invalidWeb3telegram = new IExecWeb3telegram(ethProvider, options); + let error: WorkflowError | undefined; + + try { + await invalidWeb3telegram.fetchMyContacts(); + } catch (err) { + error = err as WorkflowError; + } + + expect(error).toBeInstanceOf(WorkflowError); + expect(error?.message).toBe('Failed to fetch user contacts'); + expect(error?.isProtocolError).toBe(false); + }, + 2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME + ); + }); });