From 8d1cf0c02f8032298001d220faa9959ff212ff43 Mon Sep 17 00:00:00 2001 From: gerard Date: Tue, 21 Aug 2018 11:27:42 +0200 Subject: [PATCH] Tarea #567 /ticket-tracking/changeState.js Backend unit tests --- .../methods/ticket-tracking/changeState.js | 3 +- .../ticket-tracking/specs/changeState.spec.js | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 services/ticket/common/methods/ticket-tracking/specs/changeState.spec.js diff --git a/services/ticket/common/methods/ticket-tracking/changeState.js b/services/ticket/common/methods/ticket-tracking/changeState.js index fbccacbbf..70e5d6202 100644 --- a/services/ticket/common/methods/ticket-tracking/changeState.js +++ b/services/ticket/common/methods/ticket-tracking/changeState.js @@ -8,7 +8,7 @@ module.exports = Self => { arg: 'params', type: 'object', required: true, - description: 'ticketFk', + description: 'ticketFk, stateFk', http: {source: 'body'} }], returns: { @@ -29,6 +29,7 @@ module.exports = Self => { if (ctx.req.accessToken) { let token = ctx.req.accessToken; let currentUserId = token && token.userId; + console.log(currentUserId); isProduction = await models.Account.hasRole(currentUserId, 'Production'); let worker = await models.Worker.findOne({where: {userFk: currentUserId}}); params.workerFk = worker.id; diff --git a/services/ticket/common/methods/ticket-tracking/specs/changeState.spec.js b/services/ticket/common/methods/ticket-tracking/specs/changeState.spec.js new file mode 100644 index 000000000..083deccb9 --- /dev/null +++ b/services/ticket/common/methods/ticket-tracking/specs/changeState.spec.js @@ -0,0 +1,41 @@ +const app = require(`${servicesDir}/ticket/server/server`); + +describe('ticket listSaleTracking()', () => { + it('should throw an error if the ticket is not editable and the user isnt production', async() => { + let ctx = {req: {accessToken: {userId: 110}}}; + let params = {ticketFk: 2, stateFk: 3}; + let error; + try { + await app.models.TicketTracking.changeState(ctx, params); + } catch (e) { + error = e; + } + + expect(error).toEqual(new Error(`You don't have enough privileges to change the state of this ticket`)); + }); + + it('should be able to create a ticket tracking line for a not editable ticket if the user has te production role', async() => { + let ctx = {req: {accessToken: {userId: 50}}}; + let params = {ticketFk: 20, stateFk: 3}; + + 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(50); + expect(res.__data.id).toBeDefined(); + }); + + it('return an array with the created ticket tracking line', async() => { + let ctx = {req: {accessToken: {userId: 108}}}; + let params = {ticketFk: 1, stateFk: 3}; + + 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(110); + expect(res.__data.id).toBeDefined(); + }); +}); +