git.delta.rocks / unique-network / refs/commits / 337b8d4395cb

difftreelog

Merge pull request #91 from usetech-llc/feature/NFTPAR-287_removeFromContractWhiteList

Greg Zaitsev2021-02-09parents: #a6c7a54 #2d320f7.patch.diff
in: master
Add tests for removeFromContractWhitelist

2 files changed

addedtests/src/removeFromContractWhiteList.test.tsdiffbeforeafterboth
after · tests/src/removeFromContractWhiteList.test.ts
1import privateKey from "./substrate/privateKey";2import usingApi from "./substrate/substrate-api";3import { deployFlipper, toggleFlipValueExpectFailure, toggleFlipValueExpectSuccess } from "./util/contracthelpers";4import { addToContractWhiteListExpectSuccess, isWhitelistedInContract, removeFromContractWhiteListExpectFailure, removeFromContractWhiteListExpectSuccess, toggleContractWhitelistExpectSuccess } from "./util/helpers";5import { IKeyringPair } from '@polkadot/types/types';6import { expect } from "chai";78describe('Integration Test removeFromContractWhiteList', () => {9    let bob: IKeyringPair;1011    before(() => {12        bob = privateKey('//Bob');13    });1415    it('user is no longer whitelisted after removal', async () => {16        await usingApi(async (api) => {17            const [flipper, deployer] = await deployFlipper(api);1819            await addToContractWhiteListExpectSuccess(deployer, flipper.address, bob.address);20            await removeFromContractWhiteListExpectSuccess(deployer, flipper.address, bob.address);2122            expect(await isWhitelistedInContract(flipper.address, bob.address)).to.be.false;23        });24    });2526    it('user can\'t execute contract after removal', async () => {27        await usingApi(async (api) => {28            const [flipper, deployer] = await deployFlipper(api);29            await toggleContractWhitelistExpectSuccess(deployer, flipper.address, true);3031            await addToContractWhiteListExpectSuccess(deployer, flipper.address, bob.address);32            await toggleFlipValueExpectSuccess(bob, flipper);3334            await removeFromContractWhiteListExpectSuccess(deployer, flipper.address, bob.address);35            await toggleFlipValueExpectFailure(bob, flipper);36        });37    });3839    it('can be called twice', async () => {40        await usingApi(async (api) => {41            const [flipper, deployer] = await deployFlipper(api);4243            await addToContractWhiteListExpectSuccess(deployer, flipper.address, bob.address);44            await removeFromContractWhiteListExpectSuccess(deployer, flipper.address, bob.address);45            await removeFromContractWhiteListExpectSuccess(deployer, flipper.address, bob.address);46        });47    });48});4950describe('Negative Integration Test removeFromContractWhiteList', () => {51    let alice: IKeyringPair;52    let bob: IKeyringPair;5354    before(() => {55        alice = privateKey('//Alice');56        bob = privateKey('//Bob');57    });5859    it('fails when called with non-contract address', async () => {60        await usingApi(async () => {61            await removeFromContractWhiteListExpectFailure(alice, alice.address, bob.address);62        });63    });6465    it('fails when executed by non owner', async () => {66        await usingApi(async (api) => {67            const [flipper, _] = await deployFlipper(api);6869            await removeFromContractWhiteListExpectFailure(alice, flipper.address, bob.address);70        });71    });72});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- 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'));