'use strict'; // Public sync API + OAuth: auth flow, scopes, isolation between users, // uploads/downloads, delta feed and conflict detection. const fs = require('fs'); const os = require('os'); const path = require('path'); const http = require('http'); const crypto = require('crypto'); const express = require('express'); const bcrypt = require('bcrypt'); const { once } = require('events'); const mockBase = fs.mkdtempSync(path.join(os.tmpdir(), 'fm-api-')); const mockConfig = { port: 3000, paths: [], userPath: null, rootAliases: {}, api: { scanIntervalSeconds: 0.001, maxChunkBytes: 1024 * 1024, uploadDir: null }, }; jest.mock('../server/db', () => { const { Database } = require('node-sqlite3-wasm'); let db; function getDb () { if (!db) { db = new Database(':memory:'); db.exec(` CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE NOT NULL, password_hash TEXT NOT NULL, is_admin INTEGER NOT NULL DEFAULT 0, permissions TEXT NOT NULL DEFAULT '{}'); CREATE TABLE file_meta (file_path TEXT PRIMARY KEY, uploader TEXT NOT NULL DEFAULT '', downloaders TEXT NOT NULL DEFAULT '[]', watched_by TEXT NOT NULL DEFAULT '{}', uploaded_at TEXT, sha256 TEXT); CREATE TABLE user_last_navigation (username TEXT PRIMARY KEY, last_dir TEXT, updated_at INTEGER NOT NULL); `); } return db; } return { getDb, initDb: () => {}, closeDb: () => {} }; }); jest.mock('../server/config', () => { const actual = jest.requireActual('../server/config'); return { ...actual, loadConfig: () => mockConfig, saveConfig: () => {} }; }); jest.mock('../server/auth', () => { const actual = jest.requireActual('../server/auth'); return { ...actual, // Web-session auth stand-in for the management routes. requireAuth: (req, res, next) => { const name = req.get('x-test-user'); const { getDb } = require('../server/db'); const row = name ? getDb().prepare('SELECT * FROM users WHERE username = ?').get([name]) : null; if (!row) return res.status(401).json({ error: 'Session invalid or expired.' }); req.user = { id: row.id, username: row.username, isAdmin: row.is_admin === 1, permissions: JSON.parse(row.permissions) }; next(); }, }; }); jest.mock('../server/search-index', () => ({ searchFiles: async () => [], deleteIndexPath: () => {}, deleteIndexUnder: () => {}, rewriteIndexPaths: () => {}, reindexDirectoryTree: async () => {}, })); jest.mock('../server/file-hash-queue', () => ({ enqueueFileHash: () => {} })); jest.mock('../server/thumbnail-cache', () => ({ getCachedThumbnailInfo: async () => null, getFilesWithThumbnailsInDir: async () => [], getFolderIconSourceFile: async () => null, setFolderIconAssignment: () => {}, getThumbnailManifestSourcePathSet: async () => new Set(), needsPreviewImageConversion: () => false, streamPreviewImage: async () => {}, MAX_PREVIEW_IMAGE_BYTES: 1, THUMB_HTTP_MAX_AGE_SEC: 1, })); const { getDb } = require('../server/db'); const store = require('../server/api/store'); const oauthRoutes = require('../server/api/oauth'); const apiV1Routes = require('../server/api/v1'); const manageRoutes = require('../server/api/manage'); const ROOTS = { shared: path.join(mockBase, 'roots', 'shared'), team: path.join(mockBase, 'roots', 'team'), secret: path.join(mockBase, 'roots', 'secret'), }; const USERS_DIR = path.join(mockBase, 'users'); const REDIRECT = 'https://sync.example.com/callback'; let server; let base; let client; function verifierPair () { const verifier = crypto.randomBytes(32).toString('base64url'); return { verifier, challenge: crypto.createHash('sha256').update(verifier).digest('base64url') }; } async function req (method, url, { token, headers = {}, body, form, json, redirect = 'manual' } = {}) { const h = { ...headers }; if (token) h.Authorization = `Bearer ${token}`; let payload = body; if (form) { h['Content-Type'] = 'application/x-www-form-urlencoded'; payload = new URLSearchParams(form).toString(); } if (json !== undefined) { h['Content-Type'] = 'application/json'; payload = JSON.stringify(json); } const res = await fetch(base + url, { method, headers: h, body: payload, redirect }); const buf = Buffer.from(await res.arrayBuffer()); let data = null; if ((res.headers.get('content-type') || '').includes('json') && buf.length) data = JSON.parse(buf.toString('utf8')); return { status: res.status, headers: res.headers, data, buf, text: buf.toString('utf8') }; } async function authorize (username, password, scope = 'files:read files:write', extra = {}) { const { verifier, challenge } = verifierPair(); const params = { response_type: 'code', client_id: client.clientId, redirect_uri: REDIRECT, scope, state: 'st-1', code_challenge: challenge, code_challenge_method: 'S256', device_name: 'Test phone', ...extra, }; const r = await req('POST', '/oauth/authorize', { form: { ...params, username, password, decision: 'allow' } }); return { r, verifier, params }; } async function login (username, scope) { const { r, verifier } = await authorize(username, 'pw-' + username, scope); expect(r.status).toBe(302); const code = new URL(r.headers.get('location')).searchParams.get('code'); const t = await req('POST', '/oauth/token', { form: { grant_type: 'authorization_code', client_id: client.clientId, code, redirect_uri: REDIRECT, code_verifier: verifier } }); expect(t.status).toBe(200); return t.data; } function vq (p) { return encodeURIComponent(p); } beforeAll(async () => { for (const r of Object.values(ROOTS)) fs.mkdirSync(r, { recursive: true }); fs.mkdirSync(USERS_DIR, { recursive: true }); fs.writeFileSync(path.join(ROOTS.secret, 'secret.txt'), 'top secret'); fs.writeFileSync(path.join(ROOTS.shared, 'readme.txt'), 'shared readme'); mockConfig.paths = Object.values(ROOTS); mockConfig.userPath = USERS_DIR; mockConfig.api.uploadDir = path.join(mockBase, 'staging'); const d = getDb(); store.initApiSchema(d); const hash = (pw) => bcrypt.hashSync(pw, 4); d.prepare('INSERT INTO users (username, password_hash, is_admin, permissions) VALUES (?, ?, ?, ?)') .run(['alice', hash('pw-alice'), 0, JSON.stringify({ shared: { read: true, write: false }, team: { read: true, write: true } })]); d.prepare('INSERT INTO users (username, password_hash, is_admin, permissions) VALUES (?, ?, ?, ?)') .run(['bob', hash('pw-bob'), 0, JSON.stringify({ team: { read: true, write: false } })]); d.prepare('INSERT INTO users (username, password_hash, is_admin, permissions) VALUES (?, ?, ?, ?)') .run(['root', hash('pw-root'), 1, '{}']); const app = express(); app.use(oauthRoutes); app.use('/api/v1', apiV1Routes); app.use(express.json()); app.use('/api/account/api-access', manageRoutes); server = http.createServer(app).listen(0, '127.0.0.1'); await once(server, 'listening'); base = `http://127.0.0.1:${server.address().port}`; client = store.createClient({ name: 'Sync App', redirectUris: [REDIRECT, 'http://127.0.0.1/cb'], createdBy: 'root' }); }); afterAll(async () => { server.close(); fs.rmSync(mockBase, { recursive: true, force: true }); }); beforeEach(() => oauthRoutes.resetLoginFailures()); // --------------------------------------------------------------------------- describe('OAuth authorization code + PKCE', () => { test('metadata advertises PKCE S256 and public clients', async () => { const r = await req('GET', '/.well-known/oauth-authorization-server'); expect(r.data.code_challenge_methods_supported).toEqual(['S256']); expect(r.data.token_endpoint_auth_methods_supported).toEqual(['none']); }); test('consent page shows client, device and scopes and cannot be framed', async () => { const { challenge } = verifierPair(); 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>' }); const r = await req('GET', `/oauth/authorize?${q}`); expect(r.status).toBe(200); expect(r.text).toContain('Sync App'); expect(r.text).toContain('Pixel <8>'); expect(r.text).toContain('files:read'); expect(r.headers.get('x-frame-options')).toBe('DENY'); }); test('unknown client or unregistered redirect never redirects', async () => { const { challenge } = verifierPair(); let r = await req('GET', `/oauth/authorize?response_type=code&client_id=nope&redirect_uri=${vq(REDIRECT)}&code_challenge=${challenge}&code_challenge_method=S256`); expect(r.status).toBe(400); 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`); expect(r.status).toBe(400); }); test('PKCE is mandatory and only S256', async () => { let r = await req('GET', `/oauth/authorize?response_type=code&client_id=${client.clientId}&redirect_uri=${vq(REDIRECT)}&state=x`); expect(r.status).toBe(302); expect(new URL(r.headers.get('location')).searchParams.get('error')).toBe('invalid_request'); 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`); expect(new URL(r.headers.get('location')).searchParams.get('error')).toBe('invalid_request'); }); test('unknown scopes are rejected', async () => { const { r } = await authorize('alice', 'pw-alice', 'files:read admin'); expect(new URL(r.headers.get('location')).searchParams.get('error')).toBe('invalid_scope'); }); test('wrong password re-renders the consent page; deny returns access_denied', async () => { const { r } = await authorize('alice', 'wrong'); expect(r.status).toBe(401); expect(r.text).toContain('Invalid username or password'); const { params } = await authorize('alice', 'x'); const d = await req('POST', '/oauth/authorize', { form: { ...params, decision: 'deny' } }); const loc = new URL(d.headers.get('location')); expect(loc.searchParams.get('error')).toBe('access_denied'); expect(loc.searchParams.get('state')).toBe('st-1'); }); test('repeated failures are rate limited', async () => { for (let i = 0; i < 10; i++) await authorize('alice', 'wrong'); const { r } = await authorize('alice', 'pw-alice'); expect(r.status).toBe(401); expect(r.text).toContain('Too many failed attempts'); }); test('code exchange needs the right verifier and works only once', async () => { const { r, verifier } = await authorize('alice', 'pw-alice'); const loc = new URL(r.headers.get('location')); expect(loc.searchParams.get('state')).toBe('st-1'); expect(loc.searchParams.get('iss')).toBe(base); const code = loc.searchParams.get('code'); 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) } }); expect(bad.status).toBe(400); // The failed attempt burned the code. const again = await req('POST', '/oauth/token', { form: { grant_type: 'authorization_code', client_id: client.clientId, code, redirect_uri: REDIRECT, code_verifier: verifier } }); expect(again.data.error).toBe('invalid_grant'); }); test('loopback redirect accepts any port (RFC 8252)', async () => { const { r } = await authorize('alice', 'pw-alice', 'files:read', { redirect_uri: 'http://127.0.0.1:53111/cb' }); expect(r.status).toBe(302); expect(r.headers.get('location')).toMatch(/^http:\/\/127\.0\.0\.1:53111\/cb\?code=/); }); test('tokens are stored hashed only', async () => { const t = await login('alice'); const d = getDb(); const dump = JSON.stringify([ d.prepare('SELECT * FROM api_grants').all(), d.prepare('SELECT * FROM api_access_tokens').all(), ]); expect(dump).not.toContain(t.access_token); expect(dump).not.toContain(t.refresh_token); expect(dump).toContain(store.sha256Hex(t.access_token)); }); test('refresh rotates tokens and reuse of an old refresh token revokes the grant', async () => { const t1 = await login('alice'); const r2 = await req('POST', '/oauth/token', { form: { grant_type: 'refresh_token', client_id: client.clientId, refresh_token: t1.refresh_token } }); expect(r2.status).toBe(200); expect(r2.data.refresh_token).not.toBe(t1.refresh_token); expect((await req('GET', '/api/v1/me', { token: r2.data.access_token })).status).toBe(200); const reuse = await req('POST', '/oauth/token', { form: { grant_type: 'refresh_token', client_id: client.clientId, refresh_token: t1.refresh_token } }); expect(reuse.status).toBe(400); expect((await req('GET', '/api/v1/me', { token: r2.data.access_token })).status).toBe(401); const r3 = await req('POST', '/oauth/token', { form: { grant_type: 'refresh_token', client_id: client.clientId, refresh_token: r2.data.refresh_token } }); expect(r3.status).toBe(400); }); test('refresh token is bound to its client', async () => { const other = store.createClient({ name: 'Other', redirectUris: [REDIRECT], createdBy: 'root' }); const t = await login('alice'); const r = await req('POST', '/oauth/token', { form: { grant_type: 'refresh_token', client_id: other.clientId, refresh_token: t.refresh_token } }); expect(r.status).toBe(400); }); test('revocation endpoint kills the grant', async () => { const t = await login('alice'); expect((await req('POST', '/oauth/revoke', { form: { token: t.refresh_token } })).status).toBe(200); expect((await req('GET', '/api/v1/me', { token: t.access_token })).status).toBe(401); }); test('disabling a client invalidates its tokens', async () => { const c = store.createClient({ name: 'Temp', redirectUris: [REDIRECT], createdBy: 'root' }); const saved = client; client = c; const t = await login('alice'); client = saved; expect((await req('GET', '/api/v1/me', { token: t.access_token })).status).toBe(200); store.deleteClient(c.clientId); expect((await req('GET', '/api/v1/me', { token: t.access_token })).status).toBe(401); }); }); describe('Access management (web session)', () => { test('user sees own grants only and can revoke them', async () => { const a = await login('alice'); await login('bob'); const list = await req('GET', '/api/account/api-access/grants', { headers: { 'x-test-user': 'alice' } }); expect(list.data.grants.length).toBeGreaterThan(0); const mine = list.data.grants.find((g) => g.id === a.grant_id); expect(mine.name).toBe('Test phone'); expect(mine.clientName).toBe('Sync App'); const bobList = await req('GET', '/api/account/api-access/grants', { headers: { 'x-test-user': 'bob' } }); expect(bobList.data.grants.some((g) => g.id === a.grant_id)).toBe(false); expect((await req('DELETE', `/api/account/api-access/grants/${a.grant_id}`, { headers: { 'x-test-user': 'bob' } })).status).toBe(404); expect((await req('DELETE', `/api/account/api-access/grants/${a.grant_id}`, { headers: { 'x-test-user': 'alice' } })).status).toBe(200); expect((await req('GET', '/api/v1/me', { token: a.access_token })).status).toBe(401); }); test('personal access tokens work and can be revoked', async () => { const r = await req('POST', '/api/account/api-access/tokens', { headers: { 'x-test-user': 'alice' }, json: { name: 'script', scopes: 'files:read' } }); expect(r.status).toBe(201); expect(r.data.token).toMatch(/^fmp_/); const me = await req('GET', '/api/v1/me', { token: r.data.token }); expect(me.data.username).toBe('alice'); expect(me.data.scopes).toEqual(['files:read']); await req('DELETE', `/api/account/api-access/grants/${r.data.id}`, { headers: { 'x-test-user': 'alice' } }); expect((await req('GET', '/api/v1/me', { token: r.data.token })).status).toBe(401); }); test('only admins manage clients and redirect URIs are validated', async () => { expect((await req('POST', '/api/account/api-access/clients', { headers: { 'x-test-user': 'alice' }, json: { name: 'x', redirectUris: [REDIRECT] } })).status).toBe(403); 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); 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'] } }); expect(ok.status).toBe(201); expect(ok.data.client.clientId).toMatch(/^fmc_/); }); }); describe('Scopes and isolation', () => { let alice; let aliceRead; let bob; beforeAll(async () => { alice = await login('alice'); aliceRead = await login('alice', 'files:read'); bob = await login('bob'); }); test('missing or bogus token is rejected', async () => { expect((await req('GET', '/api/v1/roots')).status).toBe(401); expect((await req('GET', '/api/v1/roots', { token: 'fma_nope' })).status).toBe(401); }); test('roots reflect the user permissions', async () => { const a = await req('GET', '/api/v1/roots', { token: alice.access_token }); expect(a.data.roots.map((r) => r.id)).toEqual(['~', 'shared', 'team']); expect(a.data.roots.find((r) => r.id === 'shared').writable).toBe(false); const b = await req('GET', '/api/v1/roots', { token: bob.access_token }); expect(b.data.roots.map((r) => r.id)).toEqual(['~', 'team']); }); test('read-only token cannot write', async () => { const r = await req('PUT', `/api/v1/files/content?path=${vq('/~/x.txt')}`, { token: aliceRead.access_token, body: 'x' }); expect(r.status).toBe(403); expect(r.data.error).toBe('insufficient_scope'); }); test('cannot read roots without permission, other users folders, or escape via .. or symlinks', async () => { expect((await req('GET', `/api/v1/files/content?path=${vq('/secret/secret.txt')}`, { token: alice.access_token })).status).toBe(404); fs.mkdirSync(path.join(USERS_DIR, 'bob'), { recursive: true }); fs.writeFileSync(path.join(USERS_DIR, 'bob', 'private.txt'), 'bob only'); const viaHome = await req('GET', `/api/v1/files/content?path=${vq('/~/private.txt')}`, { token: alice.access_token }); expect(viaHome.status).toBe(404); expect((await req('GET', `/api/v1/files/content?path=${vq('/~/../bob/private.txt')}`, { token: alice.access_token })).status).toBe(400); expect((await req('GET', `/api/v1/files/content?path=${vq('/~/private.txt')}`, { token: bob.access_token })).text).toBe('bob only'); fs.mkdirSync(path.join(USERS_DIR, 'alice'), { recursive: true }); fs.symlinkSync(ROOTS.secret, path.join(USERS_DIR, 'alice', 'link')); expect((await req('GET', `/api/v1/files/content?path=${vq('/~/link/secret.txt')}`, { token: alice.access_token })).status).toBe(404); const list = await req('GET', `/api/v1/files/list?path=${vq('/~')}`, { token: alice.access_token }); expect(list.data.entries.some((e) => e.name === 'link')).toBe(false); fs.unlinkSync(path.join(USERS_DIR, 'alice', 'link')); }); test('read-only roots cannot be written even with files:write', async () => { const r = await req('PUT', `/api/v1/files/content?path=${vq('/shared/new.txt')}`, { token: alice.access_token, body: 'x' }); expect(r.status).toBe(403); expect(fs.existsSync(path.join(ROOTS.shared, 'new.txt'))).toBe(false); const b = await req('POST', '/api/v1/folders', { token: bob.access_token, json: { path: '/team/x' } }); expect(b.status).toBe(403); }); test('admin users get admin-level access, nothing more', async () => { const root = await login('root'); const r = await req('GET', '/api/v1/roots', { token: root.access_token }); expect(r.data.roots.map((x) => x.id)).toEqual(['~', 'shared', 'team', 'secret']); }); }); describe('Files: upload, download, conflicts', () => { let t; beforeAll(async () => { t = await login('alice'); }); test('simple upload, conditional overwrite and conflicts', async () => { const mtime = Date.parse('2024-01-02T03:04:05.000Z'); const up = await req('PUT', `/api/v1/files/content?path=${vq('/team/a.txt')}&mtime=${mtime}`, { token: t.access_token, body: 'hello' }); expect(up.status).toBe(201); expect(up.data).toMatchObject({ path: '/team/a.txt', type: 'file', size: 5, mtime_ms: mtime }); expect(up.data.sha256).toBe(crypto.createHash('sha256').update('hello').digest('hex')); expect(fs.readFileSync(path.join(ROOTS.team, 'a.txt'), 'utf8')).toBe('hello'); const conflict = await req('PUT', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token, body: 'x' }); expect(conflict.status).toBe(409); expect(conflict.data.current.version).toBe(up.data.version); const noneMatch = await req('PUT', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token, body: 'x', headers: { 'If-None-Match': '*' } }); expect(noneMatch.status).toBe(412); const stale = await req('PUT', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token, body: 'x', headers: { 'If-Match': '"stale"' } }); expect(stale.status).toBe(412); 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}"` } }); expect(ok.status).toBe(200); expect(ok.data.version).not.toBe(up.data.version); expect(ok.headers.get('etag')).toBe(`"${ok.data.version}"`); const forced = await req('PUT', `/api/v1/files/content?path=${vq('/team/a.txt')}&overwrite=true`, { token: t.access_token, body: 'hello world!' }); expect(forced.status).toBe(200); }); test('upload into a missing folder fails cleanly and leaves no temp files', async () => { const r = await req('PUT', `/api/v1/files/content?path=${vq('/team/nope/a.txt')}`, { token: t.access_token, body: 'x' }); expect(r.status).toBe(409); expect(r.data.error).toBe('parent_missing'); expect(fs.readdirSync(ROOTS.team).some((n) => n.startsWith('.fmapi-'))).toBe(false); }); test('checksum mismatch is refused', async () => { 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) } }); expect(r.status).toBe(422); expect(fs.existsSync(path.join(ROOTS.team, 'sum.txt'))).toBe(false); }); test('download supports ETag, 304 and Range', async () => { const meta = await req('GET', `/api/v1/files/metadata?path=${vq('/team/a.txt')}`, { token: t.access_token }); const full = await req('GET', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token }); expect(full.status).toBe(200); expect(full.text).toBe('hello world!'); expect(full.headers.get('etag')).toBe(`"${meta.data.version}"`); 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}"` } }); expect(cached.status).toBe(304); const part = await req('GET', `/api/v1/files/content?path=${vq('/team/a.txt')}`, { token: t.access_token, headers: { Range: 'bytes=6-10' } }); expect(part.status).toBe(206); expect(part.text).toBe('world'); expect(part.headers.get('content-range')).toBe('bytes 6-10/12'); }); test('metadata can compute a hash on demand for files changed outside the API', async () => { fs.writeFileSync(path.join(ROOTS.team, 'outside.bin'), 'outside'); const m1 = await req('GET', `/api/v1/files/metadata?path=${vq('/team/outside.bin')}`, { token: t.access_token }); expect(m1.data.sha256).toBeNull(); const m2 = await req('GET', `/api/v1/files/metadata?path=${vq('/team/outside.bin')}&hash=1`, { token: t.access_token }); expect(m2.data.sha256).toBe(crypto.createHash('sha256').update('outside').digest('hex')); }); test('folders, listing, move and delete', async () => { expect((await req('POST', '/api/v1/folders', { token: t.access_token, json: { path: '/team/docs/sub', parents: true } })).status).toBe(201); expect((await req('POST', '/api/v1/folders', { token: t.access_token, json: { path: '/team/docs' } })).status).toBe(409); expect((await req('POST', '/api/v1/folders', { token: t.access_token, json: { path: '/team/docs', exist_ok: true } })).status).toBe(200); await req('PUT', `/api/v1/files/content?path=${vq('/team/docs/b.txt')}`, { token: t.access_token, body: 'b' }); const list = await req('GET', `/api/v1/files/list?path=${vq('/team/docs')}&limit=1`, { token: t.access_token }); expect(list.data.entries).toHaveLength(1); expect(list.data.next_page_token).toBe('1'); 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' } }); expect(mv.status).toBe(200); expect(mv.data.path).toBe('/team/docs/sub/c.txt'); await req('PUT', `/api/v1/files/content?path=${vq('/team/docs/d.txt')}`, { token: t.access_token, body: 'd' }); 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' } }); expect(clash.status).toBe(409); const into = await req('POST', '/api/v1/files/move', { token: t.access_token, json: { from: '/team/docs', to: '/team/docs/sub/x' } }); expect(into.status).toBe(400); const toReadOnly = await req('POST', '/api/v1/files/move', { token: t.access_token, json: { from: '/team/docs/d.txt', to: '/shared/d.txt' } }); expect(toReadOnly.status).toBe(403); expect((await req('DELETE', `/api/v1/files?path=${vq('/team/docs')}`, { token: t.access_token })).status).toBe(409); expect((await req('DELETE', `/api/v1/files?path=${vq('/team/docs/d.txt')}`, { token: t.access_token, headers: { 'If-Match': '"old"' } })).status).toBe(412); expect((await req('DELETE', `/api/v1/files?path=${vq('/team/docs')}&recursive=true`, { token: t.access_token })).status).toBe(204); expect(fs.existsSync(path.join(ROOTS.team, 'docs'))).toBe(false); expect((await req('DELETE', `/api/v1/files?path=${vq('/team')}`, { token: t.access_token })).status).toBe(400); }); test('errors are JSON, also for unknown endpoints', async () => { const r = await req('GET', '/api/v1/nope', { token: t.access_token }); expect(r.status).toBe(404); expect(r.data.error).toBe('not_found'); const bad = await req('GET', '/api/v1/files/metadata?path=relative', { token: t.access_token }); expect(bad.data.error).toBe('invalid_path'); }); }); describe('Resumable uploads', () => { let t; let other; beforeAll(async () => { t = await login('alice'); other = await login('alice'); }); test('chunked upload with offsets, resume info and checksum', async () => { const content = crypto.randomBytes(2.5 * 1024 * 1024); const sha = crypto.createHash('sha256').update(content).digest('hex'); const c = await req('POST', '/api/v1/uploads', { token: t.access_token, json: { path: '/team/big.bin', size: content.length, sha256: sha, mtime: 1700000000000 } }); expect(c.status).toBe(201); const id = c.data.id; expect(c.data.max_chunk_bytes).toBe(1024 * 1024); // Another grant (even of the same user) cannot touch this session. expect((await req('GET', `/api/v1/uploads/${id}`, { token: other.access_token })).status).toBe(404); const tooBig = await req('PATCH', `/api/v1/uploads/${id}`, { token: t.access_token, headers: { 'Upload-Offset': '0' }, body: content.subarray(0, 1024 * 1024 + 1) }); expect(tooBig.status).toBe(413); expect((await req('GET', `/api/v1/uploads/${id}`, { token: t.access_token })).data.offset).toBe(0); let offset = 0; while (offset < content.length) { const chunk = content.subarray(offset, offset + 1024 * 1024); const p = await req('PATCH', `/api/v1/uploads/${id}`, { token: t.access_token, headers: { 'Upload-Offset': String(offset) }, body: chunk }); expect(p.status).toBe(200); offset = p.data.offset; if (offset === 1024 * 1024) { const wrong = await req('PATCH', `/api/v1/uploads/${id}`, { token: t.access_token, headers: { 'Upload-Offset': '0' }, body: 'x' }); expect(wrong.status).toBe(409); expect(wrong.data.offset).toBe(1024 * 1024); const early = await req('POST', `/api/v1/uploads/${id}/complete`, { token: t.access_token }); expect(early.status).toBe(409); } } const done = await req('POST', `/api/v1/uploads/${id}/complete`, { token: t.access_token }); expect(done.status).toBe(201); expect(done.data).toMatchObject({ path: '/team/big.bin', size: content.length, sha256: sha, mtime_ms: 1700000000000 }); expect(fs.readFileSync(path.join(ROOTS.team, 'big.bin')).equals(content)).toBe(true); expect(fs.readdirSync(mockConfig.api.uploadDir)).toEqual([]); }); test('checksum mismatch discards the upload', async () => { const c = await req('POST', '/api/v1/uploads', { token: t.access_token, json: { path: '/team/bad.bin', size: 3, sha256: '0'.repeat(64) } }); await req('PATCH', `/api/v1/uploads/${c.data.id}`, { token: t.access_token, headers: { 'Upload-Offset': '0' }, body: 'abc' }); const r = await req('POST', `/api/v1/uploads/${c.data.id}/complete`, { token: t.access_token }); expect(r.status).toBe(422); expect(fs.existsSync(path.join(ROOTS.team, 'bad.bin'))).toBe(false); }); test('preconditions are checked at start and at completion', async () => { const exists = await req('POST', '/api/v1/uploads', { token: t.access_token, json: { path: '/team/big.bin', size: 1 } }); expect(exists.status).toBe(409); const c = await req('POST', '/api/v1/uploads', { token: t.access_token, json: { path: '/team/race.txt', size: 1, if_none_match: '*' } }); await req('PATCH', `/api/v1/uploads/${c.data.id}`, { token: t.access_token, headers: { 'Upload-Offset': '0' }, body: 'z' }); fs.writeFileSync(path.join(ROOTS.team, 'race.txt'), 'someone else'); const r = await req('POST', `/api/v1/uploads/${c.data.id}/complete`, { token: t.access_token }); expect(r.status).toBe(412); expect(fs.readFileSync(path.join(ROOTS.team, 'race.txt'), 'utf8')).toBe('someone else'); }); test('cancel removes the staged data', async () => { const c = await req('POST', '/api/v1/uploads', { token: t.access_token, json: { path: '/team/cancel.bin', size: 10 } }); expect((await req('DELETE', `/api/v1/uploads/${c.data.id}`, { token: t.access_token })).status).toBe(204); expect((await req('GET', `/api/v1/uploads/${c.data.id}`, { token: t.access_token })).status).toBe(404); }); }); describe('Delta feed', () => { let t; let bob; beforeAll(async () => { t = await login('alice'); bob = await login('bob'); }); async function drain (cursor) { const all = []; let c = cursor; for (;;) { const r = await req('GET', `/api/v1/delta?root=${vq('~')}&cursor=${c}&limit=2`, { token: t.access_token }); expect(r.status).toBe(200); all.push(...r.data.entries); c = r.data.cursor; if (!r.data.has_more) return { entries: all, cursor: c }; } } test('snapshot, then created/modified/moved/deleted incl. changes made outside the API', async () => { const home = path.join(USERS_DIR, 'alice'); fs.mkdirSync(path.join(home, 'photos'), { recursive: true }); fs.writeFileSync(path.join(home, 'photos', '1.jpg'), 'one'); fs.writeFileSync(path.join(home, 'notes.txt'), 'n'); // Initial snapshot, paged. let r = await req('GET', `/api/v1/delta?root=${vq('~')}&limit=2`, { token: t.access_token }); expect(r.data.reset).toBe(true); const snap = [...r.data.entries]; let cursor = r.data.cursor; while (r.data.has_more) { r = await req('GET', `/api/v1/delta?root=${vq('~')}&cursor=${cursor}&limit=2`, { token: t.access_token }); snap.push(...r.data.entries); cursor = r.data.cursor; } expect(snap.map((e) => e.path).sort()).toEqual(expect.arrayContaining(['/~/notes.txt', '/~/photos', '/~/photos/1.jpg'])); expect(snap.every((e) => e.change === 'exists')).toBe(true); // No changes. let d = await drain(cursor); expect(d.entries).toEqual([]); cursor = d.cursor; // Changes through the API. await req('PUT', `/api/v1/files/content?path=${vq('/~/new.txt')}`, { token: t.access_token, body: 'new' }); await req('POST', '/api/v1/files/move', { token: t.access_token, json: { from: '/~/photos', to: '/~/pictures' } }); d = await drain(cursor); cursor = d.cursor; expect(d.entries).toEqual(expect.arrayContaining([ expect.objectContaining({ change: 'created', path: '/~/new.txt', type: 'file', size: 3 }), expect.objectContaining({ change: 'moved', path: '/~/pictures', old_path: '/~/photos', type: 'folder' }), ])); // Children of a moved folder are implied, not repeated. expect(d.entries.some((e) => e.path === '/~/pictures/1.jpg')).toBe(false); // Changes made directly on disk / via the web UI. await new Promise((res) => setTimeout(res, 20)); fs.writeFileSync(path.join(home, 'notes.txt'), 'changed notes'); fs.rmSync(path.join(home, 'pictures'), { recursive: true }); d = await drain(cursor); cursor = d.cursor; expect(d.entries).toEqual(expect.arrayContaining([ expect.objectContaining({ change: 'modified', path: '/~/notes.txt', size: 13 }), expect.objectContaining({ change: 'deleted', path: '/~/pictures', type: 'folder' }), ])); expect(d.entries.some((e) => e.path === '/~/pictures/1.jpg')).toBe(false); const del = d.entries.find((e) => e.change === 'deleted'); expect(del.version).toBeNull(); }); test('cursor is bound to its root and unknown roots are hidden', async () => { const r = await req('GET', `/api/v1/delta?root=${vq('~')}`, { token: t.access_token }); const other = await req('GET', `/api/v1/delta?root=team&cursor=${r.data.cursor}`, { token: t.access_token }); expect(other.status).toBe(400); expect((await req('GET', '/api/v1/delta?root=secret', { token: t.access_token })).status).toBe(404); expect((await req('GET', '/api/v1/delta?root=x&cursor=%%%', { token: t.access_token })).status).toBe(400); }); test('another user sees their own home, not alice\'s', async () => { const r = await req('GET', `/api/v1/delta?root=${vq('~')}&limit=1000`, { token: bob.access_token }); expect(r.data.entries.map((e) => e.path)).toEqual(['/~/private.txt']); }); test('pruned history answers 410 so clients start over', async () => { const r = await req('GET', '/api/v1/delta?root=team&limit=1000', { token: t.access_token }); expect(r.data.has_more).toBe(false); getDb().prepare('UPDATE api_scan_state SET min_seq = min_seq + 100000 WHERE root_key = ?').run([ROOTS.team]); const gone = await req('GET', `/api/v1/delta?root=team&cursor=${r.data.cursor}`, { token: t.access_token }); expect(gone.status).toBe(410); expect(gone.data.error).toBe('cursor_expired'); }); });