From c89a23384222f6787ebfe599958a5a52a091331c Mon Sep 17 00:00:00 2001 From: Trubnikov Sergey Date: Mon, 25 Apr 2022 13:19:14 +0000 Subject: [PATCH] CORE-302 Add negative tests --- --- a/tests/src/eth/createCollection.test.ts +++ b/tests/src/eth/createCollection.test.ts @@ -18,7 +18,7 @@ import {evmToAddress} from '@polkadot/util-crypto'; import {expect} from 'chai'; import {getCreatedCollectionCount, getDetailedCollectionInfo} from '../util/helpers'; -import {collectionHelper, collectionIdFromAddress, createEthAccountWithBalance, itWeb3, normalizeAddress} from './util/helpers'; +import {collectionHelper, collectionIdFromAddress, createEthAccount, createEthAccountWithBalance, itWeb3, normalizeAddress} from './util/helpers'; async function getCollectionAddressFromResult(api: ApiPromise, result: any) { const collectionIdAddress = normalizeAddress(result.events[0].raw.topics[2]); @@ -141,4 +141,186 @@ expect(collection.limits.ownerCanDestroy.toHuman()).to.be.eq(limits.ownerCanDestroy); expect(collection.limits.transfersEnabled.toHuman()).to.be.eq(limits.transfersEnabled); }); +}); + +describe('(!negative tests!) Create collection from EVM', () => { + itWeb3('(!negative test!) Create collection (bad lengths)', async ({api, web3}) => { + const owner = await createEthAccountWithBalance(api, web3); + const helper = collectionHelper(web3, owner); + { + const MAX_NAME_LENGHT = 64; + const collectionName = 'A'.repeat(MAX_NAME_LENGHT + 1); + const description = 'A'; + const tokenPrefix = 'A'; + + await expect(helper.methods + .create721Collection(collectionName, description, tokenPrefix) + .call()).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGHT); + + } + { + const MAX_DESCRIPTION_LENGHT = 256; + const collectionName = 'A'; + const description = 'A'.repeat(MAX_DESCRIPTION_LENGHT + 1); + const tokenPrefix = 'A'; + await expect(helper.methods + .create721Collection(collectionName, description, tokenPrefix) + .call()).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGHT); + } + { + const MAX_TOKEN_PREFIX_LENGHT = 16; + const collectionName = 'A'; + const description = 'A'; + const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGHT + 1); + await expect(helper.methods + .create721Collection(collectionName, description, tokenPrefix) + .call()).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGHT); + } + }); + + itWeb3('(!negative test!) Create collection (no funds)', async ({web3}) => { + const owner = await createEthAccount(web3); + const helper = collectionHelper(web3, owner); + const collectionName = 'A'; + const description = 'A'; + const tokenPrefix = 'A'; + + await expect(helper.methods + .create721Collection(collectionName, description, tokenPrefix) + .call()).to.be.rejectedWith('NotSufficientFounds'); + }); + + itWeb3('(!negative test!) Collection address (Bad ETH prefix)', async ({api, web3}) => { + const owner = await createEthAccountWithBalance(api, web3); + const helper = collectionHelper(web3, owner); + const collectionAddressWithBadPrefix = '0x00112233445566778899AABBCCDDEEFF00112233'; + const EXPECTED_ERROR = 'Bad ETH prefix'; + { + const sponsor = await createEthAccountWithBalance(api, web3); + await expect(helper.methods + .setSponsor(collectionAddressWithBadPrefix, sponsor) + .call()).to.be.rejectedWith(EXPECTED_ERROR); + + const sponsorHelper = collectionHelper(web3, sponsor); + await expect(sponsorHelper.methods + .confirmSponsorship(collectionAddressWithBadPrefix) + .call()).to.be.rejectedWith(EXPECTED_ERROR); + } + { + const shema = 'Some shema'; + await expect(helper.methods + .setOffchainShema(collectionAddressWithBadPrefix, shema) + .call()).to.be.rejectedWith(EXPECTED_ERROR); + } + { + const variable = 'Some variable'; + await expect(helper.methods + .setVariableOnChainSchema(collectionAddressWithBadPrefix, variable) + .call()).to.be.rejectedWith(EXPECTED_ERROR); + } + { + const constData = 'Some const'; + await expect(helper.methods + .setConstOnChainSchema(collectionAddressWithBadPrefix, constData) + .call()).to.be.rejectedWith(EXPECTED_ERROR); + } + { + const limits = '{"account_token_ownership_limit":1000}'; + await expect(helper.methods + .setLimits(collectionAddressWithBadPrefix, limits) + .call()).to.be.rejectedWith(EXPECTED_ERROR); + } + }); + + itWeb3('(!negative test!) Check owner', async ({api, web3}) => { + const owner = await createEthAccountWithBalance(api, web3); + const notOwner = await createEthAccount(web3); + const helperFromOwner = collectionHelper(web3, owner); + const helperFromNotOwner = collectionHelper(web3, notOwner); + const result = await helperFromOwner.methods.create721Collection('A', 'A', 'A').send(); + const {collectionIdAddress} = await getCollectionAddressFromResult(api, result); + const EXPECTED_ERROR = 'NoPermission'; + { + const sponsor = await createEthAccountWithBalance(api, web3); + await expect(helperFromNotOwner.methods + .setSponsor(collectionIdAddress, sponsor) + .call()).to.be.rejectedWith(EXPECTED_ERROR); + + const sponsorHelper = collectionHelper(web3, sponsor); + await expect(sponsorHelper.methods + .confirmSponsorship(collectionIdAddress) + .call()).to.be.rejectedWith('Caller is not set as sponsor'); + } + { + const shema = 'Some shema'; + await expect(helperFromNotOwner.methods + .setOffchainShema(collectionIdAddress, shema) + .call()).to.be.rejectedWith(EXPECTED_ERROR); + } + { + const variable = 'Some variable'; + await expect(helperFromNotOwner.methods + .setVariableOnChainSchema(collectionIdAddress, variable) + .call()).to.be.rejectedWith(EXPECTED_ERROR); + } + { + const constData = 'Some const'; + await expect(helperFromNotOwner.methods + .setConstOnChainSchema(collectionIdAddress, constData) + .call()).to.be.rejectedWith(EXPECTED_ERROR); + } + { + const limits = '{"account_token_ownership_limit":1000}'; + await expect(helperFromNotOwner.methods + .setLimits(collectionIdAddress, limits) + .call()).to.be.rejectedWith(EXPECTED_ERROR); + } + }); + + itWeb3('(!negative test!) Set offchain shema (length limit)', async ({api, web3}) => { + const owner = await createEthAccountWithBalance(api, web3); + const helper = collectionHelper(web3, owner); + const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send(); + const {collectionIdAddress} = await getCollectionAddressFromResult(api, result); + const OFFCHAIN_SCHEMA_LIMIT = 8192; + const shema = 'A'.repeat(OFFCHAIN_SCHEMA_LIMIT + 1); + await expect(helper.methods + .setOffchainShema(collectionIdAddress, shema) + .call()).to.be.rejectedWith('shema is too long. Max length is ' + OFFCHAIN_SCHEMA_LIMIT); + }); + + itWeb3('(!negative test!) Set variable on chain schema (length limit)', async ({api, web3}) => { + const owner = await createEthAccountWithBalance(api, web3); + const helper = collectionHelper(web3, owner); + const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send(); + const {collectionIdAddress} = await getCollectionAddressFromResult(api, result); + const VARIABLE_ON_CHAIN_SCHEMA_LIMIT = 8192; + const variable = 'A'.repeat(VARIABLE_ON_CHAIN_SCHEMA_LIMIT + 1); + await expect(helper.methods + .setVariableOnChainSchema(collectionIdAddress, variable) + .call()).to.be.rejectedWith('variable is too long. Max length is ' + VARIABLE_ON_CHAIN_SCHEMA_LIMIT); + }); + + itWeb3('(!negative test!) Set const on chain schema (length limit)', async ({api, web3}) => { + const owner = await createEthAccountWithBalance(api, web3); + const helper = collectionHelper(web3, owner); + const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send(); + const {collectionIdAddress} = await getCollectionAddressFromResult(api, result); + const CONST_ON_CHAIN_SCHEMA_LIMIT = 32768; + const constData = 'A'.repeat(CONST_ON_CHAIN_SCHEMA_LIMIT + 1); + await expect(helper.methods + .setConstOnChainSchema(collectionIdAddress, constData) + .call()).to.be.rejectedWith('const_on_chain is too long. Max length is ' + CONST_ON_CHAIN_SCHEMA_LIMIT); + }); + + itWeb3('(!negative test!) Set limits', async ({api, web3}) => { + const owner = await createEthAccountWithBalance(api, web3); + const helper = collectionHelper(web3, owner); + const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send(); + const {collectionIdAddress} = await getCollectionAddressFromResult(api, result); + const badJson = '{accountTokenOwnershipLimit: 1000}'; + await expect(helper.methods + .setLimits(collectionIdAddress, badJson) + .call()).to.be.rejectedWith('Parse JSON error:'); + }); }); \ No newline at end of file -- gitstuff