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

difftreelog

source

tests/src/createItem.test.ts13.9 KiBsourcehistory
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 {default as usingApi, executeTransaction} from './substrate/substrate-api';18import chai from 'chai';19import {IKeyringPair} from '@polkadot/types/types';20import {21  createCollectionExpectSuccess,22  createItemExpectSuccess,23  addCollectionAdminExpectSuccess,24  createCollectionWithPropsExpectSuccess,25  createItemWithPropsExpectSuccess,26  createItemWithPropsExpectFailure,27  createCollection,28  transferExpectSuccess,29  itApi,30  normalizeAccountId,31  getCreateItemResult,32  requirePallets,33  Pallets,34} from './util/helpers';3536const expect = chai.expect;37let alice: IKeyringPair;38let bob: IKeyringPair;3940describe('integration test: ext. ():', () => {41  before(async () => {42    await usingApi(async (api, privateKeyWrapper) => {43      alice = privateKeyWrapper('//Alice');44      bob = privateKeyWrapper('//Bob');45    });46  });4748  it('Create new item in NFT collection', async () => {49    const createMode = 'NFT';50    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});51    await createItemExpectSuccess(alice, newCollectionID, createMode);52  });53  it('Create new item in Fungible collection', async () => {54    const createMode = 'Fungible';55    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});56    await createItemExpectSuccess(alice, newCollectionID, createMode);57  });58  itApi('Check events on create new item in Fungible collection', async ({api}) => {59    const createMode = 'Fungible';60    61    const newCollectionID = (await createCollection(api, alice, {mode: {type: createMode, decimalPoints: 0}})).collectionId;62    63    const to = normalizeAccountId(alice);64    {65      const createData = {fungible: {value: 100}};66      const tx = api.tx.unique.createItem(newCollectionID, to, createData as any);67      const events = await executeTransaction(api, alice, tx);68      const result = getCreateItemResult(events);69      expect(result.amount).to.be.equal(100);70      expect(result.collectionId).to.be.equal(newCollectionID);71      expect(result.recipient).to.be.deep.equal(to);72    }73    {74      const createData = {fungible: {value: 50}};75      const tx = api.tx.unique.createItem(newCollectionID, to, createData as any);76      const events = await executeTransaction(api, alice, tx);77      const result = getCreateItemResult(events);78      expect(result.amount).to.be.equal(50);79      expect(result.collectionId).to.be.equal(newCollectionID);80      expect(result.recipient).to.be.deep.equal(to);81    }8283  });84  it('Create new item in ReFungible collection', async function() {85    await requirePallets(this, [Pallets.ReFungible]);8687    const createMode = 'ReFungible';88    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});89    await createItemExpectSuccess(alice, newCollectionID, createMode);90  });91  it('Create new item in NFT collection with collection admin permissions', async () => {92    const createMode = 'NFT';93    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});94    await addCollectionAdminExpectSuccess(alice, newCollectionID, bob.address);95    await createItemExpectSuccess(bob, newCollectionID, createMode);96  });97  it('Create new item in Fungible collection with collection admin permissions', async () => {98    const createMode = 'Fungible';99    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});100    await addCollectionAdminExpectSuccess(alice, newCollectionID, bob.address);101    await createItemExpectSuccess(bob, newCollectionID, createMode);102  });103  it('Create new item in ReFungible collection with collection admin permissions', async function() {104    await requirePallets(this, [Pallets.ReFungible]);105106    const createMode = 'ReFungible';107    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});108    await addCollectionAdminExpectSuccess(alice, newCollectionID, bob.address);109    await createItemExpectSuccess(bob, newCollectionID, createMode);110  });111112  it('Set property Admin', async () => {113    const createMode = 'NFT';114    const newCollectionID = await createCollectionWithPropsExpectSuccess({mode: {type: createMode}, 115      propPerm:   [{key: 'k', permission: {mutable: true, collectionAdmin: true, tokenOwner: false}}]});116    117    await createItemWithPropsExpectSuccess(alice, newCollectionID, createMode, [{key: 'k', value: 't2'}]);118  });119120  it('Set property AdminConst', async () => {121    const createMode = 'NFT';122    const newCollectionID = await createCollectionWithPropsExpectSuccess({mode: {type: createMode}, 123      propPerm:   [{key: 'key1', permission: {mutable: false, collectionAdmin: true, tokenOwner: false}}]});124    125    await createItemWithPropsExpectSuccess(alice, newCollectionID, createMode, [{key: 'key1', value: 'val1'}]);126  });127128  it('Set property itemOwnerOrAdmin', async () => {129    const createMode = 'NFT';130    const newCollectionID = await createCollectionWithPropsExpectSuccess({mode: {type: createMode},131      propPerm:   [{key: 'key1', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}}]});132    133    await createItemWithPropsExpectSuccess(alice, newCollectionID, createMode, [{key: 'key1', value: 'val1'}]);134  });135136  it('Check total pieces of Fungible token', async () => {137    await usingApi(async api => {138      const createMode = 'Fungible';139      const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});140      const amountPieces = 10n;141      const tokenId = await createItemExpectSuccess(alice, collectionId, createMode, bob.address);142      {143        const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);144        expect(totalPieces.isSome).to.be.true;145        expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);146      }147148      await transferExpectSuccess(collectionId, tokenId, bob, alice, 1, createMode);149      {150        const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);151        expect(totalPieces.isSome).to.be.true;152        expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);153      }154155      const totalPieces = (await api.rpc.unique.tokenData(collectionId, tokenId, [])).pieces;156      expect(totalPieces.toBigInt()).to.be.eq(amountPieces);157    });158  });159160  it('Check total pieces of NFT token', async () => {161    await usingApi(async api => {162      const createMode = 'NFT';163      const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});164      const amountPieces = 1n;165      const tokenId = await createItemExpectSuccess(alice, collectionId, createMode, bob.address);166      {167        const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);168        expect(totalPieces.isSome).to.be.true;169        expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);170      }171172      await transferExpectSuccess(collectionId, tokenId, bob, alice, 1, createMode);173      {174        const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);175        expect(totalPieces.isSome).to.be.true;176        expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);177      }178179      const totalPieces = (await api.rpc.unique.tokenData(collectionId, tokenId, [])).pieces;180      expect(totalPieces.toBigInt()).to.be.eq(amountPieces);181    });182  });183184  it('Check total pieces of ReFungible token', async function() {185    await requirePallets(this, [Pallets.ReFungible]);186187    await usingApi(async api => {188      const createMode = 'ReFungible';189      const createCollectionResult = await createCollection(api, alice, {mode: {type: createMode}});190      const collectionId  = createCollectionResult.collectionId;191      const amountPieces = 100n;192      const tokenId = await createItemExpectSuccess(alice, collectionId, createMode, bob.address);193      {194        const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);195        expect(totalPieces.isSome).to.be.true;196        expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);197      }198199      await transferExpectSuccess(collectionId, tokenId, bob, alice, 60n, createMode);200      {201        const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);202        expect(totalPieces.isSome).to.be.true;203        expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);204      }205206      const totalPieces = (await api.rpc.unique.tokenData(collectionId, tokenId, [])).pieces;207      expect(totalPieces.toBigInt()).to.be.eq(amountPieces);208    });209  });210});211212describe('Negative integration test: ext. createItem():', () => {213  before(async () => {214    await usingApi(async (api, privateKeyWrapper) => {215      alice = privateKeyWrapper('//Alice');216      bob = privateKeyWrapper('//Bob');217    });218  });219220  it('Regular user cannot create new item in NFT collection', async () => {221    const createMode = 'NFT';222    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});223    await expect(createItemExpectSuccess(bob, newCollectionID, createMode)).to.be.rejected;224  });225  it('Regular user cannot create new item in Fungible collection', async () => {226    const createMode = 'Fungible';227    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});228    await expect(createItemExpectSuccess(bob, newCollectionID, createMode)).to.be.rejected;229  });230  it('Regular user cannot create new item in ReFungible collection', async function() {231    await requirePallets(this, [Pallets.ReFungible]);232233    const createMode = 'ReFungible';234    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});235    await expect(createItemExpectSuccess(bob, newCollectionID, createMode)).to.be.rejected;236  });237238  it('No editing rights', async () => {239    await usingApi(async () => {240      const createMode = 'NFT';241      const newCollectionID = await createCollectionWithPropsExpectSuccess({mode: {type: createMode}, 242        propPerm:   [{key: 'key1', permission: {mutable: false, collectionAdmin: false, tokenOwner: false}}]});243      await addCollectionAdminExpectSuccess(alice, newCollectionID, bob.address);244245      await createItemWithPropsExpectFailure(bob, newCollectionID, 'NFT', [{key: 'key1', value: 'v'}]);246    });247  });248249  it('User doesnt have editing rights', async () => {250    await usingApi(async () => {251      const newCollectionID = await createCollectionWithPropsExpectSuccess({propPerm: [{key: 'key1', permission: {mutable: true, collectionAdmin: false, tokenOwner: false}}]});252      await createItemWithPropsExpectFailure(bob, newCollectionID, 'NFT', [{key: 'key1', value: 'v'}]);253    });254  });255256  it('Adding property without access rights', async () => {257    await usingApi(async () => {258      const newCollectionID = await createCollectionWithPropsExpectSuccess();259      await addCollectionAdminExpectSuccess(alice, newCollectionID, bob.address);260261      await createItemWithPropsExpectFailure(bob, newCollectionID, 'NFT', [{key: 'k', value: 'v'}]);262    });263  });264265  it('Adding more than 64 prps', async () => {266    await usingApi(async () => {267      const prps = [];268269      for (let i = 0; i < 65; i++) {270        prps.push({key: `key${i}`, value: `value${i}`});271      }272273      const newCollectionID = await createCollectionWithPropsExpectSuccess();274      275      await createItemWithPropsExpectFailure(alice, newCollectionID, 'NFT', prps);276    });277  });278279  it('Trying to add bigger property than allowed', async () => {280    await usingApi(async () => {281      const newCollectionID = await createCollectionWithPropsExpectSuccess();282      283      await createItemWithPropsExpectFailure(alice, newCollectionID, 'NFT', [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}]);284    });285  });286287  it('Check total pieces for invalid Fungible token', async () => {288    await usingApi(async api => {289      const createCollectionResult = await createCollection(api, alice, {mode: {type: 'Fungible', decimalPoints: 0}});290      const collectionId  = createCollectionResult.collectionId;291      const invalidTokenId = 1000_000;292      293      expect((await api.rpc.unique.totalPieces(collectionId, invalidTokenId)).isNone).to.be.true;294      expect((await api.rpc.unique.tokenData(collectionId, invalidTokenId, [])).pieces.toBigInt()).to.be.eq(0n);295    });296  });297298  it('Check total pieces for invalid NFT token', async () => {299    await usingApi(async api => {300      const createCollectionResult = await createCollection(api, alice, {mode: {type: 'NFT'}});301      const collectionId  = createCollectionResult.collectionId;302      const invalidTokenId = 1000_000;303      304      expect((await api.rpc.unique.totalPieces(collectionId, invalidTokenId)).isNone).to.be.true;305      expect((await api.rpc.unique.tokenData(collectionId, invalidTokenId, [])).pieces.toBigInt()).to.be.eq(0n);306    });307  });308309  it('Check total pieces for invalid Refungible token', async function() {310    await requirePallets(this, [Pallets.ReFungible]);311312    await usingApi(async api => {313      const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});314      const collectionId  = createCollectionResult.collectionId;315      const invalidTokenId = 1000_000;316      317      expect((await api.rpc.unique.totalPieces(collectionId, invalidTokenId)).isNone).to.be.true;318      expect((await api.rpc.unique.tokenData(collectionId, invalidTokenId, [])).pieces.toBigInt()).to.be.eq(0n);319    });320  });321});