--- a/tests/src/identity.seqtest.ts +++ b/tests/src/identity.seqtest.ts @@ -29,6 +29,14 @@ return (await getIdentities(helper)).flatMap(([key, _value]) => key); } +async function getSubIdentityAccounts(helper: UniqueHelper, address: string) { + return ((await helper.getApi().query.identity.subsOf(address)).toHuman() as any)[1]; +} + +async function getSubIdentityName(helper: UniqueHelper, address: string) { + return ((await helper.getApi().query.identity.superOf(address)).toHuman() as any); +} + describe('Integration Test: Identities Manipulation', () => { let superuser: IKeyringPair; @@ -45,49 +53,220 @@ .to.be.rejectedWith(/Transaction call is not expected/); }); - itSub('Sets identities', async ({helper}) => { - const oldIdentitiesCount = (await getIdentityAccounts(helper)).length; + describe('Identities', () => { + itSub('Sets identities', async ({helper}) => { + const oldIdentitiesCount = (await getIdentityAccounts(helper)).length; - const crowdSize = 10; - const crowd = await helper.arrange.createCrowd(crowdSize, 0n, superuser); - const identities = crowd.map((acc, i) => [acc.address, {info: {display: {Raw: `accounter #${i}`}}}]); - await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identities]); + const crowdSize = 10; + const crowd = await helper.arrange.createCrowd(crowdSize, 0n, superuser); + const identities = crowd.map((acc, i) => [acc.address, {info: {display: {Raw: `accounter #${i}`}}}]); + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identities]); - expect((await getIdentityAccounts(helper)).length).to.be.equal(oldIdentitiesCount + crowdSize); - }); + expect((await getIdentityAccounts(helper)).length).to.be.equal(oldIdentitiesCount + crowdSize); + }); - itSub('Setting identities does not delete existing but does overwrite', async ({helper}) => { - const crowd = await helper.arrange.createCrowd(10, 0n, superuser); - const identities = crowd.map((acc, i) => [acc.address, {info: {display: {Raw: `accounter #${i}`}}}]); + itSub('Setting identities does not delete existing but does overwrite', async ({helper}) => { + const crowd = await helper.arrange.createCrowd(10, 0n, superuser); + const identities = crowd.map((acc, i) => [acc.address, {info: {display: {Raw: `accounter #${i}`}}}]); + + // insert a single identity + let singleIdentity = identities.pop()!; + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [[singleIdentity]]); - // insert a single identity - let singleIdentity = identities.pop()!; - await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [[singleIdentity]]); + const oldIdentitiesCount = (await getIdentityAccounts(helper)).length; - const oldIdentitiesCount = (await getIdentityAccounts(helper)).length; + // change an identity and push it with a few new others + singleIdentity = [singleIdentity[0], {info: {display: {Raw: 'something special'}}}]; + identities.push(singleIdentity); + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identities]); - // change an identity and push it with a few new others - singleIdentity = [singleIdentity[0], {info: {display: {Raw: 'something special'}}}]; - identities.push(singleIdentity); - await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identities]); + // oldIdentitiesCount + 9 because one identity is overwritten, not inserted on top + expect((await getIdentityAccounts(helper)).length).to.be.equal(oldIdentitiesCount + 9); + expect((await helper.callRpc('api.query.identity.identityOf', [singleIdentity[0]])).toHuman().info.display) + .to.be.deep.equal({Raw: 'something special'}); + }); - // oldIdentitiesCount + 9 because one identity is overwritten, not inserted on top - expect((await getIdentityAccounts(helper)).length).to.be.equal(oldIdentitiesCount + 9); - expect((await helper.callRpc('api.query.identity.identityOf', [singleIdentity[0]])).toHuman().info.display) - .to.be.deep.equal({Raw: 'something special'}); + itSub('Removes identities', async ({helper}) => { + const crowd = await helper.arrange.createCrowd(10, 0n, superuser); + const identities = crowd.map((acc, i) => [acc.address, {info: {display: {Raw: `accounter #${i}`}}}]); + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identities]); + const oldIdentities = await getIdentityAccounts(helper); + + // delete a couple, check that they are no longer there + const scapegoats = [crowd.pop()!.address, crowd.pop()!.address]; + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceRemoveIdentities', [scapegoats]); + const newIdentities = await getIdentityAccounts(helper); + expect(newIdentities.concat(scapegoats)).to.be.have.members(oldIdentities); + }); }); - itSub('Removes identities', async ({helper}) => { - const crowd = await helper.arrange.createCrowd(10, 0n, superuser); - const identities = crowd.map((acc, i) => [acc.address, {info: {display: {Raw: `accounter #${i}`}}}]); - await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identities]); - const oldIdentities = await getIdentityAccounts(helper); + describe('Sub-identities', () => { + itSub('Sets subs', async ({helper}) => { + const crowdSize = 18; + const crowd = await helper.arrange.createCrowd(crowdSize, 0n, superuser); + const supers = [crowd.pop()!, crowd.pop()!, crowd.pop()!]; - // delete a couple, check that they are no longer there - const scapegoats = [crowd.pop()!.address, crowd.pop()!.address]; - await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceRemoveIdentities', [scapegoats]); - const newIdentities = await getIdentityAccounts(helper); - expect(newIdentities.concat(scapegoats)).to.be.have.members(oldIdentities); + const subsPerSup = crowd.length / supers.length; + let subCount = 0; + const subs = [ + crowd.slice(subCount, subCount += subsPerSup + 1), + crowd.slice(subCount, subCount += subsPerSup), + crowd.slice(subCount, subCount += subsPerSup - 1), + ]; + + const subsInfo = supers.map((acc, i) => [ + acc.address, [ + 1000000n + BigInt(i + 1), + subs[i].map((sub, j) => [ + sub.address, {Raw: `accounter #${j}`}, + ]), + ], + ]); + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceSetSubs', [subsInfo]); + + for (let i = 0; i < supers.length; i++) { + // check deposit + expect(((await helper.getApi().query.identity.subsOf(supers[i].address)).toJSON() as any)[0]).to.be.equal(1000001 + i); + + const subsAccounts = await getSubIdentityAccounts(helper, supers[i].address); + // check sub-identities as account ids + expect(subsAccounts).to.include.members(subs[i].map(x => x.address)); + + for (let j = 0; j < subsAccounts.length; j++) { + // check sub-identities' names + expect((await getSubIdentityName(helper, subsAccounts[j]))[1]).to.be.deep.equal({Raw: `accounter #${j}`}); + } + } + }); + + itSub('Setting sub-identities does not delete other existing but does overwrite own', async ({helper}) => { + const crowdSize = 18; + const crowd = await helper.arrange.createCrowd(crowdSize, 0n, superuser); + const supers = [crowd.pop()!, crowd.pop()!, crowd.pop()!]; + + const subsPerSup = crowd.length / supers.length; + let subCount = 0; + const subs = [ + crowd.slice(subCount, subCount += subsPerSup + 1), + crowd.slice(subCount, subCount += subsPerSup), + crowd.slice(subCount, subCount += subsPerSup - 1), + ]; + + const subsInfo1 = supers.map((acc, i) => [ + acc.address, [ + 1000000n + BigInt(i + 1), + subs[i].map((sub, j) => [ + sub.address, {Raw: `accounter #${j}`}, + ]), + ], + ]); + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceSetSubs', [subsInfo1]); + + // change some sub-identities... + subs[2].pop(); subs[2].pop(); subs[2].push(...await helper.arrange.createAccounts([0n], superuser)); + + // ...and set them + const subsInfo2 = [[ + supers[2].address, [ + 999999n, + subs[2].map((sub, j) => [ + sub.address, {Raw: `discounter #${j}`}, + ]), + ], + ]]; + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceSetSubs', [subsInfo2]); + + // make sure everything else is the same + for (let i = 0; i < supers.length - 1; i++) { + // check deposit + expect(((await helper.getApi().query.identity.subsOf(supers[i].address)).toJSON() as any)[0]).to.be.equal(1000001 + i); + + const subsAccounts = await getSubIdentityAccounts(helper, supers[i].address); + // check sub-identities as account ids + expect(subsAccounts).to.include.members(subs[i].map(x => x.address)); + + for (let j = 0; j < subsAccounts; j++) { + // check sub-identities' names + expect((await getSubIdentityName(helper, subsAccounts[j]))[1]).to.be.deep.equal({Raw: `accounter #${j}`}); + } + } + + // check deposit + expect(((await helper.getApi().query.identity.subsOf(supers[2].address)).toJSON() as any)[0]).to.be.equal(999999); + + const subsAccounts = await getSubIdentityAccounts(helper, supers[2].address); + // check sub-identities as account ids + expect(subsAccounts).to.include.members(subs[2].map(x => x.address)); + + for (let j = 0; j < subsAccounts.length; j++) { + // check sub-identities' names + expect((await getSubIdentityName(helper, subsAccounts[j]))[1]).to.be.deep.equal({Raw: `discounter #${j}`}); + } + }); + + itSub('Removes sub-identities', async ({helper}) => { + const crowdSize = 3; + const crowd = await helper.arrange.createCrowd(crowdSize, 0n, superuser); + const sup = crowd.pop()!; + + const subsInfo1 = [[ + sup.address, [ + 1000000n, + crowd.map((sub, j) => [ + sub.address, {Raw: `accounter #${j}`}, + ]), + ], + ]]; + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceSetSubs', [subsInfo1]); + + // empty sub-identities should delete the records + const subsInfo2 = [[ + sup.address, [ + 1000000n, + [], + ], + ]]; + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceSetSubs', [subsInfo2]); + + // check deposit + expect((await helper.getApi().query.identity.subsOf(sup.address)).toHuman()).to.be.deep.equal(['0', []]); + + for (let j = 0; j < crowd.length; j++) { + // check sub-identities' names + expect((await getSubIdentityName(helper, crowd[j].address))).to.be.null; + } + }); + + itSub('Removing identities deletes associated sub-identities', async ({helper}) => { + const crowd = await helper.arrange.createCrowd(3, 0n, superuser); + const sup = crowd.pop()!; + + // insert identity + const identities = [[sup.address, {info: {display: {Raw: 'mental'}}}]]; + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identities]); + + // and its sub-identities + const subsInfo = [[ + sup.address, [ + 1000000n, + crowd.map((sub, j) => [ + sub.address, {Raw: `accounter #${j}`}, + ]), + ], + ]]; + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceSetSubs', [subsInfo]); + + // delete top identity + await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceRemoveIdentities', [[sup.address]]); + + // check that sub-identities are deleted + expect((await helper.getApi().query.identity.subsOf(sup.address)).toHuman()).to.be.deep.equal(['0', []]); + + for (let j = 0; j < crowd.length; j++) { + // check sub-identities' names + expect((await getSubIdentityName(helper, crowd[j].address))).to.be.null; + } + }); }); after(async function() {