1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {usingPlaygrounds, expect, itSub, Pallets, requirePalletsOrSkip} from '../util';19import {UniqueHelper} from '../util/playgrounds/unique';2021async function getIdentities(helper: UniqueHelper) {22 const identities: [string, any][] = [];23 for(const [key, value] of await helper.getApi().query.identity.identityOf.entries())24 identities.push([(key as any).toHuman(), (value as any).unwrap()]);25 return identities;26}2728async function getIdentityAccounts(helper: UniqueHelper) {29 return (await getIdentities(helper)).flatMap(([key, _value]) => key);30}3132describe('Integration Test: Identities Manipulation', () => {33 let superuser: IKeyringPair;3435 before(async function() {36 if (!process.env.RUN_COLLATOR_TESTS) this.skip();3738 await usingPlaygrounds(async (helper, privateKey) => {39 requirePalletsOrSkip(this, helper, [Pallets.Identity]);40 superuser = await privateKey('//Alice');41 });42 });4344 itSub('Normal calls do not work', async ({helper}) => {45 46 await expect(helper.executeExtrinsic(superuser, 'api.tx.identity.setIdentity', [{info: {display: {Raw: 'Meowser'}}}]))47 .to.be.rejectedWith(/Transaction call is not expected/);48 });4950 itSub('Sets identities', async ({helper}) => {51 const oldIdentitiesCount = (await getIdentityAccounts(helper)).length;5253 const crowdSize = 10;54 const crowd = await helper.arrange.createCrowd(crowdSize, 0n, superuser);55 const identities = crowd.map((acc, i) => [acc.address, {info: {display: {Raw: `accounter #${i}`}}}]);56 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identities]);5758 expect((await getIdentityAccounts(helper)).length).to.be.equal(oldIdentitiesCount + crowdSize);59 });6061 itSub('Setting identities does not delete existing but does overwrite', async ({helper}) => {62 const crowd = await helper.arrange.createCrowd(10, 0n, superuser);63 const identities = crowd.map((acc, i) => [acc.address, {info: {display: {Raw: `accounter #${i}`}}}]);6465 66 let singleIdentity = identities.pop()!;67 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [[singleIdentity]]);6869 const oldIdentitiesCount = (await getIdentityAccounts(helper)).length;7071 72 singleIdentity = [singleIdentity[0], {info: {display: {Raw: 'something special'}}}];73 identities.push(singleIdentity);74 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identities]);7576 77 expect((await getIdentityAccounts(helper)).length).to.be.equal(oldIdentitiesCount + 9);78 expect((await helper.callRpc('api.query.identity.identityOf', [singleIdentity[0]])).toHuman().info.display)79 .to.be.deep.equal({Raw: 'something special'});80 });8182 itSub('Removes identities', async ({helper}) => {83 const crowd = await helper.arrange.createCrowd(10, 0n, superuser);84 const identities = crowd.map((acc, i) => [acc.address, {info: {display: {Raw: `accounter #${i}`}}}]);85 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identities]);86 const oldIdentities = await getIdentityAccounts(helper);8788 89 const scapegoats = [crowd.pop()!.address, crowd.pop()!.address];90 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceRemoveIdentities', [scapegoats]);91 const newIdentities = await getIdentityAccounts(helper);92 expect(newIdentities.concat(scapegoats)).to.be.have.members(oldIdentities);93 });9495 after(async function() {96 if (!process.env.RUN_COLLATOR_TESTS) return;9798 await usingPlaygrounds(async helper => {99 if (helper.fetchMissingPalletNames([Pallets.Identity]).length != 0) return;100101 const identitiesToRemove: string[] = await getIdentityAccounts(helper);102 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceRemoveIdentities', [identitiesToRemove]);103 });104 });105});