diff --git a/_regroup/test-etapi/create-entities.http b/_regroup/test-etapi/create-entities.http deleted file mode 100644 index 98dae28b1..000000000 --- a/_regroup/test-etapi/create-entities.http +++ /dev/null @@ -1,158 +0,0 @@ -POST {{triliumHost}}/etapi/create-note -Authorization: {{authToken}} -Content-Type: application/json - -{ - "noteId": "forcedId{{$randomInt}}", - "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" -} - -> {% - client.assert(response.status === 201); - client.assert(response.body.note.noteId.startsWith("forcedId")); - client.assert(response.body.note.title == "Hello"); - client.assert(response.body.note.dateCreated == "2023-08-21 23:38:51.123+0200"); - client.assert(response.body.note.utcDateCreated == "2023-08-21 23:38:51.123Z"); - client.assert(response.body.branch.parentNoteId == "root"); - - client.log(`Created note ` + response.body.note.noteId + ` and branch ` + response.body.branch.branchId); - - client.global.set("createdNoteId", response.body.note.noteId); - client.global.set("createdBranchId", response.body.branch.branchId); -%} - -### Clone to another location - -POST {{triliumHost}}/etapi/branches -Authorization: {{authToken}} -Content-Type: application/json - -{ - "noteId": "{{createdNoteId}}", - "parentNoteId": "_hidden" -} - -> {% - client.assert(response.status === 201); - client.assert(response.body.parentNoteId == "_hidden"); - - client.global.set("clonedBranchId", response.body.branchId); - - client.log(`Created cloned branch ` + response.body.branchId); -%} - -### - -GET {{triliumHost}}/etapi/notes/{{createdNoteId}} -Authorization: {{authToken}} - -> {% - client.assert(response.status === 200); - client.assert(response.body.noteId == client.global.get("createdNoteId")); - client.assert(response.body.title == "Hello"); - // order is not defined and may fail in the future - client.assert(response.body.parentBranchIds[0] == client.global.get("clonedBranchId")) - client.assert(response.body.parentBranchIds[1] == client.global.get("createdBranchId")); -%} - -### - -GET {{triliumHost}}/etapi/notes/{{createdNoteId}}/content -Authorization: {{authToken}} - -> {% - client.assert(response.status === 200); - client.assert(response.body == "Hi there!"); -%} - -### - -GET {{triliumHost}}/etapi/branches/{{createdBranchId}} -Authorization: {{authToken}} - -> {% - client.assert(response.status === 200); - client.assert(response.body.branchId == client.global.get("createdBranchId")); - client.assert(response.body.parentNoteId == "root"); -%} - -### - -GET {{triliumHost}}/etapi/branches/{{clonedBranchId}} -Authorization: {{authToken}} - -> {% - client.assert(response.status === 200); - client.assert(response.body.branchId == client.global.get("clonedBranchId")); - client.assert(response.body.parentNoteId == "_hidden"); -%} - -### - -POST {{triliumHost}}/etapi/attributes -Content-Type: application/json -Authorization: {{authToken}} - -{ - "attributeId": "forcedAttributeId{{$randomInt}}", - "noteId": "{{createdNoteId}}", - "type": "label", - "name": "mylabel", - "value": "val", - "isInheritable": true -} - -> {% - client.assert(response.status === 201); - client.assert(response.body.attributeId.startsWith("forcedAttributeId")); - - client.global.set("createdAttributeId", response.body.attributeId); -%} - -### - -GET {{triliumHost}}/etapi/attributes/{{createdAttributeId}} -Authorization: {{authToken}} - -> {% - client.assert(response.status === 200); - client.assert(response.body.attributeId == client.global.get("createdAttributeId")); -%} - -### - -POST {{triliumHost}}/etapi/attachments -Content-Type: application/json -Authorization: {{authToken}} - -{ - "ownerId": "{{createdNoteId}}", - "role": "file", - "mime": "plain/text", - "title": "my attachment", - "content": "my text" -} - -> {% - client.assert(response.status === 201); - - client.global.set("createdAttachmentId", response.body.attachmentId); -%} - -### - -GET {{triliumHost}}/etapi/attachments/{{createdAttachmentId}} -Authorization: {{authToken}} - -> {% - client.assert(response.status === 200); - client.assert(response.body.attachmentId == client.global.get("createdAttachmentId")); - client.assert(response.body.role == "file"); - client.assert(response.body.mime == "plain/text"); - client.assert(response.body.title == "my attachment"); -%} diff --git a/apps/server/spec/etapi/create-entities.spec.ts b/apps/server/spec/etapi/create-entities.spec.ts new file mode 100644 index 000000000..25dab1d45 --- /dev/null +++ b/apps/server/spec/etapi/create-entities.spec.ts @@ -0,0 +1,178 @@ +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; +let clonedBranchId: string; +let createdAttributeId: string; +let createdAttachmentId: 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()); + clonedBranchId = await createClone(); + createdAttributeId = await createAttribute(); + createdAttachmentId = await createAttachment(); + }); + + it("returns note info", async () => { + const response = await supertest(app) + .get(`/etapi/notes/${createdNoteId}`) + .auth(USER, token, { "type": "basic"}) + .send({ + noteId: createdNoteId, + parentNoteId: "_hidden" + }) + .expect(200); + expect(response.body).toMatchObject({ + noteId: createdNoteId, + title: "Hello" + }); + expect(new Set(response.body.parentBranchIds)) + .toStrictEqual(new Set([ clonedBranchId, createdBranchId ])); + }); + + it("obtains note content", async () => { + await supertest(app) + .get(`/etapi/notes/${createdNoteId}/content`) + .auth(USER, token, { "type": "basic"}) + .expect(200) + .expect("Hi there!"); + }); + + it("obtains created branch information", async () => { + const response = await supertest(app) + .get(`/etapi/branches/${createdBranchId}`) + .auth(USER, token, { "type": "basic"}) + .expect(200); + expect(response.body).toMatchObject({ + branchId: createdBranchId, + parentNoteId: "root" + }); + }); + + it("obtains cloned branch information", async () => { + const response = await supertest(app) + .get(`/etapi/branches/${clonedBranchId}`) + .auth(USER, token, { "type": "basic"}) + .expect(200); + expect(response.body).toMatchObject({ + branchId: clonedBranchId, + parentNoteId: "_hidden" + }); + }); + + it("obtains attribute information", async () => { + const response = await supertest(app) + .get(`/etapi/attributes/${createdAttributeId}`) + .auth(USER, token, { "type": "basic"}) + .expect(200); + expect(response.body.attributeId).toStrictEqual(createdAttributeId); + }); + + it("obtains attachment information", async () => { + const response = await supertest(app) + .get(`/etapi/attachments/${createdAttachmentId}`) + .auth(USER, token, { "type": "basic"}) + .expect(200); + expect(response.body.attachmentId).toStrictEqual(createdAttachmentId); + expect(response.body).toMatchObject({ + role: "file", + mime: "plain/text", + title: "my attachment" + }); + }); +}); + +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); + expect(response.body).toMatchObject({ + note: { + noteId, + title: "Hello", + dateCreated: "2023-08-21 23:38:51.123+0200", + utcDateCreated: "2023-08-21 23:38:51.123Z" + }, + branch: { + parentNoteId: "root" + } + }); + + 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; +}