Monitoring settings (Sentry, Umami) and the CSP origins come from the build environment
One build per environment: PROD_* variables for the production artifact, TEST_* for the test build and the CSP check. The build fails when a variable is empty. Tests use the production values from config/monitoringTestEnv.ts. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,22 @@
|
||||
import fc from 'fast-check';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
monitoredHostnames,
|
||||
monitoringConfig,
|
||||
monitoringFor,
|
||||
sentryIngestOrigin,
|
||||
monitoringOrigins,
|
||||
} from './config';
|
||||
|
||||
const dsn = 'https://key@o1.ingest.de.sentry.io/2';
|
||||
const script = 'https://analytics.example.nl/script.js';
|
||||
const raw = (extra: Record<string, unknown> = {}) => ({
|
||||
environment: 'production',
|
||||
sentryDsn: dsn,
|
||||
umamiScriptUrl: script,
|
||||
umamiWebsiteId: 'site-1',
|
||||
...extra,
|
||||
});
|
||||
|
||||
// Alles wat geen test- of productiehost is
|
||||
const otherHostname = fc
|
||||
.oneof(
|
||||
@@ -25,32 +36,89 @@ const otherHostname = fc
|
||||
)
|
||||
.filter((host) => !monitoredHostnames.includes(host));
|
||||
|
||||
describe('monitoringFor', () => {
|
||||
it('productiehost meet als production met de productiewebsite', () => {
|
||||
expect(monitoringFor('andreaslop.nl')).toEqual({
|
||||
afterEach(() => vi.unstubAllEnvs());
|
||||
|
||||
describe('monitoringConfig', () => {
|
||||
it('leest de waarden uit de omgeving van de build', () => {
|
||||
// De testomgeving (vite.config.ts) is de productiebuild
|
||||
expect(monitoringConfig()).toMatchObject({
|
||||
environment: 'production',
|
||||
umamiWebsiteId: '3edbb7d1-4388-4f2d-9cef-11ade88ace96',
|
||||
});
|
||||
vi.stubEnv('VITE_APP_ENV', 'test');
|
||||
vi.stubEnv('VITE_UMAMI_WEBSITE_ID', ' abc ');
|
||||
expect(monitoringConfig()).toMatchObject({
|
||||
environment: 'test',
|
||||
umamiWebsiteId: 'abc',
|
||||
});
|
||||
});
|
||||
|
||||
it('testhost meet als test met de testwebsite', () => {
|
||||
expect(monitoringFor('test.andreaslop.nl')).toEqual({
|
||||
environment: 'test',
|
||||
umamiWebsiteId: '7fb301ab-6ecc-4aea-beee-71bcb6009bfe',
|
||||
it('geeft null als er iets ontbreekt of ongeldig is', () => {
|
||||
expect(monitoringConfig(raw())).not.toBeNull();
|
||||
for (const bad of [
|
||||
{ environment: 'staging' },
|
||||
{ environment: undefined },
|
||||
{ sentryDsn: undefined },
|
||||
{ sentryDsn: 'http://key@host/1' },
|
||||
{ sentryDsn: 'geen url' },
|
||||
{ umamiScriptUrl: '' },
|
||||
{ umamiScriptUrl: 'http://analytics.example.nl/script.js' },
|
||||
{ umamiWebsiteId: ' ' },
|
||||
{ umamiWebsiteId: 5 },
|
||||
]) {
|
||||
expect(monitoringConfig(raw(bad))).toBeNull();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('monitoringFor', () => {
|
||||
it('productiebuild meet alleen op de productiehost', () => {
|
||||
const config = monitoringConfig(raw());
|
||||
expect(monitoringFor('andreaslop.nl', config)).toEqual({
|
||||
environment: 'production',
|
||||
sentryDsn: dsn,
|
||||
umamiScriptUrl: script,
|
||||
umamiWebsiteId: 'site-1',
|
||||
});
|
||||
expect(monitoringFor('test.andreaslop.nl', config)).toBeNull();
|
||||
});
|
||||
|
||||
it('testbuild meet alleen op de testhost', () => {
|
||||
const config = monitoringConfig(raw({ environment: 'test' }));
|
||||
expect(monitoringFor('test.andreaslop.nl', config)?.environment).toBe(
|
||||
'test',
|
||||
);
|
||||
expect(monitoringFor('andreaslop.nl', config)).toBeNull();
|
||||
});
|
||||
|
||||
it('meet niets zonder instellingen', () => {
|
||||
expect(monitoringFor('andreaslop.nl', null)).toBeNull();
|
||||
});
|
||||
|
||||
it('elke andere host meet niets (M1)', () => {
|
||||
fc.assert(
|
||||
fc.property(otherHostname, (host) => {
|
||||
expect(monitoringFor(host)).toBeNull();
|
||||
expect(
|
||||
monitoringFor(
|
||||
host,
|
||||
monitoringConfig(raw({ environment: 'test' })),
|
||||
),
|
||||
).toBeNull();
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('Sentry-ingest is de EU-host uit de DSN, zonder sleutel', () => {
|
||||
expect(sentryIngestOrigin).toBe(
|
||||
'https://o4511795618185216.ingest.de.sentry.io',
|
||||
);
|
||||
describe('monitoringOrigins', () => {
|
||||
it('zijn de herkomst van het Umami-script en van de Sentry-ingest, zonder sleutel', () => {
|
||||
expect(monitoringOrigins(monitoringConfig(raw()))).toEqual({
|
||||
umami: ['https://analytics.example.nl'],
|
||||
sentry: ['https://o1.ingest.de.sentry.io'],
|
||||
});
|
||||
});
|
||||
|
||||
it('zijn leeg zonder instellingen', () => {
|
||||
expect(monitoringOrigins(null)).toEqual({ umami: [], sentry: [] });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user