git.delta.rocks / unique-network / refs/commits / 1a514428bdf0

difftreelog

source

tests/src/setSchemaVersion.test.ts6.2 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617// https://unique-network.readthedocs.io/en/latest/jsapi.html#setschemaversion18import {ApiPromise, Keyring} from '@polkadot/api';19import {IKeyringPair} from '@polkadot/types/types';20import chai from 'chai';21import chaiAsPromised from 'chai-as-promised';22import usingApi, {submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';23import {24  createCollectionExpectSuccess,25  destroyCollectionExpectSuccess,26  getCreatedCollectionCount,27  getCreateItemResult,28  getDetailedCollectionInfo,29  addCollectionAdminExpectSuccess,30} from './util/helpers';3132chai.use(chaiAsPromised);33const expect = chai.expect;3435let alice: IKeyringPair;36let bob: IKeyringPair;37let charlie: IKeyringPair;3839/*401. We create collection.412. Save just created collection id.423. Use this id for setSchemaVersion.43*/44describe('setSchemaVersion positive', () => {45  let tx;46  before(async () => {47    await usingApi(async () => {48      const keyring = new Keyring({type: 'sr25519'});49      alice = keyring.addFromUri('//Alice');50    });51  });52  it('execute setSchemaVersion with image url and unique ', async () => {53    await usingApi(async (api: ApiPromise) => {54      const collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});55      tx = api.tx.unique.setSchemaVersion(collectionIdForTesting, 'Unique');56      const events = await submitTransactionAsync(alice, tx);57      const result = getCreateItemResult(events);58      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);59      // tslint:disable-next-line:no-unused-expression60      expect(result.success).to.be.true;61      // tslint:disable-next-line:no-unused-expression62      expect(collectionInfo).to.be.exist;63      // tslint:disable-next-line:no-unused-expression64      expect(collectionInfo ? collectionInfo.schemaVersion.toString() : '').to.be.equal('Unique');65    });66  });67});6869describe('Collection admin setSchemaVersion positive', () => {70  let tx;71  let collectionIdForTesting: any;7273  before(async () => {74    await usingApi(async () => {75      const keyring = new Keyring({type: 'sr25519'});76      alice = keyring.addFromUri('//Alice');77      bob = keyring.addFromUri('//Bob');78      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});79      await addCollectionAdminExpectSuccess(alice, collectionIdForTesting, bob.address);80    });81  });82  it('execute setSchemaVersion with image url and unique ', async () => {83    await usingApi(async (api: ApiPromise) => {84      tx = api.tx.unique.setSchemaVersion(collectionIdForTesting, 'Unique');85      const events = await submitTransactionAsync(bob, tx);86      const result = getCreateItemResult(events);87      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);88      // tslint:disable-next-line:no-unused-expression89      expect(result.success).to.be.true;90      // tslint:disable-next-line:no-unused-expression91      expect(collectionInfo).to.be.exist;92      // tslint:disable-next-line:no-unused-expression93      expect(collectionInfo ? collectionInfo.schemaVersion.toString() : '').to.be.equal('Unique');94    });95  });9697  it('validate schema version with just entered data', async () => {98    await usingApi(async (api: ApiPromise) => {99      tx = api.tx.unique.setSchemaVersion(collectionIdForTesting, 'ImageURL');100      const events = await submitTransactionAsync(bob, tx);101      const result = getCreateItemResult(events);102      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);103      // tslint:disable-next-line:no-unused-expression104      expect(result.success).to.be.true;105      // tslint:disable-next-line:no-unused-expression106      expect(collectionInfo).to.be.exist;107      // tslint:disable-next-line:no-unused-expression108      expect(collectionInfo ? collectionInfo.schemaVersion.toString() : '').to.be.equal('ImageURL');109    });110  });111});112113describe('setSchemaVersion negative', () => {114  let tx;115  let collectionIdForTesting: any;116  before(async () => {117    await usingApi(async () => {118      const keyring = new Keyring({type: 'sr25519'});119      alice = keyring.addFromUri('//Alice');120      charlie = keyring.addFromUri('//Charlie');121      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});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.unique.setSchemaVersion(nonExistedCollectionId, 'ImageURL');129      await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;130    });131  });132  it('execute setSchemaVersion for deleted collection', async () => {133    await usingApi(async (api: ApiPromise) => {134      await destroyCollectionExpectSuccess(collectionIdForTesting);135      tx = api.tx.unique.setSchemaVersion(collectionIdForTesting, 'ImageURL');136      await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;137    });138  });139  it('Regular user can`t execute setSchemaVersion with image url and unique ', async () => {140    await usingApi(async (api: ApiPromise) => {141      tx = api.tx.unique.setSchemaVersion(collectionIdForTesting, 'Unique');142      await expect(submitTransactionAsync(charlie, tx)).to.be.rejected;143    });144  });145});