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 = async (resource: ResourceInfo, resType: string, expectedId: number, expected: {116 src: string,117 metadata: string,118 license: string,119 thumb: string120 }) => {121122 123 124 125 126 127 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(() => {435 after(async() => { await api.disconnect(); });436 });437});