-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstorage-module-cache.test.ts
More file actions
209 lines (181 loc) · 7.73 KB
/
Copy pathstorage-module-cache.test.ts
File metadata and controls
209 lines (181 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* Unit tests for resolveStorageConfigFromCodec.
*
* These are pure (no DB) tests covering both the single-tenant path (exact
* physical schema match) and the blueprint-pooling path, where the codec's
* build-time schema belongs to the REPRESENTATIVE tenant but the request's
* configs carry a DIFFERENT tenant's actual (differently-hashed) schema. In
* that case the resolver must fall back to matching on the LOGICAL schema
* name (the tenant hash prefix stripped from both sides).
*/
import { resolveStorageConfigFromCodec } from '../src/storage-module-cache';
import type { StorageModuleConfig } from '../src/types';
// --- Fixtures -------------------------------------------------------------
// Two real-shaped hashed tenant schemas that share the SAME logical suffix
// (`storage-public`). Under pooling, the shared instance is built against the
// representative tenant's schema, while requests route to other tenants.
const REPRESENTATIVE_SCHEMA = 'marketplace-db-tenant1-5e6b13b2-storage-public';
const TENANT_SCHEMA = 'marketplace-db-tenant2-35a03232-storage-public';
function makeConfig(
overrides: Partial<StorageModuleConfig> &
Pick<StorageModuleConfig, 'id' | 'schemaName' | 'bucketsTableName' | 'filesTableName'>,
): StorageModuleConfig {
return {
bucketsQualifiedName: `"${overrides.schemaName}"."${overrides.bucketsTableName}"`,
filesQualifiedName: `"${overrides.schemaName}"."${overrides.filesTableName}"`,
scope: 'app',
entityTableId: null,
entityQualifiedName: null,
endpoint: null,
publicUrlPrefix: null,
provider: null,
allowedOrigins: null,
uploadUrlExpirySeconds: 900,
downloadUrlExpirySeconds: 3600,
defaultMaxFileSize: 200 * 1024 * 1024,
maxFilenameLength: 1024,
cacheTtlSeconds: 3600,
hasPathShares: false,
maxBulkFiles: 100,
maxBulkTotalSize: 1073741824,
...overrides,
};
}
function makeCodec(schemaName: string | undefined, name: string | undefined) {
return { name: name as string, extensions: { pg: { schemaName, name } } };
}
// --- Tests ----------------------------------------------------------------
describe('resolveStorageConfigFromCodec', () => {
describe('single-tenant (exact physical schema match)', () => {
it('matches a files codec by exact schema + files table name', () => {
const config = makeConfig({
id: 'sm-1',
schemaName: TENANT_SCHEMA,
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const codec = makeCodec(TENANT_SCHEMA, 'app_files');
expect(resolveStorageConfigFromCodec(codec, [config])).toBe(config);
});
it('matches a buckets codec by exact schema + buckets table name', () => {
const config = makeConfig({
id: 'sm-1',
schemaName: TENANT_SCHEMA,
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const codec = makeCodec(TENANT_SCHEMA, 'app_buckets');
expect(resolveStorageConfigFromCodec(codec, [config])).toBe(config);
});
it('matches plain (non-hashed) schemas exactly, unaffected by hash stripping', () => {
const config = makeConfig({
id: 'sm-1',
schemaName: 'app_public',
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const codec = makeCodec('app_public', 'app_files');
expect(resolveStorageConfigFromCodec(codec, [config])).toBe(config);
});
});
describe('blueprint pooling (logical schema match across tenants)', () => {
it('matches a files codec built for the representative tenant against another tenant config', () => {
// Build-time codec: representative tenant. Request config: a DIFFERENT tenant.
const tenantConfig = makeConfig({
id: 'sm-tenant2',
schemaName: TENANT_SCHEMA,
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const codec = makeCodec(REPRESENTATIVE_SCHEMA, 'app_files');
// Physical schemas differ; logical suffix (`storage-public`) is shared.
expect(REPRESENTATIVE_SCHEMA).not.toEqual(TENANT_SCHEMA);
expect(resolveStorageConfigFromCodec(codec, [tenantConfig])).toBe(tenantConfig);
});
it('matches a buckets codec built for the representative tenant against another tenant config', () => {
const tenantConfig = makeConfig({
id: 'sm-tenant2',
schemaName: TENANT_SCHEMA,
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const codec = makeCodec(REPRESENTATIVE_SCHEMA, 'app_buckets');
expect(resolveStorageConfigFromCodec(codec, [tenantConfig])).toBe(tenantConfig);
});
it('does NOT match when the logical schema suffix differs', () => {
// A codec whose logical schema is `analytics-public` must not resolve to a
// `storage-public` config even though both are hashed tenant schemas.
const tenantConfig = makeConfig({
id: 'sm-tenant2',
schemaName: TENANT_SCHEMA,
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const codec = makeCodec('marketplace-db-tenant1-5e6b13b2-analytics-public', 'app_files');
expect(resolveStorageConfigFromCodec(codec, [tenantConfig])).toBeNull();
});
});
describe('exact-physical priority', () => {
it('prefers an exact physical match over a logical-only match', () => {
const exactConfig = makeConfig({
id: 'sm-exact',
schemaName: REPRESENTATIVE_SCHEMA,
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const logicalOnlyConfig = makeConfig({
id: 'sm-logical',
schemaName: TENANT_SCHEMA,
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const codec = makeCodec(REPRESENTATIVE_SCHEMA, 'app_files');
// exactConfig listed AFTER the logical-only one to prove priority is by
// match quality, not array order.
const result = resolveStorageConfigFromCodec(codec, [logicalOnlyConfig, exactConfig]);
expect(result).toBe(exactConfig);
});
});
describe('non-matches and guards', () => {
it('returns null when the table name matches nothing', () => {
const config = makeConfig({
id: 'sm-1',
schemaName: TENANT_SCHEMA,
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const codec = makeCodec(REPRESENTATIVE_SCHEMA, 'some_unrelated_table');
expect(resolveStorageConfigFromCodec(codec, [config])).toBeNull();
});
it('returns null when the codec has no schemaName', () => {
const config = makeConfig({
id: 'sm-1',
schemaName: TENANT_SCHEMA,
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const codec = makeCodec(undefined, 'app_files');
expect(resolveStorageConfigFromCodec(codec, [config])).toBeNull();
});
it('returns null when the codec has no resolvable table name', () => {
const config = makeConfig({
id: 'sm-1',
schemaName: TENANT_SCHEMA,
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const codec = { name: undefined as unknown as string, extensions: { pg: { schemaName: REPRESENTATIVE_SCHEMA } } };
expect(resolveStorageConfigFromCodec(codec, [config])).toBeNull();
});
it('falls back to codec.name for the table name when extensions.pg.name is absent', () => {
const config = makeConfig({
id: 'sm-1',
schemaName: TENANT_SCHEMA,
bucketsTableName: 'app_buckets',
filesTableName: 'app_files',
});
const codec = { name: 'app_files', extensions: { pg: { schemaName: REPRESENTATIVE_SCHEMA } } };
expect(resolveStorageConfigFromCodec(codec, [config])).toBe(config);
});
});
});