fixed login/out problems at random + others
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2020-10-16 11:16:24 +02:00
parent c622175192
commit b96007aa62
4 changed files with 74 additions and 58 deletions

View File

@ -3,15 +3,23 @@ const app = require('vn-loopback/server/server');
describe('account login()', () => { describe('account login()', () => {
describe('when credentials are correct', () => { describe('when credentials are correct', () => {
it('should return the token', async() => { 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() => { 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);
}); });
}); });

View File

@ -2,15 +2,15 @@ const app = require('vn-loopback/server/server');
describe('account logout()', () => { describe('account logout()', () => {
it('should logout and remove token after valid login', async() => { 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 accessToken = await app.models.AccessToken.findById(loginResponse.token);
let ctx = {req: {accessToken: accessToken}}; let ctx = {req: {accessToken: accessToken}};
let response = await app.models.Account.logout(ctx); let logoutResponse = await app.models.Account.logout(ctx);
let afterToken = await app.models.AccessToken.findById(loginResponse.token); let tokenAfterLogout = await app.models.AccessToken.findById(loginResponse.token);
expect(response).toBeTruthy(); expect(logoutResponse).toBeTrue();
expect(afterToken).toBeNull(); expect(tokenAfterLogout).toBeNull();
}); });
it('should throw a 401 error when token is invalid', async() => { it('should throw a 401 error when token is invalid', async() => {

View File

@ -1,7 +1,8 @@
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');
describe('ticket componentUpdate()', () => { describe('ticket componentUpdate()', () => {
const ticketId = 11; const userID = 101;
const ticketID = 11;
const today = new Date(); const today = new Date();
const tomorrow = new Date(); const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1); tomorrow.setDate(tomorrow.getDate() + 1);
@ -13,6 +14,7 @@ describe('ticket componentUpdate()', () => {
let componentOfSaleEight; let componentOfSaleEight;
beforeAll(async done => { beforeAll(async done => {
try {
let deliveryComponenet = await app.models.Component.findOne({where: {code: 'delivery'}}); let deliveryComponenet = await app.models.Component.findOne({where: {code: 'delivery'}});
deliveryComponentId = deliveryComponenet.id; deliveryComponentId = deliveryComponenet.id;
componentOfSaleSeven = `SELECT value FROM vn.saleComponent WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`; componentOfSaleSeven = `SELECT value FROM vn.saleComponent WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`;
@ -23,29 +25,32 @@ describe('ticket componentUpdate()', () => {
[componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight); [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight);
secondvalueBeforeChange = componentValue.value; secondvalueBeforeChange = componentValue.value;
} catch (error) {
console.error(error);
}
done(); done();
}); });
it('should change the agencyMode to modify the sale components value', async() => { it('should change the agencyMode to modify the sale components value and then undo the changes', async() => {
const clientId = 102; const clientID = 102;
const addressId = 122; const addressID = 122;
const agencyModeId = 8; const agencyModeID = 8;
const warehouseId = 1; const warehouseID = 1;
const zoneId = 5; const zoneID = 5;
const shipped = today; const shipped = today;
const companyId = 442; const companyID = 442;
const isDeleted = false; const isDeleted = false;
const landed = tomorrow; const landed = tomorrow;
const option = 1; const option = 1;
let ctx = { let ctx = {
args: {clientFk: 102, args: {clientFk: clientID,
agencyModeFk: 8}, agencyModeFk: agencyModeID},
req: {accessToken: {userId: 101}}}; req: {accessToken: {userId: userID}}};
await app.models.Ticket.componentUpdate(ctx, ticketId, clientId, agencyModeId, addressId, await app.models.Ticket.componentUpdate(ctx, ticketID, clientID, agencyModeID, addressID,
zoneId, warehouseId, companyId, shipped, landed, isDeleted, option); zoneID, warehouseID, companyID, shipped, landed, isDeleted, option);
[componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven); [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven);
let firstvalueAfterChange = componentValue.value; let firstvalueAfterChange = componentValue.value;
@ -55,33 +60,32 @@ describe('ticket componentUpdate()', () => {
expect(firstvalueBeforeChange).not.toEqual(firstvalueAfterChange); expect(firstvalueBeforeChange).not.toEqual(firstvalueAfterChange);
expect(secondvalueBeforeChange).not.toEqual(secondvalueAfterChange); expect(secondvalueBeforeChange).not.toEqual(secondvalueAfterChange);
});
it('should change the agencyMode to go back to the originals sale components value', async() => { // restores
const clientId = 102; const restores = {
const addressId = 122; clientID: 102,
const agencyModeId = 7; addressID: 122,
const warehouseId = 1; agencyModeID: 7,
const zoneId = 3; warehouseID: 1,
const shipped = today; zoneID: 3,
const companyId = 442; shipped: today,
const isDeleted = false; companyID: 442,
const landed = tomorrow; isDeleted: false,
const option = 1; landed: tomorrow,
option: 1,
};
let ctx = { ctx.clientFk = restores.clientID;
args: {clientFk: 102, ctx.agencyModeFk = restores.agencyModeID;
agencyModeFk: 7},
req: {accessToken: {userId: 101}}};
await app.models.Ticket.componentUpdate(ctx, ticketId, clientId, agencyModeId, addressId, await app.models.Ticket.componentUpdate(ctx, ticketID, restores.clientID, restores.agencyModeID, restores.addressID,
zoneId, warehouseId, companyId, shipped, landed, isDeleted, option); restores.zoneID, restores.warehouseID, restores.companyID, restores.shipped, restores.landed, restores.isDeleted, restores.option);
[componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven); [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven);
let firstvalueAfterChange = componentValue.value; firstvalueAfterChange = componentValue.value;
[componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight); [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight);
let secondvalueAfterChange = componentValue.value; secondvalueAfterChange = componentValue.value;
expect(firstvalueBeforeChange).toEqual(firstvalueAfterChange); expect(firstvalueBeforeChange).toEqual(firstvalueAfterChange);
expect(secondvalueBeforeChange).toEqual(secondvalueAfterChange); expect(secondvalueBeforeChange).toEqual(secondvalueAfterChange);

View File

@ -1,20 +1,24 @@
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context');
const models = app.models; const models = app.models;
describe('ticket restore()', () => { describe('ticket restore()', () => {
const employeeUser = 110; const employeeUser = 110;
const ctx = { const activeCtx = {
req: {
accessToken: {userId: employeeUser}, accessToken: {userId: employeeUser},
headers: { headers: {
origin: 'http://localhost:5000' origin: 'http://localhost:5000'
}, },
__: () => {} __: () => {}
}
}; };
const ctx = {req: activeCtx};
let createdTicket; let createdTicket;
beforeEach(async done => { beforeEach(async done => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
try { try {
const sampleTicket = await models.Ticket.findById(11); const sampleTicket = await models.Ticket.findById(11);
sampleTicket.id = undefined; sampleTicket.id = undefined;