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
--- a/tests/package.json
+++ b/tests/package.json
@@ -41,6 +41,7 @@
     "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
     "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",
     "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",
+    "testSetOffchainSchema": "mocha --timeout 9999999 -r ts-node/register ./**/setOffchainSchema.test.ts",
     "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts"
   },
   "author": "",
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
488 });488 });
489}489}
490
491export async function setOffchainSchemaExpectSuccess(sender: IKeyringPair, collectionId: number, data: number[]) {
492 await usingApi(async (api) => {
493 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));
494 const events = await submitTransactionAsync(sender, tx);
495 const result = getGenericResult(events);
496
497 expect(result.success).to.be.true;
498 });
499}
500
501export async function setOffchainSchemaExpectFailure(sender: IKeyringPair, collectionId: number, data: number[]) {
502 await usingApi(async (api) => {
503 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));
504 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
505 });
506}
490507
491export interface CreateFungibleData {508export interface CreateFungibleData {
492 readonly Value: bigint;509 readonly Value: bigint;
863 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();880 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();
864};881};
882
883export async function queryCollectionExpectSuccess(collectionId: number): Promise<ICollectionInterface> {
884 return await usingApi(async (api) => {
885 return (await api.query.nft.collection(collectionId)) as unknown as ICollectionInterface;
886 });
887}
865888