git.delta.rocks / unique-network / refs/commits / 5a7505aa2f8f

difftreelog

refactor Move tests to playgrounds.

Trubnikov Sergey2022-09-08parent: #294c410.patch.diff
in: master

2 files changed

modifiedtests/src/eth/allowlist.test.tsdiffbeforeafterboth
before · tests/src/eth/allowlist.test.ts
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 {expect} from 'chai';18import {isAllowlisted} from '../util/helpers';19import {20  contractHelpers,21  createEthAccount,22  createEthAccountWithBalance,23  deployFlipper,24  evmCollection,25  evmCollectionHelpers,26  getCollectionAddressFromResult,27  itWeb3,28} from './util/helpers';2930describe('EVM contract allowlist', () => {31  itWeb3('Contract allowlist can be toggled', async ({api, web3, privateKeyWrapper}) => {32    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);33    const flipper = await deployFlipper(web3, owner);3435    const helpers = contractHelpers(web3, owner);3637    // Any user is allowed by default38    expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;3940    // Enable41    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});42    expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.true;4344    // Disable45    await helpers.methods.toggleAllowlist(flipper.options.address, false).send({from: owner});46    expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;47  });4849  itWeb3('Non-allowlisted user can\'t call contract with allowlist enabled', async ({api, web3, privateKeyWrapper}) => {50    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);51    const flipper = await deployFlipper(web3, owner);52    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);5354    const helpers = contractHelpers(web3, owner);5556    // User can flip with allowlist disabled57    await flipper.methods.flip().send({from: caller});58    expect(await flipper.methods.getValue().call()).to.be.true;5960    // Tx will be reverted if user is not in allowlist61    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});62    await expect(flipper.methods.flip().send({from: caller})).to.rejected;63    expect(await flipper.methods.getValue().call()).to.be.true;6465    // Adding caller to allowlist will make contract callable again66    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});67    await flipper.methods.flip().send({from: caller});68    expect(await flipper.methods.getValue().call()).to.be.false;69  });70});7172describe('EVM collection allowlist', () => {73  itWeb3('Collection allowlist can be added and removed by [eth] address', async ({api, web3, privateKeyWrapper}) => {74    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);75    const user = createEthAccount(web3);76    77    const collectionHelpers = evmCollectionHelpers(web3, owner);78    const result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();79    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);80    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);81    82    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;83    await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});84    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;85    86    await collectionEvm.methods.removeFromCollectionAllowList(user).send({from: owner});87    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;88  });8990  itWeb3('Collection allowlist can be added and removed by [sub] address', async ({api, web3, privateKeyWrapper}) => {91    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);92    const user = privateKeyWrapper('//Alice');93    94    const collectionHelpers = evmCollectionHelpers(web3, owner);95    const result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();96    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);97    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);98    99    expect(await isAllowlisted(api, collectionId, user)).to.be.false;100    await collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).send({from: owner});101    expect(await isAllowlisted(api, collectionId, user)).to.be.true;102103    await collectionEvm.methods.removeFromCollectionAllowListSubstrate(user.addressRaw).send({from: owner});104    expect(await isAllowlisted(api, collectionId, user)).to.be.false;105  });106107  itWeb3('Collection allowlist can not be add and remove [eth] address by not owner', async ({api, web3, privateKeyWrapper}) => {108    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);109    const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);110    const user = createEthAccount(web3);111    112    const collectionHelpers = evmCollectionHelpers(web3, owner);113    const result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();114    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);115    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);116    117    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;118    await expect(collectionEvm.methods.addToCollectionAllowList(user).call({from: notOwner})).to.be.rejectedWith('NoPermission');119    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;120    await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});121    122    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;123    await expect(collectionEvm.methods.removeFromCollectionAllowList(user).call({from: notOwner})).to.be.rejectedWith('NoPermission');124    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;125  });126127  itWeb3('Collection allowlist can not be add and remove [sub] address by not owner', async ({api, web3, privateKeyWrapper}) => {128    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);129    const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);130    const user = privateKeyWrapper('//Alice');131    132    const collectionHelpers = evmCollectionHelpers(web3, owner);133    const result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();134    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);135    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);136    137    expect(await isAllowlisted(api, collectionId, user)).to.be.false;138    await expect(collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).call({from: notOwner})).to.be.rejectedWith('NoPermission');139    expect(await isAllowlisted(api, collectionId, user)).to.be.false;140    await collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).send({from: owner});141    142    expect(await isAllowlisted(api, collectionId, user)).to.be.true;143    await expect(collectionEvm.methods.removeFromCollectionAllowListSubstrate(user.addressRaw).call({from: notOwner})).to.be.rejectedWith('NoPermission');144    expect(await isAllowlisted(api, collectionId, user)).to.be.true;145  });146});
after · tests/src/eth/allowlist.test.ts
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 {IKeyringPair} from '@polkadot/types/types';18import {expect} from 'chai';19import {isAllowlisted, normalizeAccountId} from '../util/helpers';20import {21  contractHelpers,22  createEthAccount,23  createEthAccountWithBalance,24  deployFlipper,25  evmCollection,26  evmCollectionHelpers,27  getCollectionAddressFromResult,28  itWeb3,29} from './util/helpers';30import {itEth, usingEthPlaygrounds} from './util/playgrounds';3132describe('EVM contract allowlist', () => {33  itWeb3('Contract allowlist can be toggled', async ({api, web3, privateKeyWrapper}) => {34    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);35    const flipper = await deployFlipper(web3, owner);3637    const helpers = contractHelpers(web3, owner);3839    // Any user is allowed by default40    expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;4142    // Enable43    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});44    expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.true;4546    // Disable47    await helpers.methods.toggleAllowlist(flipper.options.address, false).send({from: owner});48    expect(await helpers.methods.allowlistEnabled(flipper.options.address).call()).to.be.false;49  });5051  itWeb3('Non-allowlisted user can\'t call contract with allowlist enabled', async ({api, web3, privateKeyWrapper}) => {52    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);53    const flipper = await deployFlipper(web3, owner);54    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);5556    const helpers = contractHelpers(web3, owner);5758    // User can flip with allowlist disabled59    await flipper.methods.flip().send({from: caller});60    expect(await flipper.methods.getValue().call()).to.be.true;6162    // Tx will be reverted if user is not in allowlist63    await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});64    await expect(flipper.methods.flip().send({from: caller})).to.rejected;65    expect(await flipper.methods.getValue().call()).to.be.true;6667    // Adding caller to allowlist will make contract callable again68    await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});69    await flipper.methods.flip().send({from: caller});70    expect(await flipper.methods.getValue().call()).to.be.false;71  });72});7374describe('EVM collection allowlist', () => {75  let donor: IKeyringPair;7677  before(async function() {78    await usingEthPlaygrounds(async (_helper, privateKey) => {79      donor = privateKey('//Alice');80    });81  });82  83  itEth('Collection allowlist can be added and removed by [eth] address', async ({helper}) => {84    const owner = await helper.eth.createAccountWithBalance(donor);85    const user = helper.eth.createAccount();86    87    const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');88    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);89    90    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;91    await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});92    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;93    94    await collectionEvm.methods.removeFromCollectionAllowList(user).send({from: owner});95    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;96  });9798  itEth('Collection allowlist can be added and removed by [sub] address', async ({helper}) => {99    const owner = await helper.eth.createAccountWithBalance(donor);100    const user = donor;101    102    const {collectionAddress, collectionId} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');103    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);104    105    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;106    await collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).send({from: owner});107    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;108    109    await collectionEvm.methods.removeFromCollectionAllowListSubstrate(user.addressRaw).send({from: owner});110    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;111  });112113  itEth('Collection allowlist can not be add and remove [eth] address by not owner', async ({helper}) => {114    const owner = await helper.eth.createAccountWithBalance(donor);115    const notOwner = await helper.eth.createAccountWithBalance(donor);116    const user = helper.eth.createAccount();117    118    const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');119    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);120    121    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;122    await expect(collectionEvm.methods.addToCollectionAllowList(user).call({from: notOwner})).to.be.rejectedWith('NoPermission');123    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.false;124    await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});125    126    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;127    await expect(collectionEvm.methods.removeFromCollectionAllowList(user).call({from: notOwner})).to.be.rejectedWith('NoPermission');128    expect(await collectionEvm.methods.allowed(user).call({from: owner})).to.be.true;129  });130131  itEth('Collection allowlist can not be add and remove [sub] address by not owner', async ({helper}) => {132    const owner = await helper.eth.createAccountWithBalance(donor);133    const notOwner = await helper.eth.createAccountWithBalance(donor);134    const user = donor;135    136    const {collectionAddress, collectionId} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');137    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);138    139    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;140    await expect(collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).call({from: notOwner})).to.be.rejectedWith('NoPermission');141    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;142    await collectionEvm.methods.addToCollectionAllowListSubstrate(user.addressRaw).send({from: owner});143    144    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;145    await expect(collectionEvm.methods.removeFromCollectionAllowListSubstrate(user.addressRaw).call({from: notOwner})).to.be.rejectedWith('NoPermission');146    expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;147  });148});
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
--- a/tests/src/util/playgrounds/unique.ts
+++ b/tests/src/util/playgrounds/unique.ts
@@ -730,6 +730,18 @@
   }
 
   /**
+   * Check if user is in allow list.
+   * 
+   * @param collectionId ID of collection
+   * @param user Account to check
+   * @example await getAdmins(1)
+   * @returns is user in allow list
+   */
+  async allowed(collectionId: number, user: ICrossAccountId): Promise<boolean> {
+    return (await this.helper.callRpc('api.rpc.unique.allowed', [collectionId, user])).toJSON();
+  }
+
+  /**
    * Adds an address to allow list
    * @param signer keyring of signer
    * @param collectionId ID of collection