mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
* Step 1 * Step 2 * False sense of confidence * Couple more before dinner * Update schema package * Update format-title * Upgrade specs file * Close * Replace ts-node-dev with tsx, and various others * Replace lodash with lodash-es * Add lodash-es types * Update knex import * More fun is had * FSE * Consolidate repos * Various tweaks and fixes * Fix specs * Remove dependency on knex-schema-inspector * Fix wrong imports of inspector * Move shared exceptions to new package * Move constants to separate module * Move types to new types package * Use directus/types * I believe this is no longer needed * [WIP] Start moving utils to esm * ESMify Shared * Move shared utils to @directus/utils * Use @directus/utils instead of @directus/shared/utils * It runs! * Use correct schemaoverview type * Fix imports * Fix the thing * Start on new update-checker lib * Use new update-check package * Swap out directus/shared in app * Pushing through the last bits now * Dangerously make extensions SDK ESM * Use @directus/types in tests * Copy util function to test * Fix linter config * Add missing import * Hot takes * Fix build * Curse these default exports * No tests in constants * Add tests * Remove tests from types * Add tests for exceptions * Fix test * Fix app tests * Fix import in test * Fix various tests * Fix specs export * Some more tests * Remove broken integration tests These were broken beyond repair.. They were also written before we really knew what we we're doing with tests, so I think it's better to say goodbye and start over with these * Regenerate lockfile * Fix imports from merge * I create my own problems * Make sharp play nice * Add vitest config * Install missing blackbox dep * Consts shouldn't be in types tsk tsk tsk tsk * Fix type/const usage in extensions-sdk * cursed.default * Reduce circular deps * Fix circular dep in items service * vvv * Trigger testing for all vendors * Add workaround for rollup * Prepend the file protocol for the ESM loader to be compatible with Windows "WARN: Only URLs with a scheme in: file and data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'" * Fix postgres * Schema package updates Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com> * Resolve cjs/mjs extensions * Clean-up eslint config * fixed extension concatination * using string interpolation for consistency * Revert MySQL optimisation * Revert testing for all vendors * Replace tsx with esbuild-kit/esm-loader Is a bit faster and we can rely on the built-in `watch` and `inspect` functionalities of Node.js Note: The possibility to watch other files (.env in our case) might be added in the future, see https://github.com/nodejs/node/issues/45467 * Use exact version for esbuild-kit/esm-loader * Fix import --------- Co-authored-by: ian <licitdev@gmail.com> Co-authored-by: Brainslug <tim@brainslug.nl> Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com> Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
105 lines
3.0 KiB
TypeScript
105 lines
3.0 KiB
TypeScript
import { randIp, randUrl, randWord } from '@ngneat/falso';
|
|
import type { InternalAxiosRequestConfig } from 'axios';
|
|
import axios from 'axios';
|
|
import type { LookupAddress } from 'node:dns';
|
|
import { lookup } from 'node:dns/promises';
|
|
import { isIP } from 'node:net';
|
|
import { URL } from 'node:url';
|
|
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
|
|
import logger from '../logger.js';
|
|
import { requestInterceptor } from './request-interceptor.js';
|
|
import { validateIP } from './validate-ip.js';
|
|
|
|
vi.mock('axios');
|
|
vi.mock('node:net');
|
|
vi.mock('node:url');
|
|
vi.mock('node:dns/promises');
|
|
vi.mock('./validate-ip');
|
|
vi.mock('../logger');
|
|
|
|
let sample: {
|
|
config: InternalAxiosRequestConfig;
|
|
url: string;
|
|
hostname: string;
|
|
ip: string;
|
|
};
|
|
|
|
beforeEach(() => {
|
|
sample = {
|
|
config: {} as InternalAxiosRequestConfig,
|
|
url: randUrl(),
|
|
hostname: randWord(),
|
|
ip: randIp(),
|
|
};
|
|
|
|
vi.mocked(axios.getUri).mockReturnValue(sample.url);
|
|
vi.mocked(URL).mockReturnValue({ hostname: sample.hostname } as URL);
|
|
vi.mocked(lookup).mockResolvedValue({ address: sample.ip } as LookupAddress);
|
|
vi.mocked(isIP).mockReturnValue(0);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.resetAllMocks();
|
|
});
|
|
|
|
test('Uses axios getUri to get full URI', async () => {
|
|
await requestInterceptor(sample.config);
|
|
expect(axios.getUri).toHaveBeenCalledWith(sample.config);
|
|
});
|
|
|
|
test('Gets hostname using URL', async () => {
|
|
await requestInterceptor(sample.config);
|
|
expect(URL).toHaveBeenCalledWith(sample.url);
|
|
});
|
|
|
|
test('Checks if hostname is IP', async () => {
|
|
await requestInterceptor(sample.config);
|
|
expect(isIP).toHaveBeenCalledWith(sample.hostname);
|
|
});
|
|
|
|
test('Looks up IP address using dns lookup if hostname is not an IP address', async () => {
|
|
await requestInterceptor(sample.config);
|
|
expect(lookup).toHaveBeenCalledWith(sample.hostname);
|
|
});
|
|
|
|
test('Logs when the lookup throws an error', async () => {
|
|
const mockError = new Error();
|
|
vi.mocked(lookup).mockRejectedValue(mockError);
|
|
|
|
try {
|
|
await requestInterceptor(sample.config);
|
|
} catch {
|
|
// Expect to error
|
|
} finally {
|
|
expect(logger.warn).toHaveBeenCalledWith(mockError, `Couldn't lookup the DNS for url "${sample.url}"`);
|
|
}
|
|
});
|
|
|
|
test('Throws error when dns lookup fails', async () => {
|
|
const mockError = new Error();
|
|
vi.mocked(lookup).mockRejectedValue(mockError);
|
|
|
|
try {
|
|
await requestInterceptor(sample.config);
|
|
} catch (err: any) {
|
|
expect(err).toBeInstanceOf(Error);
|
|
expect(err.message).toBe(`Requested URL "${sample.url}" resolves to a denied IP address`);
|
|
}
|
|
});
|
|
|
|
test('Validates IP', async () => {
|
|
await requestInterceptor(sample.config);
|
|
expect(validateIP).toHaveBeenCalledWith(sample.ip, sample.url);
|
|
});
|
|
|
|
test('Validates IP from hostname if URL hostname is IP', async () => {
|
|
vi.mocked(isIP).mockReturnValue(4);
|
|
await requestInterceptor(sample.config);
|
|
expect(validateIP).toHaveBeenCalledWith(sample.hostname, sample.url);
|
|
});
|
|
|
|
test('Returns config unmodified', async () => {
|
|
const config = await requestInterceptor(sample.config);
|
|
expect(config).toBe(config);
|
|
});
|