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('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);
});
});

View File

@ -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() => {

View File

@ -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);

View File

@ -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;