-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
etapi test infrastructure plus a basic note creation test
- Loading branch information
Showing
5 changed files
with
173 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,6 @@ cert.crt | |
server-package.json | ||
.idea/httpRequests/ | ||
data/ | ||
data-test/ | ||
tmp/ | ||
.eslintcache | ||
.eslintcache |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const {describeEtapi, postEtapi, getEtapi, getEtapiContent} = require("../support/etapi"); | ||
|
||
describeEtapi("notes", () => { | ||
it("create", async () => { | ||
const {note, branch} = await postEtapi('create-note', { | ||
parentNoteId: 'root', | ||
type: 'text', | ||
title: 'Hello World!', | ||
content: 'Content', | ||
prefix: 'Custom prefix' | ||
}); | ||
|
||
expect(note.title).toEqual("Hello World!"); | ||
expect(branch.parentNoteId).toEqual("root"); | ||
expect(branch.prefix).toEqual("Custom prefix"); | ||
|
||
const rNote = await getEtapi(`notes/${note.noteId}`); | ||
expect(rNote.title).toEqual("Hello World!"); | ||
|
||
const rContent = await getEtapiContent(`notes/${note.noteId}/content`); | ||
expect(rContent).toEqual("Content"); | ||
|
||
const rBranch = await getEtapi(`branches/${branch.branchId}`); | ||
expect(rBranch.parentNoteId).toEqual("root"); | ||
expect(rBranch.prefix).toEqual("Custom prefix"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
const {spawn} = require("child_process"); | ||
const kill = require('tree-kill'); | ||
|
||
let etapiAuthToken; | ||
|
||
const getEtapiAuthorizationHeader = () => "Basic " + Buffer.from(`etapi:${etapiAuthToken}`).toString('base64'); | ||
|
||
const PORT = '9999'; | ||
const HOST = 'http://localhost:' + PORT; | ||
|
||
function describeEtapi(description, specDefinitions) { | ||
describe(description, () => { | ||
let appProcess; | ||
|
||
beforeAll(async () => { | ||
appProcess = spawn('npm', ['run', 'start-test-server']); | ||
|
||
await new Promise(res => { | ||
appProcess.stdout.on('data', data => { | ||
console.log("Trilium: " + data.toString().trim()); | ||
|
||
if (data.toString().includes('Listening on port')) { | ||
res(); | ||
} | ||
}); | ||
}); | ||
|
||
await fetch(HOST + '/api/setup/new-document', { method: 'POST' }); | ||
|
||
const formData = new URLSearchParams(); | ||
formData.append('password1', '1234'); | ||
formData.append('password2', '1234'); | ||
|
||
await fetch(HOST + '/set-password', { method: 'POST', body: formData }); | ||
|
||
etapiAuthToken = (await (await fetch(HOST + '/etapi/auth/login', { | ||
method: 'POST', | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ password: '1234' }) | ||
})).json()).authToken; | ||
}); | ||
|
||
afterAll(() => { | ||
console.log("Attempting to kill the Trilium process as part of the cleanup..."); | ||
kill(appProcess.pid, 'SIGKILL', () => { console.log("Trilium process killed.") }); | ||
}); | ||
|
||
specDefinitions(); | ||
}); | ||
} | ||
|
||
async function getEtapi(url) { | ||
const response = await fetch(`${HOST}/etapi/${url}`, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: getEtapiAuthorizationHeader() | ||
} | ||
}); | ||
return await processEtapiResponse(response); | ||
} | ||
|
||
async function getEtapiContent(url) { | ||
const response = await fetch(`${HOST}/etapi/${url}`, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: getEtapiAuthorizationHeader() | ||
} | ||
}); | ||
return await response.text(); | ||
} | ||
|
||
async function postEtapi(url, data = {}) { | ||
const response = await fetch(`${HOST}/etapi/${url}`, { | ||
method: 'POST', | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: getEtapiAuthorizationHeader() | ||
}, | ||
body: JSON.stringify(data) | ||
}); | ||
return await processEtapiResponse(response); | ||
} | ||
|
||
async function putEtapi(url, data = {}) { | ||
const response = await fetch(`${HOST}/etapi/${url}`, { | ||
method: 'PUT', | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: getEtapiAuthorizationHeader() | ||
}, | ||
body: JSON.stringify(data) | ||
}); | ||
return await processEtapiResponse(response); | ||
} | ||
|
||
async function deleteEtapi(url) { | ||
const response = await fetch(`${HOST}/etapi/${url}`, { | ||
method: 'DELETE', | ||
headers: { | ||
Authorization: getEtapiAuthorizationHeader() | ||
} | ||
}); | ||
return await processEtapiResponse(response); | ||
} | ||
|
||
async function processEtapiResponse(response) { | ||
const json = await response.json(); | ||
|
||
if (response.status < 200 || response.status >= 300) { | ||
throw new Error("ETAPI error: " + JSON.stringify(json)); | ||
} | ||
|
||
return json; | ||
} | ||
|
||
module.exports = { | ||
describeEtapi, | ||
getEtapi, | ||
getEtapiContent, | ||
postEtapi, | ||
putEtapi, | ||
deleteEtapi | ||
}; |