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
45 let mut properties = Map::new();45 let mut properties = Map::new();
46 properties.insert("tokenSymbol".into(), "UniqueTest".into());46 properties.insert("tokenSymbol".into(), "UniqueTest".into());
47 properties.insert("tokenDecimals".into(), 15.into());47 properties.insert("tokenDecimals".into(), 15.into());
48 properties.insert("ss58Format".into(), 0.into());48 properties.insert("ss58Format".into(), 42.into()); // Generic Substrate wildcard (SS58 checksum preimage)
4949
50 Ok(ChainSpec::from_genesis(50 Ok(ChainSpec::from_genesis(
51 // Name51 // 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
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -52,7 +52,8 @@
   return result;
 }
 
-export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string) {
+export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string): Promise<number> {
+  let collectionId: number = 0;
   await usingApi(async (api) => {
     // Get number of collections before the transaction
     const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());
@@ -78,7 +79,11 @@
     expect(utf16ToStr(collection.Name)).to.be.equal(name);
     expect(utf16ToStr(collection.Description)).to.be.equal(description);
     expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);
+
+    collectionId = result.collectionId;
   });
+
+  return collectionId;
 }
   
 export async function createCollectionExpectFailure(name: string, description: string, tokenPrefix: string, mode: string) {