1
import { useEnv } from '@directus/env';
2
import type { Request } from 'express';
3
import { expect, test, vi } from 'vitest';
4
import { shouldSkipCache } from './should-skip-cache.js';
6
vi.mock('@directus/env');
9
{ scenario: 'not relative', publicURL: 'http://admin.example.com', refererHost: '' },
10
{ scenario: 'relative', publicURL: '/', refererHost: 'http://ignore.domain' },
11
{ scenario: 'relative with subdirectory', publicURL: '/test/subfolder', refererHost: 'http://ignore.domain' },
13
'should always skip cache for requests coming from data studio when public URL is $scenario and CACHE_AUTO_PURGE is false',
14
({ publicURL, refererHost }) => {
15
vi.mocked(useEnv).mockReturnValue({
16
PUBLIC_URL: publicURL,
17
CACHE_SKIP_ALLOWED: false,
18
CACHE_AUTO_PURGE: false,
19
CACHE_AUTO_PURGE_IGNORE_LIST: ['directus_activity', 'directus_presets'],
26
return `${refererHost}${publicURL}/admin/settings/data-model`;
31
originalUrl: 'http://admin.example.com/items/test',
32
} as unknown as Request;
34
expect(shouldSkipCache(req)).toBe(true);
39
{ scenario: 'not relative', publicURL: 'http://admin.example.com', refererHost: '' },
40
{ scenario: 'relative', publicURL: '/', refererHost: 'http://ignore.domain' },
41
{ scenario: 'relative with subdirectory', publicURL: '/test/subfolder', refererHost: 'http://ignore.domain' },
43
'should not skip cache for requests coming from data studio when public URL is $scenario and CACHE_AUTO_PURGE is true',
44
({ publicURL, refererHost }) => {
45
vi.mocked(useEnv).mockReturnValue({
46
PUBLIC_URL: publicURL,
47
CACHE_SKIP_ALLOWED: false,
48
CACHE_AUTO_PURGE: true,
49
CACHE_AUTO_PURGE_IGNORE_LIST: ['directus_activity', 'directus_presets', 'ignore_collection'],
56
return `${refererHost}${publicURL}/admin/settings/data-model`;
61
originalUrl: 'http://admin.example.com/items/some_collection',
62
} as unknown as Request;
64
expect(shouldSkipCache(req)).toBe(false);
69
{ scenario: 'not relative', publicURL: 'http://admin.example.com', refererHost: '' },
70
{ scenario: 'relative', publicURL: '/', refererHost: 'http://ignore.domain' },
71
{ scenario: 'relative with subdirectory', publicURL: '/test/subfolder', refererHost: 'http://ignore.domain' },
73
'should skip cache for requests with collections in CACHE_AUTO_PURGE_IGNORE_LIST coming from data studio when public URL is $scenario and CACHE_AUTO_PURGE is true',
74
({ publicURL, refererHost }) => {
75
vi.mocked(useEnv).mockReturnValue({
76
PUBLIC_URL: publicURL,
77
CACHE_SKIP_ALLOWED: false,
78
CACHE_AUTO_PURGE: true,
79
CACHE_AUTO_PURGE_IGNORE_LIST: ['directus_activity', 'directus_presets', 'ignore_collection'],
86
return `${refererHost}${publicURL}/admin/settings/data-model`;
91
originalUrl: 'http://admin.example.com/items/ignore_collection',
92
} as unknown as Request;
94
expect(shouldSkipCache(req)).toBe(true);
98
test('should not skip cache for requests coming outside of data studio', () => {
99
vi.mocked(useEnv).mockReturnValue({
100
PUBLIC_URL: 'http://admin.example.com',
101
CACHE_SKIP_ALLOWED: 'false',
105
get: vi.fn((str) => {
108
return `http://elsewhere.example.com/admin/settings/data-model`;
113
} as unknown as Request;
115
expect(shouldSkipCache(req)).toBe(false);
119
{ scenario: 'accept', value: true },
120
{ scenario: 'ignore', value: false },
122
'should $scenario Cache-Control request header containing "no-store" when CACHE_SKIP_ALLOWED is $value',
124
vi.mocked(useEnv).mockReturnValue({
126
CACHE_SKIP_ALLOWED: value,
130
get: vi.fn((str) => {
132
case 'cache-control':
138
} as unknown as Request;
140
expect(shouldSkipCache(req)).toBe(value);