git.delta.rocks / unique-network / refs/commits / 2ace66dc79b6

difftreelog

Merge pull request #55 from usetech-llc/feature/NFTPAR-233

Greg Zaitsev2021-01-18parents: #07025d8 #d029160.patch.diff
in: master
addCollectionAdmin.test.ts

2 files changed

modifiedtests/package.jsondiffbeforeafterboth
before · tests/package.json
1{2  "name": "NftTests",3  "version": "1.0.0",4  "description": "Substrate Nft tests",5  "main": "",6  "devDependencies": {7    "@polkadot/dev": "^0.52.11",8    "@polkadot/ts": "^0.3.41",9    "@types/chai": "^4.2.12",10    "@types/chai-as-promised": "^7.1.3",11    "@types/mocha": "^8.0.3",12    "chai": "^4.2.0",13    "mocha": "^8.1.1",14    "ts-node": "^9.0.0",15    "tslint": "^5.20.1",16    "typescript": "^3.9.7"17  },18  "scripts": {19    "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",20    "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.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",23    "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",24    "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",25    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",26    "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",27    "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",28    "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"29  },30  "author": "",31  "license": "Apache 2.0",32  "homepage": "",33  "dependencies": {34    "@polkadot/api": "^2.3.1",35    "@polkadot/api-contract": "^2.3.1",36    "@polkadot/types": "^2.3.1",37    "@polkadot/util": "^3.4.1",38    "bignumber.js": "^9.0.0",39    "chai-as-promised": "^7.1.1"40  },41  "standard": {42    "globals": [43      "it",44      "assert",45      "beforeEach",46      "afterEach",47      "describe",48      "contract",49      "artifacts"50    ]51  }52}
after · tests/package.json
1{2  "name": "NftTests",3  "version": "1.0.0",4  "description": "Substrate Nft tests",5  "main": "",6  "devDependencies": {7    "@polkadot/dev": "^0.52.11",8    "@polkadot/ts": "^0.3.41",9    "@types/chai": "^4.2.12",10    "@types/chai-as-promised": "^7.1.3",11    "@types/mocha": "^8.0.3",12    "chai": "^4.2.0",13    "mocha": "^8.1.1",14    "ts-node": "^9.0.0",15    "tslint": "^5.20.1",16    "typescript": "^3.9.7"17  },18  "scripts": {19    "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",20    "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",21    "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",22    "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",23    "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",24    "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",25    "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",26    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",27    "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",28    "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",29    "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"30  },31  "author": "",32  "license": "Apache 2.0",33  "homepage": "",34  "dependencies": {35    "@polkadot/api": "^2.3.1",36    "@polkadot/api-contract": "^2.3.1",37    "@polkadot/types": "^2.3.1",38    "@polkadot/util": "^3.4.1",39    "bignumber.js": "^9.0.0",40    "chai-as-promised": "^7.1.1"41  },42  "standard": {43    "globals": [44      "it",45      "assert",46      "beforeEach",47      "afterEach",48      "describe",49      "contract",50      "artifacts"51    ]52  }53}
addedtests/src/addCollectionAdmin.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/addCollectionAdmin.test.ts
@@ -0,0 +1,130 @@
+import { ApiPromise } from '@polkadot/api';
+import BN from 'bn.js';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';
+import {createCollectionExpectSuccess, destroyCollectionExpectSuccess} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+describe('Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {
+  it('Add collection admin.', async () => {
+    await usingApi(async (api) => {
+      const collectionId = await createCollectionExpectSuccess();
+      const alice = privateKey('//Alice');
+      const bob = privateKey('//Bob');
+
+      const collection: any = (await api.query.nft.collection(collectionId));
+      expect(collection.Owner.toString()).to.be.eq(alice.address);
+
+      const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, bob.address);
+      await submitTransactionAsync(alice, changeAdminTx);
+
+      const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
+      expect(adminListAfterAddAdmin).to.be.contains(bob.address);
+    });
+  });
+
+  it('Add admin using added collection admin.', async () => {
+    await usingApi(async (api) => {
+      const collectionId = await createCollectionExpectSuccess();
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      const Charlie = privateKey('//CHARLIE');
+
+      const collection: any = (await api.query.nft.collection(collectionId));
+      expect(collection.Owner.toString()).to.be.eq(Alice.address);
+
+      const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address);
+      await submitTransactionAsync(Alice, changeAdminTx);
+
+      const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
+      expect(adminListAfterAddAdmin).to.be.contains(Bob.address);
+
+      const changeAdminTxCharlie = api.tx.nft.addCollectionAdmin(collectionId, Charlie.address);
+      await submitTransactionAsync(Bob, changeAdminTxCharlie);
+      const adminListAfterAddNewAdmin: any = (await api.query.nft.adminList(collectionId));
+      expect(adminListAfterAddNewAdmin).to.be.contains(Bob.address);
+      expect(adminListAfterAddNewAdmin).to.be.contains(Charlie.address);
+    });
+  });
+});
+
+describe('Negative Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {
+  it("Not owner can't add collection admin.", async () => {
+    await usingApi(async (api) => {
+      const collectionId = await createCollectionExpectSuccess();
+      const alice = privateKey('//Alice');
+      const nonOwner = privateKey('//Bob_stash');
+
+      const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, alice.address);
+      await expect(submitTransactionExpectFailAsync(nonOwner, changeAdminTx)).to.be.rejected;
+
+      const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
+      expect(adminListAfterAddAdmin).not.to.be.contains(alice.address);
+
+      // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
+      await createCollectionExpectSuccess();
+    });
+  });
+  it("Can't add collection admin of not existing collection.", async () => {
+    await usingApi(async (api) => {
+      // tslint:disable-next-line: no-bitwise
+      const collectionId = (1 << 32) - 1;
+      const alice = privateKey('//Alice');
+      const bob = privateKey('//Bob');
+
+      const changeOwnerTx = api.tx.nft.addCollectionAdmin(collectionId, bob.address);
+      await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;
+
+      // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
+      await createCollectionExpectSuccess();
+    });
+  });
+
+  it("Can't add an admin to a destroyed collection.", async () => {
+    await usingApi(async (api) => {
+      const collectionId = await createCollectionExpectSuccess();
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      await destroyCollectionExpectSuccess(collectionId);
+      const changeOwnerTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address);
+      await expect(submitTransactionExpectFailAsync(Alice, changeOwnerTx)).to.be.rejected;
+
+      // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
+      await createCollectionExpectSuccess();
+    });
+  });
+
+  it('Add an admin to a collection that has reached the maximum number of admins limit', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const accounts = [
+        'GsvVmjr1CBHwQHw84pPHMDxgNY3iBLz6Qn7qS3CH8qPhrHz',
+        'FoQJpPyadYccjavVdTWxpxU7rUEaYhfLCPwXgkfD6Zat9QP',
+        'JKspFU6ohf1Grg3Phdzj2pSgWvsYWzSfKghhfzMbdhNBWs5',
+        'Fr4NzY1udSFFLzb2R3qxVQkwz9cZraWkyfH4h3mVVk7BK7P',
+        'DfnTB4z7eUvYRqcGtTpFsLC69o6tvBSC1pEv8vWPZFtCkaK',
+        'HnMAUz7r2G8G3hB27SYNyit5aJmh2a5P4eMdDtACtMFDbam',
+        'DE14BzQ1bDXWPKeLoAqdLAm1GpyAWaWF1knF74cEZeomTBM',
+      ];
+      const collectionId = await createCollectionExpectSuccess();
+
+      const chainLimit = await api.query.nft.chainLimit() as unknown as { collections_admins_limit: BN };
+      const chainLimitNumber = chainLimit.collections_admins_limit.toNumber();
+      expect(chainLimitNumber).to.be.equal(5);
+
+      for (let i = 0; i < chainLimitNumber; i++) {
+        const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, accounts[i]);
+        await submitTransactionAsync(Alice, changeAdminTx);
+        const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
+        expect(adminListAfterAddAdmin).to.be.contains(accounts[i]);
+      }
+
+      const tx = api.tx.nft.addCollectionAdmin(collectionId, accounts[chainLimitNumber]);
+      await expect(submitTransactionExpectFailAsync(Alice, tx)).to.be.rejected;
+    });
+  });
+});