test(etapi): port attachment content

This commit is contained in:
Elian Doran 2025-06-03 08:54:52 +03:00
parent d75e86789d
commit dddbb9d4d1
No known key found for this signature in database
5 changed files with 66 additions and 88 deletions

View File

@ -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); %}

View File

@ -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"); %}

View File

@ -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);
});
});

View File

@ -3,14 +3,12 @@ import { beforeAll, describe, expect, it } from "vitest";
import supertest from "supertest"; import supertest from "supertest";
import { createNote, login } from "./utils.js"; import { createNote, login } from "./utils.js";
import config from "../../src/services/config.js"; import config from "../../src/services/config.js";
import { randomUUID } from "crypto";
let app: Application; let app: Application;
let token: string; let token: string;
const USER = "etapi"; const USER = "etapi";
let createdNoteId: string; let createdNoteId: string;
let createdBranchId: string;
describe("etapi/note-content", () => { describe("etapi/note-content", () => {
beforeAll(async () => { beforeAll(async () => {
@ -19,7 +17,7 @@ describe("etapi/note-content", () => {
app = await buildApp(); app = await buildApp();
token = await login(app); token = await login(app);
({ createdNoteId, createdBranchId } = await createNote(app, token)); createdNoteId = await createNote(app, token);
}); });
it("get content", async () => { it("get content", async () => {

View File

@ -27,7 +27,7 @@ export async function createNote(app: Application, token: string, content?: stri
}) })
.expect(201); .expect(201);
const noteId = response.body.note.noteId; const noteId = response.body.note.noteId as string;
expect(noteId).toStrictEqual(noteId); expect(noteId).toStrictEqual(noteId);
return noteId; return noteId;
} }