From d75e86789d7259818f3e46b4c6c7bda7fabbffcf Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 3 Jun 2025 08:43:32 +0300 Subject: [PATCH] test(etapi): port note content --- _regroup/test-etapi/get-note-content.http | 25 ------- .../test-etapi/put-note-content-binary.http | 25 ------- _regroup/test-etapi/put-note-content.http | 30 -------- apps/server/spec/etapi/note-content.spec.ts | 74 +++++++++++++++++++ 4 files changed, 74 insertions(+), 80 deletions(-) delete mode 100644 _regroup/test-etapi/get-note-content.http delete mode 100644 _regroup/test-etapi/put-note-content-binary.http delete mode 100644 _regroup/test-etapi/put-note-content.http create mode 100644 apps/server/spec/etapi/note-content.spec.ts diff --git a/_regroup/test-etapi/get-note-content.http b/_regroup/test-etapi/get-note-content.http deleted file mode 100644 index 50c677dd8..000000000 --- a/_regroup/test-etapi/get-note-content.http +++ /dev/null @@ -1,25 +0,0 @@ -POST {{triliumHost}}/etapi/create-note -Authorization: {{authToken}} -Content-Type: application/json - -{ - "parentNoteId": "root", - "title": "Hello", - "type": "text", - "content": "Hi there!" -} - -> {% - client.global.set("createdNoteId", response.body.note.noteId); - client.global.set("createdBranchId", response.body.branch.branchId); -%} - -### - -GET {{triliumHost}}/etapi/notes/{{createdNoteId}}/content -Authorization: {{authToken}} - -> {% - client.assert(response.status === 200); - client.assert(response.body === "Hi there!"); -%} diff --git a/_regroup/test-etapi/put-note-content-binary.http b/_regroup/test-etapi/put-note-content-binary.http deleted file mode 100644 index 545b3c111..000000000 --- a/_regroup/test-etapi/put-note-content-binary.http +++ /dev/null @@ -1,25 +0,0 @@ -POST {{triliumHost}}/etapi/create-note -Authorization: {{authToken}} -Content-Type: application/json - -{ - "parentNoteId": "root", - "title": "Hello", - "type": "image", - "mime": "image/png", - "content": "" -} - -> {% client.global.set("createdNoteId", response.body.note.noteId); %} - -### - -PUT {{triliumHost}}/etapi/notes/{{createdNoteId}}/content -Authorization: {{authToken}} -Content-Type: application/octet-stream -Content-Transfer-Encoding: binary - -< ../images/icon-color.png - -> {% client.assert(response.status === 204); %} - diff --git a/_regroup/test-etapi/put-note-content.http b/_regroup/test-etapi/put-note-content.http deleted file mode 100644 index 670195ac2..000000000 --- a/_regroup/test-etapi/put-note-content.http +++ /dev/null @@ -1,30 +0,0 @@ -POST {{triliumHost}}/etapi/create-note -Authorization: {{authToken}} -Content-Type: application/json - -{ - "parentNoteId": "root", - "title": "Hello", - "type": "code", - "mime": "text/plain", - "content": "Hi there!" -} - -> {% client.global.set("createdNoteId", response.body.note.noteId); %} - -### - -PUT {{triliumHost}}/etapi/notes/{{createdNoteId}}/content -Authorization: {{authToken}} -Content-Type: text/plain - -Changed content - -> {% client.assert(response.status === 204); %} - -### - -GET {{triliumHost}}/etapi/notes/{{createdNoteId}}/content -Authorization: {{authToken}} - -> {% client.assert(response.body === "Changed content"); %} diff --git a/apps/server/spec/etapi/note-content.spec.ts b/apps/server/spec/etapi/note-content.spec.ts new file mode 100644 index 000000000..cca3e3f81 --- /dev/null +++ b/apps/server/spec/etapi/note-content.spec.ts @@ -0,0 +1,74 @@ +import { Application } from "express"; +import { beforeAll, describe, expect, it } from "vitest"; +import supertest from "supertest"; +import { createNote, login } from "./utils.js"; +import config from "../../src/services/config.js"; +import { randomUUID } from "crypto"; + +let app: Application; +let token: string; + +const USER = "etapi"; +let createdNoteId: string; +let createdBranchId: string; + +describe("etapi/note-content", () => { + beforeAll(async () => { + config.General.noAuthentication = false; + const buildApp = (await (import("../../src/app.js"))).default; + app = await buildApp(); + token = await login(app); + + ({ createdNoteId, createdBranchId } = await createNote(app, token)); + }); + + it("get content", async () => { + const response = await getContentResponse(); + expect(response.text).toStrictEqual("Hi there!"); + }); + + it("put note content", async () => { + const text = "Changed content"; + await supertest(app) + .put(`/etapi/notes/${createdNoteId}/content`) + .auth(USER, token, { "type": "basic"}) + .set("Content-Type", "text/plain") + .send(text) + .expect(204); + + const response = await getContentResponse(); + expect(response.text).toStrictEqual(text); + }); + + it("put note content binary", async () => { + // First, create a binary note + const response = await supertest(app) + .post("/etapi/create-note") + .auth("etapi", token, { "type": "basic"}) + .send({ + "parentNoteId": "root", + "title": "Hello", + "mime": "image/png", + "type": "image", + "content": "" + }) + .expect(201); + const createdNoteId = response.body.note.noteId; + + // Put binary content + await supertest(app) + .put(`/etapi/notes/${createdNoteId}/content`) + .auth(USER, token, { "type": "basic"}) + .set("Content-Type", "application/octet-stream") + .set("Content-Transfer-Encoding", "binary") + .send(Buffer.from("Hello world")) + .expect(204); + }); + + function getContentResponse() { + return supertest(app) + .get(`/etapi/notes/${createdNoteId}/content`) + .auth(USER, token, { "type": "basic"}) + .expect(200); + } +});