From fe19e0571543f97f3e8112a0ec989c6613cc5fda Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 3 Jun 2025 19:08:50 +0300 Subject: [PATCH] test(etapi): port delete-attachment --- _regroup/test-etapi/delete-attachment.http | 52 -------- .../server/spec/etapi/delete-entities.spec.ts | 117 ++++++++++++++++++ 2 files changed, 117 insertions(+), 52 deletions(-) delete mode 100644 _regroup/test-etapi/delete-attachment.http create mode 100644 apps/server/spec/etapi/delete-entities.spec.ts diff --git a/_regroup/test-etapi/delete-attachment.http b/_regroup/test-etapi/delete-attachment.http deleted file mode 100644 index d12e8de43..000000000 --- a/_regroup/test-etapi/delete-attachment.http +++ /dev/null @@ -1,52 +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); %} - -### - -DELETE {{triliumHost}}/etapi/attachments/{{createdAttachmentId}} -Authorization: {{authToken}} - -> {% client.assert(response.status === 204, "Response status is not 204"); %} - -### repeat the DELETE request to test the idempotency - -DELETE {{triliumHost}}/etapi/attachments/{{createdAttachmentId}} -Authorization: {{authToken}} - -> {% client.assert(response.status === 204, "Response status is not 204"); %} - -### - -GET {{triliumHost}}/etapi/attachments/{{createdAttachmentId}} -Authorization: {{authToken}} - -> {% - client.assert(response.status === 404, "Response status is not 404"); - client.assert(response.body.code === "ATTACHMENT_NOT_FOUND"); -%} diff --git a/apps/server/spec/etapi/delete-entities.spec.ts b/apps/server/spec/etapi/delete-entities.spec.ts new file mode 100644 index 000000000..0a2bb0bee --- /dev/null +++ b/apps/server/spec/etapi/delete-entities.spec.ts @@ -0,0 +1,117 @@ +import { Application } from "express"; +import { beforeAll, describe, expect, it } from "vitest"; +import supertest from "supertest"; +import { login } from "./utils.js"; +import config from "../../src/services/config.js"; +import { randomInt } from "crypto"; + +let app: Application; +let token: string; +let createdNoteId: string; +let createdBranchId: string; + +const USER = "etapi"; + +describe("etapi/create-entities", () => { + 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()); + }); + + it("deletes attachemnt", async () => { + const attachmentId = await createAttachment(); + + // Delete the attachment + deleteEntity("attachments", attachmentId); + + // Ensure the attachment can no longer be found. + const response = await supertest(app) + .get(`/etapi/attachments/${attachmentId}`) + .auth(USER, token, { "type": "basic"}) + .expect(404); + expect(response.body.code).toStrictEqual("ATTACHMENT_NOT_FOUND"); + }); +}); + +async function createNote() { + const noteId = `forcedId${randomInt(1000)}`; + const response = await supertest(app) + .post("/etapi/create-note") + .auth(USER, token, { "type": "basic"}) + .send({ + "noteId": noteId, + "parentNoteId": "root", + "title": "Hello", + "type": "text", + "content": "Hi there!", + "dateCreated": "2023-08-21 23:38:51.123+0200", + "utcDateCreated": "2023-08-21 23:38:51.123Z" + }) + .expect(201); + expect(response.body.note.noteId).toStrictEqual(noteId); + + return { + createdNoteId: response.body.note.noteId, + createdBranchId: response.body.branch.branchId + }; +} + +async function createClone() { + const response = await supertest(app) + .post("/etapi/branches") + .auth(USER, token, { "type": "basic"}) + .send({ + noteId: createdNoteId, + parentNoteId: "_hidden" + }) + .expect(201); + expect(response.body.parentNoteId).toStrictEqual("_hidden"); + return response.body.branchId; +} + +async function createAttribute() { + const attributeId = `forcedId${randomInt(1000)}`; + const response = await supertest(app) + .post("/etapi/attributes") + .auth(USER, token, { "type": "basic"}) + .send({ + "attributeId": attributeId, + "noteId": createdNoteId, + "type": "label", + "name": "mylabel", + "value": "val", + "isInheritable": true + }) + .expect(201); + expect(response.body.attributeId).toStrictEqual(attributeId); + return response.body.attributeId; +} + +async function createAttachment() { + const response = await supertest(app) + .post("/etapi/attachments") + .auth(USER, token, { "type": "basic"}) + .send({ + "ownerId": createdNoteId, + "role": "file", + "mime": "plain/text", + "title": "my attachment", + "content": "my text" + }) + .expect(201); + return response.body.attachmentId; +} + +async function deleteEntity(entity: "attachments", id: string) { + // Delete twice to test idempotency. + for (let i=0; i < 2; i++) { + await supertest(app) + .delete(`/etapi/${entity}/${id}`) + .auth(USER, token, { "type": "basic"}) + .expect(204); + } +}