--- 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": "", --- /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); + }); +}); --- 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 { + return await usingApi(async (api) => { + return (await api.query.nft.collection(collectionId)) as unknown as ICollectionInterface; + }); +}