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

93 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-11-18 10:00:09 +00:00
const app = require('vn-loopback/server/server');
describe('workerTimeControl addTimeEntry()', () => {
let timeEntry;
let createdTimeEntry;
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() => {
let error;
let ctx = {req: {accessToken: {userId: 1}}};
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() => {
let error;
let ctx = {req: {accessToken: {userId: 1}}};
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`);
});
it('should add if the current user is team boss and the target user is a subordinate', async() => {
todayAtSix = new Date();
todayAtSix.setHours(18, 30, 0, 0);
let teamBossId = 13;
let ctx = {req: {accessToken: {userId: teamBossId}}};
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();
});
it('should try but fail to delete the created time entry for the team boss as team boss', async() => {
let error;
let teamBossId = 13;
let ctx = {req: {accessToken: {userId: teamBossId}}};
try {
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`);
});
it('should delete the created time entry for the team boss as HHRR', async() => {
let HHRRId = 37;
let ctx = {req: {accessToken: {userId: HHRRId}}};
expect(createdTimeEntry).toBeDefined();
ctx.req.accessToken.userId = HHRRId;
await app.models.WorkerTimeControl.deleteTimeEntry(ctx, timeEntry.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();
});
});