Ctrl K
ComunidadGuías…

api/src/app.test.ts

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
import http from 'node:http';
import type { AddressInfo } from 'node:net';
import { useEnv } from '@directus/env';
import { Router } from 'express';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import createApp from './app.js';
import { mockEnv } from './test-utils/env.js';

const { mockMcpOAuthGuard } = vi.hoisted(() => ({
	mockMcpOAuthGuard: vi.fn((_req: unknown, _res: unknown, next: () => void) => next()),
}));

vi.mock('./database', () => ({
	default: vi.fn(),
	getDatabaseClient: vi.fn().mockReturnValue('postgres'),
	isInstalled: vi.fn(),
	validateDatabaseConnection: vi.fn(),
	validateDatabaseExtensions: vi.fn(),
	validateMigrations: vi.fn(),
}));

vi.mock('./telemetry/index.js');

// This is required because logger uses global env which is imported before the tests run. Can be
// reduce to just mock the file when logger is also using useLogger everywhere @TODO
vi.mock('@directus/env', async () => {
	const { mockUseEnv } = await import('./test-utils/env.js');
	return mockUseEnv();
});

const mockGetEndpointRouter = vi.fn().mockReturnValue(Router());
const mockGetEmbeds = vi.fn().mockReturnValue({ head: '', body: '' });

vi.mock('./extensions', () => ({
	getExtensionManager: vi.fn().mockImplementation(() => {
		return {
			initialize: vi.fn(),
			getEndpointRouter: mockGetEndpointRouter,
			getEmbeds: mockGetEmbeds,
		};
	}),
}));

vi.mock('./flows', () => ({
	getFlowManager: vi.fn().mockImplementation(() => {
		return {
			initialize: vi.fn(),
		};
	}),
}));

const licenseManager = vi.hoisted(() => ({
	initialize: vi.fn(),
	getLicense: vi.fn().mockResolvedValue({ meta: { validation_interval: -1 } }),
	scheduleCheck: vi.fn(),
}));

vi.mock('./license/index.js', () => ({
	getLicenseManager: () => licenseManager,
	getEntitlementManager: vi.fn().mockImplementation(() => ({
		initialize: vi.fn(),
		isEntitled: vi.fn().mockReturnValue(false),
	})),
}));

vi.mock('./license/manager.js', () => ({
	getLicenseManager: () => licenseManager,
}));

vi.mock('./middleware/schema', () => ({
	default: Router(),
}));

vi.mock('./middleware/mcp-oauth-guard.js', () => ({
	default: mockMcpOAuthGuard,
}));

vi.mock('./auth', () => ({
	registerAuthProviders: vi.fn(),
}));

vi.mock('./deployment.js', () => ({
	registerDeploymentDrivers: vi.fn(),
	ensureDeploymentWebhooks: vi.fn(),
}));

function mockAppEnv(overrides: Partial<ReturnType<typeof useEnv>> = {}) {
	vi.mocked(useEnv).mockReturnValue(
		mockEnv({
			SECRET: 'abcdef',
			SERVE_APP: true,
			PUBLIC_URL: 'http://localhost:8055/directus',
			TELEMETRY: false,
			LOG_STYLE: 'raw',
			ROBOTS_TXT: 'User-agent: *\nDisallow: /',
			ROOT_REDIRECT: './admin',
			IP_TRUST_PROXY: true,
			...overrides,
		}),
	);
}

beforeEach(() => {
	mockAppEnv();
});

afterEach(() => {
	vi.clearAllMocks();
});

const request = async (path: string = '') => {
	const app = await createApp();
	const server = http.createServer(app);
	server.listen(0);
	const address = server.address() as AddressInfo;
	const baseUrl = `http://127.0.0.1:${address.port}`;

	const response = await fetch(`${baseUrl}${path}`, { redirect: 'manual' });

	server.close();

	return response;
};

describe('createApp', async () => {
	test('Should schedule the license check', async () => {
		await createApp();

		expect(licenseManager.scheduleCheck).toHaveBeenCalledOnce();
	});

	describe('Content Security Policy', () => {
		test('Should set content-security-policy header by default', async () => {
			const response = await request();

			expect(response.headers.has('content-security-policy')).toBe(true);
		});
	});

	describe('Root Redirect', () => {
		test('Should redirect root path by default', async () => {
			const response = await request();

			expect(response.status).toEqual(302);
		});
	});

	describe('robots.txt file', () => {
		test('Should respond with default robots.txt content', async () => {
			const response = await request('/robots.txt');
			const body = await response.text();

			expect(body).toEqual('User-agent: *\nDisallow: /');
		});
	});

	describe('Admin App', () => {
		test('Should set <base /> tag href to public url with admin relative path', async () => {
			const response = await request('/admin');
			const body = await response.text();

			expect(body).toEqual(expect.stringContaining(`<base href="/directus/admin/" />`));
		});

		test('Should remove <embed-head /> and <embed-body /> tags when there are no custom embeds', async () => {
			mockGetEmbeds.mockReturnValueOnce({ head: '', body: '' });

			const response = await request('/admin');
			const body = await response.text();

			expect(body).not.toEqual(expect.stringContaining(`<embed-head />`));
			expect(body).not.toEqual(expect.stringContaining(`<embed-body />`));
		});

		test('Should replace <embed-head /> tag with custom embed head', async () => {
			const mockEmbedHead = '<!-- Test Embed Head -->';
			mockGetEmbeds.mockReturnValueOnce({ head: mockEmbedHead, body: '' });

			const response = await request('/admin');
			const body = await response.text();

			expect(body).toEqual(expect.stringContaining(mockEmbedHead));
		});

		test('Should replace <embed-body /> tag with custom embed body', async () => {
			const mockEmbedBody = '<!-- Test Embed Body -->';
			mockGetEmbeds.mockReturnValueOnce({ head: '', body: mockEmbedBody });

			const response = await request('/admin');
			const body = await response.text();

			expect(body).toEqual(expect.stringContaining(mockEmbedBody));
		});
	});

	describe('Server ping endpoint', () => {
		test('Should respond with pong', async () => {
			const response = await request('/server/ping');
			const body = await response.text();

			expect(body).toEqual('pong');
		});
	});

	describe('MCP OAuth guard', () => {
		test('Should mount after authenticate even when OAuth endpoints are disabled', async () => {
			mockAppEnv({ MCP_OAUTH_ENABLED: false });

			await request('/items/test');

			expect(mockMcpOAuthGuard).toHaveBeenCalledTimes(1);
		});

		test('Should mount OAuth guard once when OAuth endpoints are enabled', async () => {
			mockAppEnv({ MCP_ENABLED: true, MCP_OAUTH_ENABLED: true });

			await request('/items/test');

			expect(mockMcpOAuthGuard).toHaveBeenCalledTimes(1);
		});
	});

	describe('Custom Endpoints', () => {
		test('Should not contain route for custom endpoint', async () => {
			const testRoute = '/custom-endpoint-to-test';

			const response = await request(testRoute);
			const body = await response.json();

			expect(body).toEqual({
				errors: [
					{
						extensions: {
							code: 'ROUTE_NOT_FOUND',
							path: '/custom-endpoint-to-test',
						},
						message: `Route ${testRoute} doesn't exist.`,
					},
				],
			});
		});

		test('Should contain route for custom endpoint', async () => {
			const testRoute = '/custom-endpoint-to-test';
			const testResponse = { key: 'value' };
			const mockRouter = Router();

			mockRouter.use(testRoute, (_, res) => {
				res.json(testResponse);
			});

			mockGetEndpointRouter.mockReturnValueOnce(mockRouter);

			const response = await request(testRoute);
			const body = await response.json();

			expect(body).toEqual(testResponse);
		});
	});

	describe('Not Found Handler', () => {
		test('Should return ROUTE_NOT_FOUND error when a route does not exist', async () => {
			const testRoute = '/this-route-does-not-exist';

			const response = await request(testRoute);
			const body = await response.json();

			expect(body).toEqual({
				errors: [
					{
						extensions: {
							code: 'ROUTE_NOT_FOUND',
							path: '/this-route-does-not-exist',
						},
						message: `Route ${testRoute} doesn't exist.`,
					},
				],
			});
		});
	});
});