git.delta.rocks / unique-network / refs/commits / 6adc689f67ef

difftreelog

Refactor admin tests

Max Andreev2022-11-30parent: #d7cd036.patch.diff
in: master
combine tests
remove Bob
add e2e test

2 files changed

modifiedtests/src/eth/collectionAdmin.test.tsdiffbeforeafterboth
39 });39 });
40 });40 });
41
42 // Soft-deprecated
43 itEth('Add admin by owner', async ({helper}) => {
44 const owner = await helper.eth.createAccountWithBalance(donor);
45 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');
46 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);
47
48 const newAdmin = helper.eth.createAccount();
49
50 await collectionEvm.methods.addCollectionAdmin(newAdmin).send();
51 const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
52 expect(adminList[0].asEthereum.toString().toLocaleLowerCase())
53 .to.be.eq(newAdmin.toLocaleLowerCase());
54 });
5541
56 itEth('Add cross account admin by owner', async ({helper, privateKey}) => {42 itEth('can add account admin by owner', async ({helper, privateKey}) => {
43 // arrange
57 const owner = await helper.eth.createAccountWithBalance(donor);44 const owner = await helper.eth.createAccountWithBalance(donor);
58 45 const adminSub = await privateKey('//admin2');
46 const adminEth = helper.eth.createAccount().toLowerCase();
47
48 const adminDeprecated = helper.eth.createAccount().toLowerCase();
49 const adminCrossSub = helper.ethCrossAccount.fromKeyringPair(adminSub);
50 const adminCrossEth = helper.ethCrossAccount.fromAddress(adminEth);
51
59 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');52 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');
60 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);53 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);
61 54
55 // Soft-deprecated: can addCollectionAdmin
62 const newAdmin = await privateKey('//Bob');56 await collectionEvm.methods.addCollectionAdmin(adminDeprecated).send();
57 // Can addCollectionAdminCross for substrate and ethereum address
63 const newAdminCross = helper.ethCrossAccount.fromKeyringPair(newAdmin);58 await collectionEvm.methods.addCollectionAdminCross(adminCrossSub).send();
64 await collectionEvm.methods.addCollectionAdminCross(newAdminCross).send();59 await collectionEvm.methods.addCollectionAdminCross(adminCrossEth).send();
6560
61 // 1. Expect api.rpc.unique.adminlist returns admins:
66 const adminList = await helper.collection.getAdmins(collectionId);62 const adminListRpc = await helper.collection.getAdmins(collectionId);
63 expect(adminListRpc).to.has.length(3);
67 expect(adminList).to.be.like([{Substrate: newAdmin.address}]);64 expect(adminListRpc).to.be.deep.contain.members([{Substrate: adminSub.address}, {Ethereum: adminEth}, {Ethereum: adminDeprecated}]);
65
66 // 2. Expect methods.collectionAdmins == api.rpc.unique.adminlist
67 let adminListEth = await collectionEvm.methods.collectionAdmins().call();
68 adminListEth = adminListEth.map((element: IEthCrossAccountId) => {
69 return helper.address.convertCrossAccountFromEthCrossAccount(element);
70 });
71 expect(adminListRpc).to.be.like(adminListEth);
68 });72 });
6973
70 itEth('Check adminlist', async ({helper, privateKey}) => {74 itEth('cross account admin can mint', async ({helper}) => {
75 // arrange: create collection and accounts
71 const owner = await helper.eth.createAccountWithBalance(donor);76 const owner = await helper.eth.createAccountWithBalance(donor);
72
73 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');77 const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', 'a', 'b', 'uri');
74 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);78 const adminEth = (await helper.eth.createAccountWithBalance(donor)).toLowerCase();
75
76 const admin1 = helper.eth.createAccount();79 const adminCrossEth = helper.ethCrossAccount.fromAddress(adminEth);
77 const admin2 = await privateKey('admin');
78 const admin2Cross = helper.ethCrossAccount.fromKeyringPair(admin2);80 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);
79 81
80 // Soft-deprecated82 // cannot mint while not admin
81 await collectionEvm.methods.addCollectionAdmin(admin1).send();83 await expect(collectionEvm.methods.mint(owner).call({from: adminEth})).to.be.rejectedWith('PublicMintingNotAllowed');
84
85 // admin can mint token:
82 await collectionEvm.methods.addCollectionAdminCross(admin2Cross).send();86 await collectionEvm.methods.addCollectionAdminCross(adminCrossEth).send();
83
84 const adminListRpc = await helper.collection.getAdmins(collectionId);87 const result = await collectionEvm.methods.mint(owner).call({from: adminEth});
85 let adminListEth = await collectionEvm.methods.collectionAdmins().call();88
86 adminListEth = adminListEth.map((element: IEthCrossAccountId) => {89 // TODO: Why fail
87 return helper.address.convertCrossAccountFromEthCrossAcoount(element);90 expect(await helper.collection.getLastTokenId(collectionId)).to.eq(1);
88 });
89 expect(adminListRpc).to.be.like(adminListEth);
90 });91 });
9192
92 // Soft-deprecated
93 itEth('Verify owner or admin', async ({helper}) => {93 itEth('cannot add invalid cross account admin', async ({helper}) => {
94 const owner = await helper.eth.createAccountWithBalance(donor);94 const owner = await helper.eth.createAccountWithBalance(donor);
95 const [admin] = await helper.arrange.createAccounts([100n, 100n], donor);
96
95 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');97 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');
96
97 const newAdmin = helper.eth.createAccount();
98 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);98 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
99 99
100 expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.false;100 const adminCross = {
101 await collectionEvm.methods.addCollectionAdmin(newAdmin).send();101 eth: helper.address.substrateToEth(admin.address),
102 sub: admin.addressRaw,
103 };
102 expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.true;104 await expect(collectionEvm.methods.addCollectionAdminCross(adminCross).send()).to.be.rejected;
103 });105 });
104106
105 itEth('Verify owner or admin cross', async ({helper, privateKey}) => {107 itEth('can verify owner with methods.isOwnerOrAdmin[Cross]', async ({helper, privateKey}) => {
106 const owner = await helper.eth.createAccountWithBalance(donor);108 const owner = await helper.eth.createAccountWithBalance(donor);
107 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');109 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');
108110
111 const adminDeprecated = helper.eth.createAccount();
109 const newAdmin = await privateKey('admin');112 const admin1Cross = helper.ethCrossAccount.fromKeyringPair(await privateKey('admin'));
110 const newAdminCross = helper.ethCrossAccount.fromKeyringPair(newAdmin);113 const admin2Cross = helper.ethCrossAccount.fromAddress(helper.address.substrateToEth((await privateKey('admin3')).address));
111 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);114 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);
112 115
116 // Soft-deprecated:
117 expect(await collectionEvm.methods.isOwnerOrAdmin(adminDeprecated).call()).to.be.false;
113 expect(await collectionEvm.methods.isOwnerOrAdminCross(newAdminCross).call()).to.be.false;118 expect(await collectionEvm.methods.isOwnerOrAdminCross(admin1Cross).call()).to.be.false;
119 expect(await collectionEvm.methods.isOwnerOrAdminCross(admin2Cross).call()).to.be.false;
120
121 await collectionEvm.methods.addCollectionAdmin(adminDeprecated).send();
114 await collectionEvm.methods.addCollectionAdminCross(newAdminCross).send();122 await collectionEvm.methods.addCollectionAdminCross(admin1Cross).send();
123 await collectionEvm.methods.addCollectionAdminCross(admin2Cross).send();
124
125 // Soft-deprecated: isOwnerOrAdmin returns true
126 expect(await collectionEvm.methods.isOwnerOrAdmin(adminDeprecated).call()).to.be.true;
127 // Expect isOwnerOrAdminCross return true
115 expect(await collectionEvm.methods.isOwnerOrAdminCross(newAdminCross).call()).to.be.true;128 expect(await collectionEvm.methods.isOwnerOrAdminCross(admin1Cross).call()).to.be.true;
129 expect(await collectionEvm.methods.isOwnerOrAdminCross(admin2Cross).call()).to.be.true;
116 });130 });
117131
118 // Soft-deprecated132 // Soft-deprecated
154 const owner = await helper.eth.createAccountWithBalance(donor);168 const owner = await helper.eth.createAccountWithBalance(donor);
155 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');169 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');
156170
157 const [admin] = await helper.arrange.createAccounts([10n], donor);171 const [admin, notAdmin] = await helper.arrange.createAccounts([10n, 10n], donor);
158 const adminCross = helper.ethCrossAccount.fromKeyringPair(admin);172 const adminCross = helper.ethCrossAccount.fromKeyringPair(admin);
159 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);173 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
160 await collectionEvm.methods.addCollectionAdminCross(adminCross).send();174 await collectionEvm.methods.addCollectionAdminCross(adminCross).send();
161175
162 const [notAdmin] = await helper.arrange.createAccounts([10n], donor);
163 const notAdminCross = helper.ethCrossAccount.fromKeyringPair(notAdmin);176 const notAdminCross = helper.ethCrossAccount.fromKeyringPair(notAdmin);
164 await expect(collectionEvm.methods.addCollectionAdminCross(notAdminCross).call({from: adminCross.eth}))177 await expect(collectionEvm.methods.addCollectionAdminCross(notAdminCross).call({from: adminCross.eth}))
165 .to.be.rejectedWith('NoPermission');178 .to.be.rejectedWith('NoPermission');
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
2429 * @param ethCrossAccount etherium cross account2429 * @param ethCrossAccount etherium cross account
2430 * @returns substrate cross account id2430 * @returns substrate cross account id
2431 */2431 */
2432 convertCrossAccountFromEthCrossAcoount(ethCrossAccount: IEthCrossAccountId): ICrossAccountId {2432 convertCrossAccountFromEthCrossAccount(ethCrossAccount: IEthCrossAccountId): ICrossAccountId {
2433 if (ethCrossAccount.sub === '0') {2433 if (ethCrossAccount.sub === '0') {
2434 return {Ethereum: ethCrossAccount.eth.toLocaleLowerCase()};2434 return {Ethereum: ethCrossAccount.eth.toLocaleLowerCase()};
2435 }2435 }