Refactor: Introduce requiredString helper for consistent validation across routes and services
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 8m21s

This commit is contained in:
2025-12-15 19:19:23 -08:00
parent d5f185ad99
commit 08340a4cf2
16 changed files with 81 additions and 48 deletions

View File

@@ -1,12 +1,11 @@
// src/routes/auth.routes.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest';
import supertest from 'supertest';
import { Request, Response, NextFunction, RequestHandler } from 'express';
import { Request, Response, NextFunction } from 'express';
import cookieParser from 'cookie-parser';
import * as bcrypt from 'bcrypt';
import { createMockUserProfile, createMockUserWithPasswordHash } from '../tests/utils/mockFactories';
import { mockLogger } from '../tests/utils/mockLogger';
import { createTestApp } from '../tests/utils/createTestApp';
// --- FIX: Hoist passport mocks to be available for vi.mock ---
const passportMocks = vi.hoisted(() => {
@@ -199,7 +198,10 @@ describe('Auth Routes (/api/auth)', () => {
expect(response.status).toBe(400);
// The validation middleware returns errors in an array.
// We check if any of the error messages contain the expected text.
const errorMessages = response.body.errors?.map((e: any) => e.message).join(' ');
interface ZodError {
message: string;
}
const errorMessages = response.body.errors?.map((e: ZodError) => e.message).join(' ');
expect(errorMessages).toMatch(/Password is too weak/i);
});
@@ -468,11 +470,12 @@ describe('Auth Routes (/api/auth)', () => {
});
it('should return 403 if refresh token is invalid', async () => {
vi.mocked(db.userRepo.findUserByRefreshToken).mockRejectedValue(new Error('Invalid or expired refresh token.'));
// Mock finding no user for this token, which should trigger the 403 logic
vi.mocked(db.userRepo.findUserByRefreshToken).mockResolvedValue(undefined as any);
const response = await supertest(app)
.post('/api/auth/refresh-token')
.set('Cookie', 'refreshToken=invalid-token'); // This was a duplicate, fixed.
.set('Cookie', 'refreshToken=invalid-token');
expect(response.status).toBe(403);
});