git.delta.rocks / unique-network / refs/commits / c89a23384222

difftreelog

CORE-302 Add negative tests

Trubnikov Sergey2022-04-25parent: #14581b7.patch.diff
in: master

1 file changed

modifiedtests/src/eth/createCollection.test.tsdiffbeforeafterboth
18import {evmToAddress} from '@polkadot/util-crypto';18import {evmToAddress} from '@polkadot/util-crypto';
19import {expect} from 'chai';19import {expect} from 'chai';
20import {getCreatedCollectionCount, getDetailedCollectionInfo} from '../util/helpers';20import {getCreatedCollectionCount, getDetailedCollectionInfo} from '../util/helpers';
21import {collectionHelper, collectionIdFromAddress, createEthAccountWithBalance, itWeb3, normalizeAddress} from './util/helpers';21import {collectionHelper, collectionIdFromAddress, createEthAccount, createEthAccountWithBalance, itWeb3, normalizeAddress} from './util/helpers';
2222
23async function getCollectionAddressFromResult(api: ApiPromise, result: any) {23async function getCollectionAddressFromResult(api: ApiPromise, result: any) {
24 const collectionIdAddress = normalizeAddress(result.events[0].raw.topics[2]);24 const collectionIdAddress = normalizeAddress(result.events[0].raw.topics[2]);
143 });143 });
144});144});
145
146describe('(!negative tests!) Create collection from EVM', () => {
147 itWeb3('(!negative test!) Create collection (bad lengths)', async ({api, web3}) => {
148 const owner = await createEthAccountWithBalance(api, web3);
149 const helper = collectionHelper(web3, owner);
150 {
151 const MAX_NAME_LENGHT = 64;
152 const collectionName = 'A'.repeat(MAX_NAME_LENGHT + 1);
153 const description = 'A';
154 const tokenPrefix = 'A';
155
156 await expect(helper.methods
157 .create721Collection(collectionName, description, tokenPrefix)
158 .call()).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGHT);
159
160 }
161 {
162 const MAX_DESCRIPTION_LENGHT = 256;
163 const collectionName = 'A';
164 const description = 'A'.repeat(MAX_DESCRIPTION_LENGHT + 1);
165 const tokenPrefix = 'A';
166 await expect(helper.methods
167 .create721Collection(collectionName, description, tokenPrefix)
168 .call()).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGHT);
169 }
170 {
171 const MAX_TOKEN_PREFIX_LENGHT = 16;
172 const collectionName = 'A';
173 const description = 'A';
174 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGHT + 1);
175 await expect(helper.methods
176 .create721Collection(collectionName, description, tokenPrefix)
177 .call()).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGHT);
178 }
179 });
180
181 itWeb3('(!negative test!) Create collection (no funds)', async ({web3}) => {
182 const owner = await createEthAccount(web3);
183 const helper = collectionHelper(web3, owner);
184 const collectionName = 'A';
185 const description = 'A';
186 const tokenPrefix = 'A';
187
188 await expect(helper.methods
189 .create721Collection(collectionName, description, tokenPrefix)
190 .call()).to.be.rejectedWith('NotSufficientFounds');
191 });
192
193 itWeb3('(!negative test!) Collection address (Bad ETH prefix)', async ({api, web3}) => {
194 const owner = await createEthAccountWithBalance(api, web3);
195 const helper = collectionHelper(web3, owner);
196 const collectionAddressWithBadPrefix = '0x00112233445566778899AABBCCDDEEFF00112233';
197 const EXPECTED_ERROR = 'Bad ETH prefix';
198 {
199 const sponsor = await createEthAccountWithBalance(api, web3);
200 await expect(helper.methods
201 .setSponsor(collectionAddressWithBadPrefix, sponsor)
202 .call()).to.be.rejectedWith(EXPECTED_ERROR);
203
204 const sponsorHelper = collectionHelper(web3, sponsor);
205 await expect(sponsorHelper.methods
206 .confirmSponsorship(collectionAddressWithBadPrefix)
207 .call()).to.be.rejectedWith(EXPECTED_ERROR);
208 }
209 {
210 const shema = 'Some shema';
211 await expect(helper.methods
212 .setOffchainShema(collectionAddressWithBadPrefix, shema)
213 .call()).to.be.rejectedWith(EXPECTED_ERROR);
214 }
215 {
216 const variable = 'Some variable';
217 await expect(helper.methods
218 .setVariableOnChainSchema(collectionAddressWithBadPrefix, variable)
219 .call()).to.be.rejectedWith(EXPECTED_ERROR);
220 }
221 {
222 const constData = 'Some const';
223 await expect(helper.methods
224 .setConstOnChainSchema(collectionAddressWithBadPrefix, constData)
225 .call()).to.be.rejectedWith(EXPECTED_ERROR);
226 }
227 {
228 const limits = '{"account_token_ownership_limit":1000}';
229 await expect(helper.methods
230 .setLimits(collectionAddressWithBadPrefix, limits)
231 .call()).to.be.rejectedWith(EXPECTED_ERROR);
232 }
233 });
234
235 itWeb3('(!negative test!) Check owner', async ({api, web3}) => {
236 const owner = await createEthAccountWithBalance(api, web3);
237 const notOwner = await createEthAccount(web3);
238 const helperFromOwner = collectionHelper(web3, owner);
239 const helperFromNotOwner = collectionHelper(web3, notOwner);
240 const result = await helperFromOwner.methods.create721Collection('A', 'A', 'A').send();
241 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);
242 const EXPECTED_ERROR = 'NoPermission';
243 {
244 const sponsor = await createEthAccountWithBalance(api, web3);
245 await expect(helperFromNotOwner.methods
246 .setSponsor(collectionIdAddress, sponsor)
247 .call()).to.be.rejectedWith(EXPECTED_ERROR);
248
249 const sponsorHelper = collectionHelper(web3, sponsor);
250 await expect(sponsorHelper.methods
251 .confirmSponsorship(collectionIdAddress)
252 .call()).to.be.rejectedWith('Caller is not set as sponsor');
253 }
254 {
255 const shema = 'Some shema';
256 await expect(helperFromNotOwner.methods
257 .setOffchainShema(collectionIdAddress, shema)
258 .call()).to.be.rejectedWith(EXPECTED_ERROR);
259 }
260 {
261 const variable = 'Some variable';
262 await expect(helperFromNotOwner.methods
263 .setVariableOnChainSchema(collectionIdAddress, variable)
264 .call()).to.be.rejectedWith(EXPECTED_ERROR);
265 }
266 {
267 const constData = 'Some const';
268 await expect(helperFromNotOwner.methods
269 .setConstOnChainSchema(collectionIdAddress, constData)
270 .call()).to.be.rejectedWith(EXPECTED_ERROR);
271 }
272 {
273 const limits = '{"account_token_ownership_limit":1000}';
274 await expect(helperFromNotOwner.methods
275 .setLimits(collectionIdAddress, limits)
276 .call()).to.be.rejectedWith(EXPECTED_ERROR);
277 }
278 });
279
280 itWeb3('(!negative test!) Set offchain shema (length limit)', async ({api, web3}) => {
281 const owner = await createEthAccountWithBalance(api, web3);
282 const helper = collectionHelper(web3, owner);
283 const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();
284 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);
285 const OFFCHAIN_SCHEMA_LIMIT = 8192;
286 const shema = 'A'.repeat(OFFCHAIN_SCHEMA_LIMIT + 1);
287 await expect(helper.methods
288 .setOffchainShema(collectionIdAddress, shema)
289 .call()).to.be.rejectedWith('shema is too long. Max length is ' + OFFCHAIN_SCHEMA_LIMIT);
290 });
291
292 itWeb3('(!negative test!) Set variable on chain schema (length limit)', async ({api, web3}) => {
293 const owner = await createEthAccountWithBalance(api, web3);
294 const helper = collectionHelper(web3, owner);
295 const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();
296 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);
297 const VARIABLE_ON_CHAIN_SCHEMA_LIMIT = 8192;
298 const variable = 'A'.repeat(VARIABLE_ON_CHAIN_SCHEMA_LIMIT + 1);
299 await expect(helper.methods
300 .setVariableOnChainSchema(collectionIdAddress, variable)
301 .call()).to.be.rejectedWith('variable is too long. Max length is ' + VARIABLE_ON_CHAIN_SCHEMA_LIMIT);
302 });
303
304 itWeb3('(!negative test!) Set const on chain schema (length limit)', async ({api, web3}) => {
305 const owner = await createEthAccountWithBalance(api, web3);
306 const helper = collectionHelper(web3, owner);
307 const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();
308 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);
309 const CONST_ON_CHAIN_SCHEMA_LIMIT = 32768;
310 const constData = 'A'.repeat(CONST_ON_CHAIN_SCHEMA_LIMIT + 1);
311 await expect(helper.methods
312 .setConstOnChainSchema(collectionIdAddress, constData)
313 .call()).to.be.rejectedWith('const_on_chain is too long. Max length is ' + CONST_ON_CHAIN_SCHEMA_LIMIT);
314 });
315
316 itWeb3('(!negative test!) Set limits', async ({api, web3}) => {
317 const owner = await createEthAccountWithBalance(api, web3);
318 const helper = collectionHelper(web3, owner);
319 const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();
320 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);
321 const badJson = '{accountTokenOwnershipLimit: 1000}';
322 await expect(helper.methods
323 .setLimits(collectionIdAddress, badJson)
324 .call()).to.be.rejectedWith('Parse JSON error:');
325 });
326});