testing routes
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 3m25s
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 3m25s
This commit is contained in:
@@ -64,14 +64,14 @@ describe('Public Routes (/api)', () => {
|
|||||||
|
|
||||||
describe('GET /health/storage', () => {
|
describe('GET /health/storage', () => {
|
||||||
it('should return 200 OK if storage is writable', async () => {
|
it('should return 200 OK if storage is writable', async () => {
|
||||||
mockedFs.access.mockResolvedValue(undefined);
|
(mockedFs.access as Mocked<any>).mockResolvedValue(undefined);
|
||||||
const response = await supertest(app).get('/api/health/storage');
|
const response = await supertest(app).get('/api/health/storage');
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
expect(response.body.success).toBe(true);
|
expect(response.body.success).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return 500 if storage is not accessible', async () => {
|
it('should return 500 if storage is not accessible', async () => {
|
||||||
mockedFs.access.mockRejectedValue(new Error('Permission denied'));
|
(mockedFs.access as Mocked<any>).mockRejectedValue(new Error('Permission denied'));
|
||||||
const response = await supertest(app).get('/api/health/storage');
|
const response = await supertest(app).get('/api/health/storage');
|
||||||
expect(response.status).toBe(500);
|
expect(response.status).toBe(500);
|
||||||
expect(response.body.success).toBe(false);
|
expect(response.body.success).toBe(false);
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
// src/routes/system.test.ts
|
// src/routes/system.test.ts
|
||||||
import { describe, it, expect, vi, beforeEach, type Mocked } from 'vitest';
|
import { describe, it, expect, vi, beforeEach, type Mocked, type Mock } from 'vitest';
|
||||||
import supertest from 'supertest';
|
import supertest from 'supertest';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import type { ExecException } from 'child_process';
|
import type { ExecException } from 'child_process';
|
||||||
import systemRouter from './system';
|
import systemRouter from './system';
|
||||||
import { exec } from 'child_process';
|
import { exec } from 'child_process';
|
||||||
|
|
||||||
// Mock the child_process module to control the behavior of `exec`
|
// Define a mock function at the module level. This will be our controlled version of `exec`.
|
||||||
vi.mock('child_process');
|
const mockedExec = vi.fn();
|
||||||
|
|
||||||
|
// Mock the child_process module. We explicitly define its exports.
|
||||||
|
vi.mock('child_process', () => ({
|
||||||
|
exec: mockedExec,
|
||||||
|
}));
|
||||||
|
|
||||||
// Mock the logger to keep test output clean
|
|
||||||
vi.mock('../services/logger.server', () => ({
|
vi.mock('../services/logger.server', () => ({
|
||||||
logger: {
|
logger: {
|
||||||
info: vi.fn(),
|
info: vi.fn(),
|
||||||
@@ -25,9 +29,6 @@ app.use(express.json());
|
|||||||
app.use('/api/system', systemRouter);
|
app.use('/api/system', systemRouter);
|
||||||
|
|
||||||
describe('System Routes (/api/system)', () => {
|
describe('System Routes (/api/system)', () => {
|
||||||
// Cast the mocked exec to vi.Mock to access mock methods like mockImplementation
|
|
||||||
const mockedExec = exec as Mocked<typeof exec>;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
});
|
});
|
||||||
@@ -42,7 +43,7 @@ describe('System Routes (/api/system)', () => {
|
|||||||
└───────────┴───────────┘
|
└───────────┴───────────┘
|
||||||
`;
|
`;
|
||||||
// The `exec` callback receives (error, stdout, stderr). For success, error is null.
|
// The `exec` callback receives (error, stdout, stderr). For success, error is null.
|
||||||
(mockedExec as any).mockImplementation((
|
mockedExec.mockImplementation((
|
||||||
command: string,
|
command: string,
|
||||||
callback: (error: ExecException | null, stdout: string, stderr: string) => void
|
callback: (error: ExecException | null, stdout: string, stderr: string) => void
|
||||||
) => {
|
) => {
|
||||||
@@ -63,7 +64,7 @@ describe('System Routes (/api/system)', () => {
|
|||||||
const pm2StoppedOutput = `
|
const pm2StoppedOutput = `
|
||||||
│ status │ stopped │
|
│ status │ stopped │
|
||||||
`;
|
`;
|
||||||
(mockedExec as any).mockImplementation((
|
mockedExec.mockImplementation((
|
||||||
command: string,
|
command: string,
|
||||||
callback: (error: ExecException | null, stdout: string, stderr: string) => void
|
callback: (error: ExecException | null, stdout: string, stderr: string) => void
|
||||||
) => {
|
) => {
|
||||||
@@ -82,7 +83,7 @@ describe('System Routes (/api/system)', () => {
|
|||||||
it('should return success: false when pm2 process does not exist', async () => {
|
it('should return success: false when pm2 process does not exist', async () => {
|
||||||
// Arrange: Simulate the error and stdout when a process is not found.
|
// Arrange: Simulate the error and stdout when a process is not found.
|
||||||
const processNotFoundOutput = "[PM2][ERROR] Process or Namespace flyer-crawler-api doesn't exist";
|
const processNotFoundOutput = "[PM2][ERROR] Process or Namespace flyer-crawler-api doesn't exist";
|
||||||
(mockedExec as any).mockImplementation((
|
mockedExec.mockImplementation((
|
||||||
command: string,
|
command: string,
|
||||||
callback: (error: ExecException | null, stdout: string, stderr: string) => void
|
callback: (error: ExecException | null, stdout: string, stderr: string) => void
|
||||||
) => {
|
) => {
|
||||||
@@ -100,7 +101,7 @@ describe('System Routes (/api/system)', () => {
|
|||||||
|
|
||||||
it('should return 500 on a generic exec error', async () => {
|
it('should return 500 on a generic exec error', async () => {
|
||||||
// Arrange: Simulate a generic failure of the `exec` command.
|
// Arrange: Simulate a generic failure of the `exec` command.
|
||||||
(mockedExec as any).mockImplementation((
|
mockedExec.mockImplementation((
|
||||||
command: string,
|
command: string,
|
||||||
callback: (error: ExecException | null, stdout: string, stderr: string) => void
|
callback: (error: ExecException | null, stdout: string, stderr: string) => void
|
||||||
) => {
|
) => {
|
||||||
|
|||||||
@@ -47,6 +47,11 @@ app.use('/api/users', userRouter);
|
|||||||
describe('User Routes (/api/users)', () => {
|
describe('User Routes (/api/users)', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
|
// Reset the authenticate mock to its default "unauthorized" state before each test
|
||||||
|
mockedAuthenticate.mockImplementation(
|
||||||
|
(strategy: string, options: object) => (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
res.status(401).json({ message: 'Unauthorized' });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when user is not authenticated', () => {
|
describe('when user is not authenticated', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user