git.delta.rocks / unique-network / refs/commits / 6804826993dd

difftreelog

source

tests/src/createItem.test.ts14.0 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 chai from 'chai';18import chaiAsPromised from 'chai-as-promised';19import {IKeyringPair} from '@polkadot/types/types';20import {21  createCollection,22  itApi,23  normalizeAccountId,24  getCreateItemResult,25} from './util/helpers';26import {usingPlaygrounds} from './util/playgrounds';27import {IProperty} from './util/playgrounds/types';28import {executeTransaction} from './substrate/substrate-api';2930chai.use(chaiAsPromised);31const expect = chai.expect;3233let donor: IKeyringPair;3435before(async () => {36  await usingPlaygrounds(async (_, privateKey) => {37    donor = privateKey('//Alice');38  });39});4041let alice: IKeyringPair;42let bob: IKeyringPair;4344describe('integration test: ext. ():', () => {45  before(async () => {46    await usingPlaygrounds(async (helper) => {47      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);48    });49  });5051  it('Create new item in NFT collection', async () => {52    await usingPlaygrounds(async (helper) => {53      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});54      await collection.mintToken(alice, {Substrate: alice.address});55    });56  });57  it('Create new item in Fungible collection', async () => {58    await usingPlaygrounds(async (helper) => {59      const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);60      await collection.mint(alice, {Substrate: alice.address}, 10n);61    });62  });63  itApi.skip('Check events on create new item in Fungible collection', async ({api}) => {64    const createMode = 'Fungible';6566    const newCollectionID = (await createCollection(api, alice, {mode: {type: createMode, decimalPoints: 0}})).collectionId;6768    const to = normalizeAccountId(alice);69    {70      const createData = {fungible: {value: 100}};71      const tx = api.tx.unique.createItem(newCollectionID, to, createData as any);72      const events = await executeTransaction(api, alice, tx);73      const result = getCreateItemResult(events);74      expect(result.amount).to.be.equal(100);75      expect(result.collectionId).to.be.equal(newCollectionID);76      expect(result.recipient).to.be.deep.equal(to);77    }78    {79      const createData = {fungible: {value: 50}};80      const tx = api.tx.unique.createItem(newCollectionID, to, createData as any);81      const events = await executeTransaction(api, alice, tx);82      const result = getCreateItemResult(events);83      expect(result.amount).to.be.equal(50);84      expect(result.collectionId).to.be.equal(newCollectionID);85      expect(result.recipient).to.be.deep.equal(to);86    }8788  });89  it('Create new item in ReFungible collection', async function() {90    await usingPlaygrounds(async (helper) => {91      const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});92      await collection.mintToken(alice, {Substrate: alice.address}, 100n);93    });94  });95  it('Create new item in NFT collection with collection admin permissions', async () => {96    await usingPlaygrounds(async (helper) => {97      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});98      await collection.addAdmin(alice, {Substrate: bob.address});99      await collection.mintToken(bob, {Substrate: alice.address});100    });101  });102  it('Create new item in Fungible collection with collection admin permissions', async () => {103    await usingPlaygrounds(async (helper) => {104      const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);105      await collection.addAdmin(alice, {Substrate: bob.address});106      await collection.mint(bob, {Substrate: alice.address}, 10n);107    });108  });109  it('Create new item in ReFungible collection with collection admin permissions', async function() {110    await usingPlaygrounds(async (helper) => {111      const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});112      await collection.addAdmin(alice, {Substrate: bob.address});113      await collection.mintToken(bob, {Substrate: alice.address}, 100n);114    });115  });116117  it('Set property Admin', async () => {118    await usingPlaygrounds(async (helper) => {119      const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'descr', tokenPrefix: 'COL',120        properties: [{key: 'k', value: 'v'}],121        tokenPropertyPermissions: [{key: 'k', permission: {tokenOwner: false, mutable: true, collectionAdmin: true}}],122      });123      await collection.mintToken(alice, {Substrate: bob.address}, [{key: 'k', value: 'v'}]);124    });125  });126127  it('Set property AdminConst', async () => {128    await usingPlaygrounds(async (helper) => {129      const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'descr', tokenPrefix: 'COL',130        properties: [{key: 'k', value: 'v'}],131        tokenPropertyPermissions: [{key: 'k', permission: {tokenOwner: false, mutable: false, collectionAdmin: true}}],132      });133      await collection.mintToken(alice, {Substrate: bob.address}, [{key: 'k', value: 'v'}]);134    });135  });136137  it('Set property itemOwnerOrAdmin', async () => {138    await usingPlaygrounds(async (helper) => {139      const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'descr', tokenPrefix: 'COL',140        properties: [{key: 'k', value: 'v'}],141        tokenPropertyPermissions: [{key: 'k', permission: {tokenOwner: true, mutable: true, collectionAdmin: true}}],142      });143      await collection.mintToken(alice, {Substrate: bob.address}, [{key: 'k', value: 'v'}]);144    });145  });146147  it('Check total pieces of Fungible token', async () => {148    await usingPlaygrounds(async (helper) => {149      const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);150      const amount = 10n;151      await collection.mint(alice, {Substrate: bob.address}, amount);152      {153        const totalPieces = await collection.getTotalPieces();154        expect(totalPieces).to.be.equal(amount);155      }156      await collection.transfer(bob, {Substrate: alice.address}, 1n);157      {158        const totalPieces = await collection.getTotalPieces();159        expect(totalPieces).to.be.equal(amount);160      }161    });162  });163164  it('Check total pieces of NFT token', async () => {165    await usingPlaygrounds(async (helper) => {166      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});167      const amount = 1n;168      const token = await collection.mintToken(alice, {Substrate: bob.address});169      {170        const totalPieces = await helper.api?.rpc.unique.totalPieces(collection.collectionId, token.tokenId);171        expect(totalPieces?.unwrap().toBigInt()).to.be.equal(amount);172      }173      await token.transfer(bob, {Substrate: alice.address});174      {175        const totalPieces = await helper.api?.rpc.unique.totalPieces(collection.collectionId, token.tokenId);176        expect(totalPieces?.unwrap().toBigInt()).to.be.equal(amount);177      }178    });179  });180181  it('Check total pieces of ReFungible token', async function() {182    await usingPlaygrounds(async (helper) => {183      const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});184      const amount = 100n;185      const token = await collection.mintToken(alice, {Substrate: bob.address}, amount);186      {187        const totalPieces = await token.getTotalPieces();188        expect(totalPieces).to.be.equal(amount);189      }190      await token.transfer(bob, {Substrate: alice.address}, 60n);191      {192        const totalPieces = await token.getTotalPieces();193        expect(totalPieces).to.be.equal(amount);194      }195    });196  });197});198199describe('Negative integration test: ext. createItem():', () => {200  before(async () => {201    await usingPlaygrounds(async (helper) => {202      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);203    });204  });205206  it('Regular user cannot create new item in NFT collection', async () => {207    await usingPlaygrounds(async (helper) => {208      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});209      const mintTx = async () => collection.mintToken(bob, {Substrate: bob.address});210      await expect(mintTx()).to.be.rejected;211    });212  });213  it('Regular user cannot create new item in Fungible collection', async () => {214    await usingPlaygrounds(async (helper) => {215      const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);216      const mintTx = async () => collection.mint(bob, {Substrate: bob.address}, 10n);217      await expect(mintTx()).to.be.rejected;218    });219  });220  it('Regular user cannot create new item in ReFungible collection', async () => {221    await usingPlaygrounds(async (helper) => {222      const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});223      const mintTx = async () => collection.mintToken(bob, {Substrate: bob.address});224      await expect(mintTx()).to.be.rejected;225    });226  });227228  it('No editing rights', async () => {229    await usingPlaygrounds(async (helper) => {230      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL',231        tokenPropertyPermissions: [{key: 'k', permission: {mutable: false, collectionAdmin: false, tokenOwner: false}}],232      });233      const mintTx = async () => collection.mintToken(alice, {Substrate: bob.address}, [{key: 'k', value: 'v'}]);234      await expect(mintTx()).to.be.rejected;235    });236  });237238  it('User doesnt have editing rights', async () => {239    await usingPlaygrounds(async (helper) => {240      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL',241        tokenPropertyPermissions: [{key: 'k', permission: {mutable: true, collectionAdmin: false, tokenOwner: false}}],242      });243      const mintTx = async () => collection.mintToken(alice, {Substrate: bob.address}, [{key: 'k', value: 'v'}]);244      await expect(mintTx()).to.be.rejected;245    });246  });247248  it('Adding property without access rights', async () => {249    await usingPlaygrounds(async (helper) => {250      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});251      const mintTx = async () => collection.mintToken(alice, {Substrate: bob.address}, [{key: 'k', value: 'v'}]);252      await expect(mintTx()).to.be.rejected;253    });254  });255256  it('Adding more than 64 prps', async () => {257    const props: IProperty[] = [];258259    for (let i = 0; i < 65; i++) {260      props.push({key: `key${i}`, value: `value${i}`});261    }262263    await usingPlaygrounds(async (helper) => {264      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});265      const mintTx = async () => collection.mintToken(alice, {Substrate: bob.address}, props);266      await expect(mintTx()).to.be.rejected;267    });268  });269270  it('Trying to add bigger property than allowed', async () => {271    await usingPlaygrounds(async (helper) => {272      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL',273        tokenPropertyPermissions: [274          {key: 'k1', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}},275          {key: 'k2', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}},276        ],277      });278      const mintTx = async () => collection.mintToken(alice, {Substrate: bob.address}, [279        {key: 'k1', value: 'vvvvvv'.repeat(5000)},280        {key: 'k2', value: 'vvv'.repeat(5000)},281      ]);282      await expect(mintTx()).to.be.rejected;283    });284  });285286  it('Check total pieces for invalid Fungible token', async () => {287    await usingPlaygrounds(async (helper) => {288      const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);289      const invalidTokenId = 1_000_000;290      expect((await helper.api?.rpc.unique.totalPieces(collection.collectionId, invalidTokenId))?.isNone).to.be.true;291      expect((await helper.api?.rpc.unique.tokenData(collection.collectionId, invalidTokenId))?.pieces.toBigInt()).to.be.equal(0n);292    });293  });294295  it('Check total pieces for invalid NFT token', async () => {296    await usingPlaygrounds(async (helper) => {297      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});298      const invalidTokenId = 1_000_000;299      expect((await helper.api?.rpc.unique.totalPieces(collection.collectionId, invalidTokenId))?.isNone).to.be.true;300      expect((await helper.api?.rpc.unique.tokenData(collection.collectionId, invalidTokenId))?.pieces.toBigInt()).to.be.equal(0n);301    });302  });303304  it('Check total pieces for invalid Refungible token', async function() {305    await usingPlaygrounds(async (helper) => {306      const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});307      const invalidTokenId = 1_000_000;308      expect((await helper.api?.rpc.unique.totalPieces(collection.collectionId, invalidTokenId))?.isNone).to.be.true;309      expect((await helper.api?.rpc.unique.tokenData(collection.collectionId, invalidTokenId))?.pieces.toBigInt()).to.be.equal(0n);310    });311  });312});