Repository navigation
Expand file tree
/
Copy pathdev-admin-seed-workspace-timezone.test.ts
More file actions
256 lines (228 loc) · 10.6 KB
/
Copy pathdev-admin-seed-workspace-timezone.test.ts
File metadata and controls
256 lines (228 loc) · 10.6 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* [#23175] A fresh `os dev` database opens with its workspace time zone set to
* the host's, so the console's "Set the workspace timezone" dialog does not
* open over the shell — and a workspace with no zone still gets the dialog.
*
* ## The condition the dialog reads
*
* The console (`WorkspaceTimezonePrompt`, objectui at this repo's
* `.objectui-sha`) asks an administrator while `GET /api/settings/localization`
* answers `values.timezone.source === 'default'`. That route returns
* `SettingsService.getNamespace('localization', { tenantId })` for the
* session's active organization, so every case below reads exactly that, for
* the organization the seeded admin is a member of.
*
* ## The composition
*
* The cheapest real one in which the dev seed's admin lands in a workspace: a
* real `ObjectKernel`, a real ObjectQL engine on in-memory SQLite, the real
* `AuthPlugin` (the seed and the `single` posture's Default Organization), the
* real `SecurityPlugin` (first-user promotion, which is what the
* default-organization bootstrap binds its owner from) and the real
* `SettingsServicePlugin` (the door, and the reader). `NODE_ENV=development`
* arms the seed the way `os dev` and the verify harness do.
*
* The host's zone is set the way a developer sets it, `TZ`, which Node re-reads
* on assignment; each case first asserts the zone really moved, so no case can
* pass on the container's own zone.
*
* ## Resolution
*
* `AuthPlugin` is imported from SOURCE; `@objectstack/service-settings` and
* `@objectstack/plugin-security` are aliased to their `src/` in this package's
* `vitest.config.ts`. `ObjectKernel`, `ObjectQLPlugin` and `SqlDriver` are the
* fixed instruments, read from `dist/` as this package's other kernel tests
* read them.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { ObjectKernel, type Plugin, type PluginContext } from '@objectstack/core';
import { ObjectQLPlugin, type ObjectQL } from '@objectstack/objectql';
import { SqlDriver } from '@objectstack/driver-sql';
import { SecurityPlugin } from '@objectstack/plugin-security';
import {
InMemoryCryptoProvider,
SettingsService,
SettingsServicePlugin,
} from '@objectstack/service-settings';
import { AuthPlugin } from './auth-plugin.js';
const SYS = { isSystem: true } as const;
const SEED_EMAIL = 'admin@objectos.ai';
/** A zone no CI container runs in, so a write of it cannot be the container's own. */
const HOST_ZONE = 'Asia/Shanghai';
/** The zone an earlier boot stored, for the never-overwrite case. */
const STORED_ZONE = 'Europe/Paris';
/** Env the seed, the posture or the localization cascade reads; cleared per case. */
const ENV_KEYS = [
'NODE_ENV',
'TZ',
'OS_SEED_ADMIN',
'OS_SEED_ADMIN_EMAIL',
'OS_SEED_ADMIN_PASSWORD',
'OS_SEED_ADMIN_NAME',
'OS_TENANCY_POSTURE',
'OS_MULTI_ORG_ENABLED',
'OS_PLATFORM_OWNER_EMAIL',
'OS_LOCALIZATION_TIMEZONE',
// [#25495] The same seed writes `localization.currency` from the host's
// POSIX locale; cleared so the "no localization write" cases below cannot
// pick up a currency write from the machine running them.
'LC_ALL',
'LC_MONETARY',
'LANG',
] as const;
/** Publishes an in-memory SQLite driver the way a datasource plugin does. */
function sqliteDriverPlugin(): Plugin {
return {
name: 'test.driver.sqlite',
type: 'standard',
version: '1.0.0',
async init(ctx: PluginContext) {
ctx.registerService(
'driver.default',
new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }),
);
},
};
}
/**
* An app plugin that leaves the Default Organization the zone an earlier boot
* stored: the `tenant` row the dialog's own confirm writes. It starts after
* `AuthPlugin` (registered later, no edge), so the organization exists, and
* before `kernel:ready`, so the row is there when the seed runs.
*/
function storedZoneAppPlugin(): Plugin {
return {
name: 'com.example.stored-zone',
type: 'app',
version: '1.0.0',
dependencies: ['com.objectstack.engine.objectql'],
async init() {},
async start(ctx: PluginContext) {
const ql = ctx.getService<ObjectQL>('objectql');
const org = await ql.findOne('sys_organization', { where: { slug: 'default' }, context: SYS });
expect(org?.id, 'the Default Organization must exist before the seed for this case to mean anything').toBeTruthy();
await ql.insert(
'sys_setting',
{
namespace: 'localization',
key: 'timezone',
scope: 'tenant',
organization_id: org!.id,
user_id: null,
value: STORED_ZONE,
encrypted: false,
locked: false,
},
{ context: SYS },
);
},
};
}
describe('the dev seed gives the workspace it lands its admin in the host time zone (#23175)', () => {
let kernel: ObjectKernel | undefined;
const savedEnv: Partial<Record<(typeof ENV_KEYS)[number], string | undefined>> = {};
beforeEach(() => {
for (const key of ENV_KEYS) {
savedEnv[key] = process.env[key];
delete process.env[key];
}
process.env.NODE_ENV = 'development';
});
afterEach(async () => {
try {
await kernel?.shutdown();
} catch {
/* a refused boot leaves the kernel stopped */
}
kernel = undefined;
for (const key of ENV_KEYS) {
if (savedEnv[key] === undefined) delete process.env[key];
else process.env[key] = savedEnv[key];
}
vi.restoreAllMocks();
});
/** Set the host's zone, and prove the process now resolves it. */
function hostZone(tz: string, expected: string | undefined): void {
process.env.TZ = tz;
expect(
Intl.DateTimeFormat().resolvedOptions().timeZone,
'TZ did not move the zone this process resolves, so this case would measure the container',
).toBe(expected);
}
async function boot(extra: Plugin[] = []) {
const settingsWrites = vi.spyOn(SettingsService.prototype, 'set');
kernel = new ObjectKernel({ logger: { level: 'silent' } });
await kernel.use(sqliteDriverPlugin());
await kernel.use(new ObjectQLPlugin());
await kernel.use(new AuthPlugin({ secret: 'test-secret-at-least-32-chars-long', baseUrl: 'http://localhost:3000' }));
await kernel.use(new SecurityPlugin());
await kernel.use(new SettingsServicePlugin({ registerRoutes: false, cryptoProvider: new InMemoryCryptoProvider() }));
for (const p of extra) await kernel.use(p);
await kernel.bootstrap();
const ql = kernel.getService<ObjectQL>('objectql');
const settings = kernel.getService<SettingsService>('settings');
/** What the dialog reads for one organization: the resolved `localization.timezone`. */
const zoneOf = async (organizationId: string) =>
(await settings.getNamespace('localization', { tenantId: organizationId })).values.timezone;
/** The organizations the seeded admin is a member of — the workspace its session opens in. */
const seededAdminOrganizations = async (): Promise<string[]> => {
const user = await ql.findOne('sys_user', { where: { email: SEED_EMAIL }, context: SYS });
if (!user?.id) return [];
const rows = (await ql.find('sys_member', { where: { user_id: user.id }, context: SYS })) as Array<{
organization_id?: string;
}>;
return rows.map((r) => String(r.organization_id));
};
const defaultOrganization = async (): Promise<string> => {
const org = await ql.findOne('sys_organization', { where: { slug: 'default' }, context: SYS });
expect(org?.id, 'the single posture boots with a Default Organization').toBeTruthy();
return String(org!.id);
};
return { ql, zoneOf, seededAdminOrganizations, defaultOrganization, settingsWrites };
}
it("1. a fresh dev database: the seeded admin's workspace resolves the host's zone, not the default — no dialog", async () => {
hostZone(HOST_ZONE, HOST_ZONE);
const { zoneOf, seededAdminOrganizations, defaultOrganization, settingsWrites } = await boot();
// The workspace is the one the admin's session opens in, and under the
// default `single` posture that is the Default Organization.
const orgs = await seededAdminOrganizations();
expect(orgs, 'the seed must land its admin in a workspace, or this case measures nothing').toEqual([
await defaultOrganization(),
]);
expect(await zoneOf(orgs[0]!)).toMatchObject({ value: HOST_ZONE, source: 'tenant' });
// Through the settings door, for that organization — not a raw row.
expect(settingsWrites).toHaveBeenCalledWith('localization', 'timezone', HOST_ZONE, { tenantId: orgs[0] });
}, 30_000);
it('2. a workspace with no zone still gets the dialog: the same boot with the seed disarmed resolves the default', async () => {
hostZone(HOST_ZONE, HOST_ZONE);
process.env.OS_SEED_ADMIN = '0';
const { zoneOf, seededAdminOrganizations, defaultOrganization } = await boot();
expect(await seededAdminOrganizations()).toEqual([]);
expect(await zoneOf(await defaultOrganization())).toMatchObject({ value: 'UTC', source: 'default' });
}, 30_000);
it('3. a host that reports no zone writes nothing: the workspace keeps the default and the dialog asks', async () => {
// An unknown TZ name: Node then resolves no zone at all (`undefined`).
hostZone('Not/AZone', undefined);
const { zoneOf, seededAdminOrganizations, settingsWrites } = await boot();
const orgs = await seededAdminOrganizations();
expect(orgs).toHaveLength(1);
expect(await zoneOf(orgs[0]!)).toMatchObject({ value: 'UTC', source: 'default' });
expect(settingsWrites.mock.calls.filter(([namespace]) => namespace === 'localization')).toEqual([]);
}, 30_000);
it('4. a zone the door refuses is not stored: ICU\'s Etc/Unknown leaves the default, and the dialog asks', async () => {
hostZone('Etc/Unknown', 'Etc/Unknown');
const { zoneOf, seededAdminOrganizations } = await boot();
const orgs = await seededAdminOrganizations();
expect(orgs).toHaveLength(1);
expect(await zoneOf(orgs[0]!)).toMatchObject({ value: 'UTC', source: 'default' });
}, 30_000);
it('5. never over a set zone: a workspace that already has one keeps it', async () => {
hostZone(HOST_ZONE, HOST_ZONE);
const { zoneOf, seededAdminOrganizations, settingsWrites } = await boot([storedZoneAppPlugin()]);
const orgs = await seededAdminOrganizations();
expect(orgs).toHaveLength(1);
expect(await zoneOf(orgs[0]!)).toMatchObject({ value: STORED_ZONE, source: 'tenant' });
expect(settingsWrites.mock.calls.filter(([namespace]) => namespace === 'localization')).toEqual([]);
}, 30_000);
});