const app = require('vn-loopback/server/server'); describe('workerTimeControl addTimeEntry()', () => { let insertedTime; 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 }; await app.models.WorkerTimeControl.addTimeEntry(ctx, data); insertedTime = await app.models.WorkerTimeControl.findOne({where: {timed: data.timed}}); let createdTimeEntry = await app.models.WorkerTimeControl.findById(insertedTime.id); 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}}}; let createdTimeEntry = await app.models.WorkerTimeControl.findById(insertedTime.id); expect(createdTimeEntry).toBeDefined(); 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}}}; let createdTimeEntry = await app.models.WorkerTimeControl.findById(insertedTime.id); expect(createdTimeEntry).toBeDefined(); ctx.req.accessToken.userId = HHRRId; await app.models.WorkerTimeControl.deleteTimeEntry(ctx, insertedTime.id); createdTimeEntry = await app.models.WorkerTimeControl.findById(insertedTime.id); expect(createdTimeEntry).toBeNull(); }); });