1234567import { 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;2930313233343536describe('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 65 expect(result.success).to.be.true;66 67 expect(collectionInfo).to.be.exist;68 69 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 91 expect(result.success).to.be.true;92 93 expect(collectionInfo).to.be.exist;94 95 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 106 expect(result.success).to.be.true;107 108 expect(collectionInfo).to.be.exist;109 110 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 141 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});