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

difftreelog

source

js-packages/tests/src/sub/nesting/unnesting.negative.test.ts4.0 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import type {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, usingPlaygrounds} from '../../util/index.js';19import {CrossAccountId} from '@unique/playgrounds/src/unique.js';2021describe('Negative Test: Unnesting', () => {22  let alice: IKeyringPair;23  let bob: IKeyringPair;2425  before(async () => {26    await usingPlaygrounds(async (helper, privateKey) => {27      const donor = await privateKey({url: import.meta.url});28      [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor);29    });30  });3132  // TODO: make this test a bit more generic33  itSub('Admin (NFT): disallows an Admin to unnest someone else\'s token', async ({helper}) => {34    const collection = await helper.nft.mintCollection(alice, {limits: {ownerCanTransfer: true}, permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});35    //await collection.addAdmin(alice, {Substrate: bob.address});36    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});37    await collection.addToAllowList(alice, {Substrate: bob.address});38    await collection.addToAllowList(alice, targetToken.nestingAccount());3940    // Try to nest somebody else's token41    const newToken = await collection.mintToken(bob);42    await expect(newToken.nest(alice, targetToken))43      .to.be.rejectedWith(/common\.NoPermission/);4445    // Try to unnest a token belonging to someone else as collection admin46    const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());47    await expect(nestedToken.unnest(alice, targetToken, {Substrate: bob.address}))48      .to.be.rejectedWith(/common\.AddressNotInAllowlist/);4950    expect(await targetToken.getChildren()).to.be.length(1);51    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});52    expect(await nestedToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));53  });5455  [56    {mode: 'ft' as const},57    {mode: 'native ft' as const},58  ].map(md => [59    {mode: md.mode, restrictedMode: true},60    {mode: md.mode, restrictedMode: false},61  ].map(testCase => {62    itSub(`Fungible: disallows a non-Owner to unnest someone else's token [${testCase.mode}${testCase.restrictedMode ? ' (Restricted nesting)' : ''}]`, async ({helper}) => {63      const collectionNFT = await helper.nft.mintCollection(alice);64      const collectionFT = await (65        testCase.mode === 'ft'66          ? helper.ft.mintCollection(alice)67          : helper.ft.getCollectionObject(0)68      );69      const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});7071      await collectionNFT.setPermissions(alice, {nesting: {72        collectionAdmin: true, tokenOwner: true, restricted: testCase.restrictedMode ? [collectionFT.collectionId] : null,73      }});7475      // Nest some tokens as Alice into Bob's token76      await (77        testCase.mode === 'ft'78          ? collectionFT.mint(alice, 5n, targetToken.nestingAccount())79          : collectionFT.transfer(alice, targetToken.nestingAccount(), 5n)80      );8182      // Try to pull it out as Alice still83      await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))84        .to.be.rejectedWith(/common\.ApprovedValueTooLow/);85      expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);86    });87  }));88});