Skip to content

Build your personal knowledge base with Trilium Notes

test(etapi): port attachment content

Elian Dorandddbb9d4d1d8
diff --git a/_regroup/test-etapi/put-attachment-content-binary.http b/_regroup/test-etapi/put-attachment-content-binary.http
deleted file mode 100644
index 6e6d6dad35..0000000000
--- a/_regroup/test-etapi/put-attachment-content-binary.http
+++ /dev/null
@@ -1,39 +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); %}
-
-###
-
-POST {{triliumHost}}/etapi/attachments
-Authorization: {{authToken}}
-Content-Type: application/json
-
-{
- "ownerId": "{{createdNoteId}}",
- "role": "file",
- "mime": "text/plain",
- "title": "my attachment",
- "content": "text"
-}
-
-> {% client.global.set("createdAttachmentId", response.body.attachmentId); %}
-
-###
-
-PUT {{triliumHost}}/etapi/attachments/{{createdAttachmentId}}/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-attachment-content.http b/_regroup/test-etapi/put-attachment-content.http
deleted file mode 100644
index 57e96a4b98..0000000000
--- a/_regroup/test-etapi/put-attachment-content.http
+++ /dev/null
@@ -1,45 +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); %}
-
-###
-
-POST {{triliumHost}}/etapi/attachments
-Authorization: {{authToken}}
-Content-Type: application/json
-
-{
- "ownerId": "{{createdNoteId}}",
- "role": "file",
- "mime": "text/plain",
- "title": "my attachment",
- "content": "text"
-}
-
-> {% client.global.set("createdAttachmentId", response.body.attachmentId); %}
-
-###
-
-PUT {{triliumHost}}/etapi/attachments/{{createdAttachmentId}}/content
-Authorization: {{authToken}}
-Content-Type: text/plain
-
-Changed content
-
-> {% client.assert(response.status === 204); %}
-
-###
-
-GET {{triliumHost}}/etapi/attachments/{{createdAttachmentId}}/content
-Authorization: {{authToken}}
-
-> {% client.assert(response.body === "Changed content"); %}
diff --git a/apps/server/spec/etapi/attachment-content.spec.ts b/apps/server/spec/etapi/attachment-content.spec.ts
new file mode 100644
index 0000000000..12c90c1551
--- /dev/null
+++ b/apps/server/spec/etapi/attachment-content.spec.ts
@@ -0,0 +1,64 @@
+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";
+
+let app: Application;
+let token: string;
+
+const USER = "etapi";
+let createdNoteId: string;
+let createdAttachmentId: string;
+
+describe("etapi/attachment-content", () => {
+ beforeAll(async () => {
+ config.General.noAuthentication = false;
+ const buildApp = (await (import("../../src/app.js"))).default;
+ app = await buildApp();
+ token = await login(app);
+
+ createdNoteId = await createNote(app, token);
+
+ // Create an attachment
+ const response = await supertest(app)
+ .post(`/etapi/attachments`)
+ .auth(USER, token, { "type": "basic"})
+ .send({
+ "ownerId": createdNoteId,
+ "role": "file",
+ "mime": "text/plain",
+ "title": "my attachment",
+ "content": "text"
+ });
+ createdAttachmentId = response.body.attachmentId;
+ expect(createdAttachmentId).toBeTruthy();
+ });
+
+ it("changes attachment content", async () => {
+ const text = "Changed content";
+ await supertest(app)
+ .put(`/etapi/attachments/${createdAttachmentId}/content`)
+ .auth(USER, token, { "type": "basic"})
+ .set("Content-Type", "text/plain")
+ .send(text)
+ .expect(204);
+
+ // Ensure it got changed.
+ const response = await supertest(app)
+ .get(`/etapi/attachments/${createdAttachmentId}/content`)
+ .auth(USER, token, { "type": "basic"});
+ expect(response.text).toStrictEqual(text);
+ });
+
+ it("supports binary content", async() => {
+ await supertest(app)
+ .put(`/etapi/attachments/${createdAttachmentId}/content`)
+ .auth(USER, token, { "type": "basic"})
+ .set("Content-Type", "application/octet-stream")
+ .set("Content-Transfer-Encoding", "binary")
+ .send(Buffer.from("Hello world"))
+ .expect(204);
+ });
+
+});
diff --git a/apps/server/spec/etapi/note-content.spec.ts b/apps/server/spec/etapi/note-content.spec.ts
index cca3e3f81c..5b7fdcba82 100644
--- a/apps/server/spec/etapi/note-content.spec.ts
+++ b/apps/server/spec/etapi/note-content.spec.ts
@@ -3,14 +3,12 @@ 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 () => {
@@ -19,7 +17,7 @@ describe("etapi/note-content", () => {
app = await buildApp();
token = await login(app);
- ({ createdNoteId, createdBranchId } = await createNote(app, token));
+ createdNoteId = await createNote(app, token);
});
it("get content", async () => {
diff --git a/apps/server/spec/etapi/utils.ts b/apps/server/spec/etapi/utils.ts
index 5096983701..140c6820d6 100644
--- a/apps/server/spec/etapi/utils.ts
+++ b/apps/server/spec/etapi/utils.ts
@@ -27,7 +27,7 @@ export async function createNote(app: Application, token: string, content?: stri
})
.expect(201);
- const noteId = response.body.note.noteId;
+ const noteId = response.body.note.noteId as string;
expect(noteId).toStrictEqual(noteId);
return noteId;
}