From dddaa3db4b866f0ce3117f9d71c10a7fa6c8378a Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Fri, 05 Feb 2021 11:04:50 +0000 Subject: [PATCH] Merge pull request #76 from usetech-llc/feature/NFTPAR-252_setMintPermission Add tests for setMintPermission --- --- 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" --- /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'); + }); + }); +}); --- a/tests/src/util/helpers.ts +++ b/tests/src/util/helpers.ts @@ -645,6 +645,17 @@ return newItemId; } +export async function createItemExpectFailure( + sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) { + await usingApi(async (api) => { + const tx = api.tx.nft.createItem(collectionId, owner, createMode); + const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected; + const result = getCreateItemResult(events); + + expect(result.success).to.be.false; + }); +} + export async function setPublicAccessModeExpectSuccess( sender: IKeyringPair, collectionId: number, accessMode: 'Normal' | 'WhiteList', @@ -674,11 +685,11 @@ await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal'); } -export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) { +export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) { await usingApi(async (api) => { // Run the transaction - const tx = api.tx.nft.setMintPermission(collectionId, true); + const tx = api.tx.nft.setMintPermission(collectionId, enabled); const events = await submitTransactionAsync(sender, tx); const result = getGenericResult(events); @@ -686,8 +697,24 @@ const collection: any = (await api.query.nft.collection(collectionId)).toJSON(); // What to expect + // tslint:disable-next-line:no-unused-expression expect(result.success).to.be.true; - expect(collection.MintMode).to.be.equal(true); + expect(collection.MintMode).to.be.equal(enabled); + }); +} + +export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) { + await setMintPermissionExpectSuccess(sender, collectionId, true); +} + +export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) { + await usingApi(async (api) => { + // Run the transaction + const tx = api.tx.nft.setMintPermission(collectionId, enabled); + const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected; + const result = getCreateCollectionResult(events); + // tslint:disable-next-line:no-unused-expression + expect(result.success).to.be.false; }); } -- gitstuff