A local-first personal finance app
Add integration tests for remaining app-sync.js endpoints% (#425)
Tom Crassetb8d2797259f7
diff --git a/src/app-sync.js b/src/app-sync.jsindex 7d2b55714b..47a818d6bf 100644--- a/src/app-sync.js+++ b/src/app-sync.js@@ -12,7 +12,9 @@ import { SyncProtoBuf } from '@actual-app/crdt';const app = express();app.use(errorMiddleware);+app.use(express.json());app.use(express.raw({ type: 'application/actual-sync' }));+app.use(express.raw({ type: 'application/encrypted-file' }));app.use(express.json());app.use(validateUserMiddleware);@@ -139,12 +141,10 @@ app.post('/user-get-key', (req, res) => {}let { encrypt_salt, encrypt_keyid, encrypt_test } = rows[0];- res.send(- JSON.stringify({- status: 'ok',- data: { id: encrypt_keyid, salt: encrypt_salt, test: encrypt_test },- }),- );+ res.send({+ status: 'ok',+ data: { id: encrypt_keyid, salt: encrypt_salt, test: encrypt_test },+ });});app.post('/user-create-key', (req, res) => {@@ -188,11 +188,20 @@ app.post('/reset-user-file', async (req, res) => {app.post('/upload-user-file', async (req, res) => {let accountDb = getAccountDb();if (typeof req.headers['x-actual-name'] !== 'string') {+ // FIXME: Not sure how this cannot be a string when the header is+ // set.res.status(400).send('single x-actual-name is required');return;}+let name = decodeURIComponent(req.headers['x-actual-name']);let fileId = req.headers['x-actual-file-id'];++ if (!fileId || typeof fileId !== 'string') {+ res.status(400).send('fileId is required');+ return;+ }+let groupId = req.headers['x-actual-group-id'] || null;let encryptMeta = req.headers['x-actual-encrypt-meta'] || null;let syncFormatVersion = req.headers['x-actual-format'] || null;@@ -202,15 +211,11 @@ app.post('/upload-user-file', async (req, res) => {? JSON.parse(encryptMeta).keyId: null;- if (!fileId || typeof fileId !== 'string') {- throw new Error('fileId is required');- }-let currentFiles = accountDb.all('SELECT group_id, encrypt_keyid, encrypt_meta FROM files WHERE id = ?',[fileId],);- if (currentFiles.length) {+ if (currentFiles.length > 0) {let currentFile = currentFiles[0];// The uploading file is part of an old group, so reject@@ -242,7 +247,8 @@ app.post('/upload-user-file', async (req, res) => {await fs.writeFile(getPathForUserFile(fileId), req.body);} catch (err) {console.log('Error writing file', err);- res.send(JSON.stringify({ status: 'error' }));+ res.status(500).send({ status: 'error' });+ return;}let rows = accountDb.all('SELECT id FROM files WHERE id = ?', [fileId]);@@ -253,7 +259,7 @@ app.post('/upload-user-file', async (req, res) => {'INSERT INTO files (id, group_id, sync_version, name, encrypt_meta) VALUES (?, ?, ?, ?, ?)',[fileId, groupId, syncFormatVersion, name, encryptMeta],);- res.send(JSON.stringify({ status: 'ok', groupId }));+ res.send({ status: 'ok', groupId });} else {if (!groupId) {// sync state was reset, create new group@@ -270,7 +276,7 @@ app.post('/upload-user-file', async (req, res) => {[syncFormatVersion, encryptMeta, name, fileId],);- res.send(JSON.stringify({ status: 'ok', groupId }));+ res.send({ status: 'ok', groupId });}});@@ -278,6 +284,8 @@ app.get('/download-user-file', async (req, res) => {let accountDb = getAccountDb();let fileId = req.headers['x-actual-file-id'];if (typeof fileId !== 'string') {+ // FIXME: Not sure how this cannot be a string when the header is+ // set.res.status(400).send('Single file ID is required');return;}@@ -306,7 +314,7 @@ app.post('/update-user-filename', (req, res) => {[fileId],);if (rows.length === 0) {- res.status(500).send('User or file not found');+ res.status(400).send('file not found');return;}@@ -319,18 +327,16 @@ app.get('/list-user-files', (req, res) => {let accountDb = getAccountDb();let rows = accountDb.all('SELECT * FROM files');- res.send(- JSON.stringify({- status: 'ok',- data: rows.map((row) => ({- deleted: row.deleted,- fileId: row.id,- groupId: row.group_id,- name: row.name,- encryptKeyId: row.encrypt_keyid,- })),- }),- );+ res.send({+ status: 'ok',+ data: rows.map((row) => ({+ deleted: row.deleted,+ fileId: row.id,+ groupId: row.group_id,+ name: row.name,+ encryptKeyId: row.encrypt_keyid,+ })),+ });});app.get('/get-user-file-info', (req, res) => {@@ -341,24 +347,24 @@ app.get('/get-user-file-info', (req, res) => {'SELECT * FROM files WHERE id = ? AND deleted = FALSE',[fileId],);+if (rows.length === 0) {- res.send(JSON.stringify({ status: 'error' }));+ res.status(400).send({ status: 'error', reason: 'file-not-found' });return;}+let row = rows[0];- res.send(- JSON.stringify({- status: 'ok',- data: {- deleted: row.deleted,- fileId: row.id,- groupId: row.group_id,- name: row.name,- encryptMeta: row.encrypt_meta ? JSON.parse(row.encrypt_meta) : null,- },- }),- );+ res.send({+ status: 'ok',+ data: {+ deleted: row.deleted,+ fileId: row.id,+ groupId: row.group_id,+ name: row.name,+ encryptMeta: row.encrypt_meta ? JSON.parse(row.encrypt_meta) : null,+ },+ });});app.post('/delete-user-file', (req, res) => {diff --git a/src/app-sync.test.js b/src/app-sync.test.jsindex 4e145c1f42..4c3328abd5 100644--- a/src/app-sync.test.js+++ b/src/app-sync.test.js@@ -6,6 +6,330 @@ import { getPathForUserFile } from './util/paths.js';import { SyncProtoBuf } from '@actual-app/crdt';import crypto from 'node:crypto';+describe('/user-get-key', () => {+ it('returns 401 if the user is not authenticated', async () => {+ const res = await request(app).post('/user-get-key');++ expect(res.statusCode).toEqual(401);+ expect(res.body).toEqual({+ details: 'token-not-found',+ reason: 'unauthorized',+ status: 'error',+ });+ });++ it('returns encryption key details for a given fileId', async () => {+ const fileId = crypto.randomBytes(16).toString('hex');+ const encrypt_salt = 'test-salt';+ const encrypt_keyid = 'test-key-id';+ const encrypt_test = 'test-encrypt-test';++ getAccountDb().mutate(+ 'INSERT INTO files (id, encrypt_salt, encrypt_keyid, encrypt_test) VALUES (?, ?, ?, ?)',+ [fileId, encrypt_salt, encrypt_keyid, encrypt_test],+ );++ const res = await request(app)+ .post('/user-get-key')+ .set('x-actual-token', 'valid-token')+ .send({ fileId });++ expect(res.statusCode).toEqual(200);+ expect(res.body).toEqual({+ status: 'ok',+ data: {+ id: encrypt_keyid,+ salt: encrypt_salt,+ test: encrypt_test,+ },+ });+ });++ it('returns 400 if the file is not found', async () => {+ const res = await request(app)+ .post('/user-get-key')+ .set('x-actual-token', 'valid-token')+ .send({ fileId: 'non-existent-file-id' });++ expect(res.statusCode).toEqual(400);+ expect(res.text).toBe('file-not-found');+ });+});++describe('/user-create-key', () => {+ it('returns 401 if the user is not authenticated', async () => {+ const res = await request(app).post('/user-create-key');++ expect(res.statusCode).toEqual(401);+ expect(res.body).toEqual({+ details: 'token-not-found',+ reason: 'unauthorized',+ status: 'error',+ });+ });+});++describe('/reset-user-file', () => {+ it('returns 401 if the user is not authenticated', async () => {+ const res = await request(app).post('/reset-user-file');++ expect(res.statusCode).toEqual(401);+ expect(res.body).toEqual({+ details: 'token-not-found',+ reason: 'unauthorized',+ status: 'error',+ });+ });++ it('resets the user file and deletes the group file', async () => {+ const fileId = crypto.randomBytes(16).toString('hex');+ const groupId = 'test-group-id';++ // Use addMockFile to insert a mock file into the database+ getAccountDb().mutate(+ 'INSERT INTO files (id, group_id, deleted) VALUES (?, ?, FALSE)',+ [fileId, groupId],+ );++ const res = await request(app)+ .post('/reset-user-file')+ .set('x-actual-token', 'valid-token')+ .send({ fileId });++ expect(res.statusCode).toEqual(200);+ expect(res.body).toEqual({ status: 'ok' });++ // Verify that the file is marked as deleted+ const rows = getAccountDb().all('SELECT group_id FROM files WHERE id = ?', [+ fileId,+ ]);++ expect(rows[0].group_id).toBeNull;+ });++ it('returns 400 if the file is not found', async () => {+ const res = await request(app)+ .post('/reset-user-file')+ .set('x-actual-token', 'valid-token')+ .send({ fileId: 'non-existent-file-id' });++ expect(res.statusCode).toEqual(400);+ expect(res.text).toBe('User or file not found');+ });+});++describe('/upload-user-file', () => {+ it('returns 401 if the user is not authenticated', async () => {+ const res = await request(app).post('/upload-user-file');++ expect(res.statusCode).toEqual(401);+ expect(res.body).toEqual({+ details: 'token-not-found',+ reason: 'unauthorized',+ status: 'error',+ });+ });++ it('returns 400 if x-actual-name header is missing', async () => {+ const res = await request(app)+ .post('/upload-user-file')+ .set('x-actual-token', 'valid-token')+ .set('x-actual-file-id', 'test-file-id')+ .send('file content');++ expect(res.statusCode).toEqual(400);+ expect(res.text).toBe('single x-actual-name is required');+ });++ it('returns 400 if fileId is missing', async () => {+ const content = Buffer.from('file content');+ const res = await request(app)+ .post('/upload-user-file')+ .set('Content-Type', 'application/encrypted-file')+ .set('x-actual-token', 'valid-token')+ .set('x-actual-name', 'test-file')+ .send(content);++ expect(res.statusCode).toEqual(400);+ expect(res.text).toBe('fileId is required');+ });++ it('uploads a new file successfully', async () => {+ const fileId = crypto.randomBytes(16).toString('hex');+ const fileName = 'test-file.txt';+ const fileContent = 'test file content';+ const fileContentBuffer = Buffer.from(fileContent);+ const syncVersion = 2;+ const encryptMeta = JSON.stringify({ keyId: 'key-id' });++ // Verify that the file does not exist before upload+ const rowsBefore = getAccountDb().all('SELECT * FROM files WHERE id = ?', [+ fileId,+ ]);++ expect(rowsBefore.length).toBe(0);++ const res = await request(app)+ .post('/upload-user-file')+ .set('Content-Type', 'application/encrypted-file')+ .set('x-actual-token', 'valid-token')+ .set('x-actual-name', fileName)+ .set('x-actual-file-id', fileId)+ .set('x-actual-format', syncVersion.toString())+ .set('x-actual-encrypt-meta', encryptMeta)+ .send(fileContentBuffer);++ expect(res.statusCode).toEqual(200);+ expect(res.body).toEqual({ status: 'ok', groupId: expect.any(String) });++ const receivedGroupid = res.body.groupId;+ // Verify that the file exists in the accountDb+ const rowsAfter = getAccountDb().all('SELECT * FROM files WHERE id = ?', [+ fileId,+ ]);+ expect(rowsAfter.length).toBe(1);+ expect(rowsAfter[0].id).toEqual(fileId);+ expect(rowsAfter[0].group_id).toEqual(receivedGroupid);+ expect(rowsAfter[0].sync_version).toEqual(syncVersion);+ expect(rowsAfter[0].name).toEqual(fileName);+ expect(rowsAfter[0].encrypt_meta).toEqual(encryptMeta);++ // Verify that the file was written to the file system+ const filePath = getPathForUserFile(fileId);+ const writtenContent = await fs.promises.readFile(filePath, 'utf8');+ expect(writtenContent).toEqual(fileContent);++ // Clean up the file+ await fs.promises.unlink(filePath);+ });++ it('uploads and updates an existing file successfully', async () => {+ const fileId = crypto.randomBytes(16).toString('hex');+ const oldGroupId = null; //sync state was reset+ const oldFileName = 'old-test-file.txt';+ const newFileName = 'new-test-file.txt';+ const oldFileContent = 'old file content';+ const newFileContent = 'new file content';+ const oldSyncVersion = 1;+ const newSyncVersion = 2;+ const oldKeyId = 'old-key-id';+ const oldEncryptMeta = JSON.stringify({ keyId: oldKeyId });+ const newEncryptMeta = JSON.stringify({+ keyId: oldKeyId,+ sentinelValue: 1,+ }); //keep the same key, but change other things++ // Create the old file version+ getAccountDb().mutate(+ 'INSERT INTO files (id, group_id, sync_version, name, encrypt_meta, encrypt_keyid) VALUES (?, ?, ?, ?, ?, ?)',+ [+ fileId,+ oldGroupId,+ oldSyncVersion,+ oldFileName,+ oldEncryptMeta,+ oldKeyId,+ ],+ );++ await fs.writeFile(getPathForUserFile(fileId), oldFileContent, (err) => {+ if (err) throw err;+ });++ const res = await request(app)+ .post('/upload-user-file')+ .set('Content-Type', 'application/encrypted-file')+ .set('x-actual-token', 'valid-token')+ .set('x-actual-file-id', fileId)+ .set('x-actual-name', newFileName)+ .set('x-actual-format', newSyncVersion.toString())+ .set('x-actual-encrypt-meta', newEncryptMeta)+ .send(Buffer.from(newFileContent));++ expect(res.statusCode).toEqual(200);+ expect(res.body).toEqual({ status: 'ok', groupId: expect.any(String) });++ const receivedGroupid = res.body.groupId;++ // Verify that the file was updated in the accountDb+ const rowsAfter = getAccountDb().all('SELECT * FROM files WHERE id = ?', [+ fileId,+ ]);+ expect(rowsAfter.length).toBe(1);+ expect(rowsAfter[0].id).toEqual(fileId);+ expect(rowsAfter[0].group_id).toEqual(receivedGroupid);+ expect(rowsAfter[0].sync_version).toEqual(newSyncVersion);+ expect(rowsAfter[0].name).toEqual(newFileName);+ expect(rowsAfter[0].encrypt_meta).toEqual(newEncryptMeta);++ // Verify that the file was written to the file system+ const filePath = getPathForUserFile(fileId);+ const writtenContent = await fs.promises.readFile(filePath, 'utf8');+ expect(writtenContent).toEqual(newFileContent);++ // Clean up the file+ await fs.promises.unlink(filePath);+ });++ it('returns 400 if the file is part of an old group', async () => {+ const fileId = crypto.randomBytes(16).toString('hex');+ const groupId = 'old-group-id';+ const fileName = 'test-file.txt';+ const keyId = 'key-id';+ const syncVersion = 2;++ // Add a mock file with the old group ID+ addMockFile(+ fileId,+ 'current-group-id',+ keyId,+ JSON.stringify({ keyId }),+ syncVersion,+ );++ const res = await request(app)+ .post('/upload-user-file')+ .set('Content-Type', 'application/encrypted-file')+ .set('x-actual-token', 'valid-token')+ .set('x-actual-file-id', fileId)+ .set('x-actual-group-id', groupId)+ .set('x-actual-name', fileName);++ expect(res.statusCode).toEqual(400);+ expect(res.text).toEqual('file-has-reset');+ });++ it('returns 400 if the file has a new encryption key', async () => {+ const fileId = crypto.randomBytes(16).toString('hex');+ const groupId = 'group-id';+ const fileName = 'test-file.txt';+ const oldKeyId = 'old-key-id';+ const newKeyId = 'new-key-id';+ const syncVersion = 2;++ // Add a mock file with the new key+ addMockFile(+ fileId,+ groupId,+ newKeyId,+ JSON.stringify({ newKeyId }),+ syncVersion,+ );++ const res = await request(app)+ .post('/upload-user-file')+ .set('Content-Type', 'application/encrypted-file')+ .set('x-actual-token', 'valid-token')+ .set('x-actual-file-id', fileId)+ .set('x-actual-group-id', groupId)+ .set('x-actual-name', fileName)+ .set('x-actual-encrypt-meta', JSON.stringify({ keyId: oldKeyId }));++ expect(res.statusCode).toEqual(400);+ expect(res.text).toEqual('file-has-new-key');+ });+});+describe('/download-user-file', () => {describe('default version', () => {it('returns 401 if the user is not authenticated', async () => {@@ -39,6 +363,7 @@ describe('/download-user-file', () => {.set('x-actual-file-id', 'non-existing-file-id');expect(res.statusCode).toEqual(400);+ expect(res.text).toBe('User or file not found');});it('returns 500 error if the file does not exist on the filesystem', async () => {@@ -56,7 +381,8 @@ describe('/download-user-file', () => {});it('returns an attachment file', async () => {- fs.writeFileSync(getPathForUserFile('file-id'), 'content');+ const fileContent = 'content';+ fs.writeFileSync(getPathForUserFile('file-id'), fileContent);getAccountDb().mutate('INSERT INTO files (id, deleted) VALUES (?, FALSE)',['file-id'],@@ -74,6 +400,184 @@ describe('/download-user-file', () => {'content-type': 'application/octet-stream',}),);++ expect(res.body).toBeInstanceOf(Buffer);+ expect(res.body.toString('utf8')).toEqual(fileContent);+ });+ });+});++describe('/update-user-filename', () => {+ it('returns 401 if the user is not authenticated', async () => {+ const res = await request(app).post('/update-user-filename');++ expect(res.statusCode).toEqual(401);+ expect(res.body).toEqual({+ details: 'token-not-found',+ reason: 'unauthorized',+ status: 'error',+ });+ });++ it('returns 400 if the file is not found', async () => {+ const res = await request(app)+ .post('/update-user-filename')+ .set('x-actual-token', 'valid-token')+ .send({ fileId: 'non-existent-file-id', name: 'new-filename' });++ expect(res.statusCode).toEqual(400);+ expect(res.text).toBe('file not found');+ });++ it('successfully updates the filename', async () => {+ const fileId = crypto.randomBytes(16).toString('hex');+ const oldName = 'old-filename';+ const newName = 'new-filename';++ // Insert a mock file into the database+ getAccountDb().mutate(+ 'INSERT INTO files (id, name, deleted) VALUES (?, ?, FALSE)',+ [fileId, oldName],+ );++ const res = await request(app)+ .post('/update-user-filename')+ .set('x-actual-token', 'valid-token')+ .send({ fileId, name: newName });++ expect(res.statusCode).toEqual(200);+ expect(res.body).toEqual({ status: 'ok' });++ // Verify that the filename was updated+ const rows = getAccountDb().all('SELECT name FROM files WHERE id = ?', [+ fileId,+ ]);++ expect(rows[0].name).toEqual(newName);+ });+});++describe('/list-user-files', () => {+ it('returns 401 if the user is not authenticated', async () => {+ const res = await request(app).get('/list-user-files');++ expect(res.statusCode).toEqual(401);+ expect(res.body).toEqual({+ details: 'token-not-found',+ reason: 'unauthorized',+ status: 'error',+ });+ });++ it('returns a list of user files for an authenticated user', async () => {+ const fileId1 = crypto.randomBytes(16).toString('hex');+ const fileId2 = crypto.randomBytes(16).toString('hex');+ const fileName1 = 'file1.txt';+ const fileName2 = 'file2.txt';++ // Insert mock files into the database+ getAccountDb().mutate(+ 'INSERT INTO files (id, name, deleted) VALUES (?, ?, FALSE)',+ [fileId1, fileName1],+ );+ getAccountDb().mutate(+ 'INSERT INTO files (id, name, deleted) VALUES (?, ?, FALSE)',+ [fileId2, fileName2],+ );++ const res = await request(app)+ .get('/list-user-files')+ .set('x-actual-token', 'valid-token');++ expect(res.statusCode).toEqual(200);+ expect(res.body).toEqual(+ expect.objectContaining({+ status: 'ok',+ data: expect.arrayContaining([+ expect.objectContaining({+ deleted: 0,+ fileId: fileId1,+ groupId: null,+ name: fileName1,+ encryptKeyId: null,+ }),+ expect.objectContaining({+ deleted: 0,+ fileId: fileId2,+ groupId: null,+ name: fileName2,+ encryptKeyId: null,+ }),+ ]),+ }),+ );+ });+});++describe('/get-user-file-info', () => {+ it('returns file info for a valid fileId', async () => {+ const fileId = crypto.randomBytes(16).toString('hex');+ const groupId = 'test-group-id';+ const fileInfo = {+ id: fileId,+ group_id: groupId,+ name: 'test-file',+ encrypt_meta: JSON.stringify({ key: 'value' }),+ deleted: 0,+ };++ getAccountDb().mutate(+ 'INSERT INTO files (id, group_id, name, encrypt_meta, deleted) VALUES (?, ?, ?, ?, ?)',+ [+ fileInfo.id,+ fileInfo.group_id,+ fileInfo.name,+ fileInfo.encrypt_meta,+ fileInfo.deleted,+ ],+ );++ const res = await request(app)+ .get('/get-user-file-info')+ .set('x-actual-token', 'valid-token')+ .set('x-actual-file-id', fileId)+ .send();++ expect(res.statusCode).toEqual(200);++ expect(res.body).toEqual({+ status: 'ok',+ data: {+ deleted: fileInfo.deleted,+ fileId: fileInfo.id,+ groupId: fileInfo.group_id,+ name: fileInfo.name,+ encryptMeta: { key: 'value' },+ },+ });+ });++ it('returns error if the file is not found', async () => {+ const fileId = 'non-existent-file-id';++ const res = await request(app)+ .get('/get-user-file-info')+ .set('x-actual-token', 'valid-token')+ .set('x-actual-file-id', fileId);++ expect(res.statusCode).toEqual(400);+ expect(res.body).toEqual({ status: 'error', reason: 'file-not-found' });+ });++ it('returns error if the user is not authenticated', async () => {+ // Simulate an unauthenticated request by not setting the necessary headers+ const res = await request(app).get('/get-user-file-info');++ expect(res.statusCode).toEqual(401);+ expect(res.body).toEqual({+ status: 'error',+ reason: 'unauthorized',+ details: 'token-not-found',});});});@@ -116,7 +620,7 @@ describe('/delete-user-file', () => {it('marks the file as deleted', async () => {const accountDb = getAccountDb();- const fileId = 'existing-file-id';+ const fileId = crypto.randomBytes(16).toString('hex');// Insert a file into the databaseaccountDb.mutate(diff --git a/upcoming-release-notes/425.md b/upcoming-release-notes/425.mdnew file mode 100644index 0000000000..5397d00bc7--- /dev/null+++ b/upcoming-release-notes/425.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [tcrasset]+---++Add integration tests for remaining app-sync.js endpoints\ No newline at end of file