|
| 1 | +const EducationTask = require('../models/educationTask'); |
| 2 | +const { mockReq, mockRes } = require('../test'); |
| 3 | +const studentTaskController = require('./studentTaskController'); |
| 4 | + |
| 5 | +const VALID_TASK_ID = '507f1f77bcf86cd799439011'; |
| 6 | +const VALID_STUDENT_ID = '65cf6c3706d8ac105827bb2e'; |
| 7 | + |
| 8 | +// Shared helpers to reduce repetition |
| 9 | +const makeTask = (overrides = {}) => ({ |
| 10 | + status: 'assigned', |
| 11 | + loggedHours: 0, |
| 12 | + suggestedTotalHours: 5, |
| 13 | + ...overrides, |
| 14 | +}); |
| 15 | + |
| 16 | +const makeUpdated = (overrides = {}) => ({ |
| 17 | + loggedHours: 1, |
| 18 | + suggestedTotalHours: 5, |
| 19 | + status: 'in_progress', |
| 20 | + ...overrides, |
| 21 | +}); |
| 22 | + |
| 23 | +const spyFindOne = (result) => jest.spyOn(EducationTask, 'findOne').mockResolvedValueOnce(result); |
| 24 | +const spyFindOneAndUpdate = (result) => |
| 25 | + jest.spyOn(EducationTask, 'findOneAndUpdate').mockResolvedValueOnce(result); |
| 26 | + |
| 27 | +describe('studentTaskController - logHours', () => { |
| 28 | + let logHours; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + logHours = studentTaskController().logHours; |
| 32 | + mockReq.params.taskId = VALID_TASK_ID; |
| 33 | + mockReq.body.requestor = { requestorId: VALID_STUDENT_ID }; |
| 34 | + mockReq.body.hours = 1; |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + jest.clearAllMocks(); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('Input validation', () => { |
| 42 | + test.each([ |
| 43 | + ['taskId is missing', { params: { taskId: '' } }, 400, { error: 'Invalid Task ID' }], |
| 44 | + [ |
| 45 | + 'taskId is not a valid ObjectId', |
| 46 | + { params: { taskId: 'bad-id' } }, |
| 47 | + 400, |
| 48 | + { error: 'Invalid Task ID' }, |
| 49 | + ], |
| 50 | + [ |
| 51 | + 'studentId is missing', |
| 52 | + { body: { requestor: {}, hours: 1 } }, |
| 53 | + 400, |
| 54 | + { error: 'Invalid Student ID' }, |
| 55 | + ], |
| 56 | + [ |
| 57 | + 'studentId is not a valid ObjectId', |
| 58 | + { body: { requestor: { requestorId: 'bad' }, hours: 1 } }, |
| 59 | + 400, |
| 60 | + { error: 'Invalid Student ID' }, |
| 61 | + ], |
| 62 | + [ |
| 63 | + 'hours is zero', |
| 64 | + { body: { requestor: { requestorId: VALID_STUDENT_ID }, hours: 0 } }, |
| 65 | + 400, |
| 66 | + { error: 'hours must be a positive number' }, |
| 67 | + ], |
| 68 | + [ |
| 69 | + 'hours is negative', |
| 70 | + { body: { requestor: { requestorId: VALID_STUDENT_ID }, hours: -1 } }, |
| 71 | + 400, |
| 72 | + { error: 'hours must be a positive number' }, |
| 73 | + ], |
| 74 | + [ |
| 75 | + 'hours is not a number', |
| 76 | + { body: { requestor: { requestorId: VALID_STUDENT_ID }, hours: 'abc' } }, |
| 77 | + 400, |
| 78 | + { error: 'hours must be a positive number' }, |
| 79 | + ], |
| 80 | + ])('Returns %i when %s', async (_, reqOverrides, expectedStatus, expectedBody) => { |
| 81 | + Object.assign(mockReq, reqOverrides); |
| 82 | + if (reqOverrides.params) Object.assign(mockReq.params, reqOverrides.params); |
| 83 | + await logHours(mockReq, mockRes); |
| 84 | + expect(mockRes.status).toHaveBeenCalledWith(expectedStatus); |
| 85 | + expect(mockRes.json).toHaveBeenCalledWith(expectedBody); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('Database interactions', () => { |
| 90 | + test('Returns 404 if task is not found', async () => { |
| 91 | + spyFindOne(null); |
| 92 | + await logHours(mockReq, mockRes); |
| 93 | + expect(mockRes.status).toHaveBeenCalledWith(404); |
| 94 | + expect(mockRes.json).toHaveBeenCalledWith({ |
| 95 | + error: 'Task not found or does not belong to you', |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + test.each([ |
| 100 | + ['completed', makeTask({ status: 'completed', loggedHours: 3 })], |
| 101 | + ['graded', makeTask({ status: 'graded', loggedHours: 5 })], |
| 102 | + ])('Returns 400 if task status is %s', async (_, task) => { |
| 103 | + spyFindOne(task); |
| 104 | + await logHours(mockReq, mockRes); |
| 105 | + expect(mockRes.status).toHaveBeenCalledWith(400); |
| 106 | + expect(mockRes.json).toHaveBeenCalledWith({ error: 'Cannot log hours for a completed task' }); |
| 107 | + }); |
| 108 | + |
| 109 | + test('Returns 200 and transitions assigned -> in_progress on first log', async () => { |
| 110 | + spyFindOne(makeTask()); |
| 111 | + spyFindOneAndUpdate(makeUpdated()); |
| 112 | + await logHours(mockReq, mockRes); |
| 113 | + expect(mockRes.status).toHaveBeenCalledWith(200); |
| 114 | + expect(mockRes.json).toHaveBeenCalledWith({ |
| 115 | + message: 'Hours logged successfully', |
| 116 | + loggedHours: 1, |
| 117 | + suggestedTotalHours: 5, |
| 118 | + status: 'in_progress', |
| 119 | + canMarkDone: false, |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + test('Returns 200 and caps loggedHours at suggestedTotalHours', async () => { |
| 124 | + mockReq.body.hours = 3; |
| 125 | + spyFindOne(makeTask({ status: 'in_progress', loggedHours: 4 })); |
| 126 | + spyFindOneAndUpdate(makeUpdated({ loggedHours: 5 })); |
| 127 | + await logHours(mockReq, mockRes); |
| 128 | + expect(mockRes.status).toHaveBeenCalledWith(200); |
| 129 | + expect(mockRes.json).toHaveBeenCalledWith({ |
| 130 | + message: 'Hours logged successfully', |
| 131 | + loggedHours: 5, |
| 132 | + suggestedTotalHours: 5, |
| 133 | + status: 'in_progress', |
| 134 | + canMarkDone: true, |
| 135 | + }); |
| 136 | + expect(EducationTask.findOneAndUpdate).toHaveBeenCalledWith( |
| 137 | + expect.any(Object), |
| 138 | + { $set: { loggedHours: 5, status: 'in_progress' } }, |
| 139 | + { new: true, runValidators: false }, |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + test('Returns 404 if findOneAndUpdate returns null', async () => { |
| 144 | + spyFindOne(makeTask()); |
| 145 | + spyFindOneAndUpdate(null); |
| 146 | + await logHours(mockReq, mockRes); |
| 147 | + expect(mockRes.status).toHaveBeenCalledWith(404); |
| 148 | + expect(mockRes.json).toHaveBeenCalledWith({ error: 'Task not found during update' }); |
| 149 | + }); |
| 150 | + |
| 151 | + test('Returns 200 with canMarkDone false when suggestedTotalHours is 0', async () => { |
| 152 | + spyFindOne(makeTask({ suggestedTotalHours: 0 })); |
| 153 | + spyFindOneAndUpdate(makeUpdated({ suggestedTotalHours: 0 })); |
| 154 | + await logHours(mockReq, mockRes); |
| 155 | + expect(mockRes.status).toHaveBeenCalledWith(200); |
| 156 | + expect(mockRes.json).toHaveBeenCalledWith(expect.objectContaining({ canMarkDone: false })); |
| 157 | + }); |
| 158 | + |
| 159 | + test('Returns 500 if findOne throws an error', async () => { |
| 160 | + spyFindOne(Promise.reject(new Error('DB error'))); |
| 161 | + await logHours(mockReq, mockRes); |
| 162 | + expect(mockRes.status).toHaveBeenCalledWith(500); |
| 163 | + expect(mockRes.json).toHaveBeenCalledWith({ error: 'Internal server error' }); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments