@lowdefy/server 7.0.0 → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/clientBuildImports.test.mjs +1 -0
- package/client/getShownPage.test.mjs +1 -0
- package/client/shouldReloadForTypes.test.mjs +1 -0
- package/lib/build/mobileConfig.js +26 -0
- package/lib/client/auth/AuthConfigured.jsx +75 -48
- package/lib/client/auth/createFetchCheckingBuild.test.mjs +3 -3
- package/lib/client/auth/sessionCaller.js +56 -0
- package/lib/client/createLogUsage.test.mjs +2 -2
- package/lib/server/getClientAddress.test.mjs +6 -7
- package/lib/server/log/createHandleError.test.mjs +5 -5
- package/lib/server/log/createLogger.test.mjs +3 -3
- package/lib/server/log/projectErrorForLog.js +6 -4
- package/lib/server/log/redactUrlQuery.test.mjs +1 -0
- package/lib/server/log/serializeErrorForLog.test.mjs +50 -113
- package/lib/server/sentry/captureSentryError.test.mjs +4 -7
- package/lib/server/sentry/initSentry.serverless.test.mjs +6 -6
- package/lib/server/sentry/initSentry.test.mjs +3 -3
- package/lib/server/sentry/scrubEvent.test.mjs +2 -0
- package/lowdefy/build.mjs +5 -1
- package/lowdefy/build.test.mjs +31 -13
- package/lowdefy/createCustomPluginMessagesMap.mjs +2 -4
- package/lowdefy/createCustomPluginTypesMap.mjs +39 -11
- package/lowdefy/createCustomPluginTypesMap.test.mjs +249 -0
- package/package.json +31 -32
- package/package.original.json +31 -32
- package/src/app.js +19 -2
- package/src/app.mobile.test.mjs +351 -0
- package/src/app.test.mjs +31 -27
- package/src/html/collectPageTypesAssets.test.mjs +1 -0
- package/src/html/getPageAssets.test.mjs +1 -0
- package/src/html/renderPage.contentSecurityPolicy.test.mjs +13 -10
- package/src/html/renderPage.test.mjs +16 -13
- package/src/html/template.test.mjs +1 -0
- package/src/initServer.test.mjs +12 -12
- package/src/lib/getPathSegments.js +16 -1
- package/src/lib/getRequestSurface.js +25 -0
- package/src/middleware/apiContext.js +7 -4
- package/src/middleware/apiContext.test.mjs +48 -30
- package/src/middleware/createSameOriginGuard.test.mjs +1 -0
- package/src/middleware/errorHandler.js +33 -45
- package/src/middleware/errorHandler.test.mjs +11 -11
- package/src/middleware/refuseOtherBuild.test.mjs +3 -3
- package/src/middleware/stampBuildId.test.mjs +1 -0
- package/src/middleware/stampCaller.test.mjs +1 -0
- package/src/routes/agentBearer.test.mjs +175 -0
- package/src/routes/apiPage.js +12 -1
- package/src/routes/apiPage.test.mjs +5 -5
- package/src/routes/auth.test.mjs +6 -6
- package/src/routes/clientError.js +14 -2
- package/src/routes/clientError.test.mjs +106 -0
- package/src/routes/endpoints.js +24 -0
- package/src/routes/endpointsLink.test.mjs +112 -0
- package/src/routes/endpointsTextAnswer.test.mjs +8 -8
- package/src/routes/mcp.js +6 -0
- package/src/routes/mcp.test.mjs +23 -8
- package/src/routes/mcpMemberToken.test.mjs +8 -8
- package/src/routes/mcpProtectedResourceMetadata.test.mjs +2 -2
- package/src/routes/mobileRoot.js +30 -0
- package/src/routes/mobileSignIn.js +46 -0
- package/src/routes/mobileSignIn.test.mjs +58 -0
- package/src/routes/mobileSignInCallback.js +34 -0
- package/src/routes/mobileSignInCallback.test.mjs +51 -0
- package/src/routes/mountMobileWellKnown.js +35 -0
- package/src/routes/mountMobileWellKnown.test.mjs +79 -0
- package/src/routes/mountOauthDiscovery.test.mjs +5 -5
- package/src/routes/mountPageRoutes.js +9 -2
- package/src/routes/mountPageRoutes.test.mjs +33 -3
- package/src/routes/normalizeMcpProtocolVersionHeader.test.mjs +1 -0
- package/src/routes/requestBody.test.mjs +12 -12
- package/src/routes/websocket.js +4 -2
- package/src/routes/websocket.test.mjs +2 -2
- package/lib/server/log/maskCredentialKeys.js +0 -37
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import fs from 'node:fs';
|
|
18
|
+
import os from 'node:os';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
|
|
21
|
+
import { vi, afterAll, beforeAll, describe, expect, test } from 'vitest';
|
|
22
|
+
|
|
23
|
+
// The real app wiring with the real page, mobile root and well-known routes,
|
|
24
|
+
// over a fake caller context: what a mobile shell and a browser each get.
|
|
25
|
+
function handler(c) {
|
|
26
|
+
return c.json({ ok: true });
|
|
27
|
+
}
|
|
28
|
+
function passThrough() {
|
|
29
|
+
return (c, next) => next();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const routes = [
|
|
33
|
+
{ pageId: 'home', path: 'home', auth: { public: true } },
|
|
34
|
+
{ pageId: 'today', path: 'today', auth: { public: true } },
|
|
35
|
+
{ pageId: 'shared', path: 'shared', auth: { public: true } },
|
|
36
|
+
{ pageId: 'admin', path: 'admin', auth: { public: false, roles: ['admin'] } },
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const pages = {
|
|
40
|
+
home: { id: 'page:home', pageId: 'home', target: 'web', auth: { public: true } },
|
|
41
|
+
today: {
|
|
42
|
+
id: 'page:today',
|
|
43
|
+
pageId: 'today',
|
|
44
|
+
target: 'mobile',
|
|
45
|
+
auth: { public: true },
|
|
46
|
+
properties: { notes: 'x'.repeat(4000) },
|
|
47
|
+
},
|
|
48
|
+
shared: { id: 'page:shared', pageId: 'shared', target: 'both', auth: { public: true } },
|
|
49
|
+
admin: {
|
|
50
|
+
id: 'page:admin',
|
|
51
|
+
pageId: 'admin',
|
|
52
|
+
target: 'mobile',
|
|
53
|
+
auth: { public: false, roles: ['admin'] },
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const mobileConfig = {
|
|
58
|
+
homePageId: 'today',
|
|
59
|
+
minShellVersion: '7.4.0',
|
|
60
|
+
menus: {
|
|
61
|
+
tabs: [
|
|
62
|
+
{ pageId: 'today', title: 'Today' },
|
|
63
|
+
{ pageId: 'admin', title: 'Admin' },
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
shells: [
|
|
67
|
+
{
|
|
68
|
+
id: 'field',
|
|
69
|
+
ios: { bundleId: 'com.example.field', teamId: 'TEAM1' },
|
|
70
|
+
android: { package: 'com.example.field', sha256: ['AA:BB'] },
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const configFiles = {
|
|
76
|
+
'routes.json': routes,
|
|
77
|
+
'mobile/config.json': mobileConfig,
|
|
78
|
+
'mobile/pageIndex.json': [
|
|
79
|
+
{ path: 'today', pageId: 'today', hash: 'h-today', typesKey: 't1' },
|
|
80
|
+
{ path: 'shared', pageId: 'shared', hash: 'h-shared', typesKey: 't1' },
|
|
81
|
+
{ path: 'admin', pageId: 'admin', hash: 'h-admin', typesKey: 't2' },
|
|
82
|
+
],
|
|
83
|
+
'mobile/typeSets.json': { t1: { blocks: [] }, t2: { blocks: [] } },
|
|
84
|
+
'mobile/theme.json': {
|
|
85
|
+
darkMode: 'system',
|
|
86
|
+
light: { colorPrimary: '#1677ff' },
|
|
87
|
+
dark: { colorPrimary: '#1668dc' },
|
|
88
|
+
},
|
|
89
|
+
'mobile/classMap.json': { 'p-4': { paddingTop: 16 } },
|
|
90
|
+
'mobile/appJs.json': { jsEntries: null },
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
function authorizeOutcome(user) {
|
|
94
|
+
return function outcome(config) {
|
|
95
|
+
const roles = config.auth.roles;
|
|
96
|
+
if (config.auth.public === true) return 'allow';
|
|
97
|
+
if (!user) return 'deny';
|
|
98
|
+
if (roles && !roles.some((role) => user.roles.includes(role))) return 'deny';
|
|
99
|
+
return 'allow';
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
vi.doMock('./middleware/apiContext.js', () => ({
|
|
104
|
+
default: () => async (c, next) => {
|
|
105
|
+
const user = c.req.header('x-test-roles')
|
|
106
|
+
? { id: 'u1', roles: c.req.header('x-test-roles').split(',') }
|
|
107
|
+
: null;
|
|
108
|
+
c.set('lowdefyContext', {
|
|
109
|
+
user,
|
|
110
|
+
authSession: null,
|
|
111
|
+
authEnforcement: null,
|
|
112
|
+
authorizeOutcome: authorizeOutcome(user),
|
|
113
|
+
logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
|
|
114
|
+
readConfigFile: async (file) => {
|
|
115
|
+
if (file.startsWith('pages/')) return pages[file.slice(6, -5)] ?? null;
|
|
116
|
+
return configFiles[file];
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
await next();
|
|
120
|
+
},
|
|
121
|
+
}));
|
|
122
|
+
vi.doMock('./routes/agent.js', () => ({ default: handler }));
|
|
123
|
+
vi.doMock('./routes/auth.js', () => ({ default: () => handler }));
|
|
124
|
+
vi.doMock('./routes/clientError.js', () => ({ default: handler }));
|
|
125
|
+
vi.doMock('./routes/cron.js', () => ({ default: handler }));
|
|
126
|
+
vi.doMock('./routes/cronForward.js', () => ({ default: handler }));
|
|
127
|
+
vi.doMock('./routes/detached.js', () => ({ default: handler }));
|
|
128
|
+
vi.doMock('./routes/endpoints.js', () => ({ default: handler }));
|
|
129
|
+
vi.doMock('./routes/mcp.js', () => ({ default: handler }));
|
|
130
|
+
vi.doMock('./routes/mountOauthDiscovery.js', () => ({ default: () => {} }));
|
|
131
|
+
vi.doMock('./routes/request.js', () => ({ default: handler }));
|
|
132
|
+
vi.doMock('./routes/usage.js', () => ({ default: handler }));
|
|
133
|
+
vi.doMock('./routes/user.js', () => ({ default: handler }));
|
|
134
|
+
vi.doMock('./routes/websocket.js', () => ({ default: handler }));
|
|
135
|
+
vi.doMock('./html/renderPage.js', () => ({
|
|
136
|
+
default: (c) => c.html('<html></html>'),
|
|
137
|
+
}));
|
|
138
|
+
vi.doMock('./middleware/sentry.js', () => ({ default: passThrough }));
|
|
139
|
+
vi.doMock('./middleware/errorHandler.js', () => ({
|
|
140
|
+
default: () => (error, c) => c.json({ error: error.message }, 500),
|
|
141
|
+
}));
|
|
142
|
+
vi.doMock('../lib/build/appMeta.js', () => ({ default: { buildId: 'build-1' } }));
|
|
143
|
+
vi.doMock('../lib/build/auth.js', () => ({
|
|
144
|
+
default: { configured: false, authPages: { signIn: '/auth/login' } },
|
|
145
|
+
}));
|
|
146
|
+
vi.doMock('../lib/build/theme.js', () => ({ default: {} }));
|
|
147
|
+
vi.doMock('../lib/build/config.js', () => ({ default: {} }));
|
|
148
|
+
vi.doMock('../lib/build/mobileConfig.js', () => ({ default: mobileConfig }));
|
|
149
|
+
vi.doMock('../lib/server/auth/getAuth.js', () => ({ default: vi.fn() }));
|
|
150
|
+
vi.doMock('../lib/server/auth/getStrategies.js', () => ({ default: vi.fn() }));
|
|
151
|
+
vi.doMock('../lib/server/log/createLogger.js', () => ({
|
|
152
|
+
default: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn() }),
|
|
153
|
+
}));
|
|
154
|
+
|
|
155
|
+
const { default: createApp } = await import('./app.js');
|
|
156
|
+
|
|
157
|
+
const originalCwd = process.cwd();
|
|
158
|
+
let serverDirectory;
|
|
159
|
+
let app;
|
|
160
|
+
|
|
161
|
+
beforeAll(() => {
|
|
162
|
+
serverDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'lowdefy-server-mobile-'));
|
|
163
|
+
fs.mkdirSync(path.join(serverDirectory, 'dist/client'), { recursive: true });
|
|
164
|
+
process.chdir(serverDirectory);
|
|
165
|
+
app = createApp();
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
afterAll(() => {
|
|
169
|
+
process.chdir(originalCwd);
|
|
170
|
+
fs.rmSync(serverDirectory, { recursive: true, force: true });
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const shell = { 'x-lowdefy-shell': '7.4.0' };
|
|
174
|
+
|
|
175
|
+
describe('GET /api/mobile/root', () => {
|
|
176
|
+
test('filters the page index, menus and type sets by the caller roles', async () => {
|
|
177
|
+
const signedOut = await app.request('/api/mobile/root', { headers: shell });
|
|
178
|
+
expect(signedOut.status).toEqual(200);
|
|
179
|
+
const signedOutBody = await signedOut.json();
|
|
180
|
+
expect(signedOutBody.pageIndex.map((entry) => entry.pageId)).toEqual(['today', 'shared']);
|
|
181
|
+
expect(signedOutBody.menus.tabs.map((item) => item.pageId)).toEqual(['today']);
|
|
182
|
+
expect(Object.keys(signedOutBody.typeSets)).toEqual(['t1']);
|
|
183
|
+
|
|
184
|
+
const admin = await app.request('/api/mobile/root', {
|
|
185
|
+
headers: { ...shell, 'x-test-roles': 'admin' },
|
|
186
|
+
});
|
|
187
|
+
const adminBody = await admin.json();
|
|
188
|
+
expect(adminBody.pageIndex.map((entry) => entry.pageId)).toEqual(['today', 'shared', 'admin']);
|
|
189
|
+
expect(adminBody.menus.tabs.map((item) => item.pageId)).toEqual(['today', 'admin']);
|
|
190
|
+
expect(adminBody.minShellVersion).toEqual('7.4.0');
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test('sends the theme and class map the build wrote', async () => {
|
|
194
|
+
const res = await app.request('/api/mobile/root', { headers: shell });
|
|
195
|
+
const body = await res.json();
|
|
196
|
+
expect(body.theme).toEqual(configFiles['mobile/theme.json']);
|
|
197
|
+
expect(body.classMap).toEqual(configFiles['mobile/classMap.json']);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test('sends a strong ETag, no-store and the caller stamp', async () => {
|
|
201
|
+
const res = await app.request('/api/mobile/root', { headers: shell });
|
|
202
|
+
expect(res.headers.get('etag')).toMatch(/^"[^"]+"$/);
|
|
203
|
+
expect(res.headers.get('cache-control')).toEqual('private, no-store');
|
|
204
|
+
expect(res.headers.get('x-lowdefy-caller')).not.toBeNull();
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test('answers 304 with the caller stamp to a matching If-None-Match', async () => {
|
|
208
|
+
const first = await app.request('/api/mobile/root', { headers: shell });
|
|
209
|
+
const etag = first.headers.get('etag');
|
|
210
|
+
const second = await app.request('/api/mobile/root', {
|
|
211
|
+
headers: { ...shell, 'if-none-match': etag },
|
|
212
|
+
});
|
|
213
|
+
expect(second.status).toEqual(304);
|
|
214
|
+
expect(await second.text()).toEqual('');
|
|
215
|
+
expect(second.headers.get('etag')).toEqual(etag);
|
|
216
|
+
expect(second.headers.get('cache-control')).toEqual('private, no-store');
|
|
217
|
+
expect(second.headers.get('x-lowdefy-caller')).not.toBeNull();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test('answers 200 when the roles changed what the caller sees', async () => {
|
|
221
|
+
const first = await app.request('/api/mobile/root', { headers: shell });
|
|
222
|
+
const res = await app.request('/api/mobile/root', {
|
|
223
|
+
headers: { ...shell, 'x-test-roles': 'admin', 'if-none-match': first.headers.get('etag') },
|
|
224
|
+
});
|
|
225
|
+
expect(res.status).toEqual(200);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
describe('GET /api/page/* surface gate', () => {
|
|
230
|
+
test('serves a mobile page to a shell and refuses it to a browser', async () => {
|
|
231
|
+
const toShell = await app.request('/api/page/today', { headers: shell });
|
|
232
|
+
expect(toShell.status).toEqual(200);
|
|
233
|
+
expect((await toShell.json()).pageId).toEqual('today');
|
|
234
|
+
|
|
235
|
+
const toBrowser = await app.request('/api/page/today');
|
|
236
|
+
expect(toBrowser.status).toEqual(404);
|
|
237
|
+
expect(await toBrowser.json()).toEqual({ pageConfig: null, surface: 'mobile' });
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test('serves a web page to a browser and refuses it to a shell', async () => {
|
|
241
|
+
const toBrowser = await app.request('/api/page/home');
|
|
242
|
+
expect(toBrowser.status).toEqual(200);
|
|
243
|
+
|
|
244
|
+
const toShell = await app.request('/api/page/home', { headers: shell });
|
|
245
|
+
expect(toShell.status).toEqual(404);
|
|
246
|
+
expect(await toShell.json()).toEqual({ pageConfig: null, surface: 'web' });
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test('serves a shared page to both', async () => {
|
|
250
|
+
expect((await app.request('/api/page/shared')).status).toEqual(200);
|
|
251
|
+
expect((await app.request('/api/page/shared', { headers: shell })).status).toEqual(200);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test('keeps page access with auth: a signed-out shell gets the sign-in redirect', async () => {
|
|
255
|
+
const res = await app.request('/api/page/admin', { headers: shell });
|
|
256
|
+
expect(res.status).toEqual(401);
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
describe('GET /api/page/* ETag', () => {
|
|
261
|
+
test('a browser page response carries no ETag, as before', async () => {
|
|
262
|
+
const res = await app.request('/api/page/shared');
|
|
263
|
+
expect(res.status).toEqual(200);
|
|
264
|
+
expect(res.headers.get('etag')).toBeNull();
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test('answers 304 to a matching If-None-Match', async () => {
|
|
268
|
+
const first = await app.request('/api/page/shared', { headers: shell });
|
|
269
|
+
const etag = first.headers.get('etag');
|
|
270
|
+
expect(etag).toMatch(/^"[^"]+"$/);
|
|
271
|
+
const second = await app.request('/api/page/shared', {
|
|
272
|
+
headers: { ...shell, 'if-none-match': etag },
|
|
273
|
+
});
|
|
274
|
+
expect(second.status).toEqual(304);
|
|
275
|
+
expect(second.headers.get('x-lowdefy-caller')).not.toBeNull();
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
test('a compressed page carries a weak ETag that still revalidates', async () => {
|
|
279
|
+
const first = await app.request('/api/page/today', {
|
|
280
|
+
headers: { ...shell, 'accept-encoding': 'gzip' },
|
|
281
|
+
});
|
|
282
|
+
expect(first.headers.get('content-encoding')).toEqual('gzip');
|
|
283
|
+
const etag = first.headers.get('etag');
|
|
284
|
+
expect(etag).toMatch(/^W\/"[^"]+"$/);
|
|
285
|
+
const second = await app.request('/api/page/today', {
|
|
286
|
+
headers: { ...shell, 'accept-encoding': 'gzip', 'if-none-match': etag },
|
|
287
|
+
});
|
|
288
|
+
expect(second.status).toEqual(304);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test('an error answer carries no ETag', async () => {
|
|
292
|
+
const res = await app.request('/api/page/missing');
|
|
293
|
+
expect(res.status).toEqual(404);
|
|
294
|
+
expect(res.headers.get('etag')).toBeNull();
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
describe('/.well-known', () => {
|
|
299
|
+
test('serves apple-app-site-association from mobile.shells', async () => {
|
|
300
|
+
const res = await app.request('/.well-known/apple-app-site-association');
|
|
301
|
+
expect(res.status).toEqual(200);
|
|
302
|
+
expect(res.headers.get('content-type')).toMatch(/^application\/json/);
|
|
303
|
+
expect(await res.json()).toEqual({
|
|
304
|
+
applinks: {
|
|
305
|
+
details: [
|
|
306
|
+
{
|
|
307
|
+
appIDs: ['TEAM1.com.example.field'],
|
|
308
|
+
components: [{ '/': '/api/*', exclude: true }, { '/': '/*' }],
|
|
309
|
+
},
|
|
310
|
+
],
|
|
311
|
+
},
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test('serves assetlinks.json from mobile.shells', async () => {
|
|
316
|
+
const res = await app.request('/.well-known/assetlinks.json');
|
|
317
|
+
expect(res.status).toEqual(200);
|
|
318
|
+
expect(await res.json()).toEqual([
|
|
319
|
+
{
|
|
320
|
+
relation: ['delegate_permission/common.handle_all_urls'],
|
|
321
|
+
target: {
|
|
322
|
+
namespace: 'android_app',
|
|
323
|
+
package_name: 'com.example.field',
|
|
324
|
+
sha256_cert_fingerprints: ['AA:BB'],
|
|
325
|
+
},
|
|
326
|
+
},
|
|
327
|
+
]);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
test('the catch-all still answers 404 for other well-known paths', async () => {
|
|
331
|
+
const res = await app.request('/.well-known/security.txt');
|
|
332
|
+
expect(res.status).toEqual(404);
|
|
333
|
+
expect((await res.json()).name).toEqual('NotFoundError');
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
describe('endpoint calls and the build stamp', () => {
|
|
338
|
+
test('an endpoint call without x-lowdefy-build runs, as a shell sends it', async () => {
|
|
339
|
+
const res = await app.request('/api/endpoints/my_endpoint', { method: 'POST' });
|
|
340
|
+
expect(res.status).toEqual(200);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
test('an endpoint call from another build is refused with 409', async () => {
|
|
344
|
+
const res = await app.request('/api/endpoints/my_endpoint', {
|
|
345
|
+
method: 'POST',
|
|
346
|
+
headers: { 'x-lowdefy-build': 'build-0' },
|
|
347
|
+
});
|
|
348
|
+
expect(res.status).toEqual(409);
|
|
349
|
+
expect((await res.json()).buildId).toEqual('build-1');
|
|
350
|
+
});
|
|
351
|
+
});
|
package/src/app.test.mjs
CHANGED
|
@@ -18,7 +18,7 @@ import fs from 'node:fs';
|
|
|
18
18
|
import os from 'node:os';
|
|
19
19
|
import path from 'node:path';
|
|
20
20
|
|
|
21
|
-
import {
|
|
21
|
+
import { vi, afterAll, beforeAll, expect, test } from 'vitest';
|
|
22
22
|
|
|
23
23
|
// The real app wiring with every handler stubbed, to check which routes carry which
|
|
24
24
|
// Cache-Control header.
|
|
@@ -32,36 +32,39 @@ function passThrough() {
|
|
|
32
32
|
return (c, next) => next();
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
35
|
+
vi.doMock('./routes/agent.js', () => ({ default: handler }));
|
|
36
|
+
vi.doMock('./routes/apiPage.js', () => ({ default: handler }));
|
|
37
|
+
vi.doMock('./routes/auth.js', () => ({ default: () => handler }));
|
|
38
|
+
vi.doMock('./routes/clientError.js', () => ({ default: handler }));
|
|
39
|
+
vi.doMock('./routes/cron.js', () => ({ default: handler }));
|
|
40
|
+
vi.doMock('./routes/cronForward.js', () => ({ default: handler }));
|
|
41
|
+
vi.doMock('./routes/detached.js', () => ({ default: handler }));
|
|
42
|
+
vi.doMock('./routes/endpoints.js', () => ({ default: throwingHandler }));
|
|
43
|
+
vi.doMock('./routes/mcp.js', () => ({ default: handler }));
|
|
44
|
+
vi.doMock('./routes/mobileRoot.js', () => ({ default: handler }));
|
|
45
|
+
vi.doMock('./routes/mountOauthDiscovery.js', () => ({ default: () => {} }));
|
|
46
|
+
vi.doMock('./routes/request.js', () => ({ default: handler }));
|
|
47
|
+
vi.doMock('./routes/usage.js', () => ({ default: handler }));
|
|
48
|
+
vi.doMock('./routes/user.js', () => ({ default: handler }));
|
|
49
|
+
vi.doMock('./routes/websocket.js', () => ({ default: handler }));
|
|
50
|
+
vi.doMock('./routes/wellKnownFallback.js', () => ({ default: handler }));
|
|
51
|
+
vi.doMock('./html/renderPage.js', () => ({
|
|
51
52
|
default: (c) => c.html('<html></html>'),
|
|
52
53
|
}));
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
vi.doMock('./middleware/apiContext.js', () => ({ default: passThrough }));
|
|
55
|
+
vi.doMock('./middleware/sentry.js', () => ({ default: passThrough }));
|
|
56
|
+
vi.doMock('./middleware/errorHandler.js', () => ({
|
|
56
57
|
default: () => (error, c) => c.json({ error: error.message }, 500),
|
|
57
58
|
}));
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
vi.doMock('../lib/build/appMeta.js', () => ({ default: { buildId: 'build-1' } }));
|
|
60
|
+
vi.doMock('../lib/build/auth.js', () => ({ default: { configured: false } }));
|
|
61
|
+
vi.doMock('../lib/build/theme.js', () => ({ default: {} }));
|
|
62
|
+
vi.doMock('../lib/build/config.js', () => ({ default: {} }));
|
|
63
|
+
vi.doMock('../lib/build/mobileConfig.js', () => ({ default: {} }));
|
|
64
|
+
vi.doMock('../lib/server/auth/getAuth.js', () => ({ default: vi.fn() }));
|
|
65
|
+
vi.doMock('../lib/server/auth/getStrategies.js', () => ({ default: vi.fn() }));
|
|
66
|
+
vi.doMock('../lib/server/log/createLogger.js', () => ({
|
|
67
|
+
default: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn() }),
|
|
65
68
|
}));
|
|
66
69
|
|
|
67
70
|
const { default: createApp } = await import('./app.js');
|
|
@@ -87,6 +90,7 @@ test.each([
|
|
|
87
90
|
['GET', '/'],
|
|
88
91
|
['GET', '/404'],
|
|
89
92
|
['GET', '/api/page/home'],
|
|
93
|
+
['GET', '/api/mobile/root'],
|
|
90
94
|
['GET', '/api/user'],
|
|
91
95
|
['POST', '/api/request/home/get_data'],
|
|
92
96
|
['POST', '/api/endpoints/my_endpoint'],
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { Hono } from 'hono';
|
|
18
|
-
import {
|
|
18
|
+
import { vi, afterEach, expect, test } from 'vitest';
|
|
19
19
|
|
|
20
20
|
const { default: getPageConfig } = await import('@lowdefy/api/routes/page/getPageConfig.js');
|
|
21
21
|
const { default: createContentSecurityPolicy } = await import(
|
|
@@ -25,13 +25,13 @@ const { default: addNonceToElements } = await import('@lowdefy/api/csp/addNonceT
|
|
|
25
25
|
|
|
26
26
|
const security = { contentSecurityPolicy: null };
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
vi.doMock('@lowdefy/api', () => ({
|
|
29
29
|
addNonceToElements,
|
|
30
30
|
createContentSecurityPolicy,
|
|
31
31
|
getPageConfig,
|
|
32
32
|
getRootConfig: async () => ({ home: { configured: true, pageId: 'home', pathParams: {} } }),
|
|
33
33
|
}));
|
|
34
|
-
|
|
34
|
+
vi.doMock('../../lib/build/app.js', () => ({
|
|
35
35
|
default: {
|
|
36
36
|
html: {
|
|
37
37
|
appendHead: '<script src="https://cdn.example.com/a.js"></script>',
|
|
@@ -39,20 +39,23 @@ jest.unstable_mockModule('../../lib/build/app.js', () => ({
|
|
|
39
39
|
},
|
|
40
40
|
},
|
|
41
41
|
}));
|
|
42
|
-
|
|
42
|
+
vi.doMock('../../lib/build/auth.js', () => ({
|
|
43
43
|
default: { authPages: { signIn: '/auth/login' } },
|
|
44
44
|
}));
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
vi.doMock('../../lib/build/config.js', () => ({ default: { basePath: '' } }));
|
|
46
|
+
vi.doMock('../../lib/build/security.js', () => ({ default: security }));
|
|
47
|
+
vi.doMock('../../lib/build/theme.js', () => ({ default: {} }));
|
|
48
|
+
vi.doMock('./getAssets.js', () => ({
|
|
49
49
|
default: () => ({ js: 'assets/main.js', css: [], imports: [] }),
|
|
50
50
|
}));
|
|
51
|
-
|
|
51
|
+
vi.doMock('./getPageAssets.js', () => ({
|
|
52
52
|
default: () => ({ js: [], css: [], prefetch: [] }),
|
|
53
53
|
}));
|
|
54
54
|
|
|
55
55
|
const { default: renderPage } = await import('./renderPage.js');
|
|
56
|
+
// The mobile sign-in route reaches the auth engine, which these tests never start.
|
|
57
|
+
vi.doMock('../routes/mobileSignIn.js', () => ({ default: () => null }));
|
|
58
|
+
vi.doMock('../routes/mobileSignInCallback.js', () => ({ default: () => null }));
|
|
56
59
|
const { default: mountPageRoutes } = await import('../routes/mountPageRoutes.js');
|
|
57
60
|
|
|
58
61
|
const policy = {
|
|
@@ -73,7 +76,7 @@ function createApp() {
|
|
|
73
76
|
c.set('lowdefyContext', {
|
|
74
77
|
authEnforcement: { pagesProtectedByDefault: false },
|
|
75
78
|
authorizeOutcome: () => 'allow',
|
|
76
|
-
logger: { info:
|
|
79
|
+
logger: { info: vi.fn() },
|
|
77
80
|
readConfigFile: async (file) => {
|
|
78
81
|
if (file === 'routes.json') return [{ pageId: 'home', path: 'home' }];
|
|
79
82
|
if (file === 'pages/home.json') return { id: 'home' };
|
|
@@ -15,33 +15,36 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { Hono } from 'hono';
|
|
18
|
-
import {
|
|
18
|
+
import { vi, beforeEach, expect, test } from 'vitest';
|
|
19
19
|
|
|
20
20
|
const { default: getPageConfig } = await import('@lowdefy/api/routes/page/getPageConfig.js');
|
|
21
21
|
|
|
22
|
-
const mockGetRootConfig =
|
|
23
|
-
|
|
24
|
-
createContentSecurityPolicy:
|
|
22
|
+
const mockGetRootConfig = vi.fn();
|
|
23
|
+
vi.doMock('@lowdefy/api', () => ({
|
|
24
|
+
createContentSecurityPolicy: vi.fn(),
|
|
25
25
|
getPageConfig,
|
|
26
26
|
getRootConfig: mockGetRootConfig,
|
|
27
27
|
}));
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
vi.doMock('../../lib/build/app.js', () => ({ default: {} }));
|
|
30
|
+
vi.doMock('../../lib/build/auth.js', () => ({
|
|
31
31
|
default: { authPages: { signIn: '/auth/login', twoFactorEnrol: '/two-factor-enrol' } },
|
|
32
32
|
}));
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
vi.doMock('../../lib/build/config.js', () => ({ default: { basePath: '' } }));
|
|
34
|
+
vi.doMock('../../lib/build/security.js', () => ({
|
|
35
35
|
default: { contentSecurityPolicy: null },
|
|
36
36
|
}));
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
vi.doMock('../../lib/build/theme.js', () => ({ default: {} }));
|
|
38
|
+
vi.doMock('./getAssets.js', () => ({ default: () => ({}) }));
|
|
39
|
+
vi.doMock('./getPageAssets.js', () => ({ default: () => ({}) }));
|
|
40
|
+
vi.doMock('./template.js', () => ({
|
|
41
41
|
default: ({ config, title }) => JSON.stringify({ config, title }),
|
|
42
42
|
}));
|
|
43
43
|
|
|
44
44
|
const { default: renderPage } = await import('./renderPage.js');
|
|
45
|
+
// The mobile sign-in route reaches the auth engine, which these tests never start.
|
|
46
|
+
vi.doMock('../routes/mobileSignIn.js', () => ({ default: () => null }));
|
|
47
|
+
vi.doMock('../routes/mobileSignInCallback.js', () => ({ default: () => null }));
|
|
45
48
|
const { default: mountPageRoutes } = await import('../routes/mountPageRoutes.js');
|
|
46
49
|
|
|
47
50
|
const routes = [
|
|
@@ -71,7 +74,7 @@ function createApp({
|
|
|
71
74
|
c.set('lowdefyContext', {
|
|
72
75
|
authEnforcement: { pagesProtectedByDefault },
|
|
73
76
|
authorizeOutcome: () => outcome,
|
|
74
|
-
logger: { info:
|
|
77
|
+
logger: { info: vi.fn() },
|
|
75
78
|
readConfigFile: async (file) => {
|
|
76
79
|
if (file === 'routes.json') return routeTable;
|
|
77
80
|
return pages[file.slice('pages/'.length, -'.json'.length)] ?? null;
|
package/src/initServer.test.mjs
CHANGED
|
@@ -14,38 +14,38 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import {
|
|
17
|
+
import { vi, expect, test } from 'vitest';
|
|
18
18
|
|
|
19
19
|
const calls = [];
|
|
20
|
-
const info =
|
|
20
|
+
const info = vi.fn();
|
|
21
21
|
const guards = { env: { BETTER_AUTH_URL: '^https://app\\.example\\.com$' } };
|
|
22
22
|
|
|
23
|
-
const checkEnvironmentGuards =
|
|
23
|
+
const checkEnvironmentGuards = vi.fn(() => {
|
|
24
24
|
calls.push('checkEnvironmentGuards');
|
|
25
25
|
});
|
|
26
|
-
const captureException =
|
|
27
|
-
const flush =
|
|
26
|
+
const captureException = vi.fn();
|
|
27
|
+
const flush = vi.fn(async () => true);
|
|
28
28
|
const sentry = { captureException, flush };
|
|
29
|
-
const initSentryServer =
|
|
29
|
+
const initSentryServer = vi.fn(async () => {
|
|
30
30
|
calls.push('initSentryServer');
|
|
31
31
|
return null;
|
|
32
32
|
});
|
|
33
|
-
const createApp =
|
|
33
|
+
const createApp = vi.fn();
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
vi.doMock('@lowdefy/node-utils/checkEnvironmentGuards.js', () => ({
|
|
36
36
|
default: checkEnvironmentGuards,
|
|
37
37
|
}));
|
|
38
|
-
|
|
38
|
+
vi.doMock('../lib/server/sentry/initSentry.js', () => ({
|
|
39
39
|
default: initSentryServer,
|
|
40
40
|
}));
|
|
41
|
-
|
|
41
|
+
vi.doMock('./app.js', () => {
|
|
42
42
|
calls.push('import app');
|
|
43
43
|
return { default: createApp };
|
|
44
44
|
});
|
|
45
|
-
|
|
45
|
+
vi.doMock('../lib/server/log/createLogger.js', () => ({
|
|
46
46
|
default: () => ({ info }),
|
|
47
47
|
}));
|
|
48
|
-
|
|
48
|
+
vi.doMock('../lib/build/config.js', () => ({
|
|
49
49
|
default: { environment: 'prod', environments: { prod: { guards } } },
|
|
50
50
|
}));
|
|
51
51
|
|