Skip to content

Commit b9c5343

Browse files
committed
feat: tests complete
1 parent 22b5fd4 commit b9c5343

3 files changed

Lines changed: 23 additions & 29 deletions

File tree

src/controllers/admin.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const createUser = async (req: Request, res: Response) => {
6060

6161
if (!parsed.success) {
6262
return res.status(400).json({
63-
message: 'Invalid payload',
63+
error: 'Invalid payload',
6464
details: parsed.error,
6565
});
6666
}
@@ -71,7 +71,7 @@ export const createUser = async (req: Request, res: Response) => {
7171
const existing = await User.findOne({ where: { email } });
7272

7373
if (existing) {
74-
return res.status(409).json({ message: 'User already exists' });
74+
return res.status(409).json({ error: 'User already exists' });
7575
}
7676

7777
const user = await User.create({
@@ -83,7 +83,7 @@ export const createUser = async (req: Request, res: Response) => {
8383
return res.status(201).json({ user });
8484
} catch (err) {
8585
logger.error(`Failed to create user. Reason: ${err}`);
86-
return res.status(500).json({ message: 'Failed to create user' });
86+
return res.status(500).json({ error: 'Failed to create user' });
8787
}
8888
};
8989

@@ -93,7 +93,7 @@ export const deleteUser = async (req: ServiceRequest, res: Response) => {
9393

9494
try {
9595
if (!userId) {
96-
return res.status(404).json({ message: 'User not found.' });
96+
return res.status(404).json({ error: 'User not found.' });
9797
}
9898

9999
try {
@@ -113,11 +113,11 @@ export const deleteUser = async (req: ServiceRequest, res: Response) => {
113113
return res.status(200).json({ message: 'Success' });
114114
} catch (error: unknown) {
115115
logger.error(`Failed to delete user: ${userId}. Error: ${error}`);
116-
return res.status(500).json({ message: 'Failed' });
116+
return res.status(500).json({ error: 'Failed' });
117117
}
118118
} catch (error) {
119119
logger.error(`Error occured deleting a user: ${error}`);
120-
return res.status(500).json({ message: `Failed` });
120+
return res.status(500).json({ error: `Failed` });
121121
}
122122
};
123123

@@ -126,15 +126,15 @@ export const updateUser = async (req: ServiceRequest, res: Response) => {
126126

127127
if (!userId) {
128128
logger.error('Missing user id for updating user');
129-
return res.status(400).json({ message: 'Bad request' });
129+
return res.status(400).json({ error: 'Bad request' });
130130
}
131131

132132
const parsed = UpdateUserSchema.safeParse(req.body);
133133

134134
if (!parsed.success || Object.keys(parsed.data).length === 0) {
135135
logger.error(`Failed to parse update user body. ${JSON.stringify(req.body)}`);
136136
return res.status(400).json({
137-
message: 'Invalid update payload',
137+
error: 'Invalid update payload',
138138
details: parsed.error,
139139
});
140140
}
@@ -143,7 +143,7 @@ export const updateUser = async (req: ServiceRequest, res: Response) => {
143143
const user = await User.findByPk(userId);
144144

145145
if (!user) {
146-
return res.status(404).json({ message: 'User not found' });
146+
return res.status(404).json({ error: 'User not found' });
147147
}
148148

149149
const before = user.toJSON();
@@ -162,15 +162,15 @@ export const updateUser = async (req: ServiceRequest, res: Response) => {
162162
});
163163
} catch (error) {
164164
logger.error(`Failed to update user ${error}`);
165-
res.status(500).json({ message: 'Failed to update user' });
165+
res.status(500).json({ error: 'Failed to update user' });
166166
return;
167167
}
168168

169169
res.status(200).json({ user });
170170
return;
171171
} catch {
172172
logger.error('Failed to find user');
173-
res.status(400).json({ message: 'Could not update users' });
173+
res.status(400).json({ error: 'Could not update users' });
174174
}
175175
};
176176

@@ -180,7 +180,7 @@ export const getUserDetail = async (req: ServiceRequest, res: Response) => {
180180
const user = await User.findByPk(userId);
181181

182182
if (!user) {
183-
return res.status(404).json({ message: 'User not found' });
183+
return res.status(404).json({ error: 'User not found' });
184184
}
185185

186186
const now = new Date();
@@ -247,7 +247,7 @@ export const getUserAnomalies = async (req: Request, res: Response) => {
247247
relatedAgents: Array.from(agents),
248248
});
249249
} catch {
250-
return res.status(500).json({ message: 'Failed to fetch anomalies' });
250+
return res.status(500).json({ error: 'Failed to fetch anomalies' });
251251
}
252252
};
253253

@@ -274,15 +274,15 @@ export const listUserSessions = async (req: Request, res: Response) => {
274274
deviceName: s.deviceName,
275275
ipAddress: s.ipAddress,
276276
userAgent: s.userAgent,
277-
lastUsedAt: s.lastUsedAt,
278-
expiresAt: s.expiresAt,
277+
lastUsedAt: s.lastUsedAt.toISOString(),
278+
expiresAt: s.expiresAt.toISOString(),
279279
current: false,
280280
})),
281281
total: sessions.length,
282282
});
283283
} catch (err) {
284284
logger.error(`Failed to fetch sessions: ${err}`);
285-
return res.status(500).json({ message: 'Failed to fetch sessions' });
285+
return res.status(500).json({ error: 'Failed to fetch sessions' });
286286
}
287287
};
288288

@@ -306,7 +306,7 @@ export const revokeAllUserSessions = async (req: Request, res: Response) => {
306306
return res.json({ message: 'Success' });
307307
} catch (err) {
308308
logger.error(`Failed to revoke sessions: ${err}`);
309-
return res.status(500).json({ message: 'Failed to revoke sessions' });
309+
return res.status(500).json({ error: 'Failed to revoke sessions' });
310310
}
311311
};
312312

@@ -378,7 +378,7 @@ export const getAuthEvents = async (req: ServiceRequest, res: Response) => {
378378
const parsed = AuthEventQuerySchema.safeParse(req.query);
379379

380380
if (!parsed.success) {
381-
return res.status(400).json({ message: 'Invalid query params' });
381+
return res.status(400).json({ error: 'Invalid query params' });
382382
}
383383

384384
const { limit, offset, userId, type, from, to } = parsed.data;
@@ -425,7 +425,7 @@ export const getAuthEvents = async (req: ServiceRequest, res: Response) => {
425425
return res.json({ events, total });
426426
} catch (err) {
427427
logger.error(`Failed to fetch auth events: ${err}`);
428-
res.status(500).json({ message: 'Failed to fetch events' });
428+
res.status(500).json({ error: 'Failed to fetch events' });
429429
}
430430
};
431431

@@ -437,6 +437,6 @@ export const getCredentialsCount = async (req: ServiceRequest, res: Response) =>
437437
return res.json({ count: credentialCount || 0 });
438438
} catch (err) {
439439
logger.error(`Failed to fetch credential count: ${err}`);
440-
res.status(500).json({ message: 'Failed to fetch credential count' });
440+
res.status(500).json({ error: 'Failed to fetch credential count' });
441441
}
442442
};

tests/factories/sessionFactory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export function buildSession(overrides: any = {}) {
77
ipAddress: '127.0.0.1',
88
userAgent: 'agent',
99
current: true,
10-
lastUsedAt: new Date().toDateString(),
11-
expiresAt: new Date(Date.now() + 100000).toDateString(),
10+
lastUsedAt: new Date(),
11+
expiresAt: new Date(Date.now() + 100000),
1212
revokedAt: null,
1313
save: vi.fn(),
1414
...overrides,

tests/integration/admin/admin.spec.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ describe('GET /admin/users', () => {
2828
const res = await request(app).get('/admin/users');
2929

3030
expect(res.status).toBe(200);
31-
console.log(res.body.users);
3231
expect(res.body.users).toHaveLength(1);
3332
expect(res.body.total).toBe(1);
3433
});
@@ -185,12 +184,7 @@ describe('POST /admin/users', () => {
185184

186185
describe('PATCH /admin/users/:userId', () => {
187186
it('updates user successfully', async () => {
188-
const user = {
189-
id: 'user-1',
190-
email: 'test@example.com',
191-
toJSON: vi.fn(() => ({ id: 'user-1' })),
192-
update: vi.fn(),
193-
};
187+
const user = buildUser();
194188

195189
(User.findByPk as any).mockResolvedValue(user);
196190

0 commit comments

Comments
 (0)