git.delta.rocks / unique-network / refs/commits / 8724ee05587d

difftreelog

Merge pull request #101 from usetech-llc/feature/NFT-253_setOffchainSchema

Greg Zaitsev2021-02-15parents: #fe8844c #026d602.patch.diff
in: master
Add tests for setOffchainSchema

3 files changed

modifiedtests/package.jsondiffbeforeafterboth
before · tests/package.json
1{2  "name": "NftTests",3  "version": "1.0.0",4  "description": "Substrate Nft tests",5  "main": "",6  "devDependencies": {7    "@polkadot/dev": "^0.61.24",8    "@polkadot/ts": "^0.3.59",9    "@polkadot/typegen": "^3.6.4",10    "@polkadot/util-crypto": "^5.4.4",11    "@types/chai": "^4.2.12",12    "@types/chai-as-promised": "^7.1.3",13    "@types/mocha": "^8.0.3",14    "chai": "^4.2.0",15    "mocha": "^8.1.1",16    "ts-node": "^9.0.0",17    "tslint": "^5.20.1",18    "typescript": "^3.9.7"19  },20  "scripts": {21    "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",22    "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",23    "loadTransfer": "ts-node src/transfer.nload.ts",24    "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",25    "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",26    "testSetVariableMetaData": "mocha --timeout 9999999 -r ts-node/register ./**/setVariableMetaData.test.ts",27    "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",28    "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",29    "testRemoveFromWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/removeFromWhiteList.test.ts",30    "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",31    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",32    "testCreateMultipleItems": "mocha --timeout 9999999 -r ts-node/register ./**/createMultipleItems.test.ts",33    "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",34    "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",35    "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",36    "testToggleContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/toggleContractWhiteList.test.ts",37    "testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",38    "testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",39    "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",40    "testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.test.ts",41    "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",42    "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",43    "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",44    "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts"45  },46  "author": "",47  "license": "SEE LICENSE IN ../LICENSE",48  "homepage": "",49  "dependencies": {50    "@polkadot/api": "3.8.1",51    "@polkadot/api-contract": "3.8.1",52    "@polkadot/util": "5.6.2",53    "bignumber.js": "^9.0.0",54    "chai-as-promised": "^7.1.1"55  },56  "standard": {57    "globals": [58      "it",59      "assert",60      "beforeEach",61      "afterEach",62      "describe",63      "contract",64      "artifacts"65    ]66  }67}
addedtests/src/setOffchainSchema.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/setOffchainSchema.test.ts
@@ -0,0 +1,80 @@
+//
+// 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 from './substrate/substrate-api';
+import {
+  createCollectionExpectSuccess,
+  destroyCollectionExpectSuccess,
+  findNotExistingCollection,
+  queryCollectionExpectSuccess,
+  setOffchainSchemaExpectFailure,
+  setOffchainSchemaExpectSuccess,
+} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+const DATA = [1, 2, 3, 4];
+
+describe('Integration Test setOffchainSchema', () => {
+  let alice: IKeyringPair;
+
+  before(async () => {
+    await usingApi(async () => {
+      alice = privateKey('//Alice');
+    });
+  });
+
+  it('execute setOffchainSchema, verify data was set', async () => {
+    const collectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+    await setOffchainSchemaExpectSuccess(alice, collectionId, DATA);
+    const collection = await queryCollectionExpectSuccess(collectionId);
+
+    expect(Array.from(collection.OffchainSchema)).to.be.deep.equal(DATA);
+  });
+});
+
+describe('Negative Integration Test setOffchainSchema', () => {
+  let alice: IKeyringPair;
+  let bob: IKeyringPair;
+
+  let validCollectionId: number;
+
+  before(async () => {
+    await usingApi(async () => {
+      alice = privateKey('//Alice');
+      bob = privateKey('//Bob');
+
+      validCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+    });
+  });
+
+  it('fails on not existing collection id', async () => {
+    const nonExistingCollectionId = await usingApi(findNotExistingCollection);
+
+    await setOffchainSchemaExpectFailure(alice, nonExistingCollectionId, DATA);
+  });
+
+  it('fails on destroyed collection id', async () => {
+    const destroyedCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+    await destroyCollectionExpectSuccess(destroyedCollectionId);
+
+    await setOffchainSchemaExpectFailure(alice, destroyedCollectionId, DATA);
+  });
+
+  it('fails on too long data', async () => {
+    const tooLongData = new Array(4097).fill(0xff);
+
+    await setOffchainSchemaExpectFailure(alice, validCollectionId, tooLongData);
+  });
+
+  it('fails on execution by non-owner', async () => {
+    await setOffchainSchemaExpectFailure(bob, validCollectionId, DATA);
+  });
+});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -488,6 +488,23 @@
   });
 }
 
+export async function setOffchainSchemaExpectSuccess(sender: IKeyringPair, collectionId: number, data: number[]) {
+  await usingApi(async (api) => {
+    const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));
+    const events = await submitTransactionAsync(sender, tx);
+    const result = getGenericResult(events);
+
+    expect(result.success).to.be.true;
+  });
+}
+
+export async function setOffchainSchemaExpectFailure(sender: IKeyringPair, collectionId: number, data: number[]) {
+  await usingApi(async (api) => {
+    const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));
+    await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
+  });
+}
+
 export interface CreateFungibleData {
   readonly Value: bigint;
 }
@@ -862,3 +879,9 @@
   // set global object - collectionsCount
   return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();
 };
+
+export async function queryCollectionExpectSuccess(collectionId: number): Promise<ICollectionInterface> {
+  return await usingApi(async (api) => {
+    return (await api.query.nft.collection(collectionId)) as unknown as ICollectionInterface;
+  });
+}