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
--- /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);
+        });
+    });
+});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
409 });409 });
410}410}
411
412export async function toggleContractWhitelistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enabled: boolean) {
413 await usingApi(async (api) => {
414 const tx = api.tx.nft.toggleContractWhiteList(contractAddress, true);
415 const events = await submitTransactionAsync(sender, tx);
416 const result = getGenericResult(events);
417
418 expect(result.success).to.be.true;
419 });
420}
421
422export async function isWhitelistedInContract(contractAddress: AccountId | string, user: string) {
423 let whitelisted: boolean = false;
424 await usingApi(async (api) => {
425 whitelisted = (await api.query.nft.contractWhiteList(contractAddress, user)).toJSON() as boolean;
426 });
427 return whitelisted;
428}
429
430export async function addToContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {
431 await usingApi(async (api) => {
432 const tx = api.tx.nft.addToContractWhiteList(contractAddress, user);
433 const events = await submitTransactionAsync(sender, tx);
434 const result = getGenericResult(events);
435
436 expect(result.success).to.be.true;
437 });
438}
439
440export async function removeFromContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {
441 await usingApi(async (api) => {
442 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);
443 const events = await submitTransactionAsync(sender, tx);
444 const result = getGenericResult(events);
445
446 expect(result.success).to.be.true;
447 });
448}
449
450export async function removeFromContractWhiteListExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {
451 await usingApi(async (api) => {
452 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);
453 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
454 const result = getGenericResult(events);
455
456 expect(result.success).to.be.false;
457 });
458}
411459
412export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {460export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {
413 await usingApi(async (api) => {461 await usingApi(async (api) => {