test(etapi): port get-inherited-attribute-cloned

This commit is contained in:
Elian Doran 2025-06-03 18:38:15 +03:00
parent 9d24ed902c
commit 3ec29b2e21
No known key found for this signature in database
3 changed files with 99 additions and 118 deletions

View File

@ -1,116 +0,0 @@
POST {{triliumHost}}/etapi/create-note
Authorization: {{authToken}}
Content-Type: application/json
{
"parentNoteId": "root",
"title": "Hello parent",
"type": "text",
"content": "Hi there!"
}
> {%
client.assert(response.status === 201);
client.global.set("parentNoteId", response.body.note.noteId);
client.global.set("parentBranchId", response.body.branch.branchId);
%}
### Create inheritable parent attribute
POST {{triliumHost}}/etapi/attributes
Authorization: {{authToken}}
Content-Type: application/json
{
"noteId": "{{parentNoteId}}",
"type": "label",
"name": "mylabel",
"value": "",
"isInheritable": true,
"position": 10
}
> {%
client.assert(response.status === 201);
client.global.set("parentAttributeId", response.body.attributeId);
%}
### Create child note under root
POST {{triliumHost}}/etapi/create-note
Authorization: {{authToken}}
Content-Type: application/json
{
"parentNoteId": "root",
"title": "Hello child",
"type": "text",
"content": "Hi there!"
}
> {%
client.assert(response.status === 201);
client.global.set("childNoteId", response.body.note.noteId);
client.global.set("childBranchId", response.body.branch.branchId);
%}
### Create child attribute
POST {{triliumHost}}/etapi/attributes
Authorization: {{authToken}}
Content-Type: application/json
{
"noteId": "{{childNoteId}}",
"type": "label",
"name": "mylabel",
"value": "val",
"isInheritable": false,
"position": 10
}
> {%
client.assert(response.status === 201);
client.global.set("childAttributeId", response.body.attributeId);
%}
### Clone child to parent
POST {{triliumHost}}/etapi/branches
Authorization: {{authToken}}
Content-Type: application/json
{
"noteId": "{{childNoteId}}",
"parentNoteId": "{{parentNoteId}}"
}
> {%
client.assert(response.status === 201);
client.assert(response.body.parentNoteId == client.global.get("parentNoteId"));
%}
###
GET {{triliumHost}}/etapi/notes/{{childNoteId}}
Authorization: {{authToken}}
> {%
function hasAttribute(list, attributeId) {
for (let i = 0; i < list.length; i++) {
if (list[i]["attributeId"] === attributeId) {
return true;
}
}
return false;
}
client.log(JSON.stringify(response.body.attributes));
client.assert(response.status === 200);
client.assert(response.body.noteId == client.global.get("childNoteId"));
client.assert(response.body.attributes.length == 2);
client.assert(hasAttribute(response.body.attributes, client.global.get("parentAttributeId")));
client.assert(hasAttribute(response.body.attributes, client.global.get("childAttributeId")));
%}

View File

@ -0,0 +1,98 @@
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;
let parentNoteId: string;
describe("etapi/get-inherited-attribute-cloned", () => {
beforeAll(async () => {
config.General.noAuthentication = false;
const buildApp = (await (import("../../src/app.js"))).default;
app = await buildApp();
token = await login(app);
parentNoteId = await createNote(app, token);
});
it("gets inherited attribute", async () => {
// Create an inheritable attribute on the parent note.
let response = await supertest(app)
.post("/etapi/attributes")
.auth("etapi", token, { "type": "basic"})
.send({
"noteId": parentNoteId,
"type": "label",
"name": "mylabel",
"value": "val",
"isInheritable": true,
"position": 10
})
.expect(201);
const parentAttributeId = response.body.attributeId;
expect(parentAttributeId).toBeTruthy();
// Create a subnote.
response = await supertest(app)
.post("/etapi/create-note")
.auth("etapi", token, { "type": "basic"})
.send({
"parentNoteId": parentNoteId,
"title": "Hello",
"type": "text",
"content": "Hi there!"
})
.expect(201);
const childNoteId = response.body.note.noteId;
// Create child attribute
response = await supertest(app)
.post("/etapi/attributes")
.auth("etapi", token, { "type": "basic"})
.send({
"noteId": childNoteId,
"type": "label",
"name": "mylabel",
"value": "val",
"isInheritable": false,
"position": 10
})
.expect(201);
const childAttributeId = response.body.attributeId;
expect(parentAttributeId).toBeTruthy();
// Clone child to parent
response = await supertest(app)
.post("/etapi/branches")
.auth("etapi", token, { "type": "basic"})
.send({
noteId: childNoteId,
parentNoteId: parentNoteId
})
.expect(200);
parentNoteId = response.body.parentNoteId;
// Check attribute IDs
response = await supertest(app)
.get(`/etapi/notes/${childNoteId}`)
.auth("etapi", token, { "type": "basic"})
.expect(200);
expect(response.body.noteId).toStrictEqual(childNoteId);
expect(response.body.attributes).toHaveLength(2);
expect(hasAttribute(response.body.attributes, parentAttributeId));
expect(hasAttribute(response.body.attributes, childAttributeId));
});
function hasAttribute(list: object[], attributeId: string) {
for (let i = 0; i < list.length; i++) {
if (list[i]["attributeId"] === attributeId) {
return true;
}
}
return false;
}
});

View File

@ -7,10 +7,9 @@ import config from "../../src/services/config.js";
let app: Application;
let token: string;
const USER = "etapi";
let parentNoteId: string;
describe("etapi/patch-note", () => {
describe("etapi/get-inherited-attribute", () => {
beforeAll(async () => {
config.General.noAuthentication = false;
const buildApp = (await (import("../../src/app.js"))).default;