git.delta.rocks / unique-network / refs/commits / fd782c893f4c

difftreelog

source

tests/src/addCollectionAdmin.test.ts6.7 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {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) => {30      const collectionId = await createCollectionExpectSuccess();31      const alice = privateKey('//Alice');32      const bob = privateKey('//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  });4445  it('Add admin using added collection admin.', async () => {46    await usingApi(async (api) => {47      const collectionId = await createCollectionExpectSuccess();48      const alice = privateKey('//Alice');49      const bob = privateKey('//Bob');50      const charlie = privateKey('//CHARLIE');5152      const collection = await queryCollectionExpectSuccess(api, collectionId);53      expect(collection.owner.toString()).to.be.equal(alice.address);5455      const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));56      await submitTransactionAsync(alice, changeAdminTx);5758      const adminListAfterAddAdmin = await getAdminList(api, collectionId);59      expect(adminListAfterAddAdmin).to.be.deep.contains(normalizeAccountId(bob.address));6061      const changeAdminTxCharlie = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(charlie.address));62      await submitTransactionAsync(bob, changeAdminTxCharlie);63      const adminListAfterAddNewAdmin = await getAdminList(api, collectionId);64      expect(adminListAfterAddNewAdmin).to.be.deep.contains(normalizeAccountId(bob.address));65      expect(adminListAfterAddNewAdmin).to.be.deep.contains(normalizeAccountId(charlie.address));66    });67  });68});6970describe('Negative Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {71  it("Not owner can't add collection admin.", async () => {72    await usingApi(async (api) => {73      const collectionId = await createCollectionExpectSuccess();74      const alice = privateKey('//Alice');75      const nonOwner = privateKey('//Bob_stash');7677      const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(alice.address));78      await expect(submitTransactionExpectFailAsync(nonOwner, changeAdminTx)).to.be.rejected;7980      const adminListAfterAddAdmin = await getAdminList(api, collectionId);81      expect(adminListAfterAddAdmin).not.to.be.deep.contains(normalizeAccountId(alice.address));8283      // Verifying that nothing bad happened (network is live, new collections can be created, etc.)84      await createCollectionExpectSuccess();85    });86  });87  it("Can't add collection admin of not existing collection.", async () => {88    await usingApi(async (api) => {89      // tslint:disable-next-line: no-bitwise90      const collectionId = (1 << 32) - 1;91      const alice = privateKey('//Alice');92      const bob = privateKey('//Bob');9394      const changeOwnerTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));95      await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;9697      // Verifying that nothing bad happened (network is live, new collections can be created, etc.)98      await createCollectionExpectSuccess();99    });100  });101102  it("Can't add an admin to a destroyed collection.", async () => {103    await usingApi(async (api) => {104      const collectionId = await createCollectionExpectSuccess();105      const alice = privateKey('//Alice');106      const bob = privateKey('//Bob');107      await destroyCollectionExpectSuccess(collectionId);108      const changeOwnerTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(bob.address));109      await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;110111      // Verifying that nothing bad happened (network is live, new collections can be created, etc.)112      await createCollectionExpectSuccess();113    });114  });115116  it('Add an admin to a collection that has reached the maximum number of admins limit', async () => {117    await usingApi(async (api: ApiPromise) => {118      const alice = privateKey('//Alice');119      const accounts = [120        privateKey('//AdminTest/1').address,121        privateKey('//AdminTest/2').address,122        privateKey('//AdminTest/3').address,123        privateKey('//AdminTest/4').address,124        privateKey('//AdminTest/5').address,125        privateKey('//AdminTest/6').address,126        privateKey('//AdminTest/7').address,127      ];128      const collectionId = await createCollectionExpectSuccess();129130      const chainAdminLimit = (api.consts.common.collectionAdminsLimit as any).toNumber();131      expect(chainAdminLimit).to.be.equal(5);132133      for (let i = 0; i < chainAdminLimit; i++) {134        await addCollectionAdminExpectSuccess(alice, collectionId, accounts[i]);135        const adminListAfterAddAdmin = await getAdminList(api, collectionId);136        expect(adminListAfterAddAdmin).to.be.deep.contains(normalizeAccountId(accounts[i]));137      }138139      const tx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(accounts[chainAdminLimit]));140      await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;141    });142  });143});