git.delta.rocks / unique-network / refs/commits / 378b7f3ae3aa

difftreelog

source

tests/src/rmrk/addResource.seqtest.ts10.1 KiBsourcehistory
1import {expect} from 'chai';2import {getApiConnection} from '../substrate/substrate-api';3import {NftIdTuple} from './util/fetch';4import {expectTxFailure, getResourceById, requirePallets, Pallets} from './util/helpers';5import {6  addNftBasicResource,7  acceptNftResource,8  createCollection,9  mintNft,10  sendNft,11  addNftSlotResource,12  addNftComposableResource,13} from './util/tx';14import {RmrkTraitsResourceResourceInfo as ResourceInfo} from '@polkadot/types/lookup';1516describe('integration test: add NFT resource', () => {17  const alice = '//Alice';18  const bob = '//Bob';19  const src = 'test-res-src';20  const metadata = 'test-res-metadata';21  const license = 'test-res-license';22  const thumb = 'test-res-thumb';2324  const nonexistentId = 99999;2526  let api: any;27  before(async function() {28    api = await getApiConnection();29    await requirePallets(this, [Pallets.RmrkCore]);30  });3132  it('add resource', async () => {33    const collectionIdAlice = await createCollection(34      api,35      alice,36      'test-metadata',37      null,38      'test-symbol',39    );4041    const nftAlice = await mintNft(42      api,43      alice,44      alice,45      collectionIdAlice,46      'nft-metadata',47    );4849    await addNftBasicResource(50      api,51      alice,52      'added',53      collectionIdAlice,54      nftAlice,55      src,56      metadata,57      license,58      thumb,59    );60  });6162  it('add a resource to the nested NFT', async () => {63    const collectionIdAlice = await createCollection(64      api,65      alice,66      'test-metadata',67      null,68      'test-symbol',69    );7071    const parentNftId = await mintNft(api, alice, alice, collectionIdAlice, 'parent-nft-metadata');72    const childNftId = await mintNft(api, alice, alice, collectionIdAlice, 'child-nft-metadata');7374    const newOwnerNFT: NftIdTuple = [collectionIdAlice, parentNftId];7576    await sendNft(api, 'sent', alice, collectionIdAlice, childNftId, newOwnerNFT);7778    await addNftBasicResource(79      api,80      alice,81      'added',82      collectionIdAlice,83      childNftId,84      src,85      metadata,86      license,87      thumb,88    );89  });9091  it('add multiple resources', async () => {92    const collectionIdAlice = await createCollection(93      api,94      alice,95      'test-metadata',96      null,97      'test-symbol',98    );99100    const nftAlice = await mintNft(101      api,102      alice,103      alice,104      collectionIdAlice,105      'nft-metadata',106    );107108    const baseId = 42;109    const slotId = 10;110    const parts = [0, 5, 2];111112    const resourcesInfo = [];113    const resourceNum = 4;114115    const checkResource = (resource: ResourceInfo, resType: string, expectedId: number, expected: {116      src: string,117      metadata: string,118      license: string,119      thumb: string120    }) => {121122      // FIXME A workaround. It seems it is a PolkadotJS bug.123      // All of the following are `false`.124      //125      // console.log('>>> basic:', resource.resource.isBasic);126      // console.log('>>> composable:', resource.resource.isComposable);127      // console.log('>>> slot:', resource.resource.isSlot);128      const resourceJson = (resource.resource.toHuman() as any)[resType];129130      expect(resource.id.toNumber(), 'Error: Invalid resource Id')131        .to.be.eq(expectedId);132133      expect(resourceJson.src, 'Error: Invalid resource src')134        .to.be.eq(expected.src);135      expect(resourceJson.metadata, 'Error: Invalid resource metadata')136        .to.be.eq(expected.metadata);137      expect(resourceJson.license, 'Error: Invalid resource license')138        .to.be.eq(expected.license);139      expect(resourceJson.thumb, 'Error: Invalid resource thumb')140        .to.be.eq(expected.thumb);141    };142143    for (let i = 0; i < resourceNum; i++) {144      resourcesInfo.push({145        src: src + 'r-' + i,146        metadata: metadata + 'r-' + i,147        license: license + 'r-' + i,148        thumb: thumb + 'r-' + i,149      });150    }151152    const firstBasicResourceId = await addNftBasicResource(153      api,154      alice,155      'added',156      collectionIdAlice,157      nftAlice,158      resourcesInfo[0].src,159      resourcesInfo[0].metadata,160      resourcesInfo[0].license,161      resourcesInfo[0].thumb,162    );163164    const secondBasicResourceId = await addNftBasicResource(165      api,166      alice,167      'added',168      collectionIdAlice,169      nftAlice,170      resourcesInfo[1].src,171      resourcesInfo[1].metadata,172      resourcesInfo[1].license,173      resourcesInfo[1].thumb,174    );175176    const composableResourceId = await addNftComposableResource(177      api,178      alice,179      'added',180      collectionIdAlice,181      nftAlice,182      parts,183      baseId,184      resourcesInfo[2].src,185      resourcesInfo[2].metadata,186      resourcesInfo[2].license,187      resourcesInfo[2].thumb,188    );189190    const slotResourceId = await addNftSlotResource(191      api,192      alice,193      'added',194      collectionIdAlice,195      nftAlice,196      baseId,197      slotId,198      resourcesInfo[3].src,199      resourcesInfo[3].metadata,200      resourcesInfo[3].license,201      resourcesInfo[3].thumb,202    );203204    const firstResource = await getResourceById(api, collectionIdAlice, nftAlice, firstBasicResourceId);205    await checkResource(firstResource, 'Basic', firstBasicResourceId, resourcesInfo[0]);206207    const secondResource = await getResourceById(api, collectionIdAlice, nftAlice, secondBasicResourceId);208    await checkResource(secondResource, 'Basic', secondBasicResourceId, resourcesInfo[1]);209210    const composableResource = await getResourceById(api, collectionIdAlice, nftAlice, composableResourceId);211    await checkResource(composableResource, 'Composable', composableResourceId, resourcesInfo[2]);212213    const slotResource = await getResourceById(api, collectionIdAlice, nftAlice, slotResourceId);214    await checkResource(slotResource, 'Slot', slotResourceId, resourcesInfo[3]);215  });216217  it('[negative]: unable to add a resource to the non-existing NFT', async () => {218    const collectionIdAlice = await createCollection(219      api,220      alice,221      'test-metadata',222      null,223      'test-symbol',224    );225226    const tx = addNftBasicResource(227      api,228      alice,229      'added',230      collectionIdAlice,231      nonexistentId,232      src,233      metadata,234      license,235      thumb,236    );237  238    await expectTxFailure(/rmrkCore\.NoAvailableNftId/, tx);239  });240241  it('[negative]: unable to add a resource by a not-an-owner user', async () => {242    const collectionIdAlice = await createCollection(243      api,244      alice,245      'test-metadata',246      null,247      'test-symbol',248    );249250    const nftAlice = await mintNft(251      api,252      alice,253      alice,254      collectionIdAlice,255      'nft-metadata',256    );257258    const tx = addNftBasicResource(259      api,260      bob,261      'added',262      collectionIdAlice,263      nftAlice,264      src,265      metadata,266      license,267      thumb,268    );269  270    await expectTxFailure(/rmrkCore\.NoPermission/, tx);271  });272273  it('[negative]: unable to add a resource to the nested NFT if it isnt root owned by the caller', async () => {274    const collectionIdAlice = await createCollection(275      api,276      alice,277      'test-metadata',278      null,279      'test-symbol',280    );281282    const parentNftId = await mintNft(api, alice, alice, collectionIdAlice, 'parent-nft-metadata');283    const childNftId = await mintNft(api, alice, alice, collectionIdAlice, 'child-nft-metadata');284285    const newOwnerNFT: NftIdTuple = [collectionIdAlice, parentNftId];286287    await sendNft(api, 'sent', alice, collectionIdAlice, childNftId, newOwnerNFT);288289    const tx = addNftBasicResource(290      api,291      bob,292      'added',293      collectionIdAlice,294      childNftId,295      src,296      metadata,297      license,298      thumb,299    );300    301    await expectTxFailure(/rmrkCore\.NoPermission/, tx);302  });303304  it('accept resource', async () => {305    const collectionIdBob = await createCollection(306      api,307      bob,308      'test-metadata',309      null,310      'test-symbol',311    );312313    const nftAlice = await mintNft(314      api,315      bob,316      alice,317      collectionIdBob,318      'nft-metadata',319    );320321    const resourceId = await addNftBasicResource(322      api,323      bob,324      'pending',325      collectionIdBob,326      nftAlice,327      src,328      metadata,329      license,330      thumb,331    );332333    await acceptNftResource(api, alice, collectionIdBob, nftAlice, resourceId);334  });335336  it('[negative]: unable to accept a non-existing resource', async () => {337    const collectionIdBob = await createCollection(338      api,339      bob,340      'test-metadata',341      null,342      'test-symbol',343    );344345    const nftAlice = await mintNft(346      api,347      bob,348      alice,349      collectionIdBob,350      'nft-metadata',351    );352353    const tx = acceptNftResource(api, alice, collectionIdBob, nftAlice, nonexistentId);354    await expectTxFailure(/rmrkCore\.ResourceDoesntExist/, tx);355  });356357  it('[negative]: unable to accept a resource by a not-an-NFT-owner user', async () => {358    const collectionIdBob = await createCollection(359      api,360      bob,361      'test-metadata',362      null,363      'test-symbol',364    );365366    const nftAlice = await mintNft(367      api,368      bob,369      alice,370      collectionIdBob,371      'nft-metadata',372    );373374    const resourceId = await addNftBasicResource(375      api,376      bob,377      'pending',378      collectionIdBob,379      nftAlice,380      src,381      metadata,382      license,383      thumb,384    );385386    const tx = acceptNftResource(api, bob, collectionIdBob, nftAlice, resourceId);387388    await expectTxFailure(/rmrkCore\.NoPermission/, tx);389  });390391  it('[negative]: unable to accept a resource to a non-target NFT', async () => {392    const collectionIdBob = await createCollection(393      api,394      bob,395      'test-metadata',396      null,397      'test-symbol',398    );399400    const nftAlice = await mintNft(401      api,402      bob,403      alice,404      collectionIdBob,405      'nft-metadata',406    );407408    const wrongNft = await mintNft(409      api,410      bob,411      alice,412      collectionIdBob,413      'nft-metadata',414    );415    416    const resourceId = await addNftBasicResource(417      api,418      bob,419      'pending',420      collectionIdBob,421      nftAlice,422      src,423      metadata,424      license,425      thumb,426    );427428    const tx = acceptNftResource(api, bob, collectionIdBob, wrongNft, resourceId);429430    await expectTxFailure(/rmrkCore\.ResourceDoesntExist/, tx);431  });432433434  after(async() => { await api.disconnect(); });435});