2021-09-21 16:48:59 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2019-08-12 10:31:31 +00:00
|
|
|
|
|
|
|
describe('state isEditable()', () => {
|
2020-02-06 10:50:32 +00:00
|
|
|
it('should return false if the state is not editable by a specific role', async() => {
|
2021-09-21 16:48:59 +00:00
|
|
|
const tx = await models.State.beginTransaction({});
|
2019-08-12 10:31:31 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const salesPersonRole = 18;
|
|
|
|
const onDeliveryState = 13;
|
|
|
|
const ctx = {req: {accessToken: {userId: salesPersonRole}}};
|
|
|
|
const result = await models.State.isEditable(ctx, onDeliveryState, options);
|
|
|
|
|
|
|
|
expect(result).toBe(false);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-08-12 10:31:31 +00:00
|
|
|
});
|
|
|
|
|
2020-02-06 10:50:32 +00:00
|
|
|
it('should return true if the state is editable by a specific role', async() => {
|
2021-09-21 16:48:59 +00:00
|
|
|
const tx = await models.State.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const salesPersonRole = 18;
|
|
|
|
const asignedState = 20;
|
|
|
|
const ctx = {req: {accessToken: {userId: salesPersonRole}}};
|
|
|
|
const result = await models.State.isEditable(ctx, asignedState, options);
|
2019-08-12 10:31:31 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
expect(result).toBe(true);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-08-12 10:31:31 +00:00
|
|
|
});
|
|
|
|
|
2020-02-06 10:50:32 +00:00
|
|
|
it('should return true again if the state is editable by a specific role', async() => {
|
2021-09-21 16:48:59 +00:00
|
|
|
const tx = await models.State.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const employeeRole = 1;
|
|
|
|
const fixingState = 1;
|
|
|
|
const ctx = {req: {accessToken: {userId: employeeRole}}};
|
|
|
|
const result = await models.State.isEditable(ctx, fixingState, options);
|
|
|
|
|
|
|
|
expect(result).toBe(true);
|
2019-08-12 10:31:31 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-08-12 10:31:31 +00:00
|
|
|
});
|
|
|
|
|
2020-02-06 10:50:32 +00:00
|
|
|
it('should return false if the state is not editable for the given role', async() => {
|
2021-09-21 16:48:59 +00:00
|
|
|
const tx = await models.State.beginTransaction({});
|
2019-08-12 10:31:31 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const employeeRole = 1;
|
|
|
|
const asignedState = 13;
|
|
|
|
const ctx = {req: {accessToken: {userId: employeeRole}}};
|
|
|
|
const result = await models.State.isEditable(ctx, asignedState, options);
|
|
|
|
|
|
|
|
expect(result).toBe(false);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-08-12 10:31:31 +00:00
|
|
|
});
|
|
|
|
|
2020-02-06 10:50:32 +00:00
|
|
|
it('should return true if the state is editable for the given role', async() => {
|
2021-09-21 16:48:59 +00:00
|
|
|
const tx = await models.State.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const productionRole = 49;
|
|
|
|
const onDeliveryState = 13;
|
|
|
|
const ctx = {req: {accessToken: {userId: productionRole}}};
|
|
|
|
const result = await models.State.isEditable(ctx, onDeliveryState, options);
|
2020-02-06 10:50:32 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
expect(result).toBe(true);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2020-02-06 10:50:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true if the ticket is editable, the role is salesPerson and the ticket state is printed', async() => {
|
2021-09-21 16:48:59 +00:00
|
|
|
const tx = await models.State.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const salesPersonRole = 18;
|
|
|
|
const printedState = 4;
|
|
|
|
const okState = 3;
|
|
|
|
const ctx = {req: {accessToken: {userId: salesPersonRole}}};
|
|
|
|
|
|
|
|
const canEditCurrent = await models.State.isEditable(ctx, printedState, options);
|
|
|
|
const canAsignNew = await models.State.isEditable(ctx, okState, options);
|
|
|
|
const result = canEditCurrent && canAsignNew;
|
2020-02-06 10:50:32 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
expect(result).toBe(true);
|
2019-08-12 10:31:31 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-08-12 10:31:31 +00:00
|
|
|
});
|
|
|
|
});
|