--- /dev/null +++ b/tests/src/removeFromContractWhiteList.test.ts @@ -0,0 +1,72 @@ +import privateKey from "./substrate/privateKey"; +import usingApi from "./substrate/substrate-api"; +import { deployFlipper, toggleFlipValueExpectFailure, toggleFlipValueExpectSuccess } from "./util/contracthelpers"; +import { addToContractWhiteListExpectSuccess, isWhitelistedInContract, removeFromContractWhiteListExpectFailure, removeFromContractWhiteListExpectSuccess, toggleContractWhitelistExpectSuccess } from "./util/helpers"; +import { IKeyringPair } from '@polkadot/types/types'; +import { expect } from "chai"; + +describe('Integration Test removeFromContractWhiteList', () => { + let bob: IKeyringPair; + + before(() => { + bob = privateKey('//Bob'); + }); + + it('user is no longer whitelisted after removal', async () => { + await usingApi(async (api) => { + const [flipper, deployer] = await deployFlipper(api); + + await addToContractWhiteListExpectSuccess(deployer, flipper.address, bob.address); + await removeFromContractWhiteListExpectSuccess(deployer, flipper.address, bob.address); + + expect(await isWhitelistedInContract(flipper.address, bob.address)).to.be.false; + }); + }); + + it('user can\'t execute contract after removal', async () => { + await usingApi(async (api) => { + const [flipper, deployer] = await deployFlipper(api); + await toggleContractWhitelistExpectSuccess(deployer, flipper.address, true); + + await addToContractWhiteListExpectSuccess(deployer, flipper.address, bob.address); + await toggleFlipValueExpectSuccess(bob, flipper); + + await removeFromContractWhiteListExpectSuccess(deployer, flipper.address, bob.address); + await toggleFlipValueExpectFailure(bob, flipper); + }); + }); + + it('can be called twice', async () => { + await usingApi(async (api) => { + const [flipper, deployer] = await deployFlipper(api); + + await addToContractWhiteListExpectSuccess(deployer, flipper.address, bob.address); + await removeFromContractWhiteListExpectSuccess(deployer, flipper.address, bob.address); + await removeFromContractWhiteListExpectSuccess(deployer, flipper.address, bob.address); + }); + }); +}); + +describe('Negative Integration Test removeFromContractWhiteList', () => { + let alice: IKeyringPair; + let bob: IKeyringPair; + + before(() => { + alice = privateKey('//Alice'); + bob = privateKey('//Bob'); + }); + + it('fails when called with non-contract address', async () => { + await usingApi(async () => { + await removeFromContractWhiteListExpectFailure(alice, alice.address, bob.address); + }); + }); + + it('fails when executed by non owner', async () => { + await usingApi(async (api) => { + const [flipper, _] = await deployFlipper(api); + + await removeFromContractWhiteListExpectFailure(alice, flipper.address, bob.address); + }); + }); +}); --- a/tests/src/util/helpers.ts +++ b/tests/src/util/helpers.ts @@ -409,6 +409,54 @@ }); } +export async function toggleContractWhitelistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enabled: boolean) { + await usingApi(async (api) => { + const tx = api.tx.nft.toggleContractWhiteList(contractAddress, true); + const events = await submitTransactionAsync(sender, tx); + const result = getGenericResult(events); + + expect(result.success).to.be.true; + }); +} + +export async function isWhitelistedInContract(contractAddress: AccountId | string, user: string) { + let whitelisted: boolean = false; + await usingApi(async (api) => { + whitelisted = (await api.query.nft.contractWhiteList(contractAddress, user)).toJSON() as boolean; + }); + return whitelisted; +} + +export async function addToContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) { + await usingApi(async (api) => { + const tx = api.tx.nft.addToContractWhiteList(contractAddress, user); + const events = await submitTransactionAsync(sender, tx); + const result = getGenericResult(events); + + expect(result.success).to.be.true; + }); +} + +export async function removeFromContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) { + await usingApi(async (api) => { + const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user); + const events = await submitTransactionAsync(sender, tx); + const result = getGenericResult(events); + + expect(result.success).to.be.true; + }); +} + +export async function removeFromContractWhiteListExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, user: string) { + await usingApi(async (api) => { + const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user); + const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected; + const result = getGenericResult(events); + + expect(result.success).to.be.false; + }); +} + export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) { await usingApi(async (api) => { const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));