|
| 1 | +import { Application } from "express"; |
| 2 | +import { beforeAll, describe, expect, it } from "vitest"; |
| 3 | +import supertest from "supertest"; |
| 4 | +import { login } from "./utils.js"; |
| 5 | +import config from "../../src/services/config.js"; |
| 6 | +import sql from "../../src/services/sql.js"; |
| 7 | + |
| 8 | +let app: Application; |
| 9 | +let token: string; |
| 10 | + |
| 11 | +const USER = "etapi"; |
| 12 | + |
| 13 | +describe("etapi/external-blobs", () => { |
| 14 | + beforeAll(async () => { |
| 15 | + config.General.noAuthentication = false; |
| 16 | + config.ExternalBlobStorage.enabled = true; |
| 17 | + config.ExternalBlobStorage.thresholdBytes = 10; |
| 18 | + |
| 19 | + const buildApp = (await import("../../src/app.js")).default; |
| 20 | + app = await buildApp(); |
| 21 | + token = await login(app); |
| 22 | + }); |
| 23 | + |
| 24 | + it("stores small note content internally", async () => { |
| 25 | + const payload = "a".repeat(10); |
| 26 | + const createRes = await supertest(app) |
| 27 | + .post("/etapi/create-note") |
| 28 | + .auth(USER, token, { "type": "basic"}) |
| 29 | + .send({ |
| 30 | + "parentNoteId": "root", |
| 31 | + "title": "Internal Blob Test", |
| 32 | + "mime": "text/plain", |
| 33 | + "type": "text", |
| 34 | + "content": payload |
| 35 | + }) |
| 36 | + .expect(201); |
| 37 | + |
| 38 | + const createdNoteId: string = createRes.body.note.noteId; |
| 39 | + expect(createdNoteId).toBeTruthy(); |
| 40 | + |
| 41 | + const blobId = sql.getValue<string>("SELECT blobId FROM notes WHERE noteId = ?", [createdNoteId]); |
| 42 | + expect(blobId).toBeTruthy(); |
| 43 | + |
| 44 | + const row = sql.getRow<{ contentLocation: string; content: string | null; contentLength: number }>( |
| 45 | + "SELECT contentLocation, content, contentLength FROM blobs WHERE blobId = ?", |
| 46 | + [blobId] |
| 47 | + ); |
| 48 | + |
| 49 | + expect(row).toBeTruthy(); |
| 50 | + expect(row.contentLength).toEqual(payload.length); |
| 51 | + expect(row.contentLocation).toEqual("internal"); |
| 52 | + expect(row.content).toEqual(payload); |
| 53 | + }); |
| 54 | + |
| 55 | + it("stores large note content externally and serves it back", async () => { |
| 56 | + const payload = "a".repeat(11); |
| 57 | + const createRes = await supertest(app) |
| 58 | + .post("/etapi/create-note") |
| 59 | + .auth(USER, token, { "type": "basic"}) |
| 60 | + .send({ |
| 61 | + "parentNoteId": "root", |
| 62 | + "title": "External Blob Test", |
| 63 | + "mime": "application/octet-stream", |
| 64 | + "type": "file", |
| 65 | + "content": payload |
| 66 | + }) |
| 67 | + .expect(201); |
| 68 | + |
| 69 | + const createdNoteId: string = createRes.body.note.noteId; |
| 70 | + expect(createdNoteId).toBeTruthy(); |
| 71 | + |
| 72 | + const blobId = sql.getValue<string>("SELECT blobId FROM notes WHERE noteId = ?", [createdNoteId]); |
| 73 | + expect(blobId).toBeTruthy(); |
| 74 | + |
| 75 | + const row = sql.getRow<{ contentLocation: string; content: string | null; contentLength: number }>( |
| 76 | + "SELECT contentLocation, content, contentLength FROM blobs WHERE blobId = ?", |
| 77 | + [blobId] |
| 78 | + ); |
| 79 | + |
| 80 | + expect(row).toBeTruthy(); |
| 81 | + expect(row.contentLength).toEqual(payload.length); |
| 82 | + expect(row.contentLocation.startsWith("file://")).toBe(true); |
| 83 | + expect(row.content).toBeNull(); |
| 84 | + |
| 85 | + const getRes = await supertest(app) |
| 86 | + .get(`/etapi/notes/${createdNoteId}/content`) |
| 87 | + .auth(USER, token, { "type": "basic"}) |
| 88 | + .expect(200); |
| 89 | + |
| 90 | + expect(getRes.body.toString()).toEqual(payload); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | + |
0 commit comments