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

111 lines
3.4 KiB
JavaScript

const app = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context');
describe('ticket changeState()', () => {
const salesPersonId = 18;
const employeeId = 1;
const productionId = 49;
let activeCtx = {
accessToken: {userId: 9},
};
let ctx = {req: activeCtx};
let ticket;
beforeAll(async done => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
done();
});
beforeEach(async done => {
try {
let originalTicket = await app.models.Ticket.findOne({where: {id: 16}});
originalTicket.id = null;
ticket = await app.models.Ticket.create(originalTicket);
} catch (error) {
console.error(error);
}
done();
});
afterEach(async done => {
try {
await app.models.Ticket.destroyById(ticket.id);
} catch (error) {
console.error(error);
}
done();
});
afterAll(async done => {
try {
await app.models.Ticket.destroyById(ticket.id);
} catch (error) {
console.error(error);
}
done();
});
it('should throw if the ticket is not editable and the user isnt production', async() => {
activeCtx.accessToken.userId = salesPersonId;
let params = {ticketFk: 2, stateFk: 3};
let errCode;
try {
await app.models.TicketTracking.changeState(ctx, params);
} catch (e) {
errCode = e.code;
}
expect(errCode).toBe('ACCESS_DENIED');
});
it('should throw an error if a worker with employee role attemps to a forbidden state', async() => {
activeCtx.accessToken.userId = employeeId;
let params = {ticketFk: 11, stateFk: 13};
let errCode;
try {
await app.models.TicketTracking.changeState(ctx, params);
} catch (e) {
errCode = e.code;
}
expect(errCode).toBe('ACCESS_DENIED');
});
it('should be able to create a ticket tracking line for a not editable ticket if the user has the production role', async() => {
activeCtx.accessToken.userId = productionId;
let params = {ticketFk: ticket.id, stateFk: 3};
let ticketTracking = await app.models.TicketTracking.changeState(ctx, params);
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();
// restores
await app.models.TicketTracking.destroyById(ticketTracking.__data.id);
});
it('should update the ticket tracking line when the user is salesperson, uses the state assigned and a valid worker id', async() => {
let ctx = {req: {accessToken: {userId: 18}}};
let assignedState = await app.models.State.findOne({where: {code: 'PICKER_DESIGNED'}});
let params = {ticketFk: ticket.id, stateFk: assignedState.id, workerFk: 1};
let res = await app.models.TicketTracking.changeState(ctx, params);
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();
});
});