diff --git a/back/methods/account/specs/login.spec.js b/back/methods/account/specs/login.spec.js index ffb19637f..59eea2612 100644 --- a/back/methods/account/specs/login.spec.js +++ b/back/methods/account/specs/login.spec.js @@ -3,15 +3,23 @@ const app = require('vn-loopback/server/server'); describe('account login()', () => { describe('when credentials are correct', () => { it('should return the token', async() => { - let response = await app.models.Account.login('employee', 'nightmare'); + let login = await app.models.Account.login('salesAssistant', 'nightmare'); + let accessToken = await app.models.AccessToken.findById(login.token); + let ctx = {req: {accessToken: accessToken}}; - expect(response.token).toBeDefined(); + expect(login.token).toBeDefined(); + + await app.models.Account.logout(ctx); }); it('should return the token if the user doesnt exist but the client does', async() => { - let response = await app.models.Account.login('PetterParker', 'nightmare'); + let login = await app.models.Account.login('PetterParker', 'nightmare'); + let accessToken = await app.models.AccessToken.findById(login.token); + let ctx = {req: {accessToken: accessToken}}; - expect(response.token).toBeDefined(); + expect(login.token).toBeDefined(); + + await app.models.Account.logout(ctx); }); }); diff --git a/back/methods/account/specs/logout.spec.js b/back/methods/account/specs/logout.spec.js index 5770be91f..b3d69d6ef 100644 --- a/back/methods/account/specs/logout.spec.js +++ b/back/methods/account/specs/logout.spec.js @@ -2,15 +2,15 @@ const app = require('vn-loopback/server/server'); describe('account logout()', () => { it('should logout and remove token after valid login', async() => { - let loginResponse = await app.models.Account.login('employee', 'nightmare'); + let loginResponse = await app.models.Account.login('buyer', 'nightmare'); let accessToken = await app.models.AccessToken.findById(loginResponse.token); let ctx = {req: {accessToken: accessToken}}; - let response = await app.models.Account.logout(ctx); - let afterToken = await app.models.AccessToken.findById(loginResponse.token); + let logoutResponse = await app.models.Account.logout(ctx); + let tokenAfterLogout = await app.models.AccessToken.findById(loginResponse.token); - expect(response).toBeTruthy(); - expect(afterToken).toBeNull(); + expect(logoutResponse).toBeTrue(); + expect(tokenAfterLogout).toBeNull(); }); it('should throw a 401 error when token is invalid', async() => { diff --git a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js index 8954e6d47..291d6f432 100644 --- a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js +++ b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js @@ -1,7 +1,8 @@ const app = require('vn-loopback/server/server'); describe('ticket componentUpdate()', () => { - const ticketId = 11; + const userID = 101; + const ticketID = 11; const today = new Date(); const tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1); @@ -13,39 +14,43 @@ describe('ticket componentUpdate()', () => { let componentOfSaleEight; beforeAll(async done => { - let deliveryComponenet = await app.models.Component.findOne({where: {code: 'delivery'}}); - deliveryComponentId = deliveryComponenet.id; - componentOfSaleSeven = `SELECT value FROM vn.saleComponent WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`; - componentOfSaleEight = `SELECT value FROM vn.saleComponent WHERE saleFk = 8 AND componentFk = ${deliveryComponentId}`; + try { + let deliveryComponenet = await app.models.Component.findOne({where: {code: 'delivery'}}); + deliveryComponentId = deliveryComponenet.id; + componentOfSaleSeven = `SELECT value FROM vn.saleComponent WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`; + componentOfSaleEight = `SELECT value FROM vn.saleComponent WHERE saleFk = 8 AND componentFk = ${deliveryComponentId}`; - [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven); - firstvalueBeforeChange = componentValue.value; + [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven); + firstvalueBeforeChange = componentValue.value; - [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight); - secondvalueBeforeChange = componentValue.value; + [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight); + secondvalueBeforeChange = componentValue.value; + } catch (error) { + console.error(error); + } done(); }); - it('should change the agencyMode to modify the sale components value', async() => { - const clientId = 102; - const addressId = 122; - const agencyModeId = 8; - const warehouseId = 1; - const zoneId = 5; + it('should change the agencyMode to modify the sale components value and then undo the changes', async() => { + const clientID = 102; + const addressID = 122; + const agencyModeID = 8; + const warehouseID = 1; + const zoneID = 5; const shipped = today; - const companyId = 442; + const companyID = 442; const isDeleted = false; const landed = tomorrow; const option = 1; let ctx = { - args: {clientFk: 102, - agencyModeFk: 8}, - req: {accessToken: {userId: 101}}}; + args: {clientFk: clientID, + agencyModeFk: agencyModeID}, + req: {accessToken: {userId: userID}}}; - await app.models.Ticket.componentUpdate(ctx, ticketId, clientId, agencyModeId, addressId, - zoneId, warehouseId, companyId, shipped, landed, isDeleted, option); + await app.models.Ticket.componentUpdate(ctx, ticketID, clientID, agencyModeID, addressID, + zoneID, warehouseID, companyID, shipped, landed, isDeleted, option); [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven); let firstvalueAfterChange = componentValue.value; @@ -55,33 +60,32 @@ describe('ticket componentUpdate()', () => { expect(firstvalueBeforeChange).not.toEqual(firstvalueAfterChange); expect(secondvalueBeforeChange).not.toEqual(secondvalueAfterChange); - }); - it('should change the agencyMode to go back to the originals sale components value', async() => { - const clientId = 102; - const addressId = 122; - const agencyModeId = 7; - const warehouseId = 1; - const zoneId = 3; - const shipped = today; - const companyId = 442; - const isDeleted = false; - const landed = tomorrow; - const option = 1; + // restores + const restores = { + clientID: 102, + addressID: 122, + agencyModeID: 7, + warehouseID: 1, + zoneID: 3, + shipped: today, + companyID: 442, + isDeleted: false, + landed: tomorrow, + option: 1, + }; - let ctx = { - args: {clientFk: 102, - agencyModeFk: 7}, - req: {accessToken: {userId: 101}}}; + ctx.clientFk = restores.clientID; + ctx.agencyModeFk = restores.agencyModeID; - await app.models.Ticket.componentUpdate(ctx, ticketId, clientId, agencyModeId, addressId, - zoneId, warehouseId, companyId, shipped, landed, isDeleted, option); + await app.models.Ticket.componentUpdate(ctx, ticketID, restores.clientID, restores.agencyModeID, restores.addressID, + restores.zoneID, restores.warehouseID, restores.companyID, restores.shipped, restores.landed, restores.isDeleted, restores.option); [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven); - let firstvalueAfterChange = componentValue.value; + firstvalueAfterChange = componentValue.value; [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight); - let secondvalueAfterChange = componentValue.value; + secondvalueAfterChange = componentValue.value; expect(firstvalueBeforeChange).toEqual(firstvalueAfterChange); expect(secondvalueBeforeChange).toEqual(secondvalueAfterChange); diff --git a/modules/ticket/back/methods/ticket/specs/restore.spec.js b/modules/ticket/back/methods/ticket/specs/restore.spec.js index 1abb52dc5..97cf78f91 100644 --- a/modules/ticket/back/methods/ticket/specs/restore.spec.js +++ b/modules/ticket/back/methods/ticket/specs/restore.spec.js @@ -1,20 +1,24 @@ const app = require('vn-loopback/server/server'); +const LoopBackContext = require('loopback-context'); const models = app.models; describe('ticket restore()', () => { const employeeUser = 110; - const ctx = { - req: { - accessToken: {userId: employeeUser}, - headers: { - origin: 'http://localhost:5000' - }, - __: () => {} - } + const activeCtx = { + accessToken: {userId: employeeUser}, + headers: { + origin: 'http://localhost:5000' + }, + __: () => {} }; + const ctx = {req: activeCtx}; + let createdTicket; beforeEach(async done => { + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); try { const sampleTicket = await models.Ticket.findById(11); sampleTicket.id = undefined;