From 5528ab7cc8882b64a4bc1bd98a6c53804997eb89 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Mon, 7 Jun 2021 15:46:35 +0200 Subject: [PATCH] Tools: Fixed tests --- packages/lib/JoplinServerApi.ts | 6 +++-- packages/lib/Synchronizer.ts | 1 + packages/lib/file-api-driver-joplinServer.ts | 8 ++++++- .../synchronizer/Synchronizer.basics.test.ts | 3 +-- packages/lib/testing/test-utils.ts | 22 ++++++++++++++----- packages/server/src/app.ts | 9 -------- 6 files changed, 29 insertions(+), 20 deletions(-) diff --git a/packages/lib/JoplinServerApi.ts b/packages/lib/JoplinServerApi.ts index 01e0962a4..6016183e9 100644 --- a/packages/lib/JoplinServerApi.ts +++ b/packages/lib/JoplinServerApi.ts @@ -71,7 +71,7 @@ export default class JoplinServerApi { return this.session_; } catch (error) { - logger.error('Could not acquire session:', error); + logger.error('Could not acquire session:', error.details, '\n', error); throw error; } } @@ -146,6 +146,8 @@ export default class JoplinServerApi { url += stringify(query); } + const startTime = Date.now(); + try { if (this.debugRequests_) { logger.debug(this.requestToCurl_(url, fetchOptions)); @@ -170,7 +172,7 @@ export default class JoplinServerApi { const responseText = await response.text(); if (this.debugRequests_) { - logger.debug('Response', options.responseFormat, responseText); + logger.debug('Response', Date.now() - startTime, options.responseFormat, responseText); } // Creates an error object with as much data as possible as it will appear in the log, which will make debugging easier diff --git a/packages/lib/Synchronizer.ts b/packages/lib/Synchronizer.ts index 0f631863f..b6317acf3 100644 --- a/packages/lib/Synchronizer.ts +++ b/packages/lib/Synchronizer.ts @@ -926,6 +926,7 @@ export default class Synchronizer { this.logger().error(error); } else { this.logger().error(error); + if (error.details) this.logger().error('Details:', error.details); // Don't save to the report errors that are due to things like temporary network errors or timeout. if (!shim.fetchRequestCanBeRetried(error)) { diff --git a/packages/lib/file-api-driver-joplinServer.ts b/packages/lib/file-api-driver-joplinServer.ts index 3f8829bcb..5d3917787 100644 --- a/packages/lib/file-api-driver-joplinServer.ts +++ b/packages/lib/file-api-driver-joplinServer.ts @@ -183,6 +183,12 @@ export default class FileApiDriverJoplinServer { } public async clearRoot(path: string) { - await this.delete(path); + const response = await this.list(path); + + for (const item of response.items) { + await this.delete(item.path); + } + + if (response.has_more) throw new Error('has_more support not implemented'); } } diff --git a/packages/lib/services/synchronizer/Synchronizer.basics.test.ts b/packages/lib/services/synchronizer/Synchronizer.basics.test.ts index ea1d7d94e..e30a795cd 100644 --- a/packages/lib/services/synchronizer/Synchronizer.basics.test.ts +++ b/packages/lib/services/synchronizer/Synchronizer.basics.test.ts @@ -1,7 +1,6 @@ import Setting from '../../models/Setting'; import { allNotesFolders, remoteNotesAndFolders, localNotesFoldersSameAsRemote } from '../../testing/test-utils-synchronizer'; - -const { syncTargetName, afterAllCleanUp, synchronizerStart, setupDatabaseAndSynchronizer, synchronizer, sleep, switchClient, syncTargetId, fileApi } = require('../../testing/test-utils.js'); +import { syncTargetName, afterAllCleanUp, synchronizerStart, setupDatabaseAndSynchronizer, synchronizer, sleep, switchClient, syncTargetId, fileApi } from '../../testing/test-utils'; import Folder from '../../models/Folder'; import Note from '../../models/Note'; import BaseItem from '../../models/BaseItem'; diff --git a/packages/lib/testing/test-utils.ts b/packages/lib/testing/test-utils.ts index 26e88eac3..41e2f0ba9 100644 --- a/packages/lib/testing/test-utils.ts +++ b/packages/lib/testing/test-utils.ts @@ -50,7 +50,7 @@ const WebDavApi = require('../WebDavApi'); const DropboxApi = require('../DropboxApi'); import JoplinServerApi from '../JoplinServerApi'; import { FolderEntity } from '../services/database/types'; -import { credentialFile } from '../utils/credentialFiles'; +import { credentialFile, readCredentialFile } from '../utils/credentialFiles'; import SyncTargetJoplinCloud from '../SyncTargetJoplinCloud'; const { loadKeychainServiceAndSettings } = require('../services/SettingUtils'); const md5 = require('md5'); @@ -435,7 +435,8 @@ async function synchronizerStart(id: number = null, extraOptions: any = null) { if (id === null) id = currentClient_; const contextKey = `sync.${syncTargetId()}.context`; - const context = Setting.value(contextKey); + const contextString = Setting.value(contextKey); + const context = contextString ? JSON.parse(contextString) : {}; const options = Object.assign({}, extraOptions); if (context) options.context = context; @@ -571,14 +572,23 @@ async function initFileApi() { } else if (syncTargetId_ == SyncTargetRegistry.nameToId('joplinServer')) { mustRunInBand(); + const joplinServerAuth = JSON.parse(await readCredentialFile('joplin-server-test-units-2.json')); + + // const joplinServerAuth = { + // "email": "admin@localhost", + // "password": "admin", + // "baseUrl": "http://api-joplincloud.local:22300", + // "userContentBaseUrl": "" + // } + // Note that to test the API in parallel mode, you need to use Postgres // as database, as the SQLite database is not reliable when being // read/write from multiple processes at the same time. const api = new JoplinServerApi({ - baseUrl: () => 'http://localhost:22300', - userContentBaseUrl: () => '', - username: () => 'admin@localhost', - password: () => 'admin', + baseUrl: () => joplinServerAuth.baseUrl, + userContentBaseUrl: () => joplinServerAuth.userContentBaseUrl, + username: () => joplinServerAuth.email, + password: () => joplinServerAuth.password, }); fileApi = new FileApi('', new FileApiDriverJoplinServer(api)); diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 2dec75abf..83aa4f5cf 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -117,15 +117,6 @@ async function main() { // we can't return void, so let's return one of the valid domains return corsAllowedDomains[0]; } - - // const requestOrigin = ctx.request.header.origin; - - // if (hostname === envVariables.USER_CONTENT_BASE_URL) return - - // if (corsAllowedDomains.indexOf(requestOrigin) === 0) { - // return requestOrigin; - // } - }, })); app.use(ownerHandler);