-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathactivityLogController.test.js
More file actions
190 lines (136 loc) · 6.1 KB
/
Copy pathactivityLogController.test.js
File metadata and controls
190 lines (136 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const mockSelect = jest.fn();
const mockSort = jest.fn(() => ({ select: mockSelect }));
const mockFind = jest.fn(() => ({ sort: mockSort }));
jest.mock('../../models/activityLog', () => ({
find: (...args) => mockFind(...args),
}));
const mongoose = require('mongoose');
const controller = require('../activityLogController')();
describe('activityLogController', () => {
let req;
let res;
beforeEach(() => {
jest.clearAllMocks();
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};
});
describe('fetchStudentDailyLog', () => {
beforeEach(() => {
req = {
body: {
requestor: { requestorId: new mongoose.Types.ObjectId().toString() },
},
query: {},
};
});
it('should return logs for the requestor', async () => {
const mockLogs = [{ action_type: 'comment', created_at: new Date() }];
mockSelect.mockResolvedValue(mockLogs);
await controller.fetchStudentDailyLog(req, res);
expect(res.json).toHaveBeenCalledWith(mockLogs);
});
it('should return 403 if requested studentId differs from requestorId', async () => {
req.query.studentId = new mongoose.Types.ObjectId().toString();
await controller.fetchStudentDailyLog(req, res);
expect(res.status).toHaveBeenCalledWith(403);
expect(res.json).toHaveBeenCalledWith({
error: "Forbidden: Cannot access another student's log",
});
});
it('should return 200 if requested studentId matches requestorId', async () => {
req.query.studentId = req.body.requestor.requestorId;
mockSelect.mockResolvedValue([]);
await controller.fetchStudentDailyLog(req, res);
expect(res.json).toHaveBeenCalledWith([]);
});
it('should return 400 for invalid studentId format', async () => {
req.body.requestor.requestorId = 'not-a-valid-id';
await controller.fetchStudentDailyLog(req, res);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error: 'Invalid studentId format' });
});
it('should return 500 on database error', async () => {
mockSelect.mockRejectedValue(new Error('DB error'));
await controller.fetchStudentDailyLog(req, res);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({ error: 'DB error' });
});
it('should return 500 when req.body.requestor is undefined', async () => {
req.body = {};
await controller.fetchStudentDailyLog(req, res);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith(expect.objectContaining({ error: expect.any(String) }));
});
it('should use requestorId when query studentId is empty string', async () => {
req.query.studentId = '';
mockSelect.mockResolvedValue([{ action_type: 'comment' }]);
await controller.fetchStudentDailyLog(req, res);
expect(mockFind).toHaveBeenCalledWith({ actor_id: expect.any(mongoose.Types.ObjectId) });
expect(res.json).toHaveBeenCalledWith([{ action_type: 'comment' }]);
});
it('should call find with correct filter, sort, and select', async () => {
mockSelect.mockResolvedValue([]);
await controller.fetchStudentDailyLog(req, res);
expect(mockFind).toHaveBeenCalledWith({ actor_id: expect.any(mongoose.Types.ObjectId) });
expect(mockSort).toHaveBeenCalledWith({ created_at: -1 });
expect(mockSelect).toHaveBeenCalledWith('action_type metadata created_at actor_id');
});
});
describe('fetchEducatorDailyLog', () => {
beforeEach(() => {
req = {
params: { studentId: new mongoose.Types.ObjectId().toString() },
};
});
it('should return logs for the given studentId', async () => {
const mockLogs = [{ action_type: 'note', created_at: new Date() }];
mockSelect.mockResolvedValue(mockLogs);
await controller.fetchEducatorDailyLog(req, res);
expect(res.json).toHaveBeenCalledWith(mockLogs);
});
it('should return 400 when studentId is missing', async () => {
req.params = {};
await controller.fetchEducatorDailyLog(req, res);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error: 'Missing studentId' });
});
it('should return 400 for invalid studentId format', async () => {
req.params.studentId = 'invalid-id';
await controller.fetchEducatorDailyLog(req, res);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error: 'Invalid studentId format' });
});
it('should return 500 on database error', async () => {
mockSelect.mockRejectedValue(new Error('DB failure'));
await controller.fetchEducatorDailyLog(req, res);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({ error: 'DB failure' });
});
it('should return empty array when no logs exist', async () => {
mockSelect.mockResolvedValue([]);
await controller.fetchEducatorDailyLog(req, res);
expect(res.json).toHaveBeenCalledWith([]);
});
it('should return 400 when studentId is empty string', async () => {
req.params.studentId = '';
await controller.fetchEducatorDailyLog(req, res);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error: 'Missing studentId' });
});
it('should return 400 when studentId is null', async () => {
req.params.studentId = null;
await controller.fetchEducatorDailyLog(req, res);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error: 'Missing studentId' });
});
it('should call find with correct filter, sort, and select', async () => {
mockSelect.mockResolvedValue([]);
await controller.fetchEducatorDailyLog(req, res);
expect(mockFind).toHaveBeenCalledWith({ actor_id: expect.any(mongoose.Types.ObjectId) });
expect(mockSort).toHaveBeenCalledWith({ created_at: -1 });
expect(mockSelect).toHaveBeenCalledWith('action_type metadata created_at actor_id');
});
});
});