difftreelog
Merge pull request #150 from usetech-llc/feature/NFTPAR-118_Collection_Limits
in: master
Add tests for setCollectionLimits
1 file changed
tests/src/setCollectionLimits.test.tsdiffbeforeafterboth1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56// https://unique-network.readthedocs.io/en/latest/jsapi.html#setchainlimits7import { ApiPromise, Keyring } from '@polkadot/api';8import { IKeyringPair } from '@polkadot/types/types';9import chai from 'chai';10import chaiAsPromised from 'chai-as-promised';11import usingApi, {submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';12import { ICollectionInterface } from './types';13import {14 createCollectionExpectSuccess, getCreatedCollectionCount,15 getCreateItemResult,16 getDetailedCollectionInfo,17 setCollectionLimitsExpectFailure,18 setCollectionLimitsExpectSuccess,19} from './util/helpers';2021chai.use(chaiAsPromised);22const expect = chai.expect;2324let alice: IKeyringPair;25let bob: IKeyringPair;26let collectionIdForTesting: number;2728const accountTokenOwnershipLimit = 0;29const sponsoredDataSize = 0;30const sponsoredMintSize = 0;31const sponsorTimeout = 1;32const tokenLimit = 1;3334describe('setCollectionLimits positive', () => {35 let tx;36 before(async () => {37 await usingApi(async () => {38 const keyring = new Keyring({ type: 'sr25519' });39 alice = keyring.addFromUri('//Alice');40 collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});41 });42 });43 it('execute setCollectionLimits with predefined params ', async () => {44 await usingApi(async (api: ApiPromise) => {45 tx = api.tx.nft.setCollectionLimits(46 collectionIdForTesting,47 {48 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,49 SponsoredMintSize: sponsoredDataSize,50 TokenLimit: tokenLimit,51 SponsorTimeout: sponsorTimeout,52 OwnerCanTransfer: true,53 OwnerCanDestroy: true54 },55 );56 const events = await submitTransactionAsync(alice, tx);57 const result = getCreateItemResult(events);5859 // get collection limits defined previously60 const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;6162 // tslint:disable-next-line:no-unused-expression63 expect(result.success).to.be.true;64 expect(collectionInfo.Limits.AccountTokenOwnershipLimit).to.be.equal(accountTokenOwnershipLimit);65 expect(collectionInfo.Limits.SponsoredDataSize).to.be.equal(sponsoredDataSize);66 expect(collectionInfo.Limits.TokenLimit).to.be.equal(tokenLimit);67 expect(collectionInfo.Limits.SponsorTimeout).to.be.equal(sponsorTimeout);68 expect(collectionInfo.Limits.OwnerCanTransfer).to.be.true;69 expect(collectionInfo.Limits.OwnerCanDestroy).to.be.true;70 });71 });72});7374describe('setCollectionLimits negative', () => {75 let tx;76 before(async () => {77 await usingApi(async () => {78 const keyring = new Keyring({ type: 'sr25519' });79 alice = keyring.addFromUri('//Alice');80 bob = keyring.addFromUri('//Bob');81 collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});82 });83 });84 it('execute setCollectionLimits for not exists collection', async () => {85 await usingApi(async (api: ApiPromise) => {86 const collectionCount = await getCreatedCollectionCount(api);87 const nonExistedCollectionId = collectionCount + 1;88 tx = api.tx.nft.setCollectionLimits(89 nonExistedCollectionId,90 {91 accountTokenOwnershipLimit,92 sponsoredDataSize,93 sponsoredMintSize,94 tokenLimit,95 },96 );97 await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;98 });99 });100 it('execute setCollectionLimits from user who is not owner of this collection', async () => {101 await usingApi(async (api: ApiPromise) => {102 tx = api.tx.nft.setCollectionLimits(103 collectionIdForTesting,104 {105 accountTokenOwnershipLimit,106 sponsoredDataSize,107 sponsoredMintSize,108 tokenLimit,109 },110 );111 await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;112 });113 });114 it('execute setCollectionLimits with incorrect limits', async () => {115 await usingApi(async (api: ApiPromise) => {116 tx = api.tx.nft.setCollectionLimits(117 collectionIdForTesting,118 {119 accountTokenOwnershipLimit: 'awdawd',120 sponsorTransferTimeout: 'awd',121 sponsoredDataSize: '12312312312312312',122 tokenLimit: '-100',123 },124 );125 await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;126 });127 });128129 it('fails when trying to enable OwnerCanTransfer after it was disabled', async () => {130 const collectionId = await createCollectionExpectSuccess();131 await setCollectionLimitsExpectSuccess(alice, collectionId, { 132 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,133 SponsoredMintSize: sponsoredDataSize,134 TokenLimit: tokenLimit,135 SponsorTimeout: sponsorTimeout,136 OwnerCanTransfer: false,137 OwnerCanDestroy: true138 });139 await setCollectionLimitsExpectFailure(alice, collectionId, { 140 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,141 SponsoredMintSize: sponsoredDataSize,142 TokenLimit: tokenLimit,143 SponsorTimeout: sponsorTimeout,144 OwnerCanTransfer: true,145 OwnerCanDestroy: true146 });147 });148149 it('fails when trying to enable OwnerCanDestroy after it was disabled', async () => {150 const collectionId = await createCollectionExpectSuccess();151 await setCollectionLimitsExpectSuccess(alice, collectionId, {152 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,153 SponsoredMintSize: sponsoredDataSize,154 TokenLimit: tokenLimit,155 SponsorTimeout: sponsorTimeout,156 OwnerCanTransfer: true,157 OwnerCanDestroy: false158 });159 await setCollectionLimitsExpectFailure(alice, collectionId, { 160 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,161 SponsoredMintSize: sponsoredDataSize,162 TokenLimit: tokenLimit,163 SponsorTimeout: sponsorTimeout,164 OwnerCanTransfer: true,165 OwnerCanDestroy: true166 });167 });168});1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56// https://unique-network.readthedocs.io/en/latest/jsapi.html#setchainlimits7import { ApiPromise, Keyring } from '@polkadot/api';8import { IKeyringPair } from '@polkadot/types/types';9import chai from 'chai';10import chaiAsPromised from 'chai-as-promised';11import usingApi, {submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';12import { ICollectionInterface } from './types';13import {14 createCollectionExpectSuccess, getCreatedCollectionCount,15 getCreateItemResult,16 getDetailedCollectionInfo,17 setCollectionLimitsExpectFailure,18 setCollectionLimitsExpectSuccess,19} from './util/helpers';2021chai.use(chaiAsPromised);22const expect = chai.expect;2324let alice: IKeyringPair;25let bob: IKeyringPair;26let collectionIdForTesting: number;2728const accountTokenOwnershipLimit = 0;29const sponsoredDataSize = 0;30const sponsoredMintSize = 0;31const sponsorTimeout = 1;32const tokenLimit = 10;3334describe('setCollectionLimits positive', () => {35 let tx;36 before(async () => {37 await usingApi(async () => {38 const keyring = new Keyring({ type: 'sr25519' });39 alice = keyring.addFromUri('//Alice');40 collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});41 });42 });43 it('execute setCollectionLimits with predefined params ', async () => {44 await usingApi(async (api: ApiPromise) => {45 tx = api.tx.nft.setCollectionLimits(46 collectionIdForTesting,47 {48 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,49 SponsoredMintSize: sponsoredDataSize,50 TokenLimit: tokenLimit,51 SponsorTimeout: sponsorTimeout,52 OwnerCanTransfer: true,53 OwnerCanDestroy: true54 },55 );56 const events = await submitTransactionAsync(alice, tx);57 const result = getCreateItemResult(events);5859 // get collection limits defined previously60 const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;6162 // tslint:disable-next-line:no-unused-expression63 expect(result.success).to.be.true;64 expect(collectionInfo.Limits.AccountTokenOwnershipLimit).to.be.equal(accountTokenOwnershipLimit);65 expect(collectionInfo.Limits.SponsoredDataSize).to.be.equal(sponsoredDataSize);66 expect(collectionInfo.Limits.TokenLimit).to.be.equal(tokenLimit);67 expect(collectionInfo.Limits.SponsorTimeout).to.be.equal(sponsorTimeout);68 expect(collectionInfo.Limits.OwnerCanTransfer).to.be.true;69 expect(collectionInfo.Limits.OwnerCanDestroy).to.be.true;70 });71 });7273 it('Set the same token limit twice', async () => {74 await usingApi(async (api: ApiPromise) => {7576 let collectionLimits = {77 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,78 SponsoredMintSize: sponsoredDataSize,79 TokenLimit: tokenLimit,80 SponsorTimeout: sponsorTimeout,81 OwnerCanTransfer: true,82 OwnerCanDestroy: true83 };8485 // The first time86 const tx1 = api.tx.nft.setCollectionLimits(87 collectionIdForTesting,88 collectionLimits,89 );90 const events1 = await submitTransactionAsync(alice, tx1);91 const result1 = getCreateItemResult(events1);92 const collectionInfo1 = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;9394 // The second time95 const tx2 = api.tx.nft.setCollectionLimits(96 collectionIdForTesting,97 collectionLimits,98 );99 const events2 = await submitTransactionAsync(alice, tx2);100 const result2 = getCreateItemResult(events2);101 const collectionInfo2 = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;102103 // tslint:disable-next-line:no-unused-expression104 expect(result1.success).to.be.true;105 expect(collectionInfo1.Limits.TokenLimit).to.be.equal(tokenLimit);106 expect(result2.success).to.be.true;107 expect(collectionInfo2.Limits.TokenLimit).to.be.equal(tokenLimit);108 });109 });110111});112113describe('setCollectionLimits negative', () => {114 let tx;115 before(async () => {116 await usingApi(async () => {117 const keyring = new Keyring({ type: 'sr25519' });118 alice = keyring.addFromUri('//Alice');119 bob = keyring.addFromUri('//Bob');120 collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});121 });122 });123 it('execute setCollectionLimits for not exists collection', async () => {124 await usingApi(async (api: ApiPromise) => {125 const collectionCount = await getCreatedCollectionCount(api);126 const nonExistedCollectionId = collectionCount + 1;127 tx = api.tx.nft.setCollectionLimits(128 nonExistedCollectionId,129 {130 accountTokenOwnershipLimit,131 sponsoredDataSize,132 sponsoredMintSize,133 tokenLimit,134 },135 );136 await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;137 });138 });139 it('execute setCollectionLimits from user who is not owner of this collection', async () => {140 await usingApi(async (api: ApiPromise) => {141 tx = api.tx.nft.setCollectionLimits(142 collectionIdForTesting,143 {144 accountTokenOwnershipLimit,145 sponsoredDataSize,146 sponsoredMintSize,147 tokenLimit,148 },149 );150 await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;151 });152 });153 it('execute setCollectionLimits with incorrect limits', async () => {154 await usingApi(async (api: ApiPromise) => {155 tx = api.tx.nft.setCollectionLimits(156 collectionIdForTesting,157 {158 accountTokenOwnershipLimit: 'awdawd',159 sponsorTransferTimeout: 'awd',160 sponsoredDataSize: '12312312312312312',161 tokenLimit: '-100',162 },163 );164 await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;165 });166 });167168 it('fails when trying to enable OwnerCanTransfer after it was disabled', async () => {169 const collectionId = await createCollectionExpectSuccess();170 await setCollectionLimitsExpectSuccess(alice, collectionId, { 171 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,172 SponsoredMintSize: sponsoredDataSize,173 TokenLimit: tokenLimit,174 SponsorTimeout: sponsorTimeout,175 OwnerCanTransfer: false,176 OwnerCanDestroy: true177 });178 await setCollectionLimitsExpectFailure(alice, collectionId, { 179 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,180 SponsoredMintSize: sponsoredDataSize,181 TokenLimit: tokenLimit,182 SponsorTimeout: sponsorTimeout,183 OwnerCanTransfer: true,184 OwnerCanDestroy: true185 });186 });187188 it('fails when trying to enable OwnerCanDestroy after it was disabled', async () => {189 const collectionId = await createCollectionExpectSuccess();190 await setCollectionLimitsExpectSuccess(alice, collectionId, {191 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,192 SponsoredMintSize: sponsoredDataSize,193 TokenLimit: tokenLimit,194 SponsorTimeout: sponsorTimeout,195 OwnerCanTransfer: true,196 OwnerCanDestroy: false197 });198 await setCollectionLimitsExpectFailure(alice, collectionId, { 199 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,200 SponsoredMintSize: sponsoredDataSize,201 TokenLimit: tokenLimit,202 SponsorTimeout: sponsorTimeout,203 OwnerCanTransfer: true,204 OwnerCanDestroy: true205 });206 });207208 it('Setting the higher token limit fails', async () => {209 await usingApi(async (api: ApiPromise) => {210211 const collectionId = await createCollectionExpectSuccess();212 let collectionLimits = {213 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,214 SponsoredMintSize: sponsoredDataSize,215 TokenLimit: tokenLimit,216 SponsorTimeout: sponsorTimeout,217 OwnerCanTransfer: true,218 OwnerCanDestroy: true219 };220221 // The first time222 await setCollectionLimitsExpectSuccess(alice, collectionId, collectionLimits);223224 // The second time - higher token limit225 collectionLimits.TokenLimit += 1;226 await setCollectionLimitsExpectFailure(alice, collectionId, collectionLimits);227 });228 });229230});