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

difftreelog

Merge pull request #76 from usetech-llc/feature/NFTPAR-252_setMintPermission

Greg Zaitsev2021-02-05parents: #ac00479 #9abd4da.patch.diff
in: master
Add tests for setMintPermission

3 files changed

modifiedtests/package.jsondiffbeforeafterboth
--- a/tests/package.json
+++ b/tests/package.json
@@ -36,6 +36,7 @@
     "testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",
     "testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",
     "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",
+    "testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.test.ts",
     "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
     "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",
     "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts"
addedtests/src/setMintPermission.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/setMintPermission.test.ts
@@ -0,0 +1,98 @@
+import { IKeyringPair } from '@polkadot/types/types';
+import privateKey from './substrate/privateKey';
+import usingApi from './substrate/substrate-api';
+import {
+  addToWhiteListExpectSuccess,
+  createCollectionExpectSuccess,
+  createItemExpectFailure,
+  createItemExpectSuccess,
+  destroyCollectionExpectSuccess,
+  enableWhiteListExpectSuccess,
+  findNotExistingCollection,
+  setMintPermissionExpectFailure,
+  setMintPermissionExpectSuccess,
+} from './util/helpers';
+
+describe('Integration Test setMintPermission', () => {
+  let alice: IKeyringPair;
+  let bob: IKeyringPair;
+
+  before(async () => {
+    await usingApi(async () => {
+      alice = privateKey('//Alice');
+      bob = privateKey('//Bob');
+    });
+  });
+
+  it('ensure white-listed non-privileged address can mint tokens', async () => {
+    await usingApi(async () => {
+      const collectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+      await enableWhiteListExpectSuccess(alice, collectionId);
+      await setMintPermissionExpectSuccess(alice, collectionId, true);
+      await addToWhiteListExpectSuccess(alice, collectionId, bob.address);
+
+      await createItemExpectSuccess(bob, collectionId, 'NFT');
+    });
+  });
+
+  it('can be enabled twice', async () => {
+    await usingApi(async () => {
+      const collectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+      await setMintPermissionExpectSuccess(alice, collectionId, true);
+      await setMintPermissionExpectSuccess(alice, collectionId, true);
+    });
+  });
+
+  it('can be disabled twice', async () => {
+    await usingApi(async () => {
+      const collectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+      await setMintPermissionExpectSuccess(alice, collectionId, true);
+      await setMintPermissionExpectSuccess(alice, collectionId, false);
+      await setMintPermissionExpectSuccess(alice, collectionId, false);
+    });
+  });
+});
+
+describe('Negative Integration Test setMintPermission', () => {
+  let alice: IKeyringPair;
+  let bob: IKeyringPair;
+
+  before(async () => {
+    await usingApi(async () => {
+      alice = privateKey('//Alice');
+      bob = privateKey('//Bob');
+    });
+  });
+
+  it('fails on not existing collection', async () => {
+    await usingApi(async (api) => {
+      const nonExistingCollection = await findNotExistingCollection(api);
+      await setMintPermissionExpectFailure(alice, nonExistingCollection, true);
+    });
+  });
+
+  it('fails on removed collection', async () => {
+    await usingApi(async () => {
+      const removedCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+      await destroyCollectionExpectSuccess(removedCollectionId);
+
+      await setMintPermissionExpectFailure(alice, removedCollectionId, true);
+    });
+  });
+
+  it('fails when not collection owner tries to set mint status', async () => {
+    const collectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+    await enableWhiteListExpectSuccess(alice, collectionId);
+    await setMintPermissionExpectFailure(bob, collectionId, true);
+  });
+
+  it('ensure non-white-listed non-privileged address can\'t mint tokens', async () => {
+    await usingApi(async () => {
+      const collectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+      await enableWhiteListExpectSuccess(alice, collectionId);
+      await setMintPermissionExpectSuccess(alice, collectionId, true);
+
+      await createItemExpectFailure(bob, collectionId, 'NFT');
+    });
+  });
+});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
645 return newItemId;645 return newItemId;
646}646}
647
648export async function createItemExpectFailure(
649 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {
650 await usingApi(async (api) => {
651 const tx = api.tx.nft.createItem(collectionId, owner, createMode);
652 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
653 const result = getCreateItemResult(events);
654
655 expect(result.success).to.be.false;
656 });
657}
647658
648export async function setPublicAccessModeExpectSuccess(659export async function setPublicAccessModeExpectSuccess(
649 sender: IKeyringPair, collectionId: number,660 sender: IKeyringPair, collectionId: number,
674 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');685 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');
675}686}
676687
677export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {688export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {
678 await usingApi(async (api) => {689 await usingApi(async (api) => {
679690
680 // Run the transaction691 // Run the transaction
681 const tx = api.tx.nft.setMintPermission(collectionId, true);692 const tx = api.tx.nft.setMintPermission(collectionId, enabled);
682 const events = await submitTransactionAsync(sender, tx);693 const events = await submitTransactionAsync(sender, tx);
683 const result = getGenericResult(events);694 const result = getGenericResult(events);
684695
685 // Get the collection696 // Get the collection
686 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();697 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
687698
688 // What to expect699 // What to expect
700 // tslint:disable-next-line:no-unused-expression
689 expect(result.success).to.be.true;701 expect(result.success).to.be.true;
690 expect(collection.MintMode).to.be.equal(true);702 expect(collection.MintMode).to.be.equal(enabled);
691 });703 });
692}704}
705
706export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {
707 await setMintPermissionExpectSuccess(sender, collectionId, true);
708}
709
710export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {
711 await usingApi(async (api) => {
712 // Run the transaction
713 const tx = api.tx.nft.setMintPermission(collectionId, enabled);
714 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
715 const result = getCreateCollectionResult(events);
716 // tslint:disable-next-line:no-unused-expression
717 expect(result.success).to.be.false;
718 });
719}
693720
694export async function isWhitelisted(collectionId: number, address: string) {721export async function isWhitelisted(collectionId: number, address: string) {
695 let whitelisted: boolean = false;722 let whitelisted: boolean = false;