123456import { ApiPromise } from '@polkadot/api';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import privateKey from './substrate/privateKey';10import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';11import {createCollectionExpectSuccess, destroyCollectionExpectSuccess, normalizeAccountId} from './util/helpers';1213chai.use(chaiAsPromised);14const expect = chai.expect;1516describe('Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {17 it('Add collection admin.', async () => {18 await usingApi(async (api) => {19 const collectionId = await createCollectionExpectSuccess();20 const alice = privateKey('//Alice');21 const bob = privateKey('//Bob');2223 const collection: any = (await api.query.nft.collectionById(collectionId)).toJSON();24 expect(collection.Owner).to.be.equal(alice.address);2526 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));27 await submitTransactionAsync(alice, changeAdminTx);2829 const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));30 expect(adminListAfterAddAdmin).to.be.contains(normalizeAccountId(bob.address));31 });32 });3334 it('Add admin using added collection admin.', async () => {35 await usingApi(async (api) => {36 const collectionId = await createCollectionExpectSuccess();37 const Alice = privateKey('//Alice');38 const Bob = privateKey('//Bob');39 const Charlie = privateKey('//CHARLIE');4041 const collection: any = (await api.query.nft.collectionById(collectionId)).toJSON();42 expect(collection.Owner).to.be.equal(Alice.address);4344 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Bob.address));45 await submitTransactionAsync(Alice, changeAdminTx);4647 const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));48 expect(adminListAfterAddAdmin).to.be.contains(normalizeAccountId(Bob.address));4950 const changeAdminTxCharlie = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Charlie.address));51 await submitTransactionAsync(Bob, changeAdminTxCharlie);52 const adminListAfterAddNewAdmin: any = (await api.query.nft.adminList(collectionId));53 expect(adminListAfterAddNewAdmin).to.be.contains(normalizeAccountId(Bob.address));54 expect(adminListAfterAddNewAdmin).to.be.contains(normalizeAccountId(Charlie.address));55 });56 });57});5859describe('Negative Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {60 it("Not owner can't add collection admin.", async () => {61 await usingApi(async (api) => {62 const collectionId = await createCollectionExpectSuccess();63 const alice = privateKey('//Alice');64 const nonOwner = privateKey('//Bob_stash');6566 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(alice.address));67 await expect(submitTransactionExpectFailAsync(nonOwner, changeAdminTx)).to.be.rejected;6869 const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));70 expect(adminListAfterAddAdmin).not.to.be.contains(normalizeAccountId(alice.address));7172 73 await createCollectionExpectSuccess();74 });75 });76 it("Can't add collection admin of not existing collection.", async () => {77 await usingApi(async (api) => {78 79 const collectionId = (1 << 32) - 1;80 const alice = privateKey('//Alice');81 const bob = privateKey('//Bob');8283 const changeOwnerTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));84 await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;8586 87 await createCollectionExpectSuccess();88 });89 });9091 it("Can't add an admin to a destroyed collection.", async () => {92 await usingApi(async (api) => {93 const collectionId = await createCollectionExpectSuccess();94 const Alice = privateKey('//Alice');95 const Bob = privateKey('//Bob');96 await destroyCollectionExpectSuccess(collectionId);97 const changeOwnerTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Bob.address));98 await expect(submitTransactionExpectFailAsync(Alice, changeOwnerTx)).to.be.rejected;99100 101 await createCollectionExpectSuccess();102 });103 });104105 it('Add an admin to a collection that has reached the maximum number of admins limit', async () => {106 await usingApi(async (api: ApiPromise) => {107 const Alice = privateKey('//Alice');108 const accounts = [109 'GsvVmjr1CBHwQHw84pPHMDxgNY3iBLz6Qn7qS3CH8qPhrHz',110 'FoQJpPyadYccjavVdTWxpxU7rUEaYhfLCPwXgkfD6Zat9QP',111 'JKspFU6ohf1Grg3Phdzj2pSgWvsYWzSfKghhfzMbdhNBWs5',112 'Fr4NzY1udSFFLzb2R3qxVQkwz9cZraWkyfH4h3mVVk7BK7P',113 'DfnTB4z7eUvYRqcGtTpFsLC69o6tvBSC1pEv8vWPZFtCkaK',114 'HnMAUz7r2G8G3hB27SYNyit5aJmh2a5P4eMdDtACtMFDbam',115 'DE14BzQ1bDXWPKeLoAqdLAm1GpyAWaWF1knF74cEZeomTBM',116 ];117 const collectionId = await createCollectionExpectSuccess();118119 const chainAdminLimit = (api.consts.nft.collectionAdminsLimit as any).toNumber();120 expect(chainAdminLimit).to.be.equal(5);121122 for (let i = 0; i < chainAdminLimit; i++) {123 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(accounts[i]));124 await submitTransactionAsync(Alice, changeAdminTx);125 const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));126 expect(adminListAfterAddAdmin).to.be.contains(normalizeAccountId(accounts[i]));127 }128129 const tx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(accounts[chainAdminLimit]));130 await expect(submitTransactionExpectFailAsync(Alice, tx)).to.be.rejected;131 });132 });133});