Skip to content

Commit

Permalink
Merge pull request #255 from CirclesUBI/profile-migration-consent
Browse files Browse the repository at this point in the history
Create methods for profile migration consent feature
  • Loading branch information
llunaCreixent authored Jun 30, 2024
2 parents aed8b74 + 93dacb0 commit 73530d5
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
uses: actions/checkout@v3
with:
repository: CirclesUBI/circles-docker.git
ref: main
ref: test-api-profile-migration
path: circles-docker

- name: Setup docker repo
Expand Down
103 changes: 103 additions & 0 deletions src/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,108 @@ export default function createUserModule(web3, contracts, utils) {
}
return null;
},

/**
* Update the profile migration consent of a user, connected (or not) to a deployed Safe address.
*
* @namespace core.user.updateProfileMigrationConsent
*
* @param {Object} account - web3 account instance
* @param {Object} userOptions - options
* @param {string} userOptions.safeAddress - owned Safe address
* @param {boolean} userOptions.profileMigrationConsent - consent (true or false)
*
* @return {boolean} - Returns true when successful
*/
updateProfileMigrationConsent: async (account, userOptions) => {
checkAccount(web3, account);

const options = checkOptions(userOptions, {
safeAddress: {
type: web3.utils.checkAddressChecksum,
},
profileMigrationConsent: {
type: 'boolean',
},
});

const { address } = account;
const { safeAddress, profileMigrationConsent } = options;
const { signature } = web3.eth.accounts.sign(
[address, safeAddress, profileMigrationConsent].join(''),
account.privateKey,
);

await utils.requestAPI({
path: ['users', safeAddress, 'update-profile-migration-consent'],
method: 'POST',
data: {
address: account.address,
signature,
data: {
safeAddress,
profileMigrationConsent,
},
},
});

return true;
},

/**
* Get the profile migration consent value of a user.
*
* @namespace core.user.getProfileMigrationConsent
*
* @param {Object} account - web3 account instance
* @param {Object} userOptions - options
* @param {string} userOptions.safeAddress - owned Safe address
*
* @return {profileMigrationConsent} - Whether or not the user has given their consent
*/
getProfileMigrationConsent: async (account, userOptions) => {
checkAccount(web3, account);

const options = checkOptions(userOptions, {
safeAddress: {
type: web3.utils.checkAddressChecksum,
},
});

const { address } = account;
const { safeAddress } = options;
const { signature } = web3.eth.accounts.sign(
[address, safeAddress].join(''),
account.privateKey,
);

try {
const response = await utils.requestAPI({
path: ['users', safeAddress, 'get-profile-migration-consent'],
method: 'POST',
data: {
address: account.address,
signature,
},
});

if (
response &&
response.data &&
response.data.profileMigrationConsent
) {
return response.data.profileMigrationConsent;
}
} catch (error) {
// Do nothing when not found or denied access ...
if (
!error.request ||
(error.request.status !== 404 && error.request.status !== 403)
) {
throw error;
}
}
return null;
},
};
}
41 changes: 41 additions & 0 deletions test/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,44 @@ describe('User - delete', () => {
});
});
});

describe('User - update profile migration consent', () => {
beforeAll(async () => {
account = getAccount(3);

// The Safe must be deployed and signedup to the Hub before trying to change the consent
deployedSafe = await deploySafeAndToken(core, account);
safeAddress = deployedSafe.safeAddress;
username = `catty${new Date().getTime()}`;
email = 'catt@yyy.com';

// This update acts as a register
await core.user.update(account, {
email,
safeAddress,
username,
});
});

describe('when a new user registers its Safe address', () => {
it('should return profile migration consent', async () => {
expect(
await core.user.getProfileMigrationConsent(account, {
safeAddress,
}),
).toBe(false);
});

it('should return correct value of profile migration consent after updatins', async () => {
await core.user.updateProfileMigrationConsent(account, {
safeAddress,
profileMigrationConsent: true,
}),
expect(
await core.user.getProfileMigrationConsent(account, {
safeAddress,
}),
).toBe(true);
});
});
});

0 comments on commit 73530d5

Please sign in to comment.