difftreelog
Merge pull request #101 from usetech-llc/feature/NFT-253_setOffchainSchema
in: master
Add tests for setOffchainSchema
3 files changed
tests/package.jsondiffbeforeafterboth41 "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.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",42 "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",43 "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",43 "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",44 "testSetOffchainSchema": "mocha --timeout 9999999 -r ts-node/register ./**/setOffchainSchema.test.ts",44 "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts"45 "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts"45 },46 },46 "author": "",47 "author": "",tests/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);
+ });
+});
tests/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;
+ });
+}