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

difftreelog

Tests refactoring WIP

Max Andreev2023-01-20parent: #ba92b38.patch.diff
in: master

4 files changed

modifiedtests/src/nesting/nest.test.tsdiffbeforeafterboth
--- a/tests/src/nesting/nest.test.ts
+++ b/tests/src/nesting/nest.test.ts
@@ -28,6 +28,7 @@
     });
   });
 
+  /// TODO move
   itSub('Performs the full suite: bundles a token, transfers, and unnests', async ({helper}) => {
     const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
     const targetToken = await collection.mintToken(alice);
@@ -56,77 +57,6 @@
     await newToken.unnest(bob, targetToken, {Substrate: bob.address});
     expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});
     expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});
-  });
-
-  itSub('Transfers an already bundled token', async ({helper}) => {
-    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
-    const tokenA = await collection.mintToken(alice);
-    const tokenB = await collection.mintToken(alice);
-
-    // Create a nested token
-    const tokenC = await collection.mintToken(alice, tokenA.nestingAccount());
-    expect(await tokenC.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());
-
-    // Transfer the nested token to another token
-    await expect(tokenC.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount())).to.be.fulfilled;
-    expect(await tokenC.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
-    expect(await tokenC.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());
-  });
-
-  itSub('Checks token children', async ({helper}) => {
-    const collectionA = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
-    const collectionB = await helper.ft.mintCollection(alice);
-
-    const targetToken = await collectionA.mintToken(alice);
-    expect((await targetToken.getChildren()).length).to.be.equal(0, 'Children length check at creation');
-
-    // Create a nested NFT token
-    const tokenA = await collectionA.mintToken(alice, targetToken.nestingAccount());
-    expect(await targetToken.getChildren()).to.have.deep.members([
-      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
-    ], 'Children contents check at nesting #1').and.be.length(1, 'Children length check at nesting #1');
-
-    // Create then nest
-    const tokenB = await collectionA.mintToken(alice);
-    await tokenB.nest(alice, targetToken);
-    expect(await targetToken.getChildren()).to.have.deep.members([
-      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
-      {tokenId: tokenB.tokenId, collectionId: collectionA.collectionId},
-    ], 'Children contents check at nesting #2').and.be.length(2, 'Children length check at nesting #2');
-
-    // Move token B to a different user outside the nesting tree
-    await tokenB.unnest(alice, targetToken, {Substrate: bob.address});
-    expect(await targetToken.getChildren()).to.be.have.deep.members([
-      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
-    ], 'Children contents check at nesting #3 (unnesting)').and.be.length(1, 'Children length check at nesting #3 (unnesting)');
-
-    // Create a fungible token in another collection and then nest
-    await collectionB.mint(alice, 10n);
-    await collectionB.transfer(alice, targetToken.nestingAccount(), 2n);
-    expect(await targetToken.getChildren()).to.be.have.deep.members([
-      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
-      {tokenId: 0, collectionId: collectionB.collectionId},
-    ], 'Children contents check at nesting #4 (from another collection)')
-      .and.be.length(2, 'Children length check at nesting #4 (from another collection)');
-
-    // Move part of the fungible token inside token A deeper in the nesting tree
-    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);
-    expect(await targetToken.getChildren()).to.be.have.deep.members([
-      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
-      {tokenId: 0, collectionId: collectionB.collectionId},
-    ], 'Children contents check at nesting #5 (deeper)').and.be.length(2, 'Children length check at nesting #5 (deeper)');
-    expect(await tokenA.getChildren()).to.be.have.deep.members([
-      {tokenId: 0, collectionId: collectionB.collectionId},
-    ], 'Children contents check at nesting #5.5 (deeper)').and.be.length(1, 'Children length check at nesting #5.5 (deeper)');
-
-    // Move the remaining part of the fungible token inside token A deeper in the nesting tree
-    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);
-    expect(await targetToken.getChildren()).to.be.have.deep.members([
-      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
-    ], 'Children contents check at nesting #6 (deeper)').and.be.length(1, 'Children length check at nesting #6 (deeper)');
-    expect(await tokenA.getChildren()).to.be.have.deep.members([
-      {tokenId: 0, collectionId: collectionB.collectionId},
-    ], 'Children contents check at nesting #6.5 (deeper)').and.be.length(1, 'Children length check at nesting #6.5 (deeper)');
   });
 });
 
modifiedtests/src/sub/nesting/common.test.tsdiffbeforeafterboth
17import {IKeyringPair} from '@polkadot/types/types';17import {IKeyringPair} from '@polkadot/types/types';
18import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';18import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';
19import {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';19import {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';
20import {itEth} from '../../eth/util';
2120
22describe('Nesting', () => {
23 let alice: IKeyringPair;21let alice: IKeyringPair;
2422
25 before(async () => {23before(async () => {
26 await usingPlaygrounds(async (helper, privateKey) => {24 await usingPlaygrounds(async (helper, privateKey) => {
27 const donor = await privateKey({filename: __filename});25 const donor = await privateKey({filename: __filename});
28 [alice] = await helper.arrange.createAccounts([50n], donor);26 [alice] = await helper.arrange.createAccounts([100n], donor);
29 });
30 });27 });
28});
3129
32 [30[
33 {mode: 'nft' as const, requiredPallets: []},31 {mode: 'nft' as const, requiredPallets: []},
34 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},32 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
35 ].map(testCase => {33].map(testCase => {
36 itSub.ifWithPallets(`Owner can nest ${testCase.mode.toUpperCase()}`, testCase.requiredPallets, async ({helper}) => {34 itSub.ifWithPallets(`Owner can nest ${testCase.mode.toUpperCase()} in NFT`, testCase.requiredPallets, async ({helper}) => {
37 // Only NFT allows nesting, permissions should be set:
38 const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
39 const targetToken = await aliceNFTCollection.mintToken(alice);
40
41 const collectionForNesting = await helper[testCase.mode].mintCollection(alice);
42
43 // 1. Alice can immediately create nested token:
44 const nestedToken1 = testCase.mode === 'nft'
45 ? await (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())
46 : await (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());
47 expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
48 expect(await nestedToken1.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
49
50 // 2. Alice can mint and nest token:
51 const nestedToken2 = await collectionForNesting.mintToken(alice);
52 await nestedToken2.nest(alice, targetToken);
53 expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
54 expect(await nestedToken2.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
55 });
56 });
57
58 itSub('Owner can nest FT', async ({helper}) => {
59 // Only NFT allows nesting, permissions should be set:35 // Only NFT allows nesting, permissions should be set:
60 const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});36 const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
61 const targetToken = await aliceNFTCollection.mintToken(alice);37 const targetToken = await aliceNFTCollection.mintToken(alice);
6238
63 const collectionForNesting = await helper.ft.mintCollection(alice);39 const collectionForNesting = await helper[testCase.mode].mintCollection(alice);
6440
65 // 1. Alice can immediately create nested tokens:41 // 1. Alice can immediately create nested token:
66 await collectionForNesting.mint(alice, 100n, targetToken.nestingAccount());42 const nestedToken1 = testCase.mode === 'nft'
43 ? await (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())
44 : await (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());
67 expect(await collectionForNesting.getTop10Owners()).deep.eq([targetToken.nestingAccount().toLowerCase()]);45 expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
68 expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(100n);46 expect(await nestedToken1.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
6947
70 // 2. Alice can mint and nest token:48 // 2. Alice can mint and nest token:
71 await collectionForNesting.mint(alice, 100n);49 const nestedToken2 = await collectionForNesting.mintToken(alice);
72 await collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n);50 await nestedToken2.nest(alice, targetToken);
51 expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
73 expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(150n);52 expect(await nestedToken2.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
74 });53 });
75});54});
7655
77describe('Nesting negative', () => {
78 let alice: IKeyringPair;
7956
80 before(async () => {57itSub('Owner can nest FT in NFT', async ({helper}) => {
81 await usingPlaygrounds(async (helper, privateKey) => {
82 const donor = await privateKey({filename: __filename});58 // Only NFT allows nesting, permissions should be set:
83 [alice] = await helper.arrange.createAccounts([50n], donor);59 const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
84 });
85 });60 const targetToken = await aliceNFTCollection.mintToken(alice);
8661
87 [62 const collectionForNesting = await helper.ft.mintCollection(alice);
88 {mode: 'nft' as const, requiredPallets: []},
89 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
90 ].map(testCase => {
91 itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting not allowed`, testCase.requiredPallets, async ({helper}) => {
92 // Create default collection, permissions are not set:
93 const aliceNFTCollection = await helper.nft.mintCollection(alice);
94 const targetToken = await aliceNFTCollection.mintToken(alice);
9563
96 const collectionForNesting = await helper[testCase.mode].mintCollection(alice);64 // 1. Alice can immediately create nested tokens:
65 await collectionForNesting.mint(alice, 100n, targetToken.nestingAccount());
66 expect(await collectionForNesting.getTop10Owners()).deep.eq([targetToken.nestingAccount().toLowerCase()]);
67 expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(100n);
9768
98 // 1. Alice cannot create immediately nested tokens:69 // 2. Alice can mint and nest token:
99 const nestingTx = testCase.mode === 'nft'70 await collectionForNesting.mint(alice, 100n);
100 ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())71 await collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n);
101 : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());72 expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(150n);
102 await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');73});
10374
104 // 2. Alice cannot mint and nest token:
105 const nestedToken2 = await collectionForNesting.mintToken(alice);
106 await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');
107 });
108 });
10975
110 itSub('Owner cannot nest FT if nesting not allowed', async ({helper}) => {76itSub.ifWithPallets('Owner can transferFrom nested tokens', [Pallets.ReFungible], async ({helper}) => {
111 // Create default collection, permissions are not set:77 const collectionToNest = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
112 const aliceNFTCollection = await helper.nft.mintCollection(alice);78 const tokenA = await collectionToNest.mintToken(alice);
113 const targetToken = await aliceNFTCollection.mintToken(alice);79 const tokenB = await collectionToNest.mintToken(alice);
11480
115 const collectionForNesting = await helper.ft.mintCollection(alice);81 // Create a nested token
82 const nftCollectionToBeNested = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
83 const rftCollectionToBeNested = await helper.rft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
84 const ftCollectionToBeNested = await helper.ft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
11685
117 // 1. Alice cannot create immediately nested tokens:86 const nestedNFT = await nftCollectionToBeNested.mintToken(alice, tokenA.nestingAccount());
87 const nestedRFT = await rftCollectionToBeNested.mintToken(alice, 100n, tokenA.nestingAccount());
118 await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');88 const _nestedFT = await ftCollectionToBeNested.mint(alice, 100n, tokenA.nestingAccount());
89 expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());
90 expect(await nestedRFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());
91 expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(100n);
11992
120 // 2. Alice can mint and nest token:93 // Transfer the nested token to another token
121 await collectionForNesting.mint(alice, 100n);94 await nestedNFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount());
122 await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');95 await nestedRFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);
123 });96 await ftCollectionToBeNested.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);
12497
125 itSub.ifWithPallets('Cannot nest to a future collection', [Pallets.ReFungible], async ({helper}) => {98 expect(await nestedNFT.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
126 const nonExistingCollectionId = await helper.collection.getTotalCount() + 1000;99 expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());
127 const futureToken = helper.nft.getTokenObject(nonExistingCollectionId, 1);
128100
129 const nftCollectionForNesting = await helper.nft.mintCollection(alice);101 expect(await nestedRFT.getBalance(tokenB.nestingAccount())).to.equal(25n);
130 const rftCollectionForNesting = await helper.rft.mintCollection(alice);
131 const ftCollectionForNesting = await helper.ft.mintCollection(alice);102 expect(await nestedRFT.getBalance(tokenA.nestingAccount())).to.equal(75n);
132103
133 // 1. Alice cannot create nested token to future token104 expect(await ftCollectionToBeNested.getBalance(tokenB.nestingAccount())).to.equal(25n);
134 await expect(nftCollectionForNesting.mintToken(alice, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');105 expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(75n);
135 await expect(rftCollectionForNesting.mintToken(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');
136 await expect(ftCollectionForNesting.mint(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');
137
138 // 2. Alice cannot mint and nest token:
139 const nft = await nftCollectionForNesting.mintToken(alice);
140 const rft = await rftCollectionForNesting.mintToken(alice, 100n);
141 const _ft = await ftCollectionForNesting.mint(alice, 100n);
142 await expect(nft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');
143 await expect(rft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');
144 await expect(ftCollectionForNesting.transfer(alice, futureToken.nestingAccount(), 50n)).to.be.rejectedWith('CollectionNotFound');
145 });
146
147 itSub.ifWithPallets('Cannot nest to a future token in a NFT collection', [Pallets.ReFungible], async ({helper}) => {
148 const {collectionId} = await helper.nft.mintCollection(alice);
149 // To avoid UserIsNotAllowedToNest error
150 await helper.collection.setPermissions(alice, collectionId, {nesting: {collectionAdmin: true}});
151 const futureToken = helper.nft.getTokenObject(collectionId, 1);
152
153 const nftCollectionForNesting = await helper.nft.mintCollection(alice);
154 const rftCollectionForNesting = await helper.rft.mintCollection(alice);
155 const ftCollectionForNesting = await helper.ft.mintCollection(alice);
156
157 // 1. Alice cannot create nested token to future token
158 await expect(nftCollectionForNesting.mintToken(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');
159 await expect(rftCollectionForNesting.mintToken(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');
160 await expect(ftCollectionForNesting.mint(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');
161
162 // 2. Alice cannot mint and nest token:
163 const nft = await nftCollectionForNesting.mintToken(alice);
164 const rft = await rftCollectionForNesting.mintToken(alice, 100n);
165 const _ft = await ftCollectionForNesting.mint(alice, 100n);
166 await expect(nft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');
167 await expect(rft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');
168 await expect(ftCollectionForNesting.transfer(alice, futureToken.nestingAccount(), 50n)).to.be.rejectedWith('TokenNotFound');
169 });
170
171 itEth.ifWithPallets('Cannot nest to collection address', [Pallets.ReFungible], async({helper}) => {
172 const existingCollection = await helper.nft.mintCollection(alice);
173 const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);
174 const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);
175
176 const nftCollectionForNesting = await helper.nft.mintCollection(alice);
177
178 // 1. Alice cannot create nested token to collection address
179 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
180 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
181
182 // 2. Alice cannot mint and nest token to collection address:
183 const nft = await nftCollectionForNesting.mintToken(alice);
184 await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
185 await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
186 });
187
188 itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {
189 // Create default collection, permissions are not set:
190 const rftCollection = await helper.rft.mintCollection(alice);
191 const ftCollection = await helper.ft.mintCollection(alice);
192
193 const rftToken = await rftCollection.mintToken(alice);
194 const _ftToken = await ftCollection.mint(alice, 100n);
195
196 const collectionForNesting = await helper.nft.mintCollection(alice);
197
198 // 1. Alice cannot create immediately nested tokens:
199 await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');
200 await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');
201
202 // 2. Alice cannot mint and nest token:
203 const nestedToken2 = await collectionForNesting.mintToken(alice);
204 await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');
205 await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');
206 });
207});106});
208107
addedtests/src/sub/nesting/e2e.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/sub/nesting/e2e.test.ts
@@ -0,0 +1,104 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+import {IKeyringPair} from '@polkadot/types/types';
+import {expect, itSub, usingPlaygrounds} from '../../util';
+
+describe('Composite nesting tests', () => {
+  let alice: IKeyringPair;
+  let bob: IKeyringPair;
+
+  before(async () => {
+    await usingPlaygrounds(async (helper, privateKey) => {
+      const donor = await privateKey({filename: __filename});
+      [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);
+    });
+  });
+
+  itSub('Checks token children e2e', async ({helper}) => {
+    const collectionA = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
+    const collectionB = await helper.ft.mintCollection(alice);
+    const collectionC = await helper.rft.mintCollection(alice);
+
+    const targetToken = await collectionA.mintToken(alice);
+    expect((await targetToken.getChildren()).length).to.be.equal(0, 'Children length check at creation');
+
+    // Create a nested NFT token
+    const tokenA = await collectionA.mintToken(alice, targetToken.nestingAccount());
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+    ]).and.has.length(1);
+
+    // Create then nest
+    const tokenB = await collectionA.mintToken(alice);
+    await tokenB.nest(alice, targetToken);
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+      {tokenId: tokenB.tokenId, collectionId: collectionA.collectionId},
+    ]).and.has.length(2);
+
+    // Move token B to a different user outside the nesting tree
+    await tokenB.unnest(alice, targetToken, {Substrate: bob.address});
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+    ]).and.has.length(1);
+
+    // Create a fungible token in another collection and then nest
+    await collectionB.mint(alice, 10n);
+    await collectionB.transfer(alice, targetToken.nestingAccount(), 2n);
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+      {tokenId: 0, collectionId: collectionB.collectionId},
+    ]).and.has.length(2);
+
+    // Create a refungible token in another collection and then nest
+    const tokenC = await collectionC.mintToken(alice, 10n);
+    await tokenC.transfer(alice, targetToken.nestingAccount(), 2n);
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+      {tokenId: 0, collectionId: collectionB.collectionId},
+      {tokenId: tokenC.tokenId, collectionId: collectionC.collectionId},
+    ]).and.has.length(3);
+
+    // Burn all nested pieces
+    await tokenC.burnFrom(alice, targetToken.nestingAccount(), 2n);
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+      {tokenId: 0, collectionId: collectionB.collectionId},
+    ])
+      .and.has.length(2);
+
+    // Move part of the fungible token inside token A deeper in the nesting tree
+    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);
+    expect(await targetToken.getChildren()).to.be.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+      {tokenId: 0, collectionId: collectionB.collectionId},
+    ]).and.has.length(2);
+    // Nested token also has children now:
+    expect(await tokenA.getChildren()).to.have.deep.members([
+      {tokenId: 0, collectionId: collectionB.collectionId},
+    ]).and.has.length(1);
+
+    // Move the remaining part of the fungible token inside token A deeper in the nesting tree
+    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+    ]).and.has.length(1);
+    expect(await tokenA.getChildren()).to.have.deep.members([
+      {tokenId: 0, collectionId: collectionB.collectionId},
+    ]).and.has.length(1);
+  });
+});
addedtests/src/sub/nesting/negative.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/sub/nesting/negative.test.ts
@@ -0,0 +1,142 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+import {IKeyringPair} from '@polkadot/types/types';
+import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';
+import {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';
+import {itEth} from '../../eth/util';
+
+let alice: IKeyringPair;
+
+before(async () => {
+  await usingPlaygrounds(async (helper, privateKey) => {
+    const donor = await privateKey({filename: __filename});
+    [alice] = await helper.arrange.createAccounts([50n], donor);
+  });
+});
+
+[
+  {mode: 'nft' as const, requiredPallets: []},
+  {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
+].map(testCase => {
+  itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting not allowed`, testCase.requiredPallets, async ({helper}) => {
+    // Create default collection, permissions are not set:
+    const aliceNFTCollection = await helper.nft.mintCollection(alice);
+    const targetToken = await aliceNFTCollection.mintToken(alice);
+
+    const collectionForNesting = await helper[testCase.mode].mintCollection(alice);
+
+    // 1. Alice cannot create immediately nested tokens:
+    const nestingTx = testCase.mode === 'nft'
+      ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())
+      : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());
+    await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');
+
+    // 2. Alice cannot mint and nest token:
+    const nestedToken2 = await collectionForNesting.mintToken(alice);
+    await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');
+  });
+});
+
+itSub('Owner cannot nest FT if nesting not allowed', async ({helper}) => {
+  // Create default collection, permissions are not set:
+  const aliceNFTCollection = await helper.nft.mintCollection(alice);
+  const targetToken = await aliceNFTCollection.mintToken(alice);
+
+  const collectionForNesting = await helper.ft.mintCollection(alice);
+
+  // 1. Alice cannot create immediately nested tokens:
+  await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');
+
+  // 2. Alice can mint and nest token:
+  await collectionForNesting.mint(alice, 100n);
+  await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');
+});
+
+itSub.ifWithPallets('Cannot nest in non existing token', [Pallets.ReFungible], async ({helper}) => {
+  const collection = await helper.nft.mintCollection(alice);
+  // To avoid UserIsNotAllowedToNest error
+  await helper.collection.setPermissions(alice, collection.collectionId, {nesting: {collectionAdmin: true}});
+
+  // The list of non-existing tokens:
+  const tokenFromNonExistingCollection = helper.nft.getTokenObject(9999999, 1);
+  const tokenBurnt = await collection.mintToken(alice);
+  await tokenBurnt.burn(alice);
+  const tokenNotMintedYet = helper.nft.getTokenObject(collection.collectionId, 2);
+
+  // The list of collections to nest tokens from:
+  const nftCollectionForNesting = await helper.nft.mintCollection(alice);
+  const rftCollectionForNesting = await helper.rft.mintCollection(alice);
+  const ftCollectionForNesting = await helper.ft.mintCollection(alice);
+
+  const testCases = [
+    {token: tokenFromNonExistingCollection, error: 'CollectionNotFound'},
+    {token: tokenBurnt, error: 'TokenNotFound'},
+    {token: tokenNotMintedYet, error: 'TokenNotFound'},
+  ];
+
+  for(const testCase of testCases) {
+    // 1. Alice cannot create nested token to non-existing token
+    await expect(nftCollectionForNesting.mintToken(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
+    await expect(rftCollectionForNesting.mintToken(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
+    await expect(ftCollectionForNesting.mint(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
+
+    // 2. Alice cannot mint and nest token:
+    const nft = await nftCollectionForNesting.mintToken(alice);
+    const rft = await rftCollectionForNesting.mintToken(alice, 100n);
+    const _ft = await ftCollectionForNesting.mint(alice, 100n);
+    await expect(nft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
+    await expect(rft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
+    await expect(ftCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);
+  }
+});
+
+itEth.ifWithPallets('Cannot nest to collection address', [Pallets.ReFungible], async({helper}) => {
+  const existingCollection = await helper.nft.mintCollection(alice);
+  const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);
+  const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);
+
+  const nftCollectionForNesting = await helper.nft.mintCollection(alice);
+
+  // 1. Alice cannot create nested token to collection address
+  await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
+  await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
+
+  // 2. Alice cannot mint and nest token to collection address:
+  const nft = await nftCollectionForNesting.mintToken(alice);
+  await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
+  await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
+});
+
+itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {
+  // Create default collection, permissions are not set:
+  const rftCollection = await helper.rft.mintCollection(alice);
+  const ftCollection = await helper.ft.mintCollection(alice);
+
+  const rftToken = await rftCollection.mintToken(alice);
+  const _ftToken = await ftCollection.mint(alice, 100n);
+
+  const collectionForNesting = await helper.nft.mintCollection(alice);
+
+  // 1. Alice cannot create immediately nested tokens:
+  await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');
+  await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');
+
+  // 2. Alice cannot mint and nest token:
+  const nestedToken2 = await collectionForNesting.mintToken(alice);
+  await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');
+  await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');
+});