Skip to content

Commit

Permalink
fix: use proper permissions to reschedule
Browse files Browse the repository at this point in the history
  • Loading branch information
monitz87 committed Aug 2, 2021
1 parent d5fe33f commit 5cbed7a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 117 deletions.
74 changes: 10 additions & 64 deletions src/api/procedures/__tests__/rescheduleInstruction.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { u64 } from '@polkadot/types';
import BigNumber from 'bignumber.js';
import { TxTags } from 'polymesh-types/types';
import sinon from 'sinon';

import {
getAuthorization,
Params,
prepareRescheduleInstruction,
prepareStorage,
Storage,
} from '~/api/procedures/rescheduleInstruction';
import { Params, prepareRescheduleInstruction } from '~/api/procedures/rescheduleInstruction';
import { Context, Instruction } from '~/internal';
import { dsMockUtils, entityMockUtils, procedureMockUtils } from '~/testUtils/mocks';
import { Mocked } from '~/testUtils/types';
import { InstructionDetails, InstructionStatus, RoleType } from '~/types';
import { InstructionStatus } from '~/types';
import * as utilsConversionModule from '~/utils/conversion';

jest.mock(
Expand Down Expand Up @@ -59,12 +52,14 @@ describe('rescheduleInstruction procedure', () => {
});

test('should throw an error if the instruction is not Failed', async () => {
const proc = procedureMockUtils.getInstance<Params, Instruction, Storage>(mockContext, {
instruction: entityMockUtils.getInstructionInstance(),
instructionDetails: {
status: InstructionStatus.Pending,
} as InstructionDetails,
entityMockUtils.configureMocks({
instructionOptions: {
details: {
status: InstructionStatus.Pending,
},
},
});
const proc = procedureMockUtils.getInstance<Params, Instruction>(mockContext);

return expect(
prepareRescheduleInstruction.call(proc, {
Expand All @@ -74,12 +69,7 @@ describe('rescheduleInstruction procedure', () => {
});

test('should add a reschedule Instruction transaction to the queue', async () => {
const proc = procedureMockUtils.getInstance<Params, Instruction, Storage>(mockContext, {
instruction: entityMockUtils.getInstructionInstance(),
instructionDetails: {
status: InstructionStatus.Failed,
} as InstructionDetails,
});
const proc = procedureMockUtils.getInstance<Params, Instruction>(mockContext);

const transaction = dsMockUtils.createTxStub('settlement', 'rescheduleInstruction');

Expand All @@ -91,48 +81,4 @@ describe('rescheduleInstruction procedure', () => {

sinon.assert.calledWith(addTransactionStub, transaction, {}, rawId);
});

describe('getAuthorization', () => {
test('should return the appropriate roles and permissions', () => {
const venueId = new BigNumber(2);
const proc = procedureMockUtils.getInstance<Params, Instruction, Storage>(mockContext, {
instruction: entityMockUtils.getInstructionInstance(),
instructionDetails: ({
venue: entityMockUtils.getVenueInstance({ id: venueId }),
} as unknown) as InstructionDetails,
});
const boundFunc = getAuthorization.bind(proc);

expect(boundFunc()).toEqual({
roles: [{ type: RoleType.VenueOwner, venueId }],
permissions: {
transactions: [TxTags.settlement.RescheduleInstruction],
},
});
});
});

describe('prepareStorage', () => {
test('should return the instruction and its details', async () => {
const proc = procedureMockUtils.getInstance<Params, Instruction, Storage>(mockContext);
const boundFunc = prepareStorage.bind(proc);
const details = { venue: entityMockUtils.getVenueInstance({ id: new BigNumber(2) }) };

entityMockUtils.configureMocks({
instructionOptions: {
id,
details,
},
});

const result = await boundFunc({
id,
});

expect(result).toEqual({
instruction: entityMockUtils.getInstructionInstance(),
instructionDetails: details,
});
});
});
});
61 changes: 8 additions & 53 deletions src/api/procedures/rescheduleInstruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import BigNumber from 'bignumber.js';
import { TxTags } from 'polymesh-types/types';

import { Instruction, PolymeshError, Procedure } from '~/internal';
import { ErrorCode, InstructionDetails, InstructionStatus, RoleType } from '~/types';
import { ProcedureAuthorization } from '~/types/internal';
import { ErrorCode, InstructionStatus } from '~/types';
import { numberToU64 } from '~/utils/conversion';

/**
Expand All @@ -13,33 +12,24 @@ export interface Params {
id: BigNumber;
}

/**
* @hidden
*/
export interface Storage {
instructionDetails: InstructionDetails;
instruction: Instruction;
}

/**
* @hidden
*/
export async function prepareRescheduleInstruction(
this: Procedure<Params, Instruction, Storage>,
this: Procedure<Params, Instruction>,
args: Params
): Promise<Instruction> {
const {
context: {
polymeshApi: { tx },
},
context,
storage: {
instructionDetails: { status },
instruction,
},
} = this;
const { id } = args;

const instruction = new Instruction({ id }, context);
const { status } = await instruction.details();

if (status !== InstructionStatus.Failed) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
Expand All @@ -60,44 +50,9 @@ export async function prepareRescheduleInstruction(
/**
* @hidden
*/
export function getAuthorization(
this: Procedure<Params, Instruction, Storage>
): ProcedureAuthorization {
const {
storage: {
instructionDetails: {
venue: { id },
},
},
} = this;

return {
roles: [{ type: RoleType.VenueOwner, venueId: id }],
export const rescheduleInstruction = (): Procedure<Params, Instruction> =>
new Procedure(prepareRescheduleInstruction, {
permissions: {
transactions: [TxTags.settlement.RescheduleInstruction],
},
};
}

/**
* @hidden
*/
export async function prepareStorage(
this: Procedure<Params, Instruction, Storage>,
{ id }: Params
): Promise<Storage> {
const { context } = this;
const instruction = new Instruction({ id }, context);
const instructionDetails = await instruction.details();

return {
instruction,
instructionDetails,
};
}

/**
* @hidden
*/
export const rescheduleInstruction = (): Procedure<Params, Instruction, Storage> =>
new Procedure(prepareRescheduleInstruction, getAuthorization);
});

0 comments on commit 5cbed7a

Please sign in to comment.