difftreelog
test multiple modifies of collection properties
in: master
1 file changed
tests/src/nesting/collectionProperties.test.tsdiffbeforeafterboth1// 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 {itSub, Pallets, usingPlaygrounds, expect} from '../util';19import {UniqueBaseCollection} from '../util/playgrounds/unique';2021describe('Integration Test: Collection Properties', () => {22 let alice: IKeyringPair;23 let bob: IKeyringPair;24 25 before(async () => {26 await usingPlaygrounds(async (helper, privateKey) => {27 const donor = await privateKey({filename: __filename});28 [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);29 });30 });31 32 itSub('Properties are initially empty', async ({helper}) => {33 const collection = await helper.nft.mintCollection(alice);34 expect(await collection.getProperties()).to.be.empty;35 });36 37 async function testSetsPropertiesForCollection(collection: UniqueBaseCollection) {38 // As owner39 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}])).to.be.fulfilled;40 41 await collection.addAdmin(alice, {Substrate: bob.address});42 43 // As administrator44 await expect(collection.setProperties(bob, [{key: 'black_hole'}])).to.be.fulfilled;45 46 const properties = await collection.getProperties();47 expect(properties).to.include.deep.members([48 {key: 'electron', value: 'come bond'},49 {key: 'black_hole', value: ''},50 ]);51 }52 53 itSub('Sets properties for a NFT collection', async ({helper}) => {54 await testSetsPropertiesForCollection(await helper.nft.mintCollection(alice));55 });56 57 itSub.ifWithPallets('Sets properties for a ReFungible collection', [Pallets.ReFungible], async ({helper}) => {58 await testSetsPropertiesForCollection(await helper.rft.mintCollection(alice));59 });60 61 async function testCheckValidNames(collection: UniqueBaseCollection) {62 // alpha symbols63 await expect(collection.setProperties(alice, [{key: 'answer'}])).to.be.fulfilled;64 65 // numeric symbols66 await expect(collection.setProperties(alice, [{key: '451'}])).to.be.fulfilled;67 68 // underscore symbol69 await expect(collection.setProperties(alice, [{key: 'black_hole'}])).to.be.fulfilled;70 71 // dash symbol72 await expect(collection.setProperties(alice, [{key: '-'}])).to.be.fulfilled;73 74 // dot symbol75 await expect(collection.setProperties(alice, [{key: 'once.in.a.long.long.while...', value: 'you get a little lost'}])).to.be.fulfilled;76 77 const properties = await collection.getProperties();78 expect(properties).to.include.deep.members([79 {key: 'answer', value: ''},80 {key: '451', value: ''},81 {key: 'black_hole', value: ''},82 {key: '-', value: ''},83 {key: 'once.in.a.long.long.while...', value: 'you get a little lost'},84 ]);85 }86 87 itSub('Check valid names for NFT collection properties keys', async ({helper}) => {88 await testCheckValidNames(await helper.nft.mintCollection(alice));89 });90 91 itSub.ifWithPallets('Check valid names for ReFungible collection properties keys', [Pallets.ReFungible], async ({helper}) => {92 await testCheckValidNames(await helper.rft.mintCollection(alice));93 });94 95 async function testChangesProperties(collection: UniqueBaseCollection) {96 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: ''}])).to.be.fulfilled;97 98 // Mutate the properties99 await expect(collection.setProperties(alice, [{key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;100 101 const properties = await collection.getProperties();102 expect(properties).to.include.deep.members([103 {key: 'electron', value: 'come bond'},104 {key: 'black_hole', value: 'LIGO'},105 ]);106 }107 108 itSub('Changes properties of a NFT collection', async ({helper}) => {109 await testChangesProperties(await helper.nft.mintCollection(alice));110 });111 112 itSub.ifWithPallets('Changes properties of a ReFungible collection', [Pallets.ReFungible], async ({helper}) => {113 await testChangesProperties(await helper.rft.mintCollection(alice));114 });115 116 async function testDeleteProperties(collection: UniqueBaseCollection) {117 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;118 119 await expect(collection.deleteProperties(alice, ['electron'])).to.be.fulfilled;120 121 const properties = await collection.getProperties(['black_hole', 'electron']);122 expect(properties).to.be.deep.equal([123 {key: 'black_hole', value: 'LIGO'},124 ]);125 }126 127 itSub('Deletes properties of a NFT collection', async ({helper}) => {128 await testDeleteProperties(await helper.nft.mintCollection(alice));129 });130 131 itSub.ifWithPallets('Deletes properties of a ReFungible collection', [Pallets.ReFungible], async ({helper}) => {132 await testDeleteProperties(await helper.rft.mintCollection(alice));133 });134});135 136describe('Negative Integration Test: Collection Properties', () => {137 let alice: IKeyringPair;138 let bob: IKeyringPair;139 140 before(async () => {141 await usingPlaygrounds(async (helper, privateKey) => {142 const donor = await privateKey({filename: __filename});143 [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor);144 });145 });146 147 async function testFailsSetPropertiesIfNotOwnerOrAdmin(collection: UniqueBaseCollection) { 148 await expect(collection.setProperties(bob, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}]))149 .to.be.rejectedWith(/common\.NoPermission/);150 151 expect(await collection.getProperties()).to.be.empty;152 }153 154 itSub('Fails to set properties in a NFT collection if not its onwer/administrator', async ({helper}) => {155 await testFailsSetPropertiesIfNotOwnerOrAdmin(await helper.nft.mintCollection(alice));156 });157 158 itSub.ifWithPallets('Fails to set properties in a ReFungible collection if not its onwer/administrator', [Pallets.ReFungible], async ({helper}) => {159 await testFailsSetPropertiesIfNotOwnerOrAdmin(await helper.rft.mintCollection(alice));160 });161 162 async function testFailsSetPropertiesThatExeedLimits(collection: UniqueBaseCollection) {163 const spaceLimit = (await (collection.helper!.api! as any).query.common.collectionProperties(collection.collectionId)).spaceLimit.toNumber();164 165 // Mute the general tx parsing error, too many bytes to process166 {167 console.error = () => {};168 await expect(collection.setProperties(alice, [169 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 9))},170 ])).to.be.rejected;171 }172 173 expect(await collection.getProperties(['electron'])).to.be.empty;174 175 await expect(collection.setProperties(alice, [176 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 18))}, 177 {key: 'black_hole', value: '0'.repeat(Math.ceil(spaceLimit! / 2))}, 178 ])).to.be.rejectedWith(/common\.NoSpaceForProperty/);179 180 expect(await collection.getProperties(['electron', 'black_hole'])).to.be.empty;181 }182 183 itSub('Fails to set properties that exceed the limits (NFT)', async ({helper}) => {184 await testFailsSetPropertiesThatExeedLimits(await helper.nft.mintCollection(alice));185 });186 187 itSub.ifWithPallets('Fails to set properties that exceed the limits (ReFungible)', [Pallets.ReFungible], async ({helper}) => {188 await testFailsSetPropertiesThatExeedLimits(await helper.rft.mintCollection(alice));189 });190 191 async function testFailsSetMorePropertiesThanAllowed(collection: UniqueBaseCollection) {192 const propertiesToBeSet = [];193 for (let i = 0; i < 65; i++) {194 propertiesToBeSet.push({195 key: 'electron_' + i,196 value: Math.random() > 0.5 ? 'high' : 'low',197 });198 }199 200 await expect(collection.setProperties(alice, propertiesToBeSet)).201 to.be.rejectedWith(/common\.PropertyLimitReached/);202 203 expect(await collection.getProperties()).to.be.empty;204 }205 206 itSub('Fails to set more properties than it is allowed (NFT)', async ({helper}) => {207 await testFailsSetMorePropertiesThanAllowed(await helper.nft.mintCollection(alice));208 });209 210 itSub.ifWithPallets('Fails to set more properties than it is allowed (ReFungible)', [Pallets.ReFungible], async ({helper}) => {211 await testFailsSetMorePropertiesThanAllowed(await helper.rft.mintCollection(alice));212 });213 214 async function testFailsSetPropertiesWithInvalidNames(collection: UniqueBaseCollection) {215 const invalidProperties = [216 [{key: 'electron', value: 'negative'}, {key: 'string theory', value: 'understandable'}],217 [{key: 'Mr/Sandman', value: 'Bring me a gene'}],218 [{key: 'déjà vu', value: 'hmm...'}],219 ];220 221 for (let i = 0; i < invalidProperties.length; i++) {222 await expect(223 collection.setProperties(alice, invalidProperties[i]), 224 `on rejecting the new badly-named property #${i}`,225 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);226 }227 228 await expect(229 collection.setProperties(alice, [{key: '', value: 'nothing must not exist'}]), 230 'on rejecting an unnamed property',231 ).to.be.rejectedWith(/common\.EmptyPropertyKey/);232 233 await expect(234 collection.setProperties(alice, [{key: 'CRISPR-Cas9', value: 'rewriting nature!'}]), 235 'on setting the correctly-but-still-badly-named property',236 ).to.be.fulfilled;237 238 const keys = invalidProperties.flatMap(propertySet => propertySet.map(property => property.key)).concat('CRISPR-Cas9').concat('');239 240 const properties = await collection.getProperties(keys);241 expect(properties).to.be.deep.equal([242 {key: 'CRISPR-Cas9', value: 'rewriting nature!'},243 ]);244 245 for (let i = 0; i < invalidProperties.length; i++) {246 await expect(247 collection.deleteProperties(alice, invalidProperties[i].map(propertySet => propertySet.key)), 248 `on trying to delete the non-existent badly-named property #${i}`,249 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);250 }251 }252 253 itSub('Fails to set properties with invalid names (NFT)', async ({helper}) => {254 await testFailsSetPropertiesWithInvalidNames(await helper.nft.mintCollection(alice));255 });256 257 itSub.ifWithPallets('Fails to set properties with invalid names (ReFungible)', [Pallets.ReFungible], async ({helper}) => {258 await testFailsSetPropertiesWithInvalidNames(await helper.rft.mintCollection(alice));259 });260});261 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 {itSub, Pallets, usingPlaygrounds, expect} from '../util';19import {UniqueBaseCollection} from '../util/playgrounds/unique';2021describe('Integration Test: Collection Properties', () => {22 let alice: IKeyringPair;23 let bob: IKeyringPair;24 25 before(async () => {26 await usingPlaygrounds(async (helper, privateKey) => {27 const donor = await privateKey({filename: __filename});28 [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);29 });30 });31 32 itSub('Properties are initially empty', async ({helper}) => {33 const collection = await helper.nft.mintCollection(alice);34 expect(await collection.getProperties()).to.be.empty;35 });36 37 async function testSetsPropertiesForCollection(collection: UniqueBaseCollection) {38 // As owner39 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}])).to.be.fulfilled;40 41 await collection.addAdmin(alice, {Substrate: bob.address});42 43 // As administrator44 await expect(collection.setProperties(bob, [{key: 'black_hole'}])).to.be.fulfilled;45 46 const properties = await collection.getProperties();47 expect(properties).to.include.deep.members([48 {key: 'electron', value: 'come bond'},49 {key: 'black_hole', value: ''},50 ]);51 }52 53 itSub('Sets properties for a NFT collection', async ({helper}) => {54 await testSetsPropertiesForCollection(await helper.nft.mintCollection(alice));55 });56 57 itSub.ifWithPallets('Sets properties for a ReFungible collection', [Pallets.ReFungible], async ({helper}) => {58 await testSetsPropertiesForCollection(await helper.rft.mintCollection(alice));59 });60 61 async function testCheckValidNames(collection: UniqueBaseCollection) {62 // alpha symbols63 await expect(collection.setProperties(alice, [{key: 'answer'}])).to.be.fulfilled;64 65 // numeric symbols66 await expect(collection.setProperties(alice, [{key: '451'}])).to.be.fulfilled;67 68 // underscore symbol69 await expect(collection.setProperties(alice, [{key: 'black_hole'}])).to.be.fulfilled;70 71 // dash symbol72 await expect(collection.setProperties(alice, [{key: '-'}])).to.be.fulfilled;73 74 // dot symbol75 await expect(collection.setProperties(alice, [{key: 'once.in.a.long.long.while...', value: 'you get a little lost'}])).to.be.fulfilled;76 77 const properties = await collection.getProperties();78 expect(properties).to.include.deep.members([79 {key: 'answer', value: ''},80 {key: '451', value: ''},81 {key: 'black_hole', value: ''},82 {key: '-', value: ''},83 {key: 'once.in.a.long.long.while...', value: 'you get a little lost'},84 ]);85 }86 87 itSub('Check valid names for NFT collection properties keys', async ({helper}) => {88 await testCheckValidNames(await helper.nft.mintCollection(alice));89 });90 91 itSub.ifWithPallets('Check valid names for ReFungible collection properties keys', [Pallets.ReFungible], async ({helper}) => {92 await testCheckValidNames(await helper.rft.mintCollection(alice));93 });94 95 async function testChangesProperties(collection: UniqueBaseCollection) {96 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: ''}])).to.be.fulfilled;97 98 // Mutate the properties99 await expect(collection.setProperties(alice, [{key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;100 101 const properties = await collection.getProperties();102 expect(properties).to.include.deep.members([103 {key: 'electron', value: 'come bond'},104 {key: 'black_hole', value: 'LIGO'},105 ]);106 }107 108 itSub('Changes properties of a NFT collection', async ({helper}) => {109 await testChangesProperties(await helper.nft.mintCollection(alice));110 });111 112 itSub.ifWithPallets('Changes properties of a ReFungible collection', [Pallets.ReFungible], async ({helper}) => {113 await testChangesProperties(await helper.rft.mintCollection(alice));114 });115 116 async function testDeleteProperties(collection: UniqueBaseCollection) {117 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;118 119 await expect(collection.deleteProperties(alice, ['electron'])).to.be.fulfilled;120 121 const properties = await collection.getProperties(['black_hole', 'electron']);122 expect(properties).to.be.deep.equal([123 {key: 'black_hole', value: 'LIGO'},124 ]);125 }126 127 itSub('Deletes properties of a NFT collection', async ({helper}) => {128 await testDeleteProperties(await helper.nft.mintCollection(alice));129 });130 131 itSub.ifWithPallets('Deletes properties of a ReFungible collection', [Pallets.ReFungible], async ({helper}) => {132 await testDeleteProperties(await helper.rft.mintCollection(alice));133 });134135 [136 {mode: 'nft' as const, requiredPallets: []},137 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, 138 ].map(testCase =>139 itSub.ifWithPallets(`Allows modifying a collection property multiple times (${testCase.mode})`, testCase.requiredPallets, async({helper}) => {140 const propKey = 'tok-prop';141142 const collection = await helper[testCase.mode].mintCollection(alice);143144 const maxCollectionPropertiesSize = 40960;145146 const propDataSize = 4096;147148 let propDataChar = 'a';149 const makeNewPropData = () => {150 propDataChar = String.fromCharCode(propDataChar.charCodeAt(0) + 1);151 return `${propDataChar}`.repeat(propDataSize);152 };153154 const getConsumedSpace = async () => {155 const props = (await helper.getApi().query.common.collectionProperties(collection.collectionId)).toJSON();156 157 return (props! as any).consumedSpace;158 };159160 await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);161 const originalSpace = await getConsumedSpace();162 expect(originalSpace).to.be.equal(propDataSize);163164 const sameSizePropertiesPossibleNum = maxCollectionPropertiesSize / propDataSize;165166 // It is possible to modify a property as many times as needed.167 // It will not consume any additional space.168 for (let i = 0; i < sameSizePropertiesPossibleNum + 1; i++) {169 await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);170 const consumedSpace = await getConsumedSpace();171 expect(consumedSpace).to.be.equal(originalSpace);172 }173 }));174});175 176describe('Negative Integration Test: Collection Properties', () => {177 let alice: IKeyringPair;178 let bob: IKeyringPair;179 180 before(async () => {181 await usingPlaygrounds(async (helper, privateKey) => {182 const donor = await privateKey({filename: __filename});183 [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor);184 });185 });186 187 async function testFailsSetPropertiesIfNotOwnerOrAdmin(collection: UniqueBaseCollection) { 188 await expect(collection.setProperties(bob, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}]))189 .to.be.rejectedWith(/common\.NoPermission/);190 191 expect(await collection.getProperties()).to.be.empty;192 }193 194 itSub('Fails to set properties in a NFT collection if not its onwer/administrator', async ({helper}) => {195 await testFailsSetPropertiesIfNotOwnerOrAdmin(await helper.nft.mintCollection(alice));196 });197 198 itSub.ifWithPallets('Fails to set properties in a ReFungible collection if not its onwer/administrator', [Pallets.ReFungible], async ({helper}) => {199 await testFailsSetPropertiesIfNotOwnerOrAdmin(await helper.rft.mintCollection(alice));200 });201 202 async function testFailsSetPropertiesThatExeedLimits(collection: UniqueBaseCollection) {203 const spaceLimit = (await (collection.helper!.api! as any).query.common.collectionProperties(collection.collectionId)).spaceLimit.toNumber();204 205 // Mute the general tx parsing error, too many bytes to process206 {207 console.error = () => {};208 await expect(collection.setProperties(alice, [209 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 9))},210 ])).to.be.rejected;211 }212 213 expect(await collection.getProperties(['electron'])).to.be.empty;214 215 await expect(collection.setProperties(alice, [216 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 18))}, 217 {key: 'black_hole', value: '0'.repeat(Math.ceil(spaceLimit! / 2))}, 218 ])).to.be.rejectedWith(/common\.NoSpaceForProperty/);219 220 expect(await collection.getProperties(['electron', 'black_hole'])).to.be.empty;221 }222 223 itSub('Fails to set properties that exceed the limits (NFT)', async ({helper}) => {224 await testFailsSetPropertiesThatExeedLimits(await helper.nft.mintCollection(alice));225 });226 227 itSub.ifWithPallets('Fails to set properties that exceed the limits (ReFungible)', [Pallets.ReFungible], async ({helper}) => {228 await testFailsSetPropertiesThatExeedLimits(await helper.rft.mintCollection(alice));229 });230 231 async function testFailsSetMorePropertiesThanAllowed(collection: UniqueBaseCollection) {232 const propertiesToBeSet = [];233 for (let i = 0; i < 65; i++) {234 propertiesToBeSet.push({235 key: 'electron_' + i,236 value: Math.random() > 0.5 ? 'high' : 'low',237 });238 }239 240 await expect(collection.setProperties(alice, propertiesToBeSet)).241 to.be.rejectedWith(/common\.PropertyLimitReached/);242 243 expect(await collection.getProperties()).to.be.empty;244 }245 246 itSub('Fails to set more properties than it is allowed (NFT)', async ({helper}) => {247 await testFailsSetMorePropertiesThanAllowed(await helper.nft.mintCollection(alice));248 });249 250 itSub.ifWithPallets('Fails to set more properties than it is allowed (ReFungible)', [Pallets.ReFungible], async ({helper}) => {251 await testFailsSetMorePropertiesThanAllowed(await helper.rft.mintCollection(alice));252 });253 254 async function testFailsSetPropertiesWithInvalidNames(collection: UniqueBaseCollection) {255 const invalidProperties = [256 [{key: 'electron', value: 'negative'}, {key: 'string theory', value: 'understandable'}],257 [{key: 'Mr/Sandman', value: 'Bring me a gene'}],258 [{key: 'déjà vu', value: 'hmm...'}],259 ];260 261 for (let i = 0; i < invalidProperties.length; i++) {262 await expect(263 collection.setProperties(alice, invalidProperties[i]), 264 `on rejecting the new badly-named property #${i}`,265 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);266 }267 268 await expect(269 collection.setProperties(alice, [{key: '', value: 'nothing must not exist'}]), 270 'on rejecting an unnamed property',271 ).to.be.rejectedWith(/common\.EmptyPropertyKey/);272 273 await expect(274 collection.setProperties(alice, [{key: 'CRISPR-Cas9', value: 'rewriting nature!'}]), 275 'on setting the correctly-but-still-badly-named property',276 ).to.be.fulfilled;277 278 const keys = invalidProperties.flatMap(propertySet => propertySet.map(property => property.key)).concat('CRISPR-Cas9').concat('');279 280 const properties = await collection.getProperties(keys);281 expect(properties).to.be.deep.equal([282 {key: 'CRISPR-Cas9', value: 'rewriting nature!'},283 ]);284 285 for (let i = 0; i < invalidProperties.length; i++) {286 await expect(287 collection.deleteProperties(alice, invalidProperties[i].map(propertySet => propertySet.key)), 288 `on trying to delete the non-existent badly-named property #${i}`,289 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);290 }291 }292 293 itSub('Fails to set properties with invalid names (NFT)', async ({helper}) => {294 await testFailsSetPropertiesWithInvalidNames(await helper.nft.mintCollection(alice));295 });296 297 itSub.ifWithPallets('Fails to set properties with invalid names (ReFungible)', [Pallets.ReFungible], async ({helper}) => {298 await testFailsSetPropertiesWithInvalidNames(await helper.rft.mintCollection(alice));299 });300});301