git.delta.rocks / unique-network / refs/commits / e01ca44f63c7

difftreelog

source

tests/src/setSchemaVersion.test.ts6.2 KiBsourcehistory
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56// https://unique-network.readthedocs.io/en/latest/jsapi.html#setschemaversion7import { ApiPromise, Keyring } from '@polkadot/api';8import { IKeyringPair } from '@polkadot/types/types';9import chai from 'chai';10import chaiAsPromised from 'chai-as-promised';11import usingApi, {submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';12import { ICollectionInterface } from './types';13import {14  createCollectionExpectSuccess,15  destroyCollectionExpectSuccess,16  getCreatedCollectionCount,17  getCreateItemResult,18  getDetailedCollectionInfo,19  addCollectionAdminExpectSuccess,20} from './util/helpers';2122chai.use(chaiAsPromised);23const expect = chai.expect;2425let alice: IKeyringPair;26let bob: IKeyringPair;27let charlie: IKeyringPair;28let collectionIdForTesting: number;2930/*311. We create collection.322. Save just created collection id.333. Use this id for setSchemaVersion.34*/3536describe('hooks', () => {37  before(async () => {38    await usingApi(async () => {39      const keyring = new Keyring({ type: 'sr25519' });40      alice = keyring.addFromUri('//Alice');41    });42  });43  it('choose or create collection for testing', async () => {44    await usingApi(async () => {45      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});46    });47  });48});4950describe('setSchemaVersion positive', () => {51  let tx;52  before(async () => {53    await usingApi(async () => {54      const keyring = new Keyring({ type: 'sr25519' });55      alice = keyring.addFromUri('//Alice');56    });57  });58  it('execute setSchemaVersion with image url and unique ', async () => {59    await usingApi(async (api: ApiPromise) => {60      tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Unique');61      const events = await submitTransactionAsync(alice, tx);62      const result = getCreateItemResult(events);63      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;64      // tslint:disable-next-line:no-unused-expression65      expect(result.success).to.be.true;66      // tslint:disable-next-line:no-unused-expression67      expect(collectionInfo).to.be.exist;68      // tslint:disable-next-line:no-unused-expression69      expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('Unique');70    });71  });72});7374describe('Collection admin setSchemaVersion positive', () => {75  let tx;76  before(async () => {77    await usingApi(async () => {78      const keyring = new Keyring({ type: 'sr25519' });79      alice = keyring.addFromUri('//Alice');80      bob = keyring.addFromUri('//Bob');81      await addCollectionAdminExpectSuccess(alice, collectionIdForTesting, bob);82    });83  });84  it('execute setSchemaVersion with image url and unique ', async () => {85    await usingApi(async (api: ApiPromise) => {86      tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Unique');87      const events = await submitTransactionAsync(bob, tx);88      const result = getCreateItemResult(events);89      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;90      // tslint:disable-next-line:no-unused-expression91      expect(result.success).to.be.true;92      // tslint:disable-next-line:no-unused-expression93      expect(collectionInfo).to.be.exist;94      // tslint:disable-next-line:no-unused-expression95      expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('Unique');96    });97  });9899  it('validate schema version with just entered data', async () => {100    await usingApi(async (api: ApiPromise) => {101      tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');102      const events = await submitTransactionAsync(bob, tx);103      const result = getCreateItemResult(events);104      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;105      // tslint:disable-next-line:no-unused-expression106      expect(result.success).to.be.true;107      // tslint:disable-next-line:no-unused-expression108      expect(collectionInfo).to.be.exist;109      // tslint:disable-next-line:no-unused-expression110      expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('ImageURL');111    });112  });113});114115describe('setSchemaVersion negative', () => {116  let tx;117  before(async () => {118    await usingApi(async () => {119      const keyring = new Keyring({ type: 'sr25519' });120      alice = keyring.addFromUri('//Alice');121      charlie = keyring.addFromUri('//Charlie');122    });123  });124  it('execute setSchemaVersion for not exists collection', async () => {125    await usingApi(async (api: ApiPromise) => {126      const collectionCount = await getCreatedCollectionCount(api);127      const nonExistedCollectionId = collectionCount + 1;128      tx = api.tx.nft.setSchemaVersion(nonExistedCollectionId, 'ImageURL');129      await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;130    });131  });132  it('execute setSchemaVersion with not correct schema version', async () => {133    await usingApi(async (api: ApiPromise) => {134      const consoleError = console.error;135      console.error = () => {};136      try {137        tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Test');138        await submitTransactionAsync(alice, tx);139      } catch (e) {140        // tslint:disable-next-line:no-unused-expression141        expect(e).to.be.exist;142      } finally {143        console.error = consoleError;144      }145    });146  });147  it('execute setSchemaVersion for deleted collection', async () => {148    await usingApi(async (api: ApiPromise) => {149      await destroyCollectionExpectSuccess(collectionIdForTesting);150      tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');151      await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;152    });153  });154  it('Regular user can`t execute setSchemaVersion with image url and unique ', async () => {155    await usingApi(async (api: ApiPromise) => {156      tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Unique');157      await expect(submitTransactionAsync(charlie, tx)).to.be.rejected;158    });159  });160});