From 1e0a302bebd7954c81c2e7e1f632e314b192a81e Mon Sep 17 00:00:00 2001 From: str-mv Date: Fri, 16 Jul 2021 17:06:14 +0000 Subject: [PATCH] CORE-37. Integration tests --- --- /dev/null +++ b/tests/src/metadataUpdate.test.ts @@ -0,0 +1,112 @@ + +// +// This file is subject to the terms and conditions defined in +// file 'LICENSE', which is part of this source code package. +// + +import chai from 'chai'; +import chaiAsPromised from 'chai-as-promised'; +import privateKey from './substrate/privateKey'; +import usingApi from './substrate/substrate-api'; +import { + createItemExpectSuccess, + createCollectionExpectSuccess, + setMetadataUpdatePermissionFlagExpectSuccess, + setVariableMetaDataExpectSuccess, + setMintPermissionExpectSuccess, + addToWhiteListExpectSuccess, + addCollectionAdminExpectSuccess, + setVariableMetaDataExpectFailure, + setMetadataUpdatePermissionFlagExpectFailure +} from './util/helpers'; + +chai.use(chaiAsPromised); + +describe('Metadata update permissions ', () => { + it('Set variable metadata with ItemOwner permission flag', async () => { + await usingApi(async () => { + const Alice = privateKey('//Alice'); + const Bob = privateKey('//Bob'); + + const data = [1, 2, 254, 255]; + + // nft + const nftCollectionId = await createCollectionExpectSuccess(); + const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT'); + await setMetadataUpdatePermissionFlagExpectSuccess(Alice, nftCollectionId, "ItemOwner"); + + await setVariableMetaDataExpectSuccess(Alice, nftCollectionId, newNftTokenId, data); + }); + }); + + it('User can\'n set variable metadata with ItemOwner permission flag', async () => { + await usingApi(async () => { + const Alice = privateKey('//Alice'); + const Bob = privateKey('//Bob'); + + const data = [1, 2, 254, 255]; + + // nft + const nftCollectionId = await createCollectionExpectSuccess(); + const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT'); + await setMetadataUpdatePermissionFlagExpectSuccess(Alice, nftCollectionId, "ItemOwner"); + + await setMintPermissionExpectSuccess(Alice, nftCollectionId, true); + await addToWhiteListExpectSuccess(Alice, nftCollectionId, Bob.address); + await addCollectionAdminExpectSuccess(Alice, nftCollectionId, Bob); + + await setVariableMetaDataExpectFailure(Bob, nftCollectionId, newNftTokenId, data); + }); + }); + + it('Admin can set variable metadata with Admin permission flag', async () => { + await usingApi(async () => { + const Alice = privateKey('//Alice'); + const Bob = privateKey('//Bob'); + + const data = [1, 2, 254, 255]; + + // nft + const nftCollectionId = await createCollectionExpectSuccess(); + const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT'); + await setMetadataUpdatePermissionFlagExpectSuccess(Alice, nftCollectionId, "Admin"); + + await setMintPermissionExpectSuccess(Alice, nftCollectionId, true); + await addToWhiteListExpectSuccess(Alice, nftCollectionId, Bob.address); + await addCollectionAdminExpectSuccess(Alice, nftCollectionId, Bob); + + await setVariableMetaDataExpectSuccess(Bob, nftCollectionId, newNftTokenId, data); + }); + }); + + it('Nobody can set variable metadata with None flag', async () => { + await usingApi(async () => { + const Alice = privateKey('//Alice'); + const Bob = privateKey('//Bob'); + + const data = [1, 2, 254, 255]; + + // nft + const nftCollectionId = await createCollectionExpectSuccess(); + const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT'); + await setMetadataUpdatePermissionFlagExpectSuccess(Alice, nftCollectionId, "None"); + + await setVariableMetaDataExpectFailure(Alice, nftCollectionId, newNftTokenId, data); + }); + }); + + it('Nobody can set variable metadata flag after freeze', async () => { + await usingApi(async () => { + const Alice = privateKey('//Alice'); + const Bob = privateKey('//Bob'); + + const data = [1, 2, 254, 255]; + + // nft + const nftCollectionId = await createCollectionExpectSuccess(); + await setMetadataUpdatePermissionFlagExpectSuccess(Alice, nftCollectionId, "None"); + await setMetadataUpdatePermissionFlagExpectFailure(Alice, nftCollectionId, "Admin"); + + }); + }); +}); --- a/tests/src/util/helpers.ts +++ b/tests/src/util/helpers.ts @@ -503,6 +503,28 @@ }); } +export async function setMetadataUpdatePermissionFlagExpectSuccess(sender: IKeyringPair, collectionId: number, flag: string) { + + await usingApi(async (api) => { + const tx = api.tx.nft.setMetaUpdatePermissionFlag(collectionId, flag); + const events = await submitTransactionAsync(sender, tx); + const result = getGenericResult(events); + + expect(result.success).to.be.true; + }); +} + +export async function setMetadataUpdatePermissionFlagExpectFailure(sender: IKeyringPair, collectionId: number, flag: string) { + + await usingApi(async (api) => { + const tx = api.tx.nft.setMetaUpdatePermissionFlag(collectionId, flag); + const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected; + const result = getGenericResult(events); + + expect(result.success).to.be.false; + }); +} + export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) { await usingApi(async (api) => { const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable); @@ -986,6 +1008,15 @@ await setMintPermissionExpectSuccess(sender, collectionId, true); } +export async function addCollectionAdminExpectSuccess(sender: IKeyringPair, collectionId: number, address: IKeyringPair) { + await usingApi(async (api) => { + const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(address.address)); + const events = await submitTransactionAsync(sender, changeAdminTx); + const result = getCreateCollectionResult(events); + expect(result.success).to.be.true; + }); +} + export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) { await usingApi(async (api) => { // Run the transaction -- gitstuff