git.delta.rocks / unique-network / refs/commits / 98ba4b374229

difftreelog

source

tests/src/toggleContractWhiteList.test.ts7.0 KiBsourcehistory
1import chai from "chai";2import chaiAsPromised from 'chai-as-promised';3import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";4import privateKey from "./substrate/privateKey";5import {6  deployFlipper,7  getFlipValue8} from "./util/contracthelpers";9import {10  getGenericResult11} from "./util/helpers"1213chai.use(chaiAsPromised);14const expect = chai.expect;1516const value = 0;17const gasLimit = 3000n * 1000000n;1819describe('Integration Test toggleContractWhiteList', () => {2021  it(`Enable white list contract mode`, async () => {22    await usingApi(async api => {23      const [contract, deployer] = await deployFlipper(api);2425      const enabledBefore = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();26      const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, true);27      const enableEvents = await submitTransactionAsync(deployer, enableWhiteListTx);28      const enabled = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();2930      expect(getGenericResult(enableEvents).success).to.be.true;31      expect(enabledBefore).to.be.false;32      expect(enabled).to.be.true;33    });34  });3536  it(`Only whitelisted account can call contract`, async () => {37    await usingApi(async api => {38      const bob = privateKey("//Bob");3940      const [contract, deployer] = await deployFlipper(api);4142      let flipValueBefore = await getFlipValue(contract, deployer);43      const flip = contract.exec('flip', value, gasLimit);44      await submitTransactionAsync(bob, flip);45      const flipValueAfter = await getFlipValue(contract,deployer);46      expect(flipValueAfter).to.be.eq(!flipValueBefore, `Anyone can call new contract.`);4748      const deployerCanFlip = async () => {49        let flipValueBefore = await getFlipValue(contract, deployer);50        const deployerFlip = contract.exec('flip', value, gasLimit);51        await submitTransactionAsync(deployer, deployerFlip);52        const aliceFlip1Response = await getFlipValue(contract, deployer);53        expect(aliceFlip1Response).to.be.eq(!flipValueBefore, `Deployer always can flip.`);54      };55      await deployerCanFlip();5657      flipValueBefore = await getFlipValue(contract, deployer);58      const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, true);59      const enableResult = await submitTransactionAsync(deployer, enableWhiteListTx);60      const flipWithEnabledWhiteList = contract.exec('flip', value, gasLimit);61      await expect(submitTransactionExpectFailAsync(bob, flipWithEnabledWhiteList)).to.be.rejected;62      const flipValueAfterEnableWhiteList = await getFlipValue(contract, deployer);63      expect(flipValueAfterEnableWhiteList).to.be.eq(flipValueBefore, `Enabling whitelist doesn't make it possible to call contract for everyone.`);6465      await deployerCanFlip();6667      flipValueBefore = await getFlipValue(contract, deployer);68      const addBobToWhiteListTx = api.tx.nft.addToContractWhiteList(contract.address, bob.address);69      const addBobResult = await submitTransactionAsync(deployer, addBobToWhiteListTx);70      const flipWithWhitelistedBob = contract.exec('flip', value, gasLimit);71      await submitTransactionAsync(bob, flipWithWhitelistedBob);72      const flipAfterWhiteListed = await getFlipValue(contract,deployer);73      expect(flipAfterWhiteListed).to.be.eq(!flipValueBefore, `Bob was whitelisted, now he can flip.`);7475      await deployerCanFlip();7677      flipValueBefore = await getFlipValue(contract, deployer);78      const removeBobFromWhiteListTx = api.tx.nft.removeFromContractWhiteList(contract.address, bob.address);79      const removeBobResult = await submitTransactionAsync(deployer, removeBobFromWhiteListTx);80      const bobRemoved = contract.exec('flip', value, gasLimit);81      await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;82      const afterBobRemoved = await getFlipValue(contract, deployer);83      expect(afterBobRemoved).to.be.eq(flipValueBefore, `Bob can't call contract, now when he is removeed from white list.`);8485      await deployerCanFlip();8687      flipValueBefore = await getFlipValue(contract, deployer);88      const disableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, false);89      const disableWhiteListResult = await submitTransactionAsync(deployer, disableWhiteListTx);90      const whiteListDisabledFlip = contract.exec('flip', value, gasLimit);91      await submitTransactionAsync(bob, whiteListDisabledFlip);92      const afterWhiteListDisabled = await getFlipValue(contract,deployer);93      expect(afterWhiteListDisabled).to.be.eq(!flipValueBefore, `Anyone can call contract with disabled whitelist.`);9495    });96  });9798  it(`Enabling white list repeatedly should not produce errors`, async () => {99    await usingApi(async api => {100      const [contract, deployer] = await deployFlipper(api);101102      const enabledBefore = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();103      const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, true);104      const enableEvents = await submitTransactionAsync(deployer, enableWhiteListTx);105      const enabled = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();106      const enableAgainEvents = await submitTransactionAsync(deployer, enableWhiteListTx);107      const enabledAgain = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();108109      expect(getGenericResult(enableEvents).success).to.be.true;110      expect(enabledBefore).to.be.false;111      expect(enabled).to.be.true;112      expect(getGenericResult(enableAgainEvents).success).to.be.true;113      expect(enabledAgain).to.be.true;114    });115  });116117});118119describe('Negative Integration Test toggleContractWhiteList', () => {120121  it(`Enable white list for a non-contract`, async () => {122    await usingApi(async api => {123      const alice = privateKey("//Alice");124      const bobGuineaPig = privateKey("//Bob");125126      const enabledBefore = (await api.query.nft.contractWhiteListEnabled(bobGuineaPig.address)).toJSON();127      const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(bobGuineaPig.address, true);128      await expect(submitTransactionExpectFailAsync(alice, enableWhiteListTx)).to.be.rejected;129      const enabled = (await api.query.nft.contractWhiteListEnabled(bobGuineaPig.address)).toJSON();130131      expect(enabledBefore).to.be.false;132      expect(enabled).to.be.false;133    });134  });135136  it(`Enable white list using a non-owner address`, async () => {137    await usingApi(async api => {138      const bob = privateKey("//Bob");139      const [contract, deployer] = await deployFlipper(api);140141      const enabledBefore = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();142      const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, true);143      await expect(submitTransactionExpectFailAsync(bob, enableWhiteListTx)).to.be.rejected;144      const enabled = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();145146      expect(enabledBefore).to.be.false;147      expect(enabled).to.be.false;148    });149  });150151});