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 {addCollectionAdminExpectSuccess, createCollectionExpectSuccess, destroyCollectionExpectSuccess, getAdminList, 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 = (await api.query.common.collectionById(collectionId)).unwrap();24 expect(collection.owner.toString()).to.be.equal(alice.address);2526 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));27 await submitTransactionAsync(alice, changeAdminTx);2829 const adminListAfterAddAdmin = await getAdminList(api, collectionId);30 expect(adminListAfterAddAdmin).to.be.deep.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 = (await api.query.common.collectionById(collectionId)).unwrap();42 expect(collection.owner.toString()).to.be.equal(alice.address);4344 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));45 await submitTransactionAsync(alice, changeAdminTx);4647 const adminListAfterAddAdmin = await getAdminList(api, collectionId);48 expect(adminListAfterAddAdmin).to.be.deep.contains(normalizeAccountId(bob.address));4950 const changeAdminTxCharlie = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(charlie.address));51 await submitTransactionAsync(bob, changeAdminTxCharlie);52 const adminListAfterAddNewAdmin = await getAdminList(api, collectionId);53 expect(adminListAfterAddNewAdmin).to.be.deep.contains(normalizeAccountId(bob.address));54 expect(adminListAfterAddNewAdmin).to.be.deep.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 = await getAdminList(api, collectionId);70 expect(adminListAfterAddAdmin).not.to.be.deep.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 privateKey('//AdminTest/1').address,110 privateKey('//AdminTest/2').address,111 privateKey('//AdminTest/3').address,112 privateKey('//AdminTest/4').address,113 privateKey('//AdminTest/5').address,114 privateKey('//AdminTest/6').address,115 privateKey('//AdminTest/7').address,116 ];117 const collectionId = await createCollectionExpectSuccess();118119 const chainAdminLimit = api.consts.common.collectionAdminsLimit.toNumber();120 expect(chainAdminLimit).to.be.equal(5);121122 for (let i = 0; i < chainAdminLimit; i++) {123 await addCollectionAdminExpectSuccess(alice, collectionId, accounts[i]);124 const adminListAfterAddAdmin = await getAdminList(api, collectionId);125 expect(adminListAfterAddAdmin).to.be.deep.contains(normalizeAccountId(accounts[i]));126 }127128 const tx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(accounts[chainAdminLimit]));129 await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;130 });131 });132});