1234567891011121314151617import {ApiPromise} from '@polkadot/api';18import chai from 'chai';19import chaiAsPromised from 'chai-as-promised';20import {default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';21import {addCollectionAdminExpectSuccess, createCollectionExpectSuccess, destroyCollectionExpectSuccess, getAdminList, normalizeAccountId, queryCollectionExpectSuccess} from './util/helpers';2223chai.use(chaiAsPromised);24const expect = chai.expect;2526describe('Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {27 it('Add collection admin.', async () => {28 await usingApi(async (api, privateKeyWrapper) => {29 const collectionId = await createCollectionExpectSuccess();30 const alice = privateKeyWrapper('//Alice');31 const bob = privateKeyWrapper('//Bob');3233 const collection = await queryCollectionExpectSuccess(api, collectionId);34 expect(collection.owner.toString()).to.be.equal(alice.address);3536 const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));37 await submitTransactionAsync(alice, changeAdminTx);3839 const adminListAfterAddAdmin = await getAdminList(api, collectionId);40 expect(adminListAfterAddAdmin).to.be.deep.contains(normalizeAccountId(bob.address));41 });42 });4344 it('Add admin using added collection admin.', async () => {45 await usingApi(async (api, privateKeyWrapper) => {46 const collectionId = await createCollectionExpectSuccess();47 const alice = privateKeyWrapper('//Alice');48 const bob = privateKeyWrapper('//Bob');49 const charlie = privateKeyWrapper('//CHARLIE');5051 const collection = await queryCollectionExpectSuccess(api, collectionId);52 expect(collection.owner.toString()).to.be.equal(alice.address);5354 const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));55 await submitTransactionAsync(alice, changeAdminTx);5657 const adminListAfterAddAdmin = await getAdminList(api, collectionId);58 expect(adminListAfterAddAdmin).to.be.deep.contains(normalizeAccountId(bob.address));5960 const changeAdminTxCharlie = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(charlie.address));61 await submitTransactionAsync(bob, changeAdminTxCharlie);62 const adminListAfterAddNewAdmin = await getAdminList(api, collectionId);63 expect(adminListAfterAddNewAdmin).to.be.deep.contains(normalizeAccountId(bob.address));64 expect(adminListAfterAddNewAdmin).to.be.deep.contains(normalizeAccountId(charlie.address));65 });66 });67});6869describe('Negative Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {70 it("Not owner can't add collection admin.", async () => {71 await usingApi(async (api, privateKeyWrapper) => {72 const collectionId = await createCollectionExpectSuccess();73 const alice = privateKeyWrapper('//Alice');74 const nonOwner = privateKeyWrapper('//Bob_stash');7576 const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(alice.address));77 await expect(submitTransactionExpectFailAsync(nonOwner, changeAdminTx)).to.be.rejected;7879 const adminListAfterAddAdmin = await getAdminList(api, collectionId);80 expect(adminListAfterAddAdmin).not.to.be.deep.contains(normalizeAccountId(alice.address));8182 83 await createCollectionExpectSuccess();84 });85 });86 it("Can't add collection admin of not existing collection.", async () => {87 await usingApi(async (api, privateKeyWrapper) => {88 89 const collectionId = (1 << 32) - 1;90 const alice = privateKeyWrapper('//Alice');91 const bob = privateKeyWrapper('//Bob');9293 const changeOwnerTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));94 await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;9596 97 await createCollectionExpectSuccess();98 });99 });100101 it("Can't add an admin to a destroyed collection.", async () => {102 await usingApi(async (api, privateKeyWrapper) => {103 const collectionId = await createCollectionExpectSuccess();104 const alice = privateKeyWrapper('//Alice');105 const bob = privateKeyWrapper('//Bob');106 await destroyCollectionExpectSuccess(collectionId);107 const changeOwnerTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));108 await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;109110 111 await createCollectionExpectSuccess();112 });113 });114115 it('Add an admin to a collection that has reached the maximum number of admins limit', async () => {116 await usingApi(async (api: ApiPromise, privateKeyWrapper) => {117 const alice = privateKeyWrapper('//Alice');118 const accounts = [119 privateKeyWrapper('//AdminTest/1').address,120 privateKeyWrapper('//AdminTest/2').address,121 privateKeyWrapper('//AdminTest/3').address,122 privateKeyWrapper('//AdminTest/4').address,123 privateKeyWrapper('//AdminTest/5').address,124 privateKeyWrapper('//AdminTest/6').address,125 privateKeyWrapper('//AdminTest/7').address,126 ];127 const collectionId = await createCollectionExpectSuccess();128129 const chainAdminLimit = (api.consts.common.collectionAdminsLimit as any).toNumber();130 expect(chainAdminLimit).to.be.equal(5);131132 for (let i = 0; i < chainAdminLimit; i++) {133 await addCollectionAdminExpectSuccess(alice, collectionId, accounts[i]);134 const adminListAfterAddAdmin = await getAdminList(api, collectionId);135 expect(adminListAfterAddAdmin).to.be.deep.contains(normalizeAccountId(accounts[i]));136 }137138 const tx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(accounts[chainAdminLimit]));139 await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;140 });141 });142});