git.delta.rocks / unique-network / refs/commits / 1813dc305c53

difftreelog

source

tests/src/rmrk/addResource.test.ts10.1 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";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 () => {28    api = await getApiConnection();29  });3031  it("add resource", async () => {32    const collectionIdAlice = await createCollection(33      api,34      Alice,35      "test-metadata",36      null,37      "test-symbol"38    );3940    const nftAlice = await mintNft(41      api,42      Alice,43      Alice,44      collectionIdAlice,45      "nft-metadata"46    );4748    await addNftBasicResource(49      api,50      Alice,51      "added",52      collectionIdAlice,53      nftAlice,54      src,55      metadata,56      license,57      thumb58    );59  });6061  it('add a resource to the nested NFT', async () => {62    const collectionIdAlice = await createCollection(63      api,64      Alice,65      "test-metadata",66      null,67      "test-symbol"68    );6970    const parentNftId = await mintNft(api, Alice, Alice, collectionIdAlice, "parent-nft-metadata");71    const childNftId = await mintNft(api, Alice, Alice, collectionIdAlice, "child-nft-metadata");7273    const newOwnerNFT: NftIdTuple = [collectionIdAlice, parentNftId];7475    await sendNft(api, "sent", Alice, collectionIdAlice, childNftId, newOwnerNFT);7677    await addNftBasicResource(78      api,79      Alice,80      "added",81      collectionIdAlice,82      childNftId,83      src,84      metadata,85      license,86      thumb87    );88  });8990  it('add multiple resources', async () => {91    const collectionIdAlice = await createCollection(92      api,93      Alice,94      "test-metadata",95      null,96      "test-symbol"97    );9899    const nftAlice = await mintNft(100      api,101      Alice,102      Alice,103      collectionIdAlice,104      "nft-metadata"105    );106107    const baseId = 42;108    const slotId = 10;109    const parts = [0, 5, 2];110111    let resourcesInfo = [];112    const resourceNum = 4;113114    const checkResource = async (resource: ResourceInfo, resType: string, expectedId: number, expected: {115      src: string,116      metadata: string,117      license: string,118      thumb: string119    }) => {120121      // FIXME A workaround. It seems it is a PolkadotJS bug.122      // All of the following are `false`.123      //124      // console.log('>>> basic:', resource.resource.isBasic);125      // console.log('>>> composable:', resource.resource.isComposable);126      // console.log('>>> slot:', resource.resource.isSlot);127      const resourceJson = (resource.resource.toHuman() as any)[resType];128129      expect(resource.id.toNumber(), 'Error: Invalid resource Id')130        .to.be.eq(expectedId);131132      expect(resourceJson.src, 'Error: Invalid resource src')133        .to.be.eq(expected.src);134      expect(resourceJson.metadata, 'Error: Invalid resource metadata')135        .to.be.eq(expected.metadata);136      expect(resourceJson.license, 'Error: Invalid resource license')137        .to.be.eq(expected.license);138      expect(resourceJson.thumb, 'Error: Invalid resource thumb')139        .to.be.eq(expected.thumb);140    };141142    for (let i = 0; i < resourceNum; i++) {143      resourcesInfo.push({144        src: src + 'r-' + i,145        metadata: metadata + 'r-' + i,146        license: license + 'r-' + i,147        thumb: thumb + 'r-' + i148      });149    }150151    const firstBasicResourceId = await addNftBasicResource(152      api,153      Alice,154      "added",155      collectionIdAlice,156      nftAlice,157      resourcesInfo[0].src,158      resourcesInfo[0].metadata,159      resourcesInfo[0].license,160      resourcesInfo[0].thumb161    );162163    const secondBasicResourceId = await addNftBasicResource(164      api,165      Alice,166      "added",167      collectionIdAlice,168      nftAlice,169      resourcesInfo[1].src,170      resourcesInfo[1].metadata,171      resourcesInfo[1].license,172      resourcesInfo[1].thumb173    );174175    const composableResourceId = await addNftComposableResource(176      api,177      Alice,178      "added",179      collectionIdAlice,180      nftAlice,181      parts,182      baseId,183      resourcesInfo[2].src,184      resourcesInfo[2].metadata,185      resourcesInfo[2].license,186      resourcesInfo[2].thumb,187    );188189    const slotResourceId = await addNftSlotResource(190      api,191      Alice,192      "added",193      collectionIdAlice,194      nftAlice,195      baseId,196      slotId,197      resourcesInfo[3].src,198      resourcesInfo[3].metadata,199      resourcesInfo[3].license,200      resourcesInfo[3].thumb201    );202203    const firstResource = await getResourceById(api, collectionIdAlice, nftAlice, firstBasicResourceId);204    await checkResource(firstResource, 'Basic', firstBasicResourceId, resourcesInfo[0]);205206    const secondResource = await getResourceById(api, collectionIdAlice, nftAlice, secondBasicResourceId);207    await checkResource(secondResource, 'Basic', secondBasicResourceId, resourcesInfo[1]);208209    const composableResource = await getResourceById(api, collectionIdAlice, nftAlice, composableResourceId);210    await checkResource(composableResource, 'Composable', composableResourceId, resourcesInfo[2]);211212    const slotResource = await getResourceById(api, collectionIdAlice, nftAlice, slotResourceId);213    await checkResource(slotResource, 'Slot', slotResourceId, resourcesInfo[3]);214  });215216  it('[negative]: unable to add a resource to the non-existing NFT', async () => {217    const collectionIdAlice = await createCollection(218      api,219      Alice,220      "test-metadata",221      null,222      "test-symbol"223    );224225    const tx = addNftBasicResource(226      api,227      Alice,228      "added",229      collectionIdAlice,230      nonexistentId,231      src,232      metadata,233      license,234      thumb235    );236  237    await expectTxFailure(/rmrkCore\.NoAvailableNftId/, tx);238  });239240  it('[negative]: unable to add a resource by a not-an-owner user', async () => {241    const collectionIdAlice = await createCollection(242      api,243      Alice,244      "test-metadata",245      null,246      "test-symbol"247    );248249    const nftAlice = await mintNft(250      api,251      Alice,252      Alice,253      collectionIdAlice,254      "nft-metadata"255    );256257    const tx = addNftBasicResource(258      api,259      Bob,260      "added",261      collectionIdAlice,262      nftAlice,263      src,264      metadata,265      license,266      thumb267    );268  269    await expectTxFailure(/rmrkCore\.NoPermission/, tx);270  });271272  it('[negative]: unable to add a resource to the nested NFT if it isnt root owned by the caller', async () => {273    const collectionIdAlice = await createCollection(274      api,275      Alice,276      "test-metadata",277      null,278      "test-symbol"279    );280281    const parentNftId = await mintNft(api, Alice, Alice, collectionIdAlice, "parent-nft-metadata");282    const childNftId = await mintNft(api, Alice, Alice, collectionIdAlice, "child-nft-metadata");283284    const newOwnerNFT: NftIdTuple = [collectionIdAlice, parentNftId];285286    await sendNft(api, "sent", Alice, collectionIdAlice, childNftId, newOwnerNFT);287288    const tx = addNftBasicResource(289      api,290      Bob,291      "added",292      collectionIdAlice,293      childNftId,294      src,295      metadata,296      license,297      thumb298    );299    300    await expectTxFailure(/rmrkCore\.NoPermission/, tx);301  });302303  it("accept resource", async () => {304    const collectionIdBob = await createCollection(305      api,306      Bob,307      "test-metadata",308      null,309      "test-symbol"310    );311312    const nftAlice = await mintNft(313        api,314        Bob,315        Alice,316        collectionIdBob,317        "nft-metadata"318    );319320    const resourceId = await addNftBasicResource(321      api,322      Bob,323      "pending",324      collectionIdBob,325      nftAlice,326      src,327      metadata,328      license,329      thumb330    );331332    await acceptNftResource(api, Alice, collectionIdBob, nftAlice, resourceId);333  });334335  it("[negative]: unable to accept a non-existing resource", async () => {336    const collectionIdBob = await createCollection(337      api,338      Bob,339      "test-metadata",340      null,341      "test-symbol"342    );343344    const nftAlice = await mintNft(345        api,346        Bob,347        Alice,348        collectionIdBob,349        "nft-metadata"350    );351352    const tx = acceptNftResource(api, Alice, collectionIdBob, nftAlice, nonexistentId);353    await expectTxFailure(/rmrkCore\.ResourceDoesntExist/, tx);354  });355356  it("[negative]: unable to accept a resource by a not-an-NFT-owner user", async () => {357    const collectionIdBob = await createCollection(358      api,359      Bob,360      "test-metadata",361      null,362      "test-symbol"363    );364365    const nftAlice = await mintNft(366        api,367        Bob,368        Alice,369        collectionIdBob,370        "nft-metadata"371    );372373    const resourceId = await addNftBasicResource(374      api,375      Bob,376      "pending",377      collectionIdBob,378      nftAlice,379      src,380      metadata,381      license,382      thumb383    );384385    const tx = acceptNftResource(api, Bob, collectionIdBob, nftAlice, resourceId);386387    await expectTxFailure(/rmrkCore\.NoPermission/, tx);388  });389390  it("[negative]: unable to accept a resource to a non-target NFT", async () => {391    const collectionIdBob = await createCollection(392      api,393      Bob,394      "test-metadata",395      null,396      "test-symbol"397    );398399    const nftAlice = await mintNft(400        api,401        Bob,402        Alice,403        collectionIdBob,404        "nft-metadata"405    );406407    const wrongNft = await mintNft(408        api,409        Bob,410        Alice,411        collectionIdBob,412        "nft-metadata"413    );414    415    const resourceId = await addNftBasicResource(416      api,417      Bob,418      "pending",419      collectionIdBob,420      nftAlice,421      src,422      metadata,423      license,424      thumb425    );426427    const tx = acceptNftResource(api, Bob, collectionIdBob, wrongNft, resourceId);428429    await expectTxFailure(/rmrkCore\.ResourceDoesntExist/, tx);430  });431432433  after(() => {434    api.disconnect();435  });436});