git.delta.rocks / unique-network / refs/commits / da457a01b0ad

difftreelog

Merge pull request #46 from usetech-llc/feature/NFTPAR-242_destroy_collection_test

str-mv2020-12-22parents: #73f8e68 #f682871.patch.diff
in: master
NFTPAR-242 Add destroy collection integration tests

4 files changed

modifiednode/src/chain_spec.rsdiffbeforeafterboth
--- a/node/src/chain_spec.rs
+++ b/node/src/chain_spec.rs
@@ -45,7 +45,7 @@
 	let mut properties = Map::new();
 	properties.insert("tokenSymbol".into(), "UniqueTest".into());
 	properties.insert("tokenDecimals".into(), 15.into());
-	properties.insert("ss58Format".into(), 0.into());
+	properties.insert("ss58Format".into(), 42.into()); // Generic Substrate wildcard (SS58 checksum preimage)
 
 	Ok(ChainSpec::from_genesis(
 		// Name
modifiedtests/src/accounts.tsdiffbeforeafterboth
--- a/tests/src/accounts.ts
+++ b/tests/src/accounts.ts
@@ -1,3 +1,4 @@
 export const bobsPublicKey = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
 export const alicesPublicKey = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
 export const ferdiesPublicKey = '5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL';
+export const nullPublicKey = '5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM';
\ No newline at end of file
addedtests/src/destroyCollection.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/destroyCollection.test.ts
@@ -0,0 +1,87 @@
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import { default as usingApi, submitTransactionAsync } from "./substrate/substrate-api";
+import { createCollectionExpectSuccess, createCollectionExpectFailure } from "./util/helpers";
+import type { AccountId, EventRecord } from '@polkadot/types/interfaces';
+import privateKey from './substrate/privateKey';
+import { nullPublicKey } from './accounts'; 
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+function getDestroyResult(events: EventRecord[]): boolean {
+  let success: boolean = false;
+  events.forEach(({ phase, event: { data, method, section } }) => {
+    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);
+    if (method == 'ExtrinsicSuccess') {
+      success = true;
+    }
+  });
+  return success;
+}
+
+async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {
+  await usingApi(async (api) => {
+    // Run the DestroyCollection transaction
+    const alicePrivateKey = privateKey(senderSeed);
+    const tx = api.tx.nft.destroyCollection(collectionId);
+    const events = await submitTransactionAsync(alicePrivateKey, tx);
+    const result = getDestroyResult(events);
+
+    // Get the collection 
+    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
+
+    // What to expect
+    expect(result).to.be.true;
+    expect(collection).to.be.not.null;
+    expect(collection.Owner).to.be.equal(nullPublicKey);
+  });
+}
+
+async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {
+  await usingApi(async (api) => {
+    // Run the DestroyCollection transaction
+    const alicePrivateKey = privateKey(senderSeed);
+    const tx = api.tx.nft.destroyCollection(collectionId);
+    const events = await submitTransactionAsync(alicePrivateKey, tx);
+    const result = getDestroyResult(events);
+
+    // What to expect
+    expect(result).to.be.false;
+  });
+}
+
+describe('integration test: ext. destroyCollection():', () => {
+  it('NFT collection can be destroyed', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+    await destroyCollectionExpectSuccess(collectionId);
+  });
+  it('Fungible collection can be destroyed', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'Fungible');
+    await destroyCollectionExpectSuccess(collectionId);
+  });
+  it('ReFungible collection can be destroyed', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'ReFungible');
+    await destroyCollectionExpectSuccess(collectionId);
+  });
+});
+
+describe('(!negative test!) integration test: ext. destroyCollection():', () => {
+  it('(!negative test!) Destroy a collection that never existed', async () => {
+    await usingApi(async (api) => {
+      // Find the collection that never existed
+      const collectionId = parseInt((await api.query.nft.createdCollectionCount()).toString()) + 1;
+      await destroyCollectionExpectFailure(collectionId);
+    });
+  });
+  it('(!negative test!) Destroy a collection that has already been destroyed', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+    await destroyCollectionExpectSuccess(collectionId);
+    await destroyCollectionExpectFailure(collectionId);
+  });
+  it('(!negative test!) Destroy a collection using non-owner account', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+    await destroyCollectionExpectFailure(collectionId, '//Bob');
+    await destroyCollectionExpectSuccess(collectionId, '//Alice');
+  });
+});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
before · tests/src/util/helpers.ts
1import chai from 'chai';2import chaiAsPromised from 'chai-as-promised';3import type { AccountId, EventRecord } from '@polkadot/types/interfaces';4import { ApiPromise, Keyring } from "@polkadot/api";5import { default as usingApi, submitTransactionAsync } from "../substrate/substrate-api";6import privateKey from '../substrate/privateKey';7import { alicesPublicKey } from "../accounts";8import { strToUTF16, utf16ToStr, hexToStr } from '../util/util';9import { IKeyringPair } from "@polkadot/types/types";10import { BigNumber } from 'bignumber.js';1112chai.use(chaiAsPromised);13const expect = chai.expect;1415type GenericResult = {16  success: boolean,17};1819type CreateCollectionResult = {20  success: boolean,21  collectionId: number22};2324export function getGenericResult(events: EventRecord[]): GenericResult {25  let result: GenericResult = {26    success: false27  }28  events.forEach(({ phase, event: { data, method, section } }) => {29    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);30    if (method == 'ExtrinsicSuccess') {31      result.success = true;32    }33  });34  return result;35}3637function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {38  let success = false;39  let collectionId: number = 0;40  events.forEach(({ phase, event: { data, method, section } }) => {41    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);42    if (method == 'ExtrinsicSuccess') {43      success = true;44    } else if ((section == 'nft') && (method == 'Created')) {45      collectionId = parseInt(data[0].toString());46    }47  });48  let result: CreateCollectionResult = {49    success,50    collectionId51  }52  return result;53}5455export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string) {56  await usingApi(async (api) => {57    // Get number of collections before the transaction58    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());5960    // Run the CreateCollection transaction61    const alicePrivateKey = privateKey('//Alice');62    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);63    const events = await submitTransactionAsync(alicePrivateKey, tx);64    const result = getCreateCollectionResult(events);6566    // Get number of collections after the transaction67    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());6869    // Get the collection 70    const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();7172    // What to expect73    expect(result.success).to.be.true;74    expect(result.collectionId).to.be.equal(BcollectionCount);75    expect(collection).to.be.not.null;76    expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');77    expect(collection.Owner).to.be.equal(alicesPublicKey);78    expect(utf16ToStr(collection.Name)).to.be.equal(name);79    expect(utf16ToStr(collection.Description)).to.be.equal(description);80    expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);81  });82}83  84export async function createCollectionExpectFailure(name: string, description: string, tokenPrefix: string, mode: string) {85  await usingApi(async (api) => {86    // Get number of collections before the transaction87    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());8889    // Run the CreateCollection transaction90    const alicePrivateKey = privateKey('//Alice');91    const tx = api.tx.nft.createCollection(name, description, tokenPrefix, mode);92    const events = await submitTransactionAsync(alicePrivateKey, tx);93    const result = getCreateCollectionResult(events);9495    // Get number of collections after the transaction96    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());9798    // What to expect99    expect(result.success).to.be.false;100    expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');101  });102}103  104export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {105  let bal = new BigNumber(0);106  let unused;107  do {108    const randomSeed = 'seed' +  Math.floor(Math.random() * Math.floor(10000));109    const keyring = new Keyring({ type: 'sr25519' });110    unused = keyring.addFromUri(`//${randomSeed}`);111    bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());112  } while (bal.toFixed() != '0');113  return unused; 114}
after · tests/src/util/helpers.ts
1import chai from 'chai';2import chaiAsPromised from 'chai-as-promised';3import type { AccountId, EventRecord } from '@polkadot/types/interfaces';4import { ApiPromise, Keyring } from "@polkadot/api";5import { default as usingApi, submitTransactionAsync } from "../substrate/substrate-api";6import privateKey from '../substrate/privateKey';7import { alicesPublicKey } from "../accounts";8import { strToUTF16, utf16ToStr, hexToStr } from '../util/util';9import { IKeyringPair } from "@polkadot/types/types";10import { BigNumber } from 'bignumber.js';1112chai.use(chaiAsPromised);13const expect = chai.expect;1415type GenericResult = {16  success: boolean,17};1819type CreateCollectionResult = {20  success: boolean,21  collectionId: number22};2324export function getGenericResult(events: EventRecord[]): GenericResult {25  let result: GenericResult = {26    success: false27  }28  events.forEach(({ phase, event: { data, method, section } }) => {29    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);30    if (method == 'ExtrinsicSuccess') {31      result.success = true;32    }33  });34  return result;35}3637function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {38  let success = false;39  let collectionId: number = 0;40  events.forEach(({ phase, event: { data, method, section } }) => {41    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);42    if (method == 'ExtrinsicSuccess') {43      success = true;44    } else if ((section == 'nft') && (method == 'Created')) {45      collectionId = parseInt(data[0].toString());46    }47  });48  let result: CreateCollectionResult = {49    success,50    collectionId51  }52  return result;53}5455export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string): Promise<number> {56  let collectionId: number = 0;57  await usingApi(async (api) => {58    // Get number of collections before the transaction59    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());6061    // Run the CreateCollection transaction62    const alicePrivateKey = privateKey('//Alice');63    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);64    const events = await submitTransactionAsync(alicePrivateKey, tx);65    const result = getCreateCollectionResult(events);6667    // Get number of collections after the transaction68    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());6970    // Get the collection 71    const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();7273    // What to expect74    expect(result.success).to.be.true;75    expect(result.collectionId).to.be.equal(BcollectionCount);76    expect(collection).to.be.not.null;77    expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');78    expect(collection.Owner).to.be.equal(alicesPublicKey);79    expect(utf16ToStr(collection.Name)).to.be.equal(name);80    expect(utf16ToStr(collection.Description)).to.be.equal(description);81    expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);8283    collectionId = result.collectionId;84  });8586  return collectionId;87}88  89export async function createCollectionExpectFailure(name: string, description: string, tokenPrefix: string, mode: string) {90  await usingApi(async (api) => {91    // Get number of collections before the transaction92    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());9394    // Run the CreateCollection transaction95    const alicePrivateKey = privateKey('//Alice');96    const tx = api.tx.nft.createCollection(name, description, tokenPrefix, mode);97    const events = await submitTransactionAsync(alicePrivateKey, tx);98    const result = getCreateCollectionResult(events);99100    // Get number of collections after the transaction101    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());102103    // What to expect104    expect(result.success).to.be.false;105    expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');106  });107}108  109export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {110  let bal = new BigNumber(0);111  let unused;112  do {113    const randomSeed = 'seed' +  Math.floor(Math.random() * Math.floor(10000));114    const keyring = new Keyring({ type: 'sr25519' });115    unused = keyring.addFromUri(`//${randomSeed}`);116    bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());117  } while (bal.toFixed() != '0');118  return unused; 119}