From a22acc20152b7b8e2732d2e6cc22987dca1cbbe5 Mon Sep 17 00:00:00 2001 From: Le-Caignec Date: Fri, 3 May 2024 12:14:35 +0200 Subject: [PATCH 1/6] fix function and add tests --- .../dataProtectorSharing/addToCollection.ts | 16 +++++---- .../addToCollection.test.ts | 36 +++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/packages/sdk/src/lib/dataProtectorSharing/addToCollection.ts b/packages/sdk/src/lib/dataProtectorSharing/addToCollection.ts index 9190e1eb8..71369d69f 100644 --- a/packages/sdk/src/lib/dataProtectorSharing/addToCollection.ts +++ b/packages/sdk/src/lib/dataProtectorSharing/addToCollection.ts @@ -82,13 +82,15 @@ export const addToCollection = async ({ protectedData: vProtectedData, sharingContractAddress, }); - vOnStatusUpdate({ - title: 'APPROVE_COLLECTION_CONTRACT', - isDone: true, - payload: { - approveTxHash: approveTx.hash, - }, - }); + if (!approveTx) { + vOnStatusUpdate({ + title: 'APPROVE_COLLECTION_CONTRACT', + isDone: true, + payload: { + approveTxHash: approveTx?.hash, + }, + }); + } try { vOnStatusUpdate({ diff --git a/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts b/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts index 295714ed0..a026662f4 100644 --- a/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts +++ b/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts @@ -2,6 +2,9 @@ import { beforeAll, describe, expect, it, jest } from '@jest/globals'; import { Wallet, type HDNodeWallet } from 'ethers'; import { IExecDataProtector } from '../../../src/index.js'; import { getTestConfig, timeouts } from '../../test-utils.js'; +import { IExec } from 'iexec'; +import { DEFAULT_SHARING_CONTRACT_ADDRESS } from '../../../src/config/config.js'; +import { approveCollectionContract } from '../../../src/lib/dataProtectorSharing/smartContract/approveCollectionContract.js'; describe('dataProtector.addToCollection()', () => { let dataProtector: IExecDataProtector; @@ -50,6 +53,39 @@ describe('dataProtector.addToCollection()', () => { timeouts.createCollection + timeouts.addToCollection ); + + it( + 'should work, if the protectedData has already been approved to the ProtectedDataSharing Contract', + async () => { + // --- GIVEN + const { address: protectedData } = await dataProtector.core.protectData( + { + data: { doNotUse: 'test' }, + name: 'test addToCollection', + } + ); + + const { collectionId } = await dataProtector.sharing.createCollection(); + + const onStatusUpdateMock = jest.fn(); + const [ethProvider, options] = getTestConfig(wallet.privateKey); + const iexec = new IExec( + { ethProvider }, + { ipfsGatewayURL: options.ipfsGateway, ...options?.iexecOptions } + ); + await approveCollectionContract({ + iexec, + protectedData, + sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS, + }); + + // --- WHEN + await dataProtector.sharing.addToCollection({ + collectionId, + addOnlyAppWhitelist, + protectedData, + onStatusUpdate: onStatusUpdateMock, + }); }); describe('When the given protected data does NOT exist', () => { From 57d976249a5ec253c2ebfa7300c24dea72568033 Mon Sep 17 00:00:00 2001 From: Le-Caignec Date: Fri, 3 May 2024 15:30:41 +0200 Subject: [PATCH 2/6] add comment to be mor explicit --- packages/sdk/src/lib/dataProtectorSharing/addToCollection.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/sdk/src/lib/dataProtectorSharing/addToCollection.ts b/packages/sdk/src/lib/dataProtectorSharing/addToCollection.ts index 71369d69f..322d00a18 100644 --- a/packages/sdk/src/lib/dataProtectorSharing/addToCollection.ts +++ b/packages/sdk/src/lib/dataProtectorSharing/addToCollection.ts @@ -82,7 +82,10 @@ export const addToCollection = async ({ protectedData: vProtectedData, sharingContractAddress, }); - if (!approveTx) { + + const hasContractAlreadyBeenApproved = !approveTx; + // If no transaction, it means the dataProtectorSharing Contract has already been approved + if (hasContractAlreadyBeenApproved) { vOnStatusUpdate({ title: 'APPROVE_COLLECTION_CONTRACT', isDone: true, From d18ec2ee49131bf18f9ccec15d36f61d57e2ac51 Mon Sep 17 00:00:00 2001 From: pjt <26487010+PierreJeanjacquot@users.noreply.github.com> Date: Mon, 6 May 2024 09:48:07 +0200 Subject: [PATCH 3/6] typo --- .../sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts b/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts index a026662f4..062261597 100644 --- a/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts +++ b/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts @@ -55,7 +55,7 @@ describe('dataProtector.addToCollection()', () => { ); it( - 'should work, if the protectedData has already been approved to the ProtectedDataSharing Contract', + 'should work, if the protectedData has already been approved to the DataProtectorSharing Contract', async () => { // --- GIVEN const { address: protectedData } = await dataProtector.core.protectData( From 3297db340eb75ae2827c048bbd4d55dc72843b2b Mon Sep 17 00:00:00 2001 From: Pierre Jeanjacquot <26487010+PierreJeanjacquot@users.noreply.github.com> Date: Mon, 6 May 2024 09:57:09 +0200 Subject: [PATCH 4/6] fix missing end of code block --- .../addToCollection.test.ts | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts b/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts index 062261597..135987c5e 100644 --- a/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts +++ b/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts @@ -54,38 +54,35 @@ describe('dataProtector.addToCollection()', () => { timeouts.addToCollection ); - it( - 'should work, if the protectedData has already been approved to the DataProtectorSharing Contract', - async () => { - // --- GIVEN - const { address: protectedData } = await dataProtector.core.protectData( - { - data: { doNotUse: 'test' }, - name: 'test addToCollection', - } - ); + it('should work, if the protectedData has already been approved to the DataProtectorSharing Contract', async () => { + // --- GIVEN + const { address: protectedData } = await dataProtector.core.protectData({ + data: { doNotUse: 'test' }, + name: 'test addToCollection', + }); - const { collectionId } = await dataProtector.sharing.createCollection(); + const { collectionId } = await dataProtector.sharing.createCollection(); - const onStatusUpdateMock = jest.fn(); - const [ethProvider, options] = getTestConfig(wallet.privateKey); - const iexec = new IExec( - { ethProvider }, - { ipfsGatewayURL: options.ipfsGateway, ...options?.iexecOptions } - ); - await approveCollectionContract({ - iexec, - protectedData, - sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS, - }); + const onStatusUpdateMock = jest.fn(); + const [ethProvider, options] = getTestConfig(wallet.privateKey); + const iexec = new IExec( + { ethProvider }, + { ipfsGatewayURL: options.ipfsGateway, ...options?.iexecOptions } + ); + await approveCollectionContract({ + iexec, + protectedData, + sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS, + }); - // --- WHEN - await dataProtector.sharing.addToCollection({ - collectionId, - addOnlyAppWhitelist, - protectedData, - onStatusUpdate: onStatusUpdateMock, - }); + // --- WHEN + await dataProtector.sharing.addToCollection({ + collectionId, + addOnlyAppWhitelist, + protectedData, + onStatusUpdate: onStatusUpdateMock, + }); + }); }); describe('When the given protected data does NOT exist', () => { From ef65869b9f7a34f0c21f2439206674bcbfda70b4 Mon Sep 17 00:00:00 2001 From: Pierre Jeanjacquot <26487010+PierreJeanjacquot@users.noreply.github.com> Date: Mon, 6 May 2024 12:00:55 +0200 Subject: [PATCH 5/6] fix test timeout --- .../addToCollection.test.ts | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts b/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts index 135987c5e..08ce21e7c 100644 --- a/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts +++ b/packages/sdk/tests/e2e/dataProtectorSharing/addToCollection.test.ts @@ -54,35 +54,49 @@ describe('dataProtector.addToCollection()', () => { timeouts.addToCollection ); - it('should work, if the protectedData has already been approved to the DataProtectorSharing Contract', async () => { - // --- GIVEN - const { address: protectedData } = await dataProtector.core.protectData({ - data: { doNotUse: 'test' }, - name: 'test addToCollection', - }); + it( + 'should work, if the protectedData has already been approved to the DataProtectorSharing Contract', + async () => { + // --- GIVEN + const { address: protectedData } = await dataProtector.core.protectData( + { + data: { doNotUse: 'test' }, + name: 'test addToCollection', + } + ); + + const { collectionId } = await dataProtector.sharing.createCollection(); - const { collectionId } = await dataProtector.sharing.createCollection(); + const onStatusUpdateMock = jest.fn(); + const [ethProvider, options] = getTestConfig(wallet.privateKey); + const iexec = new IExec( + { ethProvider }, + { ipfsGatewayURL: options.ipfsGateway, ...options?.iexecOptions } + ); + await approveCollectionContract({ + iexec, + protectedData, + sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS, + }); - const onStatusUpdateMock = jest.fn(); - const [ethProvider, options] = getTestConfig(wallet.privateKey); - const iexec = new IExec( - { ethProvider }, - { ipfsGatewayURL: options.ipfsGateway, ...options?.iexecOptions } - ); - await approveCollectionContract({ - iexec, - protectedData, - sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS, - }); + // --- WHEN + await dataProtector.sharing.addToCollection({ + collectionId, + addOnlyAppWhitelist, + protectedData, + onStatusUpdate: onStatusUpdateMock, + }); - // --- WHEN - await dataProtector.sharing.addToCollection({ - collectionId, - addOnlyAppWhitelist, - protectedData, - onStatusUpdate: onStatusUpdateMock, - }); - }); + // --- THEN + expect(onStatusUpdateMock).toHaveBeenCalledWith({ + title: 'ADD_PROTECTED_DATA_TO_COLLECTION', + isDone: true, + }); + }, + timeouts.protectData + + timeouts.createCollection + + timeouts.addToCollection + ); }); describe('When the given protected data does NOT exist', () => { From 60136bb9b2ca00ea28eb796807f58071beb25e57 Mon Sep 17 00:00:00 2001 From: Pierre Jeanjacquot <26487010+PierreJeanjacquot@users.noreply.github.com> Date: Tue, 7 May 2024 10:30:31 +0200 Subject: [PATCH 6/6] update changelog --- packages/sdk/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/sdk/CHANGELOG.md b/packages/sdk/CHANGELOG.md index 20e0df434..180dacf4b 100644 --- a/packages/sdk/CHANGELOG.md +++ b/packages/sdk/CHANGELOG.md @@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file. - `consumeProtectedData()`: Add two new status to ConsumeProtectedDataStatuses: "FETCH_WORKERPOOL_ORDERBOOK" and "PUSH_ENCRYPTION_KEY" +### Changed + +- fixed `addToCollection()` method issue when the DataProtectorSharing contract is previously approved for the protected data + ## [2.0.0-beta.1] (2024-05-02) ### Added