const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); describe('ticket changeState()', () => { const salesPersonId = 18; const employeeId = 1; const productionId = 49; const activeCtx = { 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 }; beforeAll(async done => { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx }); done(); }); it('should throw if the ticket is not editable and the user isnt production', async() => { const tx = await models.TicketTracking.beginTransaction({}); let error; try { const options = {transaction: tx}; activeCtx.accessToken.userId = salesPersonId; const params = {ticketFk: 2, stateFk: 3}; await models.TicketTracking.changeState(ctx, params, options); await tx.rollback(); } catch (e) { await tx.rollback(); error = e; } expect(error.code).toBe('ACCESS_DENIED'); }); 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; 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(); } catch (e) { await tx.rollback(); error = e; } expect(error.code).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() => { 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; } }); 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; } }); });