salix/modules/ticket/back/methods/ticket-tracking/specs/changeState.spec.js

136 lines
4.2 KiB
JavaScript
Raw Normal View History

const models = require('vn-loopback/server/server').models;
2020-10-08 14:00:19 +00:00
const LoopBackContext = require('loopback-context');
2018-10-25 09:50:18 +00:00
describe('ticket changeState()', () => {
2020-10-08 14:00:19 +00:00
const salesPersonId = 18;
const employeeId = 1;
const productionId = 49;
const activeCtx = {
2020-10-08 14:00:19 +00:00
accessToken: {userId: 9},
};
const ctx = {req: activeCtx};
const now = new Date();
const sampleTicket = {
shipped: now,
landed: now,
nickname: 'Many Places',
packages: 0,
updated: now,
priority: 1,
zoneFk: 3,
zonePrice: 5,
zoneBonus: 1,
totalWithVat: 120,
totalWithoutVat: 100,
clientFk: 1106,
warehouseFk: 1,
addressFk: 126,
routeFk: 6,
companyFk: 442,
agencyModeFk: 7
};
2018-10-25 09:50:18 +00:00
beforeAll(async done => {
2020-10-08 14:00:19 +00:00
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
2020-10-15 16:57:27 +00:00
done();
2020-10-08 14:00:19 +00:00
});
it('should throw if the ticket is not editable and the user isnt production', async() => {
const tx = await models.TicketTracking.beginTransaction({});
let error;
2018-10-25 09:50:18 +00:00
2020-10-15 16:57:27 +00:00
try {
const options = {transaction: tx};
activeCtx.accessToken.userId = salesPersonId;
const params = {ticketFk: 2, stateFk: 3};
2018-10-25 09:50:18 +00:00
await models.TicketTracking.changeState(ctx, params, options);
2019-01-29 15:37:59 +00:00
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.code).toBe('ACCESS_DENIED');
});
2019-08-19 05:56:20 +00:00
it('should throw an error if a worker with employee role attemps to a forbidden state', async() => {
const tx = await models.TicketTracking.beginTransaction({});
let error;
2019-01-29 15:37:59 +00:00
2019-01-22 09:04:42 +00:00
try {
const options = {transaction: tx};
activeCtx.accessToken.userId = employeeId;
const params = {ticketFk: 11, stateFk: 13};
await models.TicketTracking.changeState(ctx, params, options);
await tx.rollback();
2019-01-22 09:04:42 +00:00
} catch (e) {
await tx.rollback();
error = e;
2019-01-22 09:04:42 +00:00
}
expect(error.code).toBe('ACCESS_DENIED');
2019-01-22 09:04:42 +00:00
});
it('should be able to create a ticket tracking line for a not editable ticket if the user has the production role', async() => {
const tx = await models.TicketTracking.beginTransaction({});
try {
const options = {transaction: tx};
const ticket = await models.Ticket.create(sampleTicket, options);
activeCtx.accessToken.userId = productionId;
const params = {ticketFk: ticket.id, stateFk: 3};
const ticketTracking = await models.TicketTracking.changeState(ctx, params, options);
expect(ticketTracking.__data.ticketFk).toBe(params.ticketFk);
expect(ticketTracking.__data.stateFk).toBe(params.stateFk);
expect(ticketTracking.__data.workerFk).toBe(49);
expect(ticketTracking.__data.id).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
2019-01-22 09:04:42 +00:00
});
2020-10-08 14:00:19 +00:00
it('should update the ticket tracking line when the user is salesperson, uses the state assigned and a valid worker id', async() => {
const tx = await models.TicketTracking.beginTransaction({});
try {
const options = {transaction: tx};
const ticket = await models.Ticket.create(sampleTicket, options);
const ctx = {req: {accessToken: {userId: 18}}};
const assignedState = await models.State.findOne({where: {code: 'PICKER_DESIGNED'}}, options);
const params = {ticketFk: ticket.id, stateFk: assignedState.id, workerFk: 1};
const res = await models.TicketTracking.changeState(ctx, params, options);
expect(res.__data.ticketFk).toBe(params.ticketFk);
expect(res.__data.stateFk).toBe(params.stateFk);
expect(res.__data.workerFk).toBe(params.workerFk);
expect(res.__data.workerFk).toBe(1);
expect(res.__data.id).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});