difftreelog
CORE-170. White list tests
in: master
2 files changed
tests/src/util/helpers.tsdiffbeforeafterboth1142 });1142 });1143}1143}11441145export async function addToWhiteListAgainExpectSuccess(sender: IKeyringPair, collectionId: number, address: string | AccountId) {1146 await usingApi(async (api) => {11471148 const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();11491150 // Run the transaction1151 const tx = api.tx.nft.addToWhiteList(collectionId, normalizeAccountId(address));1152 const events = await submitTransactionAsync(sender, tx);1153 const result = getGenericResult(events);11541155 const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();11561157 // What to expect1158 // tslint:disable-next-line:no-unused-expression1159 expect(result.success).to.be.true;1160 // tslint:disable-next-line: no-unused-expression1161 expect(whiteListedBefore).to.be.true;1162 // tslint:disable-next-line: no-unused-expression1163 expect(whiteListedAfter).to.be.true;1164 });1165}114411661145export async function addToWhiteListExpectFail(sender: IKeyringPair, collectionId: number, address: string | AccountId) {1167export async function addToWhiteListExpectFail(sender: IKeyringPair, collectionId: number, address: string | AccountId) {1146 await usingApi(async (api) => {1168 await usingApi(async (api) => {1169 1147 // Run the transaction1170 // Run the transaction1148 const tx = api.tx.nft.addToWhiteList(collectionId, normalizeAccountId(address));1171 const tx = api.tx.nft.addToWhiteList(collectionId, normalizeAccountId(address));1149 const events = await expect(submitTransactionAsync(sender, tx)).to.be.rejected;1172 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;1150 const result = getGenericResult(events);1173 const result = getGenericResult(events);115111741152 // What to expect1175 // What to expecttests/src/whiteLists.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/whiteLists.test.ts
@@ -0,0 +1,307 @@
+//
+// This file is subject to the terms and conditions defined in
+// file 'LICENSE', which is part of this source code package.
+//
+
+import { IKeyringPair } from '@polkadot/types/types';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import usingApi, { submitTransactionExpectFailAsync } from './substrate/substrate-api';
+import {
+ addToWhiteListExpectSuccess,
+ createCollectionExpectSuccess,
+ createItemExpectSuccess,
+ destroyCollectionExpectSuccess,
+ enableWhiteListExpectSuccess,
+ normalizeAccountId,
+ addCollectionAdminExpectSuccess,
+ addToWhiteListExpectFail,
+ removeFromWhiteListExpectSuccess,
+ removeFromWhiteListExpectFailure,
+ addToWhiteListAgainExpectSuccess,
+ transferExpectFailure,
+ approveExpectSuccess,
+ approveExpectFail,
+ transferExpectSuccess,
+ transferFromExpectSuccess,
+ setMintPermissionExpectSuccess,
+ createItemExpectFailure,
+} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+let Alice: IKeyringPair;
+let Bob: IKeyringPair;
+let Charlie: IKeyringPair;
+
+describe.only('Integration Test ext. White list tests', () => {
+
+ before(async () => {
+ await usingApi(async () => {
+ Alice = privateKey('//Alice');
+ Bob = privateKey('//Bob');
+ Charlie = privateKey('//Charlie');
+ });
+ });
+
+ it('Owner can add address to white list', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);
+ });
+
+ it('Admin can add address to white list', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await addCollectionAdminExpectSuccess(Alice, collectionId, Bob);
+ await addToWhiteListExpectSuccess(Bob, collectionId, Charlie.address);
+ });
+
+ it('Non-privileged user cannot add address to white list', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await addToWhiteListExpectFail(Bob, collectionId, Charlie.address);
+ });
+
+ it('Nobody can add address to white list of non-existing collection', async () => {
+ const collectionId = (1<<32) - 1;
+ await addToWhiteListExpectFail(Alice, collectionId, Bob.address);
+ });
+
+ it('Nobody can add address to white list of non-existing collection', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await destroyCollectionExpectSuccess(collectionId, '//Alice');
+ await addToWhiteListExpectFail(Alice, collectionId, Bob.address);
+ });
+
+ it('If address is already added to white list, nothing happens', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);
+ await addToWhiteListAgainExpectSuccess(Alice, collectionId, Bob.address);
+ });
+
+ it('Owner can remove address from white list', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);
+ await removeFromWhiteListExpectSuccess(Alice, collectionId, normalizeAccountId(Bob));
+ });
+
+ it('Owner can remove address from white list', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await addCollectionAdminExpectSuccess(Alice, collectionId, Bob);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+ await removeFromWhiteListExpectSuccess(Bob, collectionId, normalizeAccountId(Charlie));
+ });
+
+ it('Non-privileged user cannot remove address from white list', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+ await removeFromWhiteListExpectFailure(Bob, collectionId, normalizeAccountId(Charlie));
+ });
+
+ it('Nobody can remove address from white list of non-existing collection', async () => {
+ const collectionId = (1<<32) - 1;
+ await removeFromWhiteListExpectFailure(Alice, collectionId, normalizeAccountId(Charlie));
+ });
+
+
+
+ it('Nobody can remove address from white list of deleted collection', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+ await destroyCollectionExpectSuccess(collectionId, '//Alice');
+ await removeFromWhiteListExpectFailure(Alice, collectionId, normalizeAccountId(Charlie));
+ });
+
+ it('If address is already removed from white list, nothing happens', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+ await removeFromWhiteListExpectSuccess(Alice, collectionId, normalizeAccountId(Charlie));
+ await removeFromWhiteListExpectSuccess(Alice, collectionId, normalizeAccountId(Charlie));
+ });
+
+ it('If Public Access mode is set to WhiteList, tokens can’t be transferred from a non-whitelisted address with transfer or transferFrom. Test1', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+
+ await transferExpectFailure(
+ collectionId,
+ itemId,
+ Alice,
+ Charlie,
+ 1,
+ );
+ });
+
+ it('If Public Access mode is set to WhiteList, tokens can’t be transferred from a non-whitelisted address with transfer or transferFrom. Test2', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+ await approveExpectSuccess(collectionId, itemId, Alice, Charlie);
+ await removeFromWhiteListExpectSuccess(Alice, collectionId, normalizeAccountId(Alice));
+
+ await transferExpectFailure(
+ collectionId,
+ itemId,
+ Alice,
+ Charlie,
+ 1,
+ );
+ });
+
+ it('If Public Access mode is set to WhiteList, tokens can’t be transferred to a non-whitelisted address with transfer or transferFrom. Test1', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+
+ await transferExpectFailure(
+ collectionId,
+ itemId,
+ Alice,
+ Charlie,
+ 1,
+ );
+ });
+
+ it('If Public Access mode is set to WhiteList, tokens can’t be transferred to a non-whitelisted address with transfer or transferFrom. Test2', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+ await approveExpectSuccess(collectionId, itemId, Alice, Charlie);
+ await removeFromWhiteListExpectSuccess(Alice, collectionId, normalizeAccountId(Charlie));
+
+ await transferExpectFailure(
+ collectionId,
+ itemId,
+ Alice,
+ Charlie,
+ 1,
+ );
+ });
+
+ it('If Public Access mode is set to WhiteList, tokens can’t be destroyed by a non-whitelisted address (even if it owned them before enabling WhiteList mode)', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.burnItem(collectionId, itemId, normalizeAccountId(Alice.address), 11);
+ const badTransaction = async function () {
+ await submitTransactionExpectFailAsync(Alice, tx);
+ };
+ await expect(badTransaction()).to.be.rejected;
+ });
+ });
+
+ it('If Public Access mode is set to WhiteList, token transfers can’t be Approved by a non-whitelisted address (see Approve method)', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await approveExpectFail(collectionId, itemId, Alice, Bob);
+ });
+
+ it('If Public Access mode is set to WhiteList, tokens can be transferred to a whitelisted address with transfer.', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+ await transferExpectSuccess(collectionId, itemId, Alice, Charlie, 1, 'NFT');
+ });
+
+ it('If Public Access mode is set to WhiteList, tokens can be transferred to a whitelisted address with transferFrom.', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+ await approveExpectSuccess(collectionId, itemId, Alice, Charlie);
+ await transferFromExpectSuccess(collectionId, itemId, Alice, Alice, Charlie, 1, 'NFT');
+ });
+
+ it('If Public Access mode is set to WhiteList, tokens can be transferred from a whitelisted address with transfer', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+ await transferExpectSuccess(collectionId, itemId, Alice, Charlie, 1, 'NFT');
+ });
+
+ it('If Public Access mode is set to WhiteList, tokens can be transferred from a whitelisted address with transferFrom', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);
+ await approveExpectSuccess(collectionId, itemId, Alice, Charlie);
+ await transferFromExpectSuccess(collectionId, itemId, Alice, Alice, Charlie, 1, 'NFT');
+ });
+
+ it('If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens can be created by owner', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await setMintPermissionExpectSuccess(Alice, collectionId, false);
+ await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ });
+
+ it('If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens can be created by admin', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await setMintPermissionExpectSuccess(Alice, collectionId, false);
+ await addCollectionAdminExpectSuccess(Alice, collectionId, Bob);
+ await createItemExpectSuccess(Bob, collectionId, 'NFT', Bob.address);
+ });
+
+ it('If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens cannot be created by non-privileged and white listed address', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await setMintPermissionExpectSuccess(Alice, collectionId, false);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);
+ await createItemExpectFailure(Bob, collectionId, 'NFT', Bob.address);
+ });
+
+ it('If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens cannot be created by non-privileged and non-white listed address', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await setMintPermissionExpectSuccess(Alice, collectionId, false);
+ await createItemExpectFailure(Bob, collectionId, 'NFT', Bob.address);
+ });
+
+ it('If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by owner', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await setMintPermissionExpectSuccess(Alice, collectionId, true);
+ await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);
+ });
+
+ it('If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by admin', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await setMintPermissionExpectSuccess(Alice, collectionId, true);
+ await addCollectionAdminExpectSuccess(Alice, collectionId, Bob);
+ await createItemExpectSuccess(Bob, collectionId, 'NFT', Bob.address);
+ });
+
+ it('If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens cannot be created by non-privileged and non-white listed address', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await setMintPermissionExpectSuccess(Alice, collectionId, true);
+ await createItemExpectFailure(Bob, collectionId, 'NFT', Bob.address);
+ });
+
+ it('If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by non-privileged and white listed address', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await setMintPermissionExpectSuccess(Alice, collectionId, true);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);
+ await createItemExpectSuccess(Bob, collectionId, 'NFT', Bob.address);
+ });
+});
+