git.delta.rocks / unique-network / refs/commits / 7af2e990ada5

difftreelog

source

tests/src/rmrk/addResource.seqtest.ts10.2 KiBsourcehistory
1import {expect} from 'chai';2import {getApiConnection} from '../substrate/substrate-api';3import {NftIdTuple} from './util/fetch';4import {expectTxFailure, getResourceById} 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';15import {requirePallets, Pallets} from '../deprecated-helpers/helpers';1617describe('integration test: add NFT resource', () => {18  const alice = '//Alice';19  const bob = '//Bob';20  const src = 'test-res-src';21  const metadata = 'test-res-metadata';22  const license = 'test-res-license';23  const thumb = 'test-res-thumb';2425  const nonexistentId = 99999;2627  let api: any;28  before(async function() {29    api = await getApiConnection();30    await requirePallets(this, [Pallets.RmrkCore]);31  });3233  it('add resource', async () => {34    const collectionIdAlice = await createCollection(35      api,36      alice,37      'test-metadata',38      null,39      'test-symbol',40    );4142    const nftAlice = await mintNft(43      api,44      alice,45      alice,46      collectionIdAlice,47      'nft-metadata',48    );4950    await addNftBasicResource(51      api,52      alice,53      'added',54      collectionIdAlice,55      nftAlice,56      src,57      metadata,58      license,59      thumb,60    );61  });6263  it('add a resource to the nested NFT', async () => {64    const collectionIdAlice = await createCollection(65      api,66      alice,67      'test-metadata',68      null,69      'test-symbol',70    );7172    const parentNftId = await mintNft(api, alice, alice, collectionIdAlice, 'parent-nft-metadata');73    const childNftId = await mintNft(api, alice, alice, collectionIdAlice, 'child-nft-metadata');7475    const newOwnerNFT: NftIdTuple = [collectionIdAlice, parentNftId];7677    await sendNft(api, 'sent', alice, collectionIdAlice, childNftId, newOwnerNFT);7879    await addNftBasicResource(80      api,81      alice,82      'added',83      collectionIdAlice,84      childNftId,85      src,86      metadata,87      license,88      thumb,89    );90  });9192  it('add multiple resources', async () => {93    const collectionIdAlice = await createCollection(94      api,95      alice,96      'test-metadata',97      null,98      'test-symbol',99    );100101    const nftAlice = await mintNft(102      api,103      alice,104      alice,105      collectionIdAlice,106      'nft-metadata',107    );108109    const baseId = 42;110    const slotId = 10;111    const parts = [0, 5, 2];112113    const resourcesInfo = [];114    const resourceNum = 4;115116    const checkResource = async (resource: ResourceInfo, resType: string, expectedId: number, expected: {117      src: string,118      metadata: string,119      license: string,120      thumb: string121    }) => {122123      // FIXME A workaround. It seems it is a PolkadotJS bug.124      // All of the following are `false`.125      //126      // console.log('>>> basic:', resource.resource.isBasic);127      // console.log('>>> composable:', resource.resource.isComposable);128      // console.log('>>> slot:', resource.resource.isSlot);129      const resourceJson = (resource.resource.toHuman() as any)[resType];130131      expect(resource.id.toNumber(), 'Error: Invalid resource Id')132        .to.be.eq(expectedId);133134      expect(resourceJson.src, 'Error: Invalid resource src')135        .to.be.eq(expected.src);136      expect(resourceJson.metadata, 'Error: Invalid resource metadata')137        .to.be.eq(expected.metadata);138      expect(resourceJson.license, 'Error: Invalid resource license')139        .to.be.eq(expected.license);140      expect(resourceJson.thumb, 'Error: Invalid resource thumb')141        .to.be.eq(expected.thumb);142    };143144    for (let i = 0; i < resourceNum; i++) {145      resourcesInfo.push({146        src: src + 'r-' + i,147        metadata: metadata + 'r-' + i,148        license: license + 'r-' + i,149        thumb: thumb + 'r-' + i,150      });151    }152153    const firstBasicResourceId = await addNftBasicResource(154      api,155      alice,156      'added',157      collectionIdAlice,158      nftAlice,159      resourcesInfo[0].src,160      resourcesInfo[0].metadata,161      resourcesInfo[0].license,162      resourcesInfo[0].thumb,163    );164165    const secondBasicResourceId = await addNftBasicResource(166      api,167      alice,168      'added',169      collectionIdAlice,170      nftAlice,171      resourcesInfo[1].src,172      resourcesInfo[1].metadata,173      resourcesInfo[1].license,174      resourcesInfo[1].thumb,175    );176177    const composableResourceId = await addNftComposableResource(178      api,179      alice,180      'added',181      collectionIdAlice,182      nftAlice,183      parts,184      baseId,185      resourcesInfo[2].src,186      resourcesInfo[2].metadata,187      resourcesInfo[2].license,188      resourcesInfo[2].thumb,189    );190191    const slotResourceId = await addNftSlotResource(192      api,193      alice,194      'added',195      collectionIdAlice,196      nftAlice,197      baseId,198      slotId,199      resourcesInfo[3].src,200      resourcesInfo[3].metadata,201      resourcesInfo[3].license,202      resourcesInfo[3].thumb,203    );204205    const firstResource = await getResourceById(api, collectionIdAlice, nftAlice, firstBasicResourceId);206    await checkResource(firstResource, 'Basic', firstBasicResourceId, resourcesInfo[0]);207208    const secondResource = await getResourceById(api, collectionIdAlice, nftAlice, secondBasicResourceId);209    await checkResource(secondResource, 'Basic', secondBasicResourceId, resourcesInfo[1]);210211    const composableResource = await getResourceById(api, collectionIdAlice, nftAlice, composableResourceId);212    await checkResource(composableResource, 'Composable', composableResourceId, resourcesInfo[2]);213214    const slotResource = await getResourceById(api, collectionIdAlice, nftAlice, slotResourceId);215    await checkResource(slotResource, 'Slot', slotResourceId, resourcesInfo[3]);216  });217218  it('[negative]: unable to add a resource to the non-existing NFT', async () => {219    const collectionIdAlice = await createCollection(220      api,221      alice,222      'test-metadata',223      null,224      'test-symbol',225    );226227    const tx = addNftBasicResource(228      api,229      alice,230      'added',231      collectionIdAlice,232      nonexistentId,233      src,234      metadata,235      license,236      thumb,237    );238  239    await expectTxFailure(/rmrkCore\.NoAvailableNftId/, tx);240  });241242  it('[negative]: unable to add a resource by a not-an-owner user', async () => {243    const collectionIdAlice = await createCollection(244      api,245      alice,246      'test-metadata',247      null,248      'test-symbol',249    );250251    const nftAlice = await mintNft(252      api,253      alice,254      alice,255      collectionIdAlice,256      'nft-metadata',257    );258259    const tx = addNftBasicResource(260      api,261      bob,262      'added',263      collectionIdAlice,264      nftAlice,265      src,266      metadata,267      license,268      thumb,269    );270  271    await expectTxFailure(/rmrkCore\.NoPermission/, tx);272  });273274  it('[negative]: unable to add a resource to the nested NFT if it isnt root owned by the caller', async () => {275    const collectionIdAlice = await createCollection(276      api,277      alice,278      'test-metadata',279      null,280      'test-symbol',281    );282283    const parentNftId = await mintNft(api, alice, alice, collectionIdAlice, 'parent-nft-metadata');284    const childNftId = await mintNft(api, alice, alice, collectionIdAlice, 'child-nft-metadata');285286    const newOwnerNFT: NftIdTuple = [collectionIdAlice, parentNftId];287288    await sendNft(api, 'sent', alice, collectionIdAlice, childNftId, newOwnerNFT);289290    const tx = addNftBasicResource(291      api,292      bob,293      'added',294      collectionIdAlice,295      childNftId,296      src,297      metadata,298      license,299      thumb,300    );301    302    await expectTxFailure(/rmrkCore\.NoPermission/, tx);303  });304305  it('accept resource', async () => {306    const collectionIdBob = await createCollection(307      api,308      bob,309      'test-metadata',310      null,311      'test-symbol',312    );313314    const nftAlice = await mintNft(315      api,316      bob,317      alice,318      collectionIdBob,319      'nft-metadata',320    );321322    const resourceId = await addNftBasicResource(323      api,324      bob,325      'pending',326      collectionIdBob,327      nftAlice,328      src,329      metadata,330      license,331      thumb,332    );333334    await acceptNftResource(api, alice, collectionIdBob, nftAlice, resourceId);335  });336337  it('[negative]: unable to accept a non-existing resource', async () => {338    const collectionIdBob = await createCollection(339      api,340      bob,341      'test-metadata',342      null,343      'test-symbol',344    );345346    const nftAlice = await mintNft(347      api,348      bob,349      alice,350      collectionIdBob,351      'nft-metadata',352    );353354    const tx = acceptNftResource(api, alice, collectionIdBob, nftAlice, nonexistentId);355    await expectTxFailure(/rmrkCore\.ResourceDoesntExist/, tx);356  });357358  it('[negative]: unable to accept a resource by a not-an-NFT-owner user', async () => {359    const collectionIdBob = await createCollection(360      api,361      bob,362      'test-metadata',363      null,364      'test-symbol',365    );366367    const nftAlice = await mintNft(368      api,369      bob,370      alice,371      collectionIdBob,372      'nft-metadata',373    );374375    const resourceId = await addNftBasicResource(376      api,377      bob,378      'pending',379      collectionIdBob,380      nftAlice,381      src,382      metadata,383      license,384      thumb,385    );386387    const tx = acceptNftResource(api, bob, collectionIdBob, nftAlice, resourceId);388389    await expectTxFailure(/rmrkCore\.NoPermission/, tx);390  });391392  it('[negative]: unable to accept a resource to a non-target NFT', async () => {393    const collectionIdBob = await createCollection(394      api,395      bob,396      'test-metadata',397      null,398      'test-symbol',399    );400401    const nftAlice = await mintNft(402      api,403      bob,404      alice,405      collectionIdBob,406      'nft-metadata',407    );408409    const wrongNft = await mintNft(410      api,411      bob,412      alice,413      collectionIdBob,414      'nft-metadata',415    );416    417    const resourceId = await addNftBasicResource(418      api,419      bob,420      'pending',421      collectionIdBob,422      nftAlice,423      src,424      metadata,425      license,426      thumb,427    );428429    const tx = acceptNftResource(api, bob, collectionIdBob, wrongNft, resourceId);430431    await expectTxFailure(/rmrkCore\.ResourceDoesntExist/, tx);432  });433434435  after(() => {436    api.disconnect();437  });438});