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

difftreelog

tests: add tests for approval limit

Yaroslav Bolyukin2021-02-19parent: #1284e5a.patch.diff
in: master

1 file changed

modifiedtests/src/approve.test.tsdiffbeforeafterboth
before · tests/src/approve.test.ts
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//5import { ApiPromise } from '@polkadot/api';6import BN from 'bn.js';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import privateKey from './substrate/privateKey';10import { default as usingApi } from './substrate/substrate-api';11import {12  approveExpectFail,13  approveExpectSuccess,14  createCollectionExpectSuccess,15  createFungibleItemExpectSuccess,16  createItemExpectSuccess,17  destroyCollectionExpectSuccess,18  transferFromExpectSuccess,19  U128_MAX,20} from './util/helpers';2122chai.use(chaiAsPromised);23const expect = chai.expect;2425describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {26  it('Execute the extrinsic and check approvedList', async () => {27    await usingApi(async (api: ApiPromise) => {28      const Alice = privateKey('//Alice');29      const Bob = privateKey('//Bob');30      const nftCollectionId = await createCollectionExpectSuccess();31      // nft32      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');33      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);34      // fungible35      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});36      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');37      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);38      // reFungible39      const reFungibleCollectionId =40        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});41      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');42      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);43    });44  });4546  it('Remove approval by using 0 amount', async () => {47    await usingApi(async (api: ApiPromise) => {48      const Alice = privateKey('//Alice');49      const Bob = privateKey('//Bob');50      const nftCollectionId = await createCollectionExpectSuccess();51      // nft52      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');53      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);54      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);55      // fungible56      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});57      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');58      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);59      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);60      // reFungible61      const reFungibleCollectionId =62        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});63      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');64      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);65      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);66    });67  });68});6970describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {71  it('Approve for a collection that does not exist', async () => {72    await usingApi(async (api: ApiPromise) => {73      const Alice = privateKey('//Alice');74      const Bob = privateKey('//Bob');75      // nft76      const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;77      await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);78      // fungible79      const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;80      await approveExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob);81      // reFungible82      const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;83      await approveExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob);84    });85  });8687  it('Approve for a collection that was destroyed', async () => {88    await usingApi(async (api: ApiPromise) => {89      const Alice = privateKey('//Alice');90      const Bob = privateKey('//Bob');91      // nft92      const nftCollectionId = await createCollectionExpectSuccess();93      await destroyCollectionExpectSuccess(nftCollectionId);94      await approveExpectFail(nftCollectionId, 1, Alice, Bob);95      // fungible96      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});97      await destroyCollectionExpectSuccess(fungibleCollectionId);98      await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);99      // reFungible100      const reFungibleCollectionId =101        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});102      await destroyCollectionExpectSuccess(reFungibleCollectionId);103      await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);104    });105  });106107  it('Approve transfer of a token that does not exist', async () => {108    await usingApi(async (api: ApiPromise) => {109      const Alice = privateKey('//Alice');110      const Bob = privateKey('//Bob');111      // nft112      const nftCollectionId = await createCollectionExpectSuccess();113      await approveExpectFail(nftCollectionId, 2, Alice, Bob);114      // fungible115      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});116      await approveExpectFail(fungibleCollectionId, 2, Alice, Bob);117      // reFungible118      const reFungibleCollectionId =119        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});120      await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);121    });122  });123124  it('Approve using the address that does not own the approved token', async () => {125    await usingApi(async (api: ApiPromise) => {126      const Alice = privateKey('//Alice');127      const Bob = privateKey('//Bob');128      const nftCollectionId = await createCollectionExpectSuccess();129      // nft130      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');131      await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);132      // fungible133      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});134      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');135      await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice);136      // reFungible137      const reFungibleCollectionId =138        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});139      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');140      await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);141    });142  });143});
after · tests/src/approve.test.ts
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//5import { IKeyringPair } from '@polkadot/types/types';6import { ApiPromise } from '@polkadot/api';7import BN from 'bn.js';8import chai from 'chai';9import chaiAsPromised from 'chai-as-promised';10import privateKey from './substrate/privateKey';11import { default as usingApi } from './substrate/substrate-api';12import {13  approveExpectFail,14  approveExpectSuccess,15  createCollectionExpectSuccess,16  createFungibleItemExpectSuccess,17  createItemExpectSuccess,18  destroyCollectionExpectSuccess,19  transferExpectSuccess,20  transferFromExpectSuccess,21  U128_MAX,22} from './util/helpers';2324chai.use(chaiAsPromised);25const expect = chai.expect;2627describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {28  it('Execute the extrinsic and check approvedList', async () => {29    await usingApi(async (api: ApiPromise) => {30      const Alice = privateKey('//Alice');31      const Bob = privateKey('//Bob');32      const nftCollectionId = await createCollectionExpectSuccess();33      // nft34      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');35      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);36      // fungible37      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});38      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');39      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);40      // reFungible41      const reFungibleCollectionId =42        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});43      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');44      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);45    });46  });4748  it('Remove approval by using 0 amount', async () => {49    await usingApi(async (api: ApiPromise) => {50      const Alice = privateKey('//Alice');51      const Bob = privateKey('//Bob');52      const nftCollectionId = await createCollectionExpectSuccess();53      // nft54      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');55      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);56      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);57      // fungible58      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});59      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');60      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);61      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);62      // reFungible63      const reFungibleCollectionId =64        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});65      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');66      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);67      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);68    });69  });70});7172describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {73  let Alice: IKeyringPair;74  let Bob: IKeyringPair;7576  before(async () => {77    await usingApi(async (api) => {78      Alice = privateKey('//Alice');79      Bob = privateKey('//Bob');80    });81  });8283  it('Approve for a collection that does not exist', async () => {84    await usingApi(async (api: ApiPromise) => {8586      // nft87      const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;88      await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);89      // fungible90      const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;91      await approveExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob);92      // reFungible93      const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;94      await approveExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob);95    });96  });9798  it('Approve for a collection that was destroyed', async () => {99    await usingApi(async (api: ApiPromise) => {100      // nft101      const nftCollectionId = await createCollectionExpectSuccess();102      await destroyCollectionExpectSuccess(nftCollectionId);103      await approveExpectFail(nftCollectionId, 1, Alice, Bob);104      // fungible105      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});106      await destroyCollectionExpectSuccess(fungibleCollectionId);107      await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);108      // reFungible109      const reFungibleCollectionId =110        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});111      await destroyCollectionExpectSuccess(reFungibleCollectionId);112      await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);113    });114  });115116  it('Approve transfer of a token that does not exist', async () => {117    await usingApi(async (api: ApiPromise) => {118      // nft119      const nftCollectionId = await createCollectionExpectSuccess();120      await approveExpectFail(nftCollectionId, 2, Alice, Bob);121      // fungible122      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});123      await approveExpectFail(fungibleCollectionId, 2, Alice, Bob);124      // reFungible125      const reFungibleCollectionId =126        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});127      await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);128    });129  });130131  it('Approve using the address that does not own the approved token', async () => {132    await usingApi(async (api: ApiPromise) => {133      const Alice = privateKey('//Alice');134      const Bob = privateKey('//Bob');135      const nftCollectionId = await createCollectionExpectSuccess();136      // nft137      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');138      await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);139      // fungible140      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});141      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');142      await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice);143      // reFungible144      const reFungibleCollectionId =145        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});146      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');147      await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);148    });149  });150151  it('should fail if approved more NFTs than owned', async () => {152    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });153    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');154    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1, 'NFT');155    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice);156    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);157  });158159  it('should fail if approved more ReFungibles than owned', async () => {160    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'ReFungible' } });161    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'ReFungible');162    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 100, 'ReFungible');163    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 100);164    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);165  });166167  it('should fail if approved more Fungibles than owned', async () => {168    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });169    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'Fungible');170    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 10, 'Fungible');171    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 10);172    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);173  });174});