many fixes resulting from latest refactoring
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 8m3s
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 8m3s
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
// --- FIX REGISTRY ---
|
||||
//
|
||||
// 2024-08-01: Corrected `auth.routes.test.ts` by separating the mock's implementation into a `vi.hoisted` block
|
||||
// and then applying it in the `vi.mock` call at the top level of the module.
|
||||
// --- END FIX REGISTRY ---
|
||||
// src/routes/auth.routes.test.ts
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import supertest from 'supertest';
|
||||
@@ -48,38 +53,39 @@ vi.mock('bcrypt', async (importOriginal) => {
|
||||
return { ...actual, compare: vi.fn() };
|
||||
});
|
||||
|
||||
// Mock Passport middleware
|
||||
vi.hoisted(() => {
|
||||
// Define a type for the custom passport callback to avoid `any`.
|
||||
type PassportCallback = (error: Error | null, user: Express.User | false, info?: { message: string }) => void;
|
||||
// --- FIX: `vi.mock` cannot be inside `vi.hoisted`. ---
|
||||
// 1. Hoist the mock implementation logic.
|
||||
const passportMocks = vi.hoisted(() => {
|
||||
// Define a type for the custom passport callback to avoid `any` and ensure type safety.
|
||||
type PassportCallback = (error: Error | null, user?: Express.User | false, info?: { message: string }) => void;
|
||||
|
||||
// Mock Passport middleware
|
||||
vi.mock('./passport.routes', () => ({
|
||||
default: {
|
||||
authenticate: (strategy: string, options: Record<string, unknown>, callback: PassportCallback) => (req: Request, res: any) => {
|
||||
// Logic to simulate passport authentication outcome based on test input
|
||||
if (req.body.password === 'wrong_password') {
|
||||
// Simulate incorrect credentials
|
||||
return callback(null, false, { message: 'Incorrect email or password.' });
|
||||
}
|
||||
if (req.body.email === 'locked@test.com') {
|
||||
// Simulate locked account
|
||||
return callback(null, false, { message: 'Account is temporarily locked.' });
|
||||
}
|
||||
if (req.body.email === 'notfound@test.com') {
|
||||
// Simulate user not found
|
||||
return callback(null, false, { message: 'Login failed' });
|
||||
}
|
||||
// This function simulates the behavior of passport.authenticate for the 'local' strategy.
|
||||
const authenticateMock = (strategy: string, options: Record<string, unknown>, callback: PassportCallback) => (req: Request) => {
|
||||
if (req.body.password === 'wrong_password') {
|
||||
return callback(null, false, { message: 'Incorrect email or password.' });
|
||||
}
|
||||
if (req.body.email === 'locked@test.com') {
|
||||
return callback(null, false, { message: 'Account is temporarily locked.' });
|
||||
}
|
||||
if (req.body.email === 'notfound@test.com') {
|
||||
return callback(null, false, { message: 'Login failed' });
|
||||
}
|
||||
// Default success case
|
||||
const user = { user_id: 'user-123', email: req.body.email };
|
||||
callback(null, user);
|
||||
};
|
||||
|
||||
// Default success case
|
||||
const user = { user_id: 'user-123', email: req.body.email };
|
||||
callback(null, user, undefined);
|
||||
},
|
||||
initialize: () => (req: any, res: any, next: any) => next(),
|
||||
},
|
||||
}));
|
||||
return { authenticateMock };
|
||||
});
|
||||
|
||||
// 2. Call vi.mock at the top level, referencing the hoisted mock implementation.
|
||||
vi.mock('./passport.routes', () => ({
|
||||
default: {
|
||||
authenticate: vi.fn().mockImplementation(passportMocks.authenticateMock),
|
||||
initialize: () => (req: any, res: any, next: any) => next(),
|
||||
},
|
||||
}));
|
||||
|
||||
// Create a minimal Express app to host our router
|
||||
const app = express();
|
||||
app.use(express.json({ strict: false }));
|
||||
|
||||
Reference in New Issue
Block a user