testing routes
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 4m27s
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 4m27s
This commit is contained in:
@@ -13,9 +13,16 @@ vi.mock('../services/logger.server', () => ({
|
||||
}));
|
||||
|
||||
// Mock the passport-local Strategy constructor to capture the verify function
|
||||
// We need to capture the arguments passed to the constructor.
|
||||
let capturedVerify: any;
|
||||
vi.mock('passport-local', () => {
|
||||
// This mock simulates a class constructor that can be instantiated with `new`.
|
||||
const MockStrategy = vi.fn();
|
||||
// This mock simulates a class constructor.
|
||||
class MockStrategy {
|
||||
name = 'local'; // Passport requires the strategy instance to have a name.
|
||||
constructor(options: object, verify: any) {
|
||||
capturedVerify = verify; // Capture the verify function for testing.
|
||||
}
|
||||
}
|
||||
return {
|
||||
Strategy: MockStrategy,
|
||||
};
|
||||
@@ -24,9 +31,6 @@ vi.mock('passport-local', () => {
|
||||
// Now, import the passport configuration which will use our mocks
|
||||
import { isAdmin } from './passport';
|
||||
|
||||
// Capture the verify function from the LocalStrategy mock
|
||||
const localStrategyVerify = (LocalStrategy as Mock).mock.calls[0][1];
|
||||
|
||||
describe('Passport Configuration', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
@@ -47,7 +51,7 @@ describe('Passport Configuration', () => {
|
||||
mockedDb.findUserByEmail.mockResolvedValue(mockUser as any);
|
||||
|
||||
// Act
|
||||
await localStrategyVerify(mockReq, 'test@test.com', 'password123', mockDone);
|
||||
await capturedVerify(mockReq, 'test@test.com', 'password123', mockDone);
|
||||
|
||||
// Assert
|
||||
expect(mockedDb.findUserByEmail).toHaveBeenCalledWith('test@test.com');
|
||||
@@ -60,7 +64,7 @@ describe('Passport Configuration', () => {
|
||||
mockedDb.findUserByEmail.mockResolvedValue(undefined);
|
||||
|
||||
// Act
|
||||
await localStrategyVerify(mockReq, 'nouser@test.com', 'password', mockDone);
|
||||
await capturedVerify(mockReq, 'nouser@test.com', 'password', mockDone);
|
||||
|
||||
// Assert
|
||||
expect(mockDone).toHaveBeenCalledWith(null, false, { message: 'Incorrect email or password.' });
|
||||
@@ -78,7 +82,7 @@ describe('Passport Configuration', () => {
|
||||
mockedDb.findUserByEmail.mockResolvedValue(mockUser as any);
|
||||
|
||||
// Act
|
||||
await localStrategyVerify(mockReq, 'user@test.com', 'wrong-password', mockDone);
|
||||
await capturedVerify(mockReq, 'user@test.com', 'wrong-password', mockDone);
|
||||
|
||||
// Assert
|
||||
expect(mockDone).toHaveBeenCalledWith(null, false, { message: 'Incorrect email or password.' });
|
||||
@@ -98,7 +102,7 @@ describe('Passport Configuration', () => {
|
||||
mockedDb.findUserByEmail.mockResolvedValue(mockUser as any);
|
||||
|
||||
// Act
|
||||
await localStrategyVerify(mockReq, 'locked@test.com', 'any-password', mockDone);
|
||||
await capturedVerify(mockReq, 'locked@test.com', 'any-password', mockDone);
|
||||
|
||||
// Assert
|
||||
expect(mockDone).toHaveBeenCalledWith(null, false, { message: expect.stringContaining('Account is temporarily locked.') });
|
||||
@@ -118,7 +122,7 @@ describe('Passport Configuration', () => {
|
||||
mockedDb.findUserByEmail.mockResolvedValue(mockUser as any);
|
||||
|
||||
// Act
|
||||
await localStrategyVerify(mockReq, 'unlocked@test.com', 'password123', mockDone);
|
||||
await capturedVerify(mockReq, 'unlocked@test.com', 'password123', mockDone);
|
||||
|
||||
// Assert
|
||||
expect(mockDone).toHaveBeenCalledWith(null, expect.objectContaining({ email: 'unlocked@test.com' }));
|
||||
|
||||
@@ -64,14 +64,14 @@ describe('Public Routes (/api)', () => {
|
||||
|
||||
describe('GET /health/storage', () => {
|
||||
it('should return 200 OK if storage is writable', async () => {
|
||||
mockedFs.access.mockResolvedValue(undefined);
|
||||
(mockedFs.access as Mock).mockResolvedValue(undefined);
|
||||
const response = await supertest(app).get('/api/health/storage');
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.success).toBe(true);
|
||||
});
|
||||
|
||||
it('should return 500 if storage is not accessible', async () => {
|
||||
mockedFs.access.mockRejectedValue(new Error('Permission denied'));
|
||||
(mockedFs.access as Mock).mockRejectedValue(new Error('Permission denied'));
|
||||
const response = await supertest(app).get('/api/health/storage');
|
||||
expect(response.status).toBe(500);
|
||||
expect(response.body.success).toBe(false);
|
||||
|
||||
Reference in New Issue
Block a user