BlueGit

timfa filemanager

Files

filemanager / tests / api.v1.test.js

api.v1.test.js

api.v1.test.js 34.7 KiB Download Raw
1'use strict';
2
3// Public sync API + OAuth: auth flow, scopes, isolation between users,
4// uploads/downloads, delta feed and conflict detection.
5
6const fs = require('fs');
7const os = require('os');
8const path = require('path');
9const http = require('http');
10const crypto = require('crypto');
11const express = require('express');
12const bcrypt = require('bcrypt');
13const { once } = require('events');
14
15const mockBase = fs.mkdtempSync(path.join(os.tmpdir(), 'fm-api-'));
16const mockConfig = {
17 port: 3000,
18 paths: [],
19 userPath: null,
20 rootAliases: {},
21 api: { scanIntervalSeconds: 0.001, maxChunkBytes: 1024 * 1024, uploadDir: null },
22};
23
24jest.mock('../server/db', () =>
25{
26 const { Database } = require('node-sqlite3-wasm');
27 let db;
28 function getDb ()
29 {
30 if (!db)
31 {
32 db = new Database(':memory:');
33 db.exec(`
34 CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE NOT NULL, password_hash TEXT NOT NULL,
35 is_admin INTEGER NOT NULL DEFAULT 0, permissions TEXT NOT NULL DEFAULT '{}');
36 CREATE TABLE file_meta (file_path TEXT PRIMARY KEY, uploader TEXT NOT NULL DEFAULT '', downloaders TEXT NOT NULL DEFAULT '[]',
37 watched_by TEXT NOT NULL DEFAULT '{}', uploaded_at TEXT, sha256 TEXT);
38 CREATE TABLE user_last_navigation (username TEXT PRIMARY KEY, last_dir TEXT, updated_at INTEGER NOT NULL);
39 `);
40 }
41 return db;
42 }
43 return { getDb, initDb: () => {}, closeDb: () => {} };
44});
45
46jest.mock('../server/config', () =>
47{
48 const actual = jest.requireActual('../server/config');
49 return { ...actual, loadConfig: () => mockConfig, saveConfig: () => {} };
50});
51
52jest.mock('../server/auth', () =>
53{
54 const actual = jest.requireActual('../server/auth');
55 return {
56 ...actual,
57 // Web-session auth stand-in for the management routes.
58 requireAuth: (req, res, next) =>
59 {
60 const name = req.get('x-test-user');
61 const { getDb } = require('../server/db');
62 const row = name ? getDb().prepare('SELECT * FROM users WHERE username = ?').get([name]) : null;
63 if (!row) return res.status(401).json({ error: 'Session invalid or expired.' });
64 req.user = { id: row.id, username: row.username, isAdmin: row.is_admin === 1, permissions: JSON.parse(row.permissions) };
65 next();
66 },
67 };
68});
69
70jest.mock('../server/search-index', () => ({
71 searchFiles: async () => [],
72 deleteIndexPath: () => {},
73 deleteIndexUnder: () => {},
74 rewriteIndexPaths: () => {},
75 reindexDirectoryTree: async () => {},
76}));
77jest.mock('../server/file-hash-queue', () => ({ enqueueFileHash: () => {} }));
78jest.mock('../server/thumbnail-cache', () => ({
79 getCachedThumbnailInfo: async () => null,
80 getFilesWithThumbnailsInDir: async () => [],
81 getFolderIconSourceFile: async () => null,
82 setFolderIconAssignment: () => {},
83 getThumbnailManifestSourcePathSet: async () => new Set(),
84 needsPreviewImageConversion: () => false,
85 streamPreviewImage: async () => {},
86 MAX_PREVIEW_IMAGE_BYTES: 1,
87 THUMB_HTTP_MAX_AGE_SEC: 1,
88}));
89
90const { getDb } = require('../server/db');
91const store = require('../server/api/store');
92const oauthRoutes = require('../server/api/oauth');
93const apiV1Routes = require('../server/api/v1');
94const manageRoutes = require('../server/api/manage');
95
96const ROOTS = {
97 shared: path.join(mockBase, 'roots', 'shared'),
98 team: path.join(mockBase, 'roots', 'team'),
99 secret: path.join(mockBase, 'roots', 'secret'),
100};
101const USERS_DIR = path.join(mockBase, 'users');
102const REDIRECT = 'https://sync.example.com/callback';
103
104let server;
105let base;
106let client;
107
108function verifierPair ()
109{
110 const verifier = crypto.randomBytes(32).toString('base64url');
111 return { verifier, challenge: crypto.createHash('sha256').update(verifier).digest('base64url') };
112}
113
114async function req (method, url, { token, headers = {}, body, form, json, redirect = 'manual' } = {})
115{
116 const h = { ...headers };
117 if (token) h.Authorization = `Bearer ${token}`;
118 let payload = body;
119 if (form) { h['Content-Type'] = 'application/x-www-form-urlencoded'; payload = new URLSearchParams(form).toString(); }
120 if (json !== undefined) { h['Content-Type'] = 'application/json'; payload = JSON.stringify(json); }
121 const res = await fetch(base + url, { method, headers: h, body: payload, redirect });
122 const buf = Buffer.from(await res.arrayBuffer());
123 let data = null;
124 if ((res.headers.get('content-type') || '').includes('json') && buf.length) data = JSON.parse(buf.toString('utf8'));
125 return { status: res.status, headers: res.headers, data, buf, text: buf.toString('utf8') };
126}
127
128async function authorize (username, password, scope = 'files:read files:write', extra = {})
129{
130 const { verifier, challenge } = verifierPair();
131 const params = {
132 response_type: 'code', client_id: client.clientId, redirect_uri: REDIRECT, scope, state: 'st-1',
133 code_challenge: challenge, code_challenge_method: 'S256', device_name: 'Test phone', ...extra,
134 };
135 const r = await req('POST', '/oauth/authorize', { form: { ...params, username, password, decision: 'allow' } });
136 return { r, verifier, params };
137}
138
139async function login (username, scope)
140{
141 const { r, verifier } = await authorize(username, 'pw-' + username, scope);
142 expect(r.status).toBe(302);
143 const code = new URL(r.headers.get('location')).searchParams.get('code');
144 const t = await req('POST', '/oauth/token', { form: { grant_type: 'authorization_code', client_id: client.clientId, code, redirect_uri: REDIRECT, code_verifier: verifier } });
145 expect(t.status).toBe(200);
146 return t.data;
147}
148
149function vq (p) { return encodeURIComponent(p); }
150
151beforeAll(async () =>
152{
153 for (const r of Object.values(ROOTS)) fs.mkdirSync(r, { recursive: true });
154 fs.mkdirSync(USERS_DIR, { recursive: true });
155 fs.writeFileSync(path.join(ROOTS.secret, 'secret.txt'), 'top secret');
156 fs.writeFileSync(path.join(ROOTS.shared, 'readme.txt'), 'shared readme');
157 mockConfig.paths = Object.values(ROOTS);
158 mockConfig.userPath = USERS_DIR;
159 mockConfig.api.uploadDir = path.join(mockBase, 'staging');
160
161 const d = getDb();
162 store.initApiSchema(d);
163 const hash = (pw) => bcrypt.hashSync(pw, 4);
164 d.prepare('INSERT INTO users (username, password_hash, is_admin, permissions) VALUES (?, ?, ?, ?)')
165 .run(['alice', hash('pw-alice'), 0, JSON.stringify({ shared: { read: true, write: false }, team: { read: true, write: true } })]);
166 d.prepare('INSERT INTO users (username, password_hash, is_admin, permissions) VALUES (?, ?, ?, ?)')
167 .run(['bob', hash('pw-bob'), 0, JSON.stringify({ team: { read: true, write: false } })]);
168 d.prepare('INSERT INTO users (username, password_hash, is_admin, permissions) VALUES (?, ?, ?, ?)')
169 .run(['root', hash('pw-root'), 1, '{}']);
170
171 const app = express();
172 app.use(oauthRoutes);
173 app.use('/api/v1', apiV1Routes);
174 app.use(express.json());
175 app.use('/api/account/api-access', manageRoutes);
176 server = http.createServer(app).listen(0, '127.0.0.1');
177 await once(server, 'listening');
178 base = `http://127.0.0.1:${server.address().port}`;
179 client = store.createClient({ name: 'Sync App', redirectUris: [REDIRECT, 'http://127.0.0.1/cb'], createdBy: 'root' });
180});
181
182afterAll(async () =>
183{
184 server.close();
185 fs.rmSync(mockBase, { recursive: true, force: true });
186});
187
188beforeEach(() => oauthRoutes.resetLoginFailures());
189
190// ---------------------------------------------------------------------------
191
192describe('OAuth authorization code + PKCE', () =>
193{
194 test('metadata advertises PKCE S256 and public clients', async () =>
195 {
196 const r = await req('GET', '/.well-known/oauth-authorization-server');
197 expect(r.data.code_challenge_methods_supported).toEqual(['S256']);
198 expect(r.data.token_endpoint_auth_methods_supported).toEqual(['none']);
199 });
200
201 test('consent page shows client, device and scopes and cannot be framed', async () =>
202 {
203 const { challenge } = verifierPair();
204 const q = new URLSearchParams({ response_type: 'code', client_id: client.clientId, redirect_uri: REDIRECT, scope: 'files:read', code_challenge: challenge, code_challenge_method: 'S256', device_name: 'Pixel <8>' });
205 const r = await req('GET', `/oauth/authorize?${q}`);
206 expect(r.status).toBe(200);
207 expect(r.text).toContain('Sync App');
208 expect(r.text).toContain('Pixel &lt;8&gt;');
209 expect(r.text).toContain('files:read');
210 expect(r.headers.get('x-frame-options')).toBe('DENY');
211 });
212
213 test('unknown client or unregistered redirect never redirects', async () =>
214 {
215 const { challenge } = verifierPair();
216 let r = await req('GET', `/oauth/authorize?response_type=code&client_id=nope&redirect_uri=${vq(REDIRECT)}&code_challenge=${challenge}&code_challenge_method=S256`);
217 expect(r.status).toBe(400);
218 r = await req('GET', `/oauth/authorize?response_type=code&client_id=${client.clientId}&redirect_uri=${vq('https://evil.example/cb')}&code_challenge=${challenge}&code_challenge_method=S256`);
219 expect(r.status).toBe(400);
220 });
221
222 test('PKCE is mandatory and only S256', async () =>
223 {
224 let r = await req('GET', `/oauth/authorize?response_type=code&client_id=${client.clientId}&redirect_uri=${vq(REDIRECT)}&state=x`);
225 expect(r.status).toBe(302);
226 expect(new URL(r.headers.get('location')).searchParams.get('error')).toBe('invalid_request');
227 r = await req('GET', `/oauth/authorize?response_type=code&client_id=${client.clientId}&redirect_uri=${vq(REDIRECT)}&code_challenge=${'a'.repeat(43)}&code_challenge_method=plain`);
228 expect(new URL(r.headers.get('location')).searchParams.get('error')).toBe('invalid_request');
229 });
230
231 test('unknown scopes are rejected', async () =>
232 {
233 const { r } = await authorize('alice', 'pw-alice', 'files:read admin');
234 expect(new URL(r.headers.get('location')).searchParams.get('error')).toBe('invalid_scope');
235 });
236
237 test('wrong password re-renders the consent page; deny returns access_denied', async () =>
238 {
239 const { r } = await authorize('alice', 'wrong');
240 expect(r.status).toBe(401);
241 expect(r.text).toContain('Invalid username or password');
242
243 const { params } = await authorize('alice', 'x');
244 const d = await req('POST', '/oauth/authorize', { form: { ...params, decision: 'deny' } });
245 const loc = new URL(d.headers.get('location'));
246 expect(loc.searchParams.get('error')).toBe('access_denied');
247 expect(loc.searchParams.get('state')).toBe('st-1');
248 });
249
250 test('repeated failures are rate limited', async () =>
251 {
252 for (let i = 0; i < 10; i++) await authorize('alice', 'wrong');
253 const { r } = await authorize('alice', 'pw-alice');
254 expect(r.status).toBe(401);
255 expect(r.text).toContain('Too many failed attempts');
256 });
257
258 test('code exchange needs the right verifier and works only once', async () =>
259 {
260 const { r, verifier } = await authorize('alice', 'pw-alice');
261 const loc = new URL(r.headers.get('location'));
262 expect(loc.searchParams.get('state')).toBe('st-1');
263 expect(loc.searchParams.get('iss')).toBe(base);
264 const code = loc.searchParams.get('code');
265
266 const bad = await req('POST', '/oauth/token', { form: { grant_type: 'authorization_code', client_id: client.clientId, code, redirect_uri: REDIRECT, code_verifier: 'x'.repeat(43) } });
267 expect(bad.status).toBe(400);
268 // The failed attempt burned the code.
269 const again = await req('POST', '/oauth/token', { form: { grant_type: 'authorization_code', client_id: client.clientId, code, redirect_uri: REDIRECT, code_verifier: verifier } });
270 expect(again.data.error).toBe('invalid_grant');
271 });
272
273 test('loopback redirect accepts any port (RFC 8252)', async () =>
274 {
275 const { r } = await authorize('alice', 'pw-alice', 'files:read', { redirect_uri: 'http://127.0.0.1:53111/cb' });
276 expect(r.status).toBe(302);
277 expect(r.headers.get('location')).toMatch(/^http:\/\/127\.0\.0\.1:53111\/cb\?code=/);
278 });
279
280 test('tokens are stored hashed only', async () =>
281 {
282 const t = await login('alice');
283 const d = getDb();
284 const dump = JSON.stringify([
285 d.prepare('SELECT * FROM api_grants').all(),
286 d.prepare('SELECT * FROM api_access_tokens').all(),
287 ]);
288 expect(dump).not.toContain(t.access_token);
289 expect(dump).not.toContain(t.refresh_token);
290 expect(dump).toContain(store.sha256Hex(t.access_token));
291 });
292
293 test('refresh rotates tokens and reuse of an old refresh token revokes the grant', async () =>
294 {
295 const t1 = await login('alice');
296 const r2 = await req('POST', '/oauth/token', { form: { grant_type: 'refresh_token', client_id: client.clientId, refresh_token: t1.refresh_token } });
297 expect(r2.status).toBe(200);
298 expect(r2.data.refresh_token).not.toBe(t1.refresh_token);
299 expect((await req('GET', '/api/v1/me', { token: r2.data.access_token })).status).toBe(200);
300
301 const reuse = await req('POST', '/oauth/token', { form: { grant_type: 'refresh_token', client_id: client.clientId, refresh_token: t1.refresh_token } });
302 expect(reuse.status).toBe(400);
303 expect((await req('GET', '/api/v1/me', { token: r2.data.access_token })).status).toBe(401);
304 const r3 = await req('POST', '/oauth/token', { form: { grant_type: 'refresh_token', client_id: client.clientId, refresh_token: r2.data.refresh_token } });
305 expect(r3.status).toBe(400);
306 });
307
308 test('refresh token is bound to its client', async () =>
309 {
310 const other = store.createClient({ name: 'Other', redirectUris: [REDIRECT], createdBy: 'root' });
311 const t = await login('alice');
312 const r = await req('POST', '/oauth/token', { form: { grant_type: 'refresh_token', client_id: other.clientId, refresh_token: t.refresh_token } });
313 expect(r.status).toBe(400);
314 });
315
316 test('revocation endpoint kills the grant', async () =>
317 {
318 const t = await login('alice');
319 expect((await req('POST', '/oauth/revoke', { form: { token: t.refresh_token } })).status).toBe(200);
320 expect((await req('GET', '/api/v1/me', { token: t.access_token })).status).toBe(401);
321 });
322
323 test('disabling a client invalidates its tokens', async () =>
324 {
325 const c = store.createClient({ name: 'Temp', redirectUris: [REDIRECT], createdBy: 'root' });
326 const saved = client;
327 client = c;
328 const t = await login('alice');
329 client = saved;
330 expect((await req('GET', '/api/v1/me', { token: t.access_token })).status).toBe(200);
331 store.deleteClient(c.clientId);
332 expect((await req('GET', '/api/v1/me', { token: t.access_token })).status).toBe(401);
333 });
334});
335
336describe('Access management (web session)', () =>
337{
338 test('user sees own grants only and can revoke them', async () =>
339 {
340 const a = await login('alice');
341 await login('bob');
342 const list = await req('GET', '/api/account/api-access/grants', { headers: { 'x-test-user': 'alice' } });
343 expect(list.data.grants.length).toBeGreaterThan(0);
344 const mine = list.data.grants.find((g) => g.id === a.grant_id);
345 expect(mine.name).toBe('Test phone');
346 expect(mine.clientName).toBe('Sync App');
347
348 const bobList = await req('GET', '/api/account/api-access/grants', { headers: { 'x-test-user': 'bob' } });
349 expect(bobList.data.grants.some((g) => g.id === a.grant_id)).toBe(false);
350 expect((await req('DELETE', `/api/account/api-access/grants/${a.grant_id}`, { headers: { 'x-test-user': 'bob' } })).status).toBe(404);
351
352 expect((await req('DELETE', `/api/account/api-access/grants/${a.grant_id}`, { headers: { 'x-test-user': 'alice' } })).status).toBe(200);
353 expect((await req('GET', '/api/v1/me', { token: a.access_token })).status).toBe(401);
354 });
355
356 test('personal access tokens work and can be revoked', async () =>
357 {
358 const r = await req('POST', '/api/account/api-access/tokens', { headers: { 'x-test-user': 'alice' }, json: { name: 'script', scopes: 'files:read' } });
359 expect(r.status).toBe(201);
360 expect(r.data.token).toMatch(/^fmp_/);
361 const me = await req('GET', '/api/v1/me', { token: r.data.token });
362 expect(me.data.username).toBe('alice');
363 expect(me.data.scopes).toEqual(['files:read']);
364 await req('DELETE', `/api/account/api-access/grants/${r.data.id}`, { headers: { 'x-test-user': 'alice' } });
365 expect((await req('GET', '/api/v1/me', { token: r.data.token })).status).toBe(401);
366 });
367
368 test('only admins manage clients and redirect URIs are validated', async () =>
369 {
370 expect((await req('POST', '/api/account/api-access/clients', { headers: { 'x-test-user': 'alice' }, json: { name: 'x', redirectUris: [REDIRECT] } })).status).toBe(403);
371 expect((await req('POST', '/api/account/api-access/clients', { headers: { 'x-test-user': 'root' }, json: { name: 'x', redirectUris: ['http://evil.example/cb'] } })).status).toBe(400);
372 const ok = await req('POST', '/api/account/api-access/clients', { headers: { 'x-test-user': 'root' }, json: { name: 'Desktop', redirectUris: ['com.example.sync:/cb', 'http://127.0.0.1/cb'] } });
373 expect(ok.status).toBe(201);
374 expect(ok.data.client.clientId).toMatch(/^fmc_/);
375 });
376});
377
378describe('Scopes and isolation', () =>
379{
380 let alice;
381 let aliceRead;
382 let bob;
383
384 beforeAll(async () =>
385 {
386 alice = await login('alice');
387 aliceRead = await login('alice', 'files:read');
388 bob = await login('bob');
389 });
390
391 test('missing or bogus token is rejected', async () =>
392 {
393 expect((await req('GET', '/api/v1/roots')).status).toBe(401);
394 expect((await req('GET', '/api/v1/roots', { token: 'fma_nope' })).status).toBe(401);
395 });
396
397 test('roots reflect the user permissions', async () =>
398 {
399 const a = await req('GET', '/api/v1/roots', { token: alice.access_token });
400 expect(a.data.roots.map((r) => r.id)).toEqual(['~', 'shared', 'team']);
401 expect(a.data.roots.find((r) => r.id === 'shared').writable).toBe(false);
402 const b = await req('GET', '/api/v1/roots', { token: bob.access_token });
403 expect(b.data.roots.map((r) => r.id)).toEqual(['~', 'team']);
404 });
405
406 test('read-only token cannot write', async () =>
407 {
408 const r = await req('PUT', `/api/v1/files/content?path=${vq('/~/x.txt')}`, { token: aliceRead.access_token, body: 'x' });
409 expect(r.status).toBe(403);
410 expect(r.data.error).toBe('insufficient_scope');
411 });
412
413 test('cannot read roots without permission, other users folders, or escape via .. or symlinks', async () =>
414 {
415 expect((await req('GET', `/api/v1/files/content?path=${vq('/secret/secret.txt')}`, { token: alice.access_token })).status).toBe(404);
416 fs.mkdirSync(path.join(USERS_DIR, 'bob'), { recursive: true });
417 fs.writeFileSync(path.join(USERS_DIR, 'bob', 'private.txt'), 'bob only');
418 const viaHome = await req('GET', `/api/v1/files/content?path=${vq('/~/private.txt')}`, { token: alice.access_token });
419 expect(viaHome.status).toBe(404);
420 expect((await req('GET', `/api/v1/files/content?path=${vq('/~/../bob/private.txt')}`, { token: alice.access_token })).status).toBe(400);
421 expect((await req('GET', `/api/v1/files/content?path=${vq('/~/private.txt')}`, { token: bob.access_token })).text).toBe('bob only');
422
423 fs.mkdirSync(path.join(USERS_DIR, 'alice'), { recursive: true });
424 fs.symlinkSync(ROOTS.secret, path.join(USERS_DIR, 'alice', 'link'));
425 expect((await req('GET', `/api/v1/files/content?path=${vq('/~/link/secret.txt')}`, { token: alice.access_token })).status).toBe(404);
426 const list = await req('GET', `/api/v1/files/list?path=${vq('/~')}`, { token: alice.access_token });
427 expect(list.data.entries.some((e) => e.name === 'link')).toBe(false);
428 fs.unlinkSync(path.join(USERS_DIR, 'alice', 'link'));
429 });
430
431 test('read-only roots cannot be written even with files:write', async () =>
432 {
433 const r = await req('PUT', `/api/v1/files/content?path=${vq('/shared/new.txt')}`, { token: alice.access_token, body: 'x' });
434 expect(r.status).toBe(403);
435 expect(fs.existsSync(path.join(ROOTS.shared, 'new.txt'))).toBe(false);
436 const b = await req('POST', '/api/v1/folders', { token: bob.access_token, json: { path: '/team/x' } });
437 expect(b.status).toBe(403);
438 });
439
440 test('admin users get admin-level access, nothing more', async () =>
441 {
442 const root = await login('root');
443 const r = await req('GET', '/api/v1/roots', { token: root.access_token });
444 expect(r.data.roots.map((x) => x.id)).toEqual(['~', 'shared', 'team', 'secret']);
445 });
446});
447
448describe('Files: upload, download, conflicts', () =>
449{
450 let t;
451 beforeAll(async () => { t = await login('alice'); });
452
453 test('simple upload, conditional overwrite and conflicts', async () =>
454 {
455 const mtime = Date.parse('2024-01-02T03:04:05.000Z');
456 const up = await req('PUT', `/api/v1/files/content?path=${vq('/team/a.txt')}&mtime=${mtime}`, { token: t.access_token, body: 'hello' });
457 expect(up.status).toBe(201);
458 expect(up.data).toMatchObject({ path: '/team/a.txt', type: 'file', size: 5, mtime_ms: mtime });
459 expect(up.data.sha256).toBe(crypto.createHash('sha256').update('hello').digest('hex'));
460 expect(fs.readFileSync(path.join(ROOTS.team, 'a.txt'), 'utf8')).toBe('hello');
461
462 const conflict = await req('PUT', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token, body: 'x' });
463 expect(conflict.status).toBe(409);
464 expect(conflict.data.current.version).toBe(up.data.version);
465
466 const noneMatch = await req('PUT', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token, body: 'x', headers: { 'If-None-Match': '*' } });
467 expect(noneMatch.status).toBe(412);
468
469 const stale = await req('PUT', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token, body: 'x', headers: { 'If-Match': '"stale"' } });
470 expect(stale.status).toBe(412);
471
472 const ok = await req('PUT', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token, body: 'hello world', headers: { 'If-Match': `"${up.data.version}"` } });
473 expect(ok.status).toBe(200);
474 expect(ok.data.version).not.toBe(up.data.version);
475 expect(ok.headers.get('etag')).toBe(`"${ok.data.version}"`);
476
477 const forced = await req('PUT', `/api/v1/files/content?path=${vq('/team/a.txt')}&overwrite=true`, { token: t.access_token, body: 'hello world!' });
478 expect(forced.status).toBe(200);
479 });
480
481 test('upload into a missing folder fails cleanly and leaves no temp files', async () =>
482 {
483 const r = await req('PUT', `/api/v1/files/content?path=${vq('/team/nope/a.txt')}`, { token: t.access_token, body: 'x' });
484 expect(r.status).toBe(409);
485 expect(r.data.error).toBe('parent_missing');
486 expect(fs.readdirSync(ROOTS.team).some((n) => n.startsWith('.fmapi-'))).toBe(false);
487 });
488
489 test('checksum mismatch is refused', async () =>
490 {
491 const r = await req('PUT', `/api/v1/files/content?path=${vq('/team/sum.txt')}`, { token: t.access_token, body: 'abc', headers: { 'X-Content-SHA256': '0'.repeat(64) } });
492 expect(r.status).toBe(422);
493 expect(fs.existsSync(path.join(ROOTS.team, 'sum.txt'))).toBe(false);
494 });
495
496 test('download supports ETag, 304 and Range', async () =>
497 {
498 const meta = await req('GET', `/api/v1/files/metadata?path=${vq('/team/a.txt')}`, { token: t.access_token });
499 const full = await req('GET', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token });
500 expect(full.status).toBe(200);
501 expect(full.text).toBe('hello world!');
502 expect(full.headers.get('etag')).toBe(`"${meta.data.version}"`);
503
504 const cached = await req('GET', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token, headers: { 'If-None-Match': `"${meta.data.version}"` } });
505 expect(cached.status).toBe(304);
506
507 const part = await req('GET', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token, headers: { Range: 'bytes=6-10' } });
508 expect(part.status).toBe(206);
509 expect(part.text).toBe('world');
510 expect(part.headers.get('content-range')).toBe('bytes 6-10/12');
511 });
512
513 test('metadata can compute a hash on demand for files changed outside the API', async () =>
514 {
515 fs.writeFileSync(path.join(ROOTS.team, 'outside.bin'), 'outside');
516 const m1 = await req('GET', `/api/v1/files/metadata?path=${vq('/team/outside.bin')}`, { token: t.access_token });
517 expect(m1.data.sha256).toBeNull();
518 const m2 = await req('GET', `/api/v1/files/metadata?path=${vq('/team/outside.bin')}&hash=1`, { token: t.access_token });
519 expect(m2.data.sha256).toBe(crypto.createHash('sha256').update('outside').digest('hex'));
520 });
521
522 test('folders, listing, move and delete', async () =>
523 {
524 expect((await req('POST', '/api/v1/folders', { token: t.access_token, json: { path: '/team/docs/sub', parents: true } })).status).toBe(201);
525 expect((await req('POST', '/api/v1/folders', { token: t.access_token, json: { path: '/team/docs' } })).status).toBe(409);
526 expect((await req('POST', '/api/v1/folders', { token: t.access_token, json: { path: '/team/docs', exist_ok: true } })).status).toBe(200);
527 await req('PUT', `/api/v1/files/content?path=${vq('/team/docs/b.txt')}`, { token: t.access_token, body: 'b' });
528
529 const list = await req('GET', `/api/v1/files/list?path=${vq('/team/docs')}&limit=1`, { token: t.access_token });
530 expect(list.data.entries).toHaveLength(1);
531 expect(list.data.next_page_token).toBe('1');
532
533 const mv = await req('POST', '/api/v1/files/move', { token: t.access_token, json: { from: '/team/docs/b.txt', to: '/team/docs/sub/c.txt' } });
534 expect(mv.status).toBe(200);
535 expect(mv.data.path).toBe('/team/docs/sub/c.txt');
536
537 await req('PUT', `/api/v1/files/content?path=${vq('/team/docs/d.txt')}`, { token: t.access_token, body: 'd' });
538 const clash = await req('POST', '/api/v1/files/move', { token: t.access_token, json: { from: '/team/docs/d.txt', to: '/team/docs/sub/c.txt' } });
539 expect(clash.status).toBe(409);
540 const into = await req('POST', '/api/v1/files/move', { token: t.access_token, json: { from: '/team/docs', to: '/team/docs/sub/x' } });
541 expect(into.status).toBe(400);
542 const toReadOnly = await req('POST', '/api/v1/files/move', { token: t.access_token, json: { from: '/team/docs/d.txt', to: '/shared/d.txt' } });
543 expect(toReadOnly.status).toBe(403);
544
545 expect((await req('DELETE', `/api/v1/files?path=${vq('/team/docs')}`, { token: t.access_token })).status).toBe(409);
546 expect((await req('DELETE', `/api/v1/files?path=${vq('/team/docs/d.txt')}`, { token: t.access_token, headers: { 'If-Match': '"old"' } })).status).toBe(412);
547 expect((await req('DELETE', `/api/v1/files?path=${vq('/team/docs')}&recursive=true`, { token: t.access_token })).status).toBe(204);
548 expect(fs.existsSync(path.join(ROOTS.team, 'docs'))).toBe(false);
549 expect((await req('DELETE', `/api/v1/files?path=${vq('/team')}`, { token: t.access_token })).status).toBe(400);
550 });
551
552 test('errors are JSON, also for unknown endpoints', async () =>
553 {
554 const r = await req('GET', '/api/v1/nope', { token: t.access_token });
555 expect(r.status).toBe(404);
556 expect(r.data.error).toBe('not_found');
557 const bad = await req('GET', '/api/v1/files/metadata?path=relative', { token: t.access_token });
558 expect(bad.data.error).toBe('invalid_path');
559 });
560});
561
562describe('Resumable uploads', () =>
563{
564 let t;
565 let other;
566 beforeAll(async () => { t = await login('alice'); other = await login('alice'); });
567
568 test('chunked upload with offsets, resume info and checksum', async () =>
569 {
570 const content = crypto.randomBytes(2.5 * 1024 * 1024);
571 const sha = crypto.createHash('sha256').update(content).digest('hex');
572 const c = await req('POST', '/api/v1/uploads', { token: t.access_token, json: { path: '/team/big.bin', size: content.length, sha256: sha, mtime: 1700000000000 } });
573 expect(c.status).toBe(201);
574 const id = c.data.id;
575 expect(c.data.max_chunk_bytes).toBe(1024 * 1024);
576
577 // Another grant (even of the same user) cannot touch this session.
578 expect((await req('GET', `/api/v1/uploads/${id}`, { token: other.access_token })).status).toBe(404);
579
580 const tooBig = await req('PATCH', `/api/v1/uploads/${id}`, { token: t.access_token, headers: { 'Upload-Offset': '0' }, body: content.subarray(0, 1024 * 1024 + 1) });
581 expect(tooBig.status).toBe(413);
582 expect((await req('GET', `/api/v1/uploads/${id}`, { token: t.access_token })).data.offset).toBe(0);
583
584 let offset = 0;
585 while (offset < content.length)
586 {
587 const chunk = content.subarray(offset, offset + 1024 * 1024);
588 const p = await req('PATCH', `/api/v1/uploads/${id}`, { token: t.access_token, headers: { 'Upload-Offset': String(offset) }, body: chunk });
589 expect(p.status).toBe(200);
590 offset = p.data.offset;
591 if (offset === 1024 * 1024)
592 {
593 const wrong = await req('PATCH', `/api/v1/uploads/${id}`, { token: t.access_token, headers: { 'Upload-Offset': '0' }, body: 'x' });
594 expect(wrong.status).toBe(409);
595 expect(wrong.data.offset).toBe(1024 * 1024);
596 const early = await req('POST', `/api/v1/uploads/${id}/complete`, { token: t.access_token });
597 expect(early.status).toBe(409);
598 }
599 }
600 const done = await req('POST', `/api/v1/uploads/${id}/complete`, { token: t.access_token });
601 expect(done.status).toBe(201);
602 expect(done.data).toMatchObject({ path: '/team/big.bin', size: content.length, sha256: sha, mtime_ms: 1700000000000 });
603 expect(fs.readFileSync(path.join(ROOTS.team, 'big.bin')).equals(content)).toBe(true);
604 expect(fs.readdirSync(mockConfig.api.uploadDir)).toEqual([]);
605 });
606
607 test('checksum mismatch discards the upload', async () =>
608 {
609 const c = await req('POST', '/api/v1/uploads', { token: t.access_token, json: { path: '/team/bad.bin', size: 3, sha256: '0'.repeat(64) } });
610 await req('PATCH', `/api/v1/uploads/${c.data.id}`, { token: t.access_token, headers: { 'Upload-Offset': '0' }, body: 'abc' });
611 const r = await req('POST', `/api/v1/uploads/${c.data.id}/complete`, { token: t.access_token });
612 expect(r.status).toBe(422);
613 expect(fs.existsSync(path.join(ROOTS.team, 'bad.bin'))).toBe(false);
614 });
615
616 test('preconditions are checked at start and at completion', async () =>
617 {
618 const exists = await req('POST', '/api/v1/uploads', { token: t.access_token, json: { path: '/team/big.bin', size: 1 } });
619 expect(exists.status).toBe(409);
620 const c = await req('POST', '/api/v1/uploads', { token: t.access_token, json: { path: '/team/race.txt', size: 1, if_none_match: '*' } });
621 await req('PATCH', `/api/v1/uploads/${c.data.id}`, { token: t.access_token, headers: { 'Upload-Offset': '0' }, body: 'z' });
622 fs.writeFileSync(path.join(ROOTS.team, 'race.txt'), 'someone else');
623 const r = await req('POST', `/api/v1/uploads/${c.data.id}/complete`, { token: t.access_token });
624 expect(r.status).toBe(412);
625 expect(fs.readFileSync(path.join(ROOTS.team, 'race.txt'), 'utf8')).toBe('someone else');
626 });
627
628 test('cancel removes the staged data', async () =>
629 {
630 const c = await req('POST', '/api/v1/uploads', { token: t.access_token, json: { path: '/team/cancel.bin', size: 10 } });
631 expect((await req('DELETE', `/api/v1/uploads/${c.data.id}`, { token: t.access_token })).status).toBe(204);
632 expect((await req('GET', `/api/v1/uploads/${c.data.id}`, { token: t.access_token })).status).toBe(404);
633 });
634});
635
636describe('Delta feed', () =>
637{
638 let t;
639 let bob;
640 beforeAll(async () => { t = await login('alice'); bob = await login('bob'); });
641
642 async function drain (cursor)
643 {
644 const all = [];
645 let c = cursor;
646 for (;;)
647 {
648 const r = await req('GET', `/api/v1/delta?root=${vq('~')}&cursor=${c}&limit=2`, { token: t.access_token });
649 expect(r.status).toBe(200);
650 all.push(...r.data.entries);
651 c = r.data.cursor;
652 if (!r.data.has_more) return { entries: all, cursor: c };
653 }
654 }
655
656 test('snapshot, then created/modified/moved/deleted incl. changes made outside the API', async () =>
657 {
658 const home = path.join(USERS_DIR, 'alice');
659 fs.mkdirSync(path.join(home, 'photos'), { recursive: true });
660 fs.writeFileSync(path.join(home, 'photos', '1.jpg'), 'one');
661 fs.writeFileSync(path.join(home, 'notes.txt'), 'n');
662
663 // Initial snapshot, paged.
664 let r = await req('GET', `/api/v1/delta?root=${vq('~')}&limit=2`, { token: t.access_token });
665 expect(r.data.reset).toBe(true);
666 const snap = [...r.data.entries];
667 let cursor = r.data.cursor;
668 while (r.data.has_more)
669 {
670 r = await req('GET', `/api/v1/delta?root=${vq('~')}&cursor=${cursor}&limit=2`, { token: t.access_token });
671 snap.push(...r.data.entries);
672 cursor = r.data.cursor;
673 }
674 expect(snap.map((e) => e.path).sort()).toEqual(expect.arrayContaining(['/~/notes.txt', '/~/photos', '/~/photos/1.jpg']));
675 expect(snap.every((e) => e.change === 'exists')).toBe(true);
676
677 // No changes.
678 let d = await drain(cursor);
679 expect(d.entries).toEqual([]);
680 cursor = d.cursor;
681
682 // Changes through the API.
683 await req('PUT', `/api/v1/files/content?path=${vq('/~/new.txt')}`, { token: t.access_token, body: 'new' });
684 await req('POST', '/api/v1/files/move', { token: t.access_token, json: { from: '/~/photos', to: '/~/pictures' } });
685 d = await drain(cursor);
686 cursor = d.cursor;
687 expect(d.entries).toEqual(expect.arrayContaining([
688 expect.objectContaining({ change: 'created', path: '/~/new.txt', type: 'file', size: 3 }),
689 expect.objectContaining({ change: 'moved', path: '/~/pictures', old_path: '/~/photos', type: 'folder' }),
690 ]));
691 // Children of a moved folder are implied, not repeated.
692 expect(d.entries.some((e) => e.path === '/~/pictures/1.jpg')).toBe(false);
693
694 // Changes made directly on disk / via the web UI.
695 await new Promise((res) => setTimeout(res, 20));
696 fs.writeFileSync(path.join(home, 'notes.txt'), 'changed notes');
697 fs.rmSync(path.join(home, 'pictures'), { recursive: true });
698 d = await drain(cursor);
699 cursor = d.cursor;
700 expect(d.entries).toEqual(expect.arrayContaining([
701 expect.objectContaining({ change: 'modified', path: '/~/notes.txt', size: 13 }),
702 expect.objectContaining({ change: 'deleted', path: '/~/pictures', type: 'folder' }),
703 ]));
704 expect(d.entries.some((e) => e.path === '/~/pictures/1.jpg')).toBe(false);
705 const del = d.entries.find((e) => e.change === 'deleted');
706 expect(del.version).toBeNull();
707 });
708
709 test('cursor is bound to its root and unknown roots are hidden', async () =>
710 {
711 const r = await req('GET', `/api/v1/delta?root=${vq('~')}`, { token: t.access_token });
712 const other = await req('GET', `/api/v1/delta?root=team&cursor=${r.data.cursor}`, { token: t.access_token });
713 expect(other.status).toBe(400);
714 expect((await req('GET', '/api/v1/delta?root=secret', { token: t.access_token })).status).toBe(404);
715 expect((await req('GET', '/api/v1/delta?root=x&cursor=%%%', { token: t.access_token })).status).toBe(400);
716 });
717
718 test('another user sees their own home, not alice\'s', async () =>
719 {
720 const r = await req('GET', `/api/v1/delta?root=${vq('~')}&limit=1000`, { token: bob.access_token });
721 expect(r.data.entries.map((e) => e.path)).toEqual(['/~/private.txt']);
722 });
723
724 test('pruned history answers 410 so clients start over', async () =>
725 {
726 const r = await req('GET', '/api/v1/delta?root=team&limit=1000', { token: t.access_token });
727 expect(r.data.has_more).toBe(false);
728 getDb().prepare('UPDATE api_scan_state SET min_seq = min_seq + 100000 WHERE root_key = ?').run([ROOTS.team]);
729 const gone = await req('GET', `/api/v1/delta?root=team&cursor=${r.data.cursor}`, { token: t.access_token });
730 expect(gone.status).toBe(410);
731 expect(gone.data.error).toBe('cursor_expired');
732 });
733});