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

difftreelog

source

tests/src/toggleContractAllowList.test.ts7.8 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 chai from 'chai';18import chaiAsPromised from 'chai-as-promised';19import usingApi, {submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';20import {21  deployFlipper,22  getFlipValue,23} from './util/contracthelpers';24import {25  getGenericResult,26} from './util/helpers';2728chai.use(chaiAsPromised);29const expect = chai.expect;3031const value = 0;32const gasLimit = 3000n * 1000000n;3334describe.skip('Integration Test toggleContractAllowList', () => {3536  it('Enable allow list contract mode', async () => {37    await usingApi(async (api, privateKeyWrapper) => {38      const [contract, deployer] = await deployFlipper(api, privateKeyWrapper);3940      const enabledBefore = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();41      const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);42      const enableEvents = await submitTransactionAsync(deployer, enableAllowListTx);43      const enabled = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();4445      expect(getGenericResult(enableEvents).success).to.be.true;46      expect(enabledBefore).to.be.false;47      expect(enabled).to.be.true;48    });49  });5051  it('Only allowlisted account can call contract', async () => {52    await usingApi(async (api, privateKeyWrapper) => {53      const bob = privateKeyWrapper('//Bob');5455      const [contract, deployer] = await deployFlipper(api, privateKeyWrapper);5657      let flipValueBefore = await getFlipValue(contract, deployer);58      const flip = contract.tx.flip({value, gasLimit});59      await submitTransactionAsync(bob, flip);60      const flipValueAfter = await getFlipValue(contract,deployer);61      expect(flipValueAfter).to.be.eq(!flipValueBefore, 'Anyone can call new contract.');6263      const deployerCanFlip = async () => {64        const flipValueBefore = await getFlipValue(contract, deployer);65        const deployerFlip = contract.tx.flip({value, gasLimit});66        await submitTransactionAsync(deployer, deployerFlip);67        const aliceFlip1Response = await getFlipValue(contract, deployer);68        expect(aliceFlip1Response).to.be.eq(!flipValueBefore, 'Deployer always can flip.');69      };70      await deployerCanFlip();7172      flipValueBefore = await getFlipValue(contract, deployer);73      const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);74      await submitTransactionAsync(deployer, enableAllowListTx);75      const flipWithEnabledAllowList = contract.tx.flip({value, gasLimit});76      await expect(submitTransactionExpectFailAsync(bob, flipWithEnabledAllowList)).to.be.rejected;77      const flipValueAfterEnableAllowList = await getFlipValue(contract, deployer);78      expect(flipValueAfterEnableAllowList).to.be.eq(flipValueBefore, 'Enabling allowlist doesn\'t make it possible to call contract for everyone.');7980      await deployerCanFlip();8182      flipValueBefore = await getFlipValue(contract, deployer);83      const addBobToAllowListTx = api.tx.unique.addToContractAllowList(contract.address, bob.address);84      await submitTransactionAsync(deployer, addBobToAllowListTx);85      const flipWithAllowlistedBob = contract.tx.flip({value, gasLimit});86      await submitTransactionAsync(bob, flipWithAllowlistedBob);87      const flipAfterAllowListed = await getFlipValue(contract,deployer);88      expect(flipAfterAllowListed).to.be.eq(!flipValueBefore, 'Bob was allowlisted, now he can flip.');8990      await deployerCanFlip();9192      flipValueBefore = await getFlipValue(contract, deployer);93      const removeBobFromAllowListTx = api.tx.unique.removeFromContractAllowList(contract.address, bob.address);94      await submitTransactionAsync(deployer, removeBobFromAllowListTx);95      const bobRemoved = contract.tx.flip({value, gasLimit});96      await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;97      const afterBobRemoved = await getFlipValue(contract, deployer);98      expect(afterBobRemoved).to.be.eq(flipValueBefore, 'Bob can\'t call contract, now when he is removeed from allow list.');99100      await deployerCanFlip();101102      flipValueBefore = await getFlipValue(contract, deployer);103      const disableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, false);104      await submitTransactionAsync(deployer, disableAllowListTx);105      const allowListDisabledFlip = contract.tx.flip({value, gasLimit});106      await submitTransactionAsync(bob, allowListDisabledFlip);107      const afterAllowListDisabled = await getFlipValue(contract,deployer);108      expect(afterAllowListDisabled).to.be.eq(!flipValueBefore, 'Anyone can call contract with disabled allowlist.');109110    });111  });112113  it('Enabling allow list repeatedly should not produce errors', async () => {114    await usingApi(async (api, privateKeyWrapper) => {115      const [contract, deployer] = await deployFlipper(api, privateKeyWrapper);116117      const enabledBefore = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();118      const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);119      const enableEvents = await submitTransactionAsync(deployer, enableAllowListTx);120      const enabled = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();121      const enableAgainEvents = await submitTransactionAsync(deployer, enableAllowListTx);122      const enabledAgain = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();123124      expect(getGenericResult(enableEvents).success).to.be.true;125      expect(enabledBefore).to.be.false;126      expect(enabled).to.be.true;127      expect(getGenericResult(enableAgainEvents).success).to.be.true;128      expect(enabledAgain).to.be.true;129    });130  });131132});133134describe.skip('Negative Integration Test toggleContractAllowList', () => {135136  it('Enable allow list for a non-contract', async () => {137    await usingApi(async (api, privateKeyWrapper) => {138      const alice = privateKeyWrapper('//Alice');139      const bobGuineaPig = privateKeyWrapper('//Bob');140141      const enabledBefore = (await api.query.unique.contractAllowListEnabled(bobGuineaPig.address)).toJSON();142      const enableAllowListTx = api.tx.unique.toggleContractAllowList(bobGuineaPig.address, true);143      await expect(submitTransactionExpectFailAsync(alice, enableAllowListTx)).to.be.rejected;144      const enabled = (await api.query.unique.contractAllowListEnabled(bobGuineaPig.address)).toJSON();145146      expect(enabledBefore).to.be.false;147      expect(enabled).to.be.false;148    });149  });150151  it('Enable allow list using a non-owner address', async () => {152    await usingApi(async (api, privateKeyWrapper) => {153      const bob = privateKeyWrapper('//Bob');154      const [contract] = await deployFlipper(api, privateKeyWrapper);155156      const enabledBefore = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();157      const enableAllowListTx = api.tx.unique.toggleContractAllowList(contract.address, true);158      await expect(submitTransactionExpectFailAsync(bob, enableAllowListTx)).to.be.rejected;159      const enabled = (await api.query.unique.contractAllowListEnabled(contract.address)).toJSON();160161      expect(enabledBefore).to.be.false;162      expect(enabled).to.be.false;163    });164  });165166});