git.delta.rocks / unique-network / refs/commits / 3d7ffe75f2a8

difftreelog

source

tests/src/eth/collectionAdmin.test.ts13.7 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.3// Unique Network is free software: you can redistribute it and/or modify4// it under the terms of the GNU General Public License as published by5// the Free Software Foundation, either version 3 of the License, or6// (at your option) any later version.7//8// Unique Network is distributed in the hope that it will be useful,9// but WITHOUT ANY WARRANTY; without even the implied warranty of10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the11// GNU General Public License for more details.1213// You should have received a copy of the GNU General Public License14// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1516import {expect} from 'chai';17import privateKey from '../substrate/privateKey';18import {19  createEthAccount,20  createEthAccountWithBalance, 21  evmCollection, 22  evmCollectionHelpers, 23  getCollectionAddressFromResult, 24  itWeb3,25} from './util/helpers';2627describe('Add collection admins', () => {28  itWeb3('Add admin by owner', async ({api, web3, privateKeyWrapper}) => {29    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);30    const collectionHelper = evmCollectionHelpers(web3, owner);31        32    const result = await collectionHelper.methods33      .createNonfungibleCollection('A', 'B', 'C')34      .send();35    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);3637    const newAdmin = await createEthAccount(web3);38    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);39    await collectionEvm.methods.addCollectionAdmin(newAdmin).send();40    const adminList = await api.rpc.unique.adminlist(collectionId);41    expect(adminList[0].asEthereum.toString().toLocaleLowerCase())42      .to.be.eq(newAdmin.toLocaleLowerCase());43  });4445  itWeb3('Add substrate admin by owner', async ({api, web3, privateKeyWrapper}) => {46    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);47    const collectionHelper = evmCollectionHelpers(web3, owner);48        49    const result = await collectionHelper.methods50      .createNonfungibleCollection('A', 'B', 'C')51      .send();52    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);5354    const newAdmin = privateKeyWrapper('//Alice');55    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);56    await collectionEvm.methods.addCollectionAdminSubstrate(newAdmin.addressRaw).send();5758    const adminList = await api.rpc.unique.adminlist(collectionId);59    expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())60      .to.be.eq(newAdmin.address.toLocaleLowerCase());61  });6263  itWeb3('(!negative tests!) Add admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {64    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);65    const collectionHelper = evmCollectionHelpers(web3, owner);66        67    const result = await collectionHelper.methods68      .createNonfungibleCollection('A', 'B', 'C')69      .send();70    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);7172    const admin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);73    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);74    await collectionEvm.methods.addCollectionAdmin(admin).send();75    76    const user = await createEthAccount(web3);77    await expect(collectionEvm.methods.addCollectionAdmin(user).call({from: admin}))78      .to.be.rejectedWith('NoPermission');7980    const adminList = await api.rpc.unique.adminlist(collectionId);81    expect(adminList.length).to.be.eq(1);82    expect(adminList[0].asEthereum.toString().toLocaleLowerCase())83      .to.be.eq(admin.toLocaleLowerCase());84  });8586  itWeb3('(!negative tests!) Add admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {87    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);88    const collectionHelper = evmCollectionHelpers(web3, owner);89        90    const result = await collectionHelper.methods91      .createNonfungibleCollection('A', 'B', 'C')92      .send();93    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);9495    const notAdmin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);96    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);97    98    const user = await createEthAccount(web3);99    await expect(collectionEvm.methods.addCollectionAdmin(user).call({from: notAdmin}))100      .to.be.rejectedWith('NoPermission');101102    const adminList = await api.rpc.unique.adminlist(collectionId);103    expect(adminList.length).to.be.eq(0);104  });105106  itWeb3('(!negative tests!) Add substrate admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {107    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);108    const collectionHelper = evmCollectionHelpers(web3, owner);109        110    const result = await collectionHelper.methods111      .createNonfungibleCollection('A', 'B', 'C')112      .send();113    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);114115    const admin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);116    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);117    await collectionEvm.methods.addCollectionAdmin(admin).send();118119    const notAdmin = privateKey('//Alice');120    await expect(collectionEvm.methods.addCollectionAdminSubstrate(notAdmin.addressRaw).call({from: admin}))121      .to.be.rejectedWith('NoPermission');122123    const adminList = await api.rpc.unique.adminlist(collectionId);124    expect(adminList.length).to.be.eq(1);125    expect(adminList[0].asEthereum.toString().toLocaleLowerCase())126      .to.be.eq(admin.toLocaleLowerCase());127  });128  129  itWeb3('(!negative tests!) Add substrate admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {130    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);131    const collectionHelper = evmCollectionHelpers(web3, owner);132        133    const result = await collectionHelper.methods134      .createNonfungibleCollection('A', 'B', 'C')135      .send();136    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);137138    const notAdmin0 = await createEthAccountWithBalance(api, web3, privateKeyWrapper);139    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);140    const notAdmin1 = privateKey('//Alice');141    await expect(collectionEvm.methods.addCollectionAdminSubstrate(notAdmin1.addressRaw).call({from: notAdmin0}))142      .to.be.rejectedWith('NoPermission');143144    const adminList = await api.rpc.unique.adminlist(collectionId);145    expect(adminList.length).to.be.eq(0);146  });147});148149describe('Remove collection admins', () => {150  itWeb3('Remove admin by owner', async ({api, web3, privateKeyWrapper}) => {151    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);152    const collectionHelper = evmCollectionHelpers(web3, owner);153        154    const result = await collectionHelper.methods155      .createNonfungibleCollection('A', 'B', 'C')156      .send();157    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);158159    const newAdmin = await createEthAccount(web3);160    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);161    await collectionEvm.methods.addCollectionAdmin(newAdmin).send();162    {163      const adminList = await api.rpc.unique.adminlist(collectionId);164      expect(adminList.length).to.be.eq(1);165      expect(adminList[0].asEthereum.toString().toLocaleLowerCase())166        .to.be.eq(newAdmin.toLocaleLowerCase());167    }168169    await collectionEvm.methods.removeCollectionAdmin(newAdmin).send();170    const adminList = await api.rpc.unique.adminlist(collectionId);171    expect(adminList.length).to.be.eq(0);172  });173174  itWeb3('Remove substrate admin by owner', async ({api, web3, privateKeyWrapper}) => {175    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);176    const collectionHelper = evmCollectionHelpers(web3, owner);177        178    const result = await collectionHelper.methods179      .createNonfungibleCollection('A', 'B', 'C')180      .send();181    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);182183    const newAdmin = privateKeyWrapper('//Alice');184    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);185    await collectionEvm.methods.addCollectionAdminSubstrate(newAdmin.addressRaw).send();186    {187      const adminList = await api.rpc.unique.adminlist(collectionId);188      expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())189        .to.be.eq(newAdmin.address.toLocaleLowerCase());190    }191    192    await collectionEvm.methods.removeCollectionAdminSubstrate(newAdmin.addressRaw).send();193    const adminList = await api.rpc.unique.adminlist(collectionId);194    expect(adminList.length).to.be.eq(0);195  });196197  itWeb3('(!negative tests!) Remove admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {198    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);199    const collectionHelper = evmCollectionHelpers(web3, owner);200        201    const result = await collectionHelper.methods202      .createNonfungibleCollection('A', 'B', 'C')203      .send();204    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);205206    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);207208    const admin0 = await createEthAccountWithBalance(api, web3, privateKeyWrapper);209    await collectionEvm.methods.addCollectionAdmin(admin0).send();210    const admin1 = await createEthAccount(web3);211    await collectionEvm.methods.addCollectionAdmin(admin1).send();212213    await expect(collectionEvm.methods.removeCollectionAdmin(admin1).call({from: admin0}))214      .to.be.rejectedWith('NoPermission');215    {216      const adminList = await api.rpc.unique.adminlist(collectionId);217      expect(adminList.length).to.be.eq(2);218      expect(adminList.toString().toLocaleLowerCase())219        .to.be.deep.contains(admin0.toLocaleLowerCase())220        .to.be.deep.contains(admin1.toLocaleLowerCase());221    }222  });223224  itWeb3('(!negative tests!) Remove admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {225    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);226    const collectionHelper = evmCollectionHelpers(web3, owner);227        228    const result = await collectionHelper.methods229      .createNonfungibleCollection('A', 'B', 'C')230      .send();231    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);232233    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);234235    const admin = await createEthAccountWithBalance(api, web3, privateKeyWrapper);236    await collectionEvm.methods.addCollectionAdmin(admin).send();237    const notAdmin = await createEthAccount(web3);238239    await expect(collectionEvm.methods.removeCollectionAdmin(admin).call({from: notAdmin}))240      .to.be.rejectedWith('NoPermission');241    {242      const adminList = await api.rpc.unique.adminlist(collectionId);243      expect(adminList[0].asEthereum.toString().toLocaleLowerCase())244        .to.be.eq(admin.toLocaleLowerCase());245      expect(adminList.length).to.be.eq(1);246    }247  });248249  itWeb3('(!negative tests!) Remove substrate admin by ADMIN is not allowed', async ({api, web3, privateKeyWrapper}) => {250    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);251    const collectionHelper = evmCollectionHelpers(web3, owner);252        253    const result = await collectionHelper.methods254      .createNonfungibleCollection('A', 'B', 'C')255      .send();256    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);257258    const adminSub = privateKeyWrapper('//Alice');259    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);260    await collectionEvm.methods.addCollectionAdminSubstrate(adminSub.addressRaw).send();261    const adminEth = await createEthAccountWithBalance(api, web3, privateKeyWrapper);262    await collectionEvm.methods.addCollectionAdmin(adminEth).send();263264    await expect(collectionEvm.methods.removeCollectionAdminSubstrate(adminSub.addressRaw).call({from: adminEth}))265      .to.be.rejectedWith('NoPermission');266267    const adminList = await api.rpc.unique.adminlist(collectionId);268    expect(adminList.length).to.be.eq(2);269    expect(adminList.toString().toLocaleLowerCase())270      .to.be.deep.contains(adminSub.address.toLocaleLowerCase())271      .to.be.deep.contains(adminEth.toLocaleLowerCase());272  });273274  itWeb3('(!negative tests!) Remove substrate admin by USER is not allowed', async ({api, web3, privateKeyWrapper}) => {275    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);276    const collectionHelper = evmCollectionHelpers(web3, owner);277        278    const result = await collectionHelper.methods279      .createNonfungibleCollection('A', 'B', 'C')280      .send();281    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);282283    const adminSub = privateKeyWrapper('//Alice');284    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);285    await collectionEvm.methods.addCollectionAdminSubstrate(adminSub.addressRaw).send();286    const notAdminEth = await createEthAccountWithBalance(api, web3, privateKeyWrapper);287288    await expect(collectionEvm.methods.removeCollectionAdminSubstrate(adminSub.addressRaw).call({from: notAdminEth}))289      .to.be.rejectedWith('NoPermission');290291    const adminList = await api.rpc.unique.adminlist(collectionId);292    expect(adminList.length).to.be.eq(1);293    expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())294      .to.be.eq(adminSub.address.toLocaleLowerCase());295  });296});