2022-12-14 17:35:22 +01:00
|
|
|
import { resolveOrigin } from './cors-origin-middleware';
|
2022-08-26 09:09:48 +02:00
|
|
|
import FakeSettingStore from '../../test/fixtures/fake-setting-store';
|
|
|
|
import { createTestConfig } from '../../test/config/test-config';
|
|
|
|
import FakeEventStore from '../../test/fixtures/fake-event-store';
|
|
|
|
import { randomId } from '../util/random-id';
|
2022-12-14 17:35:22 +01:00
|
|
|
import FakeProjectStore from '../../test/fixtures/fake-project-store';
|
2023-09-27 15:23:05 +02:00
|
|
|
import { EventService, ProxyService, SettingService } from '../../lib/services';
|
2022-12-14 17:35:22 +01:00
|
|
|
import { ISettingStore } from '../../lib/types';
|
|
|
|
import { frontendSettingsKey } from '../../lib/types/settings/frontend-settings';
|
|
|
|
import { minutesToMilliseconds } from 'date-fns';
|
2023-09-27 15:23:05 +02:00
|
|
|
import FakeFeatureTagStore from '../../test/fixtures/fake-feature-tag-store';
|
2022-08-26 09:09:48 +02:00
|
|
|
|
2022-12-14 17:35:22 +01:00
|
|
|
const createSettingService = (
|
|
|
|
frontendApiOrigins: string[],
|
|
|
|
): { proxyService: ProxyService; settingStore: ISettingStore } => {
|
2022-08-26 09:09:48 +02:00
|
|
|
const config = createTestConfig({ frontendApiOrigins });
|
|
|
|
|
|
|
|
const stores = {
|
|
|
|
settingStore: new FakeSettingStore(),
|
|
|
|
eventStore: new FakeEventStore(),
|
2023-09-27 15:23:05 +02:00
|
|
|
featureTagStore: new FakeFeatureTagStore(),
|
2022-12-14 17:35:22 +01:00
|
|
|
projectStore: new FakeProjectStore(),
|
2022-08-26 09:09:48 +02:00
|
|
|
};
|
|
|
|
|
2023-09-27 15:23:05 +02:00
|
|
|
const eventService = new EventService(stores, config);
|
|
|
|
|
2022-12-14 17:35:22 +01:00
|
|
|
const services = {
|
2023-09-27 15:23:05 +02:00
|
|
|
settingService: new SettingService(stores, config, eventService),
|
2022-12-14 17:35:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
//@ts-ignore
|
|
|
|
proxyService: new ProxyService(config, stores, services),
|
|
|
|
settingStore: stores.settingStore,
|
|
|
|
};
|
2022-08-26 09:09:48 +02:00
|
|
|
};
|
2022-08-19 08:09:44 +02:00
|
|
|
|
2022-12-14 17:35:22 +01:00
|
|
|
test('resolveOrigin', () => {
|
2022-08-19 08:09:44 +02:00
|
|
|
const dotCom = 'https://example.com';
|
|
|
|
const dotOrg = 'https://example.org';
|
|
|
|
|
2022-12-14 17:35:22 +01:00
|
|
|
expect(resolveOrigin([])).toEqual('*');
|
|
|
|
expect(resolveOrigin(['*'])).toEqual('*');
|
|
|
|
expect(resolveOrigin([dotOrg])).toEqual([dotOrg]);
|
|
|
|
expect(resolveOrigin([dotCom, dotOrg])).toEqual([dotCom, dotOrg]);
|
|
|
|
expect(resolveOrigin([dotOrg, '*'])).toEqual('*');
|
2022-08-19 08:09:44 +02:00
|
|
|
});
|
2022-08-26 09:09:48 +02:00
|
|
|
|
|
|
|
test('corsOriginMiddleware origin validation', async () => {
|
2022-12-14 17:35:22 +01:00
|
|
|
const { proxyService } = createSettingService([]);
|
2022-08-26 09:09:48 +02:00
|
|
|
const userName = randomId();
|
|
|
|
await expect(() =>
|
2022-12-14 17:35:22 +01:00
|
|
|
proxyService.setFrontendSettings(
|
|
|
|
{ frontendApiOrigins: ['a'] },
|
|
|
|
userName,
|
|
|
|
),
|
2022-08-26 09:09:48 +02:00
|
|
|
).rejects.toThrow('Invalid origin: a');
|
2022-12-14 17:35:22 +01:00
|
|
|
proxyService.destroy();
|
2022-08-26 09:09:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('corsOriginMiddleware without config', async () => {
|
2022-12-14 17:35:22 +01:00
|
|
|
const { proxyService, settingStore } = createSettingService([]);
|
2022-08-26 09:09:48 +02:00
|
|
|
const userName = randomId();
|
2022-12-14 17:35:22 +01:00
|
|
|
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
2022-08-26 09:09:48 +02:00
|
|
|
frontendApiOrigins: [],
|
|
|
|
});
|
2022-12-14 17:35:22 +01:00
|
|
|
await proxyService.setFrontendSettings(
|
|
|
|
{ frontendApiOrigins: [] },
|
|
|
|
userName,
|
|
|
|
);
|
|
|
|
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
2022-08-26 09:09:48 +02:00
|
|
|
frontendApiOrigins: [],
|
|
|
|
});
|
2022-12-14 17:35:22 +01:00
|
|
|
await proxyService.setFrontendSettings(
|
|
|
|
{ frontendApiOrigins: ['*'] },
|
|
|
|
userName,
|
|
|
|
);
|
|
|
|
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
2022-08-26 09:09:48 +02:00
|
|
|
frontendApiOrigins: ['*'],
|
|
|
|
});
|
2022-12-14 17:35:22 +01:00
|
|
|
await settingStore.delete(frontendSettingsKey);
|
|
|
|
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
2022-08-26 09:09:48 +02:00
|
|
|
frontendApiOrigins: [],
|
|
|
|
});
|
2022-12-14 17:35:22 +01:00
|
|
|
proxyService.destroy();
|
2022-08-26 09:09:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('corsOriginMiddleware with config', async () => {
|
2022-12-14 17:35:22 +01:00
|
|
|
const { proxyService, settingStore } = createSettingService(['*']);
|
2022-08-26 09:09:48 +02:00
|
|
|
const userName = randomId();
|
2022-12-14 17:35:22 +01:00
|
|
|
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
2022-08-26 09:09:48 +02:00
|
|
|
frontendApiOrigins: ['*'],
|
|
|
|
});
|
2022-12-14 17:35:22 +01:00
|
|
|
await proxyService.setFrontendSettings(
|
|
|
|
{ frontendApiOrigins: [] },
|
|
|
|
userName,
|
|
|
|
);
|
|
|
|
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
2022-08-26 09:09:48 +02:00
|
|
|
frontendApiOrigins: [],
|
|
|
|
});
|
2022-12-14 17:35:22 +01:00
|
|
|
await proxyService.setFrontendSettings(
|
2022-08-26 09:09:48 +02:00
|
|
|
{ frontendApiOrigins: ['https://example.com', 'https://example.org'] },
|
|
|
|
userName,
|
|
|
|
);
|
2022-12-14 17:35:22 +01:00
|
|
|
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
2022-08-26 09:09:48 +02:00
|
|
|
frontendApiOrigins: ['https://example.com', 'https://example.org'],
|
|
|
|
});
|
2022-12-14 17:35:22 +01:00
|
|
|
await settingStore.delete(frontendSettingsKey);
|
|
|
|
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
2022-08-26 09:09:48 +02:00
|
|
|
frontendApiOrigins: ['*'],
|
|
|
|
});
|
2022-12-14 17:35:22 +01:00
|
|
|
proxyService.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('corsOriginMiddleware with caching enabled', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
const { proxyService } = createSettingService([]);
|
|
|
|
|
|
|
|
const userName = randomId();
|
|
|
|
expect(await proxyService.getFrontendSettings()).toEqual({
|
|
|
|
frontendApiOrigins: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
//setting
|
|
|
|
await proxyService.setFrontendSettings(
|
|
|
|
{ frontendApiOrigins: ['*'] },
|
|
|
|
userName,
|
|
|
|
);
|
|
|
|
|
|
|
|
//still get cached value
|
|
|
|
expect(await proxyService.getFrontendSettings()).toEqual({
|
|
|
|
frontendApiOrigins: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(minutesToMilliseconds(2));
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
|
|
|
|
/*
|
|
|
|
This is needed because it is not enough to fake time to test the
|
2023-09-27 15:23:05 +02:00
|
|
|
updated cache, we also need to make sure that all promises are
|
|
|
|
executed and completed, in the right order.
|
2022-12-14 17:35:22 +01:00
|
|
|
*/
|
|
|
|
await new Promise<void>((resolve) =>
|
|
|
|
process.nextTick(async () => {
|
|
|
|
const settings = await proxyService.getFrontendSettings();
|
|
|
|
|
|
|
|
expect(settings).toEqual({
|
|
|
|
frontendApiOrigins: ['*'],
|
|
|
|
});
|
|
|
|
resolve();
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
proxyService.destroy();
|
2022-08-26 09:09:48 +02:00
|
|
|
});
|