1234567891011121314151617import {ApiPromise} from '@polkadot/api';18import chai from 'chai';19import chaiAsPromised from 'chai-as-promised';20import privateKey from './substrate/privateKey';21import {default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';22import {addCollectionAdminExpectSuccess, createCollectionExpectSuccess, destroyCollectionExpectSuccess, getAdminList, normalizeAccountId, queryCollectionExpectSuccess} from './util/helpers';2324chai.use(chaiAsPromised);25const expect = chai.expect;2627describe('Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {28 it('Add collection admin.', async () => {29 await usingApi(async (api, privateKeyWrapper) => {30 const collectionId = await createCollectionExpectSuccess();31 const alice = privateKeyWrapper('//Alice');32 const bob = privateKeyWrapper('//Bob');3334 const collection = await queryCollectionExpectSuccess(api, collectionId);35 expect(collection.owner.toString()).to.be.equal(alice.address);3637 const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));38 await submitTransactionAsync(alice, changeAdminTx);3940 const adminListAfterAddAdmin = await getAdminList(api, collectionId);41 expect(adminListAfterAddAdmin).to.be.deep.contains(normalizeAccountId(bob.address));42 });43 });44});4546describe('Negative Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {47 it("Not owner can't add collection admin.", async () => {48 await usingApi(async (api) => {49 const collectionId = await createCollectionExpectSuccess();50 const alice = privateKey('//Alice');51 const bob = privateKey('//Bob');52 const charlie = privateKey('//CHARLIE');5354 const collection = await queryCollectionExpectSuccess(api, collectionId);55 expect(collection.owner.toString()).to.be.equal(alice.address);5657 const adminListAfterAddAdmin = await getAdminList(api, collectionId);58 expect(adminListAfterAddAdmin).to.be.not.deep.contains(normalizeAccountId(bob.address));5960 const changeAdminTxCharlie = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(charlie.address));61 await expect(submitTransactionAsync(bob, changeAdminTxCharlie)).to.be.rejected;62 63 const adminListAfterAddNewAdmin = await getAdminList(api, collectionId);64 expect(adminListAfterAddNewAdmin).to.be.not.deep.contains(normalizeAccountId(bob.address));65 expect(adminListAfterAddNewAdmin).to.be.not.deep.contains(normalizeAccountId(charlie.address));66 });67 });6869 it("Admin can't add collection admin.", async () => {70 await usingApi(async (api) => {71 const collectionId = await createCollectionExpectSuccess();72 const alice = privateKey('//Alice');73 const bob = privateKey('//Bob');74 const charlie = privateKey('//CHARLIE');7576 const collection = await queryCollectionExpectSuccess(api, collectionId);77 expect(collection.owner.toString()).to.be.equal(alice.address);7879 const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));80 await submitTransactionAsync(alice, changeAdminTx);8182 const adminListAfterAddAdmin = await getAdminList(api, collectionId);83 expect(adminListAfterAddAdmin).to.be.deep.contains(normalizeAccountId(bob.address));8485 const changeAdminTxCharlie = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(charlie.address));86 await expect(submitTransactionAsync(bob, changeAdminTxCharlie)).to.be.rejected;87 88 const adminListAfterAddNewAdmin = await getAdminList(api, collectionId);89 expect(adminListAfterAddNewAdmin).to.be.deep.contains(normalizeAccountId(bob.address));90 expect(adminListAfterAddNewAdmin).to.be.not.deep.contains(normalizeAccountId(charlie.address));91 });92 });9394 it("Can't add collection admin of not existing collection.", async () => {95 await usingApi(async (api, privateKeyWrapper) => {96 97 const collectionId = (1 << 32) - 1;98 const alice = privateKeyWrapper('//Alice');99 const bob = privateKeyWrapper('//Bob');100101 const changeOwnerTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));102 await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;103104 105 await createCollectionExpectSuccess();106 });107 });108109 it("Can't add an admin to a destroyed collection.", async () => {110 await usingApi(async (api, privateKeyWrapper) => {111 const collectionId = await createCollectionExpectSuccess();112 const alice = privateKeyWrapper('//Alice');113 const bob = privateKeyWrapper('//Bob');114 await destroyCollectionExpectSuccess(collectionId);115 const changeOwnerTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));116 await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;117118 119 await createCollectionExpectSuccess();120 });121 });122123 it('Add an admin to a collection that has reached the maximum number of admins limit', async () => {124 await usingApi(async (api: ApiPromise, privateKeyWrapper) => {125 const alice = privateKeyWrapper('//Alice');126 const accounts = [127 privateKeyWrapper('//AdminTest/1').address,128 privateKeyWrapper('//AdminTest/2').address,129 privateKeyWrapper('//AdminTest/3').address,130 privateKeyWrapper('//AdminTest/4').address,131 privateKeyWrapper('//AdminTest/5').address,132 privateKeyWrapper('//AdminTest/6').address,133 privateKeyWrapper('//AdminTest/7').address,134 ];135 const collectionId = await createCollectionExpectSuccess();136137 const chainAdminLimit = (api.consts.common.collectionAdminsLimit as any).toNumber();138 expect(chainAdminLimit).to.be.equal(5);139140 for (let i = 0; i < chainAdminLimit; i++) {141 await addCollectionAdminExpectSuccess(alice, collectionId, accounts[i]);142 const adminListAfterAddAdmin = await getAdminList(api, collectionId);143 expect(adminListAfterAddAdmin).to.be.deep.contains(normalizeAccountId(accounts[i]));144 }145146 const tx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(accounts[chainAdminLimit]));147 await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;148 });149 });150});