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

difftreelog

set limits test scenarios

kpozdnikin2020-12-31parent: #49640f3.patch.diff
in: master

5 files changed

modifiedtests/package.jsondiffbeforeafterboth
19 "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",19 "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",
20 "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",20 "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",
21 "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",21 "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",
22 "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",
22 "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",23 "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",
23 "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"24 "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"
24 },25 },
addedtests/src/setCollectionLimits.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/setCollectionLimits.test.ts
@@ -0,0 +1,145 @@
+// https://unique-network.readthedocs.io/en/latest/jsapi.html#setchainlimits
+import { ApiPromise, Keyring } from '@polkadot/api';
+import { IKeyringPair } from '@polkadot/types/types';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import usingApi, { submitTransactionAsync } from './substrate/substrate-api';
+import { ICollectionInterface } from './types';
+import {
+  createCollectionExpectSuccess, getCreatedCollectionCount,
+  getCreateItemResult,
+  getDetailedCollectionInfo,
+} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+let alice: IKeyringPair;
+let bob: IKeyringPair;
+let collectionIdForTesting: number;
+
+const accountTokenOwnershipLimit = 0;
+const sponsoredDataSize = 0;
+const sponsoredMintSize = 0;
+const tokenLimit = 0;
+
+describe('hooks', () => {
+  before(async () => {
+    await usingApi(async () => {
+      const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri('//Alice');
+    });
+  });
+  it('choose or create collection for testing', async () => {
+    await usingApi(async () => {
+      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});
+      console.log('collectionIdForTesting', collectionIdForTesting);
+    });
+  });
+});
+
+describe('setCollectionLimits positive', () => {
+  let tx;
+  before(async () => {
+    await usingApi(async () => {
+      const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri('//Alice');
+    });
+  });
+  it('execute setCollectionLimits with predefined params ', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      tx = api.tx.nft.setCollectionLimits(
+        collectionIdForTesting,
+        {
+          accountTokenOwnershipLimit,
+          sponsoredDataSize,
+          sponsoredMintSize,
+          tokenLimit,
+        },
+      );
+      const events = await submitTransactionAsync(alice, tx);
+      const result = getCreateItemResult(events);
+      // tslint:disable-next-line:no-unused-expression
+      expect(result.success).to.be.true;
+    });
+  });
+  it('get collection limits defined in previous test', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;
+      expect(collectionInfo.Limits.AccountTokenOwnershipLimit.toNumber()).to.be.equal(accountTokenOwnershipLimit);
+      expect(collectionInfo.Limits.SponsoredMintSize.toNumber()).to.be.equal(sponsoredMintSize);
+      expect(collectionInfo.Limits.TokenLimit.toNumber()).to.be.equal(tokenLimit);
+      expect(collectionInfo.Limits.SponsorTimeout.toNumber()).to.be.equal(sponsoredDataSize);
+    });
+  });
+});
+
+describe('setCollectionLimits negative', () => {
+  let tx;
+  before(async () => {
+    await usingApi(async () => {
+      const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri('//Alice');
+      bob = keyring.addFromUri('//Bob');
+    });
+  });
+  it('execute setCollectionLimits for not exists collection', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const collectionCount = await getCreatedCollectionCount(api);
+      const nonExistedCollectionId = collectionCount + 1;
+      tx = api.tx.nft.setCollectionLimits(
+        nonExistedCollectionId,
+        {
+          accountTokenOwnershipLimit,
+          sponsoredDataSize,
+          sponsoredMintSize,
+          tokenLimit,
+        },
+      );
+      try {
+        await submitTransactionAsync(alice, tx);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
+    });
+  });
+  it('execute setCollectionLimits from user who is not owner of this collection', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      tx = api.tx.nft.setCollectionLimits(
+        collectionIdForTesting,
+        {
+          accountTokenOwnershipLimit,
+          sponsoredDataSize,
+          sponsoredMintSize,
+          tokenLimit,
+        },
+      );
+      try {
+        await submitTransactionAsync(bob, tx);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
+    });
+  });
+  it('execute setCollectionLimits with incorrect limits', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      tx = api.tx.nft.setCollectionLimits(
+        collectionIdForTesting,
+        {
+          accountTokenOwnershipLimit: 'awdawd',
+          sponsorTransferTimeout: 'awd',
+          sponsoredDataSize: '12312312312312312',
+          tokenLimit: '-100',
+        },
+      );
+      try {
+        await submitTransactionAsync(alice, tx);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
+    });
+  });
+});
modifiedtests/src/setSchemaVersion.test.tsdiffbeforeafterboth
--- a/tests/src/setSchemaVersion.test.ts
+++ b/tests/src/setSchemaVersion.test.ts
@@ -1,4 +1,4 @@
-// https://unique-network.readthedocs.io/en/latest/jsapi.html#setchainlimits
+// https://unique-network.readthedocs.io/en/latest/jsapi.html#setschemaversion
 import { ApiPromise, Keyring } from '@polkadot/api';
 import { IKeyringPair } from '@polkadot/types/types';
 import BN from 'bn.js';
@@ -6,7 +6,13 @@
 import chaiAsPromised from 'chai-as-promised';
 import usingApi, { submitTransactionAsync } from './substrate/substrate-api';
 import { ICollectionInterface } from './types';
-import { createCollectionExpectSuccess, destroyCollectionExpectSuccess, getCreateItemResult } from './util/helpers';
+import {
+  createCollectionExpectSuccess,
+  destroyCollectionExpectSuccess,
+  getCreatedCollectionCount,
+  getCreateItemResult,
+  getDetailedCollectionInfo,
+} from './util/helpers';
 
 chai.use(chaiAsPromised);
 const expect = chai.expect;
@@ -19,16 +25,6 @@
 2. Save just created collection id.
 3. Use this id for setSchemaVersion.
 */
-
-const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)
-  : Promise<ICollectionInterface | null> => {
-  return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;
-};
-
-const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {
-  // set global object - collectionsCount
-  return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();
-};
 
 describe('hooks', () => {
   before(async () => {
@@ -57,7 +53,7 @@
       tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Unique');
       const events = await submitTransactionAsync(alice, tx);
       const result = getCreateItemResult(events);
-      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);
+      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;
       // tslint:disable-next-line:no-unused-expression
       expect(result.success).to.be.true;
       // tslint:disable-next-line:no-unused-expression
@@ -72,7 +68,7 @@
       tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');
       const events = await submitTransactionAsync(alice, tx);
       const result = getCreateItemResult(events);
-      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);
+      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;
       // tslint:disable-next-line:no-unused-expression
       expect(result.success).to.be.true;
       // tslint:disable-next-line:no-unused-expression
modifiedtests/src/types.tsdiffbeforeafterboth
--- a/tests/src/types.ts
+++ b/tests/src/types.ts
@@ -7,14 +7,20 @@
   // constOnChainSchema
   Description: [BN, BN]; // utf16
   isReFungible: boolean;
+  Limits: {
+    AccountTokenOwnershipLimit: BN;
+    SponsoredMintSize: BN;
+    TokenLimit: BN;
+    SponsorTimeout: BN;
+  };
   MintMode: boolean;
   Mode: {
     Nft: null;
   };
   Name: [BN, BN]; // utf16
   OffchainSchema: [Uint8Array];
+  Owner: [Uint8Array];
   SchemaVersion: string;
-  Owner: [Uint8Array];
   // prefix
   // sponsor
   // tokenPrefix
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -10,11 +10,13 @@
 import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from "../substrate/substrate-api";
 import privateKey from '../substrate/privateKey';
 import { alicesPublicKey, nullPublicKey } from "../accounts";
-import { strToUTF16, utf16ToStr, hexToStr } from '../util/util';
-import { IKeyringPair } from "@polkadot/types/types";
+import { strToUTF16, utf16ToStr, hexToStr } from './util';
+import { IKeyringPair } from '@polkadot/types/types';
 import { BigNumber } from 'bignumber.js';
 import { Struct, Enum } from '@polkadot/types/codec';
 import { u128 } from '@polkadot/types/primitive';
+import { ICollectionInterface } from '../types';
+import BN from "bn.js";
 
 chai.use(chaiAsPromised);
 const expect = chai.expect;
@@ -399,3 +401,12 @@
   });
 }
 
+export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)
+  : Promise<ICollectionInterface | null> => {
+  return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;
+};
+
+export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {
+  // set global object - collectionsCount
+  return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();
+};