const app = require('vn-loopback/server/server'); const LoopBackContext = require('loopback-context'); const models = app.models; describe('workerTimeControl add/delete timeEntry()', () => { const HHRRId = 37; const teamBossId = 13; const employeeId = 1; const salesPersonId = 106; const salesBossId = 19; let activeCtx = { accessToken: {userId: 50}, }; let ctx = {req: activeCtx}; beforeAll(() => { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx }); }); it('should fail to add a time entry if the target user is not a subordinate', async() => { activeCtx.accessToken.userId = employeeId; const workerId = 2; let error; try { ctx.args = {timed: new Date(), direction: 'in'}; await models.WorkerTimeControl.addTimeEntry(ctx, workerId); } 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() => { activeCtx.accessToken.userId = employeeId; const workerId = employeeId; let error; try { ctx.args = {timed: new Date(), direction: 'in'}; await models.WorkerTimeControl.addTimeEntry(ctx, workerId); } 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 himself', async() => { activeCtx.accessToken.userId = teamBossId; const workerId = teamBossId; const tx = await models.WorkerTimeControl.beginTransaction({}); try { const options = {transaction: tx}; const todayAtSix = new Date(); todayAtSix.setHours(18, 30, 0, 0); ctx.args = {timed: todayAtSix, direction: 'in'}; const createdTimeEntry = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); expect(createdTimeEntry.id).toBeDefined(); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should try but fail to delete his own time entry', async() => { activeCtx.accessToken.userId = salesBossId; const workerId = salesBossId; let error; const tx = await models.WorkerTimeControl.beginTransaction({}); try { const options = {transaction: tx}; const todayAtSeven = new Date(); todayAtSeven.setHours(19, 30, 0, 0); ctx.args = {timed: todayAtSeven, direction: 'in'}; const createdTimeEntry = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); activeCtx.accessToken.userId = salesPersonId; await models.WorkerTimeControl.deleteTimeEntry(ctx, createdTimeEntry.id, options); await tx.rollback(); } catch (e) { error = e; await tx.rollback(); } 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 himself', async() => { activeCtx.accessToken.userId = teamBossId; const workerId = teamBossId; const tx = await models.WorkerTimeControl.beginTransaction({}); try { const options = {transaction: tx}; const todayAtFive = new Date(); todayAtFive.setHours(17, 30, 0, 0); ctx.args = {timed: todayAtFive, direction: 'in'}; const createdTimeEntry = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); expect(createdTimeEntry.id).toBeDefined(); await models.WorkerTimeControl.deleteTimeEntry(ctx, createdTimeEntry.id, options); const deletedTimeEntry = await models.WorkerTimeControl.findById(createdTimeEntry.id, null, options); expect(deletedTimeEntry).toBeNull(); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should delete the created time entry for the team boss as HHRR', async() => { activeCtx.accessToken.userId = HHRRId; const workerId = teamBossId; const tx = await models.WorkerTimeControl.beginTransaction({}); try { const options = {transaction: tx}; const todayAtFive = new Date(); todayAtFive.setHours(17, 30, 0, 0); ctx.args = {timed: todayAtFive, direction: 'in'}; const createdTimeEntry = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); expect(createdTimeEntry.id).toBeDefined(); await models.WorkerTimeControl.deleteTimeEntry(ctx, createdTimeEntry.id, options); const deletedTimeEntry = await models.WorkerTimeControl.findById(createdTimeEntry.id, null, options); expect(deletedTimeEntry).toBeNull(); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should edit the created time entry for the team boss as HHRR', async() => { activeCtx.accessToken.userId = HHRRId; const workerId = teamBossId; const tx = await models.WorkerTimeControl.beginTransaction({}); try { const options = {transaction: tx}; const todayAtFive = new Date(); todayAtFive.setHours(17, 30, 0, 0); ctx.args = {timed: todayAtFive, direction: 'in'}; const createdTimeEntry = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); expect(createdTimeEntry.id).toBeDefined(); ctx.args = {direction: 'out'}; const updatedTimeEntry = await models.WorkerTimeControl.updateTimeEntry(ctx, createdTimeEntry.id, options); // const deletedTimeEntry = await models.WorkerTimeControl.findById(createdTimeEntry.id, null, options); expect(updatedTimeEntry.direction).toEqual('out'); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); });