difftreelog
CORE-386 Add more test for add/remove collection admins
in: master
2 files changed
pallets/common/src/lib.rsdiffbeforeafterboth1133 user: &T::CrossAccountId,1133 user: &T::CrossAccountId,1134 admin: bool,1134 admin: bool,1135 ) -> DispatchResult {1135 ) -> DispatchResult {1136 collection.check_is_mutable()?;1136 collection.check_is_owner_or_admin(sender)?;1137 collection.check_is_owner(sender)?;113711381138 let was_admin = <IsAdmin<T>>::get((collection.id, user));1139 let was_admin = <IsAdmin<T>>::get((collection.id, user));1139 if was_admin == admin {1140 if was_admin == admin {tests/src/eth/collectionAdmin.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.2// This file is part of Unique Network.34import {expect} from 'chai';5import privateKey from '../substrate/privateKey';6import {7 createEthAccount,8 createEthAccountWithBalance, 9 evmCollection, 10 evmCollectionHelpers, 11 getCollectionAddressFromResult, 12 itWeb3,13} from './util/helpers';1415// Unique Network is free software: you can redistribute it and/or modify3// Unique Network is free software: you can redistribute it and/or modify16// it under the terms of the GNU General Public License as published by4// it under the terms of the GNU General Public License as published by17// the Free Software Foundation, either version 3 of the License, or5// the Free Software Foundation, either version 3 of the License, or25// You should have received a copy of the GNU General Public License13// You should have received a copy of the GNU General Public License26// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.14// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.271516import {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';2628describe.only('Add collection admins', () => {27describe('Add collection admins', () => {29 itWeb3('Add admin by owner', async ({api, web3}) => {28 itWeb3('Add admin by owner', async ({api, web3}) => {30 const owner = await createEthAccountWithBalance(api, web3);29 const owner = await createEthAccountWithBalance(api, web3);31 const collectionHelper = evmCollectionHelpers(web3, owner);30 const collectionHelper = evmCollectionHelpers(web3, owner);55 const newAdmin = privateKey('//Alice');54 const newAdmin = privateKey('//Alice');56 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);55 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);57 await collectionEvm.methods.addCollectionAdminSubstrate(newAdmin.addressRaw).send();56 await collectionEvm.methods.addCollectionAdminSubstrate(newAdmin.addressRaw).send();5758 const adminList = await api.rpc.unique.adminlist(collectionId);58 const adminList = await api.rpc.unique.adminlist(collectionId);59 expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())59 expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())60 .to.be.eq(newAdmin.address.toLocaleLowerCase());60 .to.be.eq(newAdmin.address.toLocaleLowerCase());61 });61 });626263 itWeb3('(!negative tests!) Add admin by admin is not allowed', async ({api, web3}) => {63 itWeb3('(!negative tests!) Add admin by ADMIN is not allowed', async ({api, web3}) => {64 const owner = await createEthAccountWithBalance(api, web3);64 const owner = await createEthAccountWithBalance(api, web3);65 const collectionHelper = evmCollectionHelpers(web3, owner);65 const collectionHelper = evmCollectionHelpers(web3, owner);66 66 72 const admin = await createEthAccountWithBalance(api, web3);72 const admin = await createEthAccountWithBalance(api, web3);73 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);73 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);74 await collectionEvm.methods.addCollectionAdmin(admin).send();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}) => {87 const owner = await createEthAccountWithBalance(api, web3);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);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}) => {107 const owner = await createEthAccountWithBalance(api, web3);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);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}) => {130 const owner = await createEthAccountWithBalance(api, web3);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);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}) => {151 const owner = await createEthAccountWithBalance(api, web3);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();75 {162 {76 const adminList = await api.rpc.unique.adminlist(collectionId);163 const adminList = await api.rpc.unique.adminlist(collectionId);164 expect(adminList.length).to.be.eq(1);77 expect(adminList[0].asEthereum.toString().toLocaleLowerCase())165 expect(adminList[0].asEthereum.toString().toLocaleLowerCase())78 .to.be.eq(admin.toLocaleLowerCase());166 .to.be.eq(newAdmin.toLocaleLowerCase());79 }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}) => {175 const owner = await createEthAccountWithBalance(api, web3);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 = privateKey('//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 }80 191 81 const user = await createEthAccount(web3);192 await collectionEvm.methods.removeCollectionAdminSubstrate(newAdmin.addressRaw).send();82 await collectionEvm.methods.addCollectionAdmin(user).send({from: admin});83 const adminList = await api.rpc.unique.adminlist(collectionId);193 const adminList = await api.rpc.unique.adminlist(collectionId);84 expect(adminList[0].asEthereum.toString().toLocaleLowerCase())194 expect(adminList.length).to.be.eq(0);85 .to.be.eq(admin.toLocaleLowerCase());195 });196197 itWeb3('(!negative tests!) Remove admin by ADMIN is not allowed', async ({api, web3}) => {198 const owner = await createEthAccountWithBalance(api, web3);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);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}) => {225 const owner = await createEthAccountWithBalance(api, web3);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);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}) => {250 const owner = await createEthAccountWithBalance(api, web3);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 = privateKey('//Alice');259 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);260 await collectionEvm.methods.addCollectionAdminSubstrate(adminSub.addressRaw).send();261 const adminEth = await createEthAccountWithBalance(api, web3);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}) => {275 const owner = await createEthAccountWithBalance(api, web3);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 = privateKey('//Alice');284 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);285 await collectionEvm.methods.addCollectionAdminSubstrate(adminSub.addressRaw).send();286 const notAdminEth = await createEthAccountWithBalance(api, web3);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);86 expect(adminList.length).to.be.eq(1);292 expect(adminList.length).to.be.eq(1);293 expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())294 .to.be.eq(adminSub.address.toLocaleLowerCase());87 });295 });88});296});