salix/modules/worker/back/methods/worker-time-control/specs/timeEntry.spec.js

164 lines
4.8 KiB
JavaScript
Raw Normal View History

2019-11-18 10:00:09 +00:00
const app = require('vn-loopback/server/server');
2020-10-08 14:00:19 +00:00
const LoopBackContext = require('loopback-context');
2021-02-02 09:12:37 +00:00
describe('workerTimeControl add/delete timeEntry()', () => {
2020-10-08 14:00:19 +00:00
const HHRRId = 37;
const teamBossId = 13;
const employeeId = 1;
2021-02-02 09:07:13 +00:00
const salesPersonId = 106;
const salesBossId = 19;
2020-10-08 14:00:19 +00:00
let activeCtx = {
accessToken: {userId: 50},
};
let ctx = {req: activeCtx};
2019-11-18 10:00:09 +00:00
let timeEntry;
let createdTimeEntry;
2019-11-18 10:00:09 +00:00
2020-10-08 14:00:19 +00:00
afterEach(async() => {
if (createdTimeEntry) {
try {
await app.models.WorkerTimeControl.destroyById(createdTimeEntry.id);
} catch (error) {
console.error(error);
}
}
});
beforeAll(() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
2019-11-18 10:00:09 +00:00
it('should fail to add a time entry if the target user is not a subordinate', async() => {
2020-10-08 14:00:19 +00:00
activeCtx.accessToken.userId = employeeId;
2019-11-18 10:00:09 +00:00
let error;
let data = {
workerFk: 2,
timed: new Date()
};
try {
await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error.statusCode).toBe(400);
expect(error.message).toBe(`You don't have enough privileges`);
});
it('should fail to add if the current and the target user are the same and is not team boss', async() => {
2020-10-08 14:00:19 +00:00
activeCtx.accessToken.userId = employeeId;
2019-11-18 10:00:09 +00:00
let error;
let data = {
workerFk: 1,
timed: new Date()
};
try {
await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error.statusCode).toBe(400);
expect(error.message).toBe(`You don't have enough privileges`);
});
2020-10-08 14:00:19 +00:00
it('should add if the current user is team boss and the target user is a himself', async() => {
activeCtx.accessToken.userId = teamBossId;
let todayAtSix = new Date();
2019-11-18 10:00:09 +00:00
todayAtSix.setHours(18, 30, 0, 0);
let data = {
workerFk: teamBossId,
timed: todayAtSix
};
timeEntry = await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
2019-11-18 10:00:09 +00:00
createdTimeEntry = await app.models.WorkerTimeControl.findById(timeEntry.id);
2019-11-18 10:00:09 +00:00
expect(createdTimeEntry).toBeDefined();
});
2020-10-08 14:00:19 +00:00
it('should try but fail to delete his own time entry', async() => {
2021-02-02 09:07:13 +00:00
activeCtx.accessToken.userId = salesBossId;
2019-11-18 10:00:09 +00:00
let error;
2020-10-08 14:00:19 +00:00
let todayAtSeven = new Date();
todayAtSeven.setHours(19, 30, 0, 0);
let data = {
2021-02-02 09:07:13 +00:00
workerFk: salesPersonId,
2020-10-08 14:00:19 +00:00
timed: todayAtSeven
};
timeEntry = await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
createdTimeEntry = await app.models.WorkerTimeControl.findById(timeEntry.id);
2019-11-18 10:00:09 +00:00
try {
2021-02-02 09:07:13 +00:00
activeCtx.accessToken.userId = salesPersonId;
2019-11-18 10:00:09 +00:00
await app.models.WorkerTimeControl.deleteTimeEntry(ctx, createdTimeEntry.id);
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error.statusCode).toBe(400);
expect(error.message).toBe(`You don't have enough privileges`);
});
2021-02-02 09:07:13 +00:00
it('should delete the created time entry for the team boss as himself', async() => {
activeCtx.accessToken.userId = teamBossId;
let todayAtFive = new Date();
todayAtFive.setHours(17, 30, 0, 0);
let data = {
workerFk: teamBossId,
timed: todayAtFive
};
timeEntry = await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
createdTimeEntry = await app.models.WorkerTimeControl.findById(timeEntry.id);
expect(createdTimeEntry).toBeDefined();
await app.models.WorkerTimeControl.deleteTimeEntry(ctx, createdTimeEntry.id);
createdTimeEntry = await app.models.WorkerTimeControl.findById(timeEntry.id);
expect(createdTimeEntry).toBeNull();
});
2019-11-18 10:00:09 +00:00
it('should delete the created time entry for the team boss as HHRR', async() => {
2020-10-08 14:00:19 +00:00
activeCtx.accessToken.userId = HHRRId;
let todayAtFive = new Date();
todayAtFive.setHours(17, 30, 0, 0);
let data = {
workerFk: teamBossId,
timed: todayAtFive
};
timeEntry = await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
createdTimeEntry = await app.models.WorkerTimeControl.findById(timeEntry.id);
2019-11-18 10:00:09 +00:00
expect(createdTimeEntry).toBeDefined();
2020-10-08 14:00:19 +00:00
await app.models.WorkerTimeControl.deleteTimeEntry(ctx, createdTimeEntry.id);
2019-11-18 10:00:09 +00:00
createdTimeEntry = await app.models.WorkerTimeControl.findById(timeEntry.id);
2019-11-18 10:00:09 +00:00
expect(createdTimeEntry).toBeNull();
});
});