From cde25fad92323406b2299f015cd4fc618892df45 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 25 May 2021 17:50:51 +0200 Subject: [PATCH] Fixed tests and server build --- .eslintignore | 3 ++ .gitignore | 3 ++ .../tests/services/plugins/PluginService.ts | 2 +- .../services/plugins/api/JoplinSettings.ts | 8 ++-- .../plugins/api/JoplinViewMenuItem.ts | 6 +-- .../services/plugins/api/JoplinWorkspace.ts | 3 +- .../tests/services/plugins/sandboxProxy.ts | 3 +- packages/app-cli/tests/testUtils.ts | 41 ++++++++++++++++++ packages/lib/testing/test-utils.ts | 43 +------------------ packages/server/src/utils/routeUtils.ts | 2 +- 10 files changed, 60 insertions(+), 54 deletions(-) create mode 100644 packages/app-cli/tests/testUtils.ts diff --git a/.eslintignore b/.eslintignore index ee30a8786..1e9949e11 100644 --- a/.eslintignore +++ b/.eslintignore @@ -104,6 +104,9 @@ packages/app-cli/tests/services/plugins/api/JoplinWorkspace.js.map packages/app-cli/tests/services/plugins/sandboxProxy.d.ts packages/app-cli/tests/services/plugins/sandboxProxy.js packages/app-cli/tests/services/plugins/sandboxProxy.js.map +packages/app-cli/tests/testUtils.d.ts +packages/app-cli/tests/testUtils.js +packages/app-cli/tests/testUtils.js.map packages/app-desktop/ElectronAppWrapper.d.ts packages/app-desktop/ElectronAppWrapper.js packages/app-desktop/ElectronAppWrapper.js.map diff --git a/.gitignore b/.gitignore index d4c5f7920..d070efdee 100644 --- a/.gitignore +++ b/.gitignore @@ -90,6 +90,9 @@ packages/app-cli/tests/services/plugins/api/JoplinWorkspace.js.map packages/app-cli/tests/services/plugins/sandboxProxy.d.ts packages/app-cli/tests/services/plugins/sandboxProxy.js packages/app-cli/tests/services/plugins/sandboxProxy.js.map +packages/app-cli/tests/testUtils.d.ts +packages/app-cli/tests/testUtils.js +packages/app-cli/tests/testUtils.js.map packages/app-desktop/ElectronAppWrapper.d.ts packages/app-desktop/ElectronAppWrapper.js packages/app-desktop/ElectronAppWrapper.js.map diff --git a/packages/app-cli/tests/services/plugins/PluginService.ts b/packages/app-cli/tests/services/plugins/PluginService.ts index 51420eb17..2cf205515 100644 --- a/packages/app-cli/tests/services/plugins/PluginService.ts +++ b/packages/app-cli/tests/services/plugins/PluginService.ts @@ -7,8 +7,8 @@ import Setting from '@joplin/lib/models/Setting'; import * as fs from 'fs-extra'; import Note from '@joplin/lib/models/Note'; import Folder from '@joplin/lib/models/Folder'; -import { newPluginScript } from '@joplin/lib/testing/test-utils'; import { expectNotThrow, setupDatabaseAndSynchronizer, switchClient, expectThrow, createTempDir, supportDir } from '@joplin/lib/testing/test-utils'; +import { newPluginScript } from '../../testUtils'; const testPluginDir = `${supportDir}/plugins`; diff --git a/packages/app-cli/tests/services/plugins/api/JoplinSettings.ts b/packages/app-cli/tests/services/plugins/api/JoplinSettings.ts index 428a100f7..897662bac 100644 --- a/packages/app-cli/tests/services/plugins/api/JoplinSettings.ts +++ b/packages/app-cli/tests/services/plugins/api/JoplinSettings.ts @@ -1,7 +1,7 @@ import Setting from '@joplin/lib/models/Setting'; -import PluginService from '@joplin/lib/services/plugins/PluginService'; -const { waitForFolderCount, newPluginService, newPluginScript, setupDatabaseAndSynchronizer, switchClient, afterEachCleanUp } = require('@joplin/lib/testing/test-utils'); +import { waitForFolderCount, setupDatabaseAndSynchronizer, switchClient, afterEachCleanUp } from '@joplin/lib/testing/test-utils'; import Folder from '@joplin/lib/models/Folder'; +import { newPluginScript, newPluginService } from '../../../testUtils'; describe('JoplinSettings', () => { @@ -16,7 +16,7 @@ describe('JoplinSettings', () => { }); test('should listen to setting change event', async () => { - const service = new newPluginService() as PluginService; + const service = newPluginService(); const pluginScript = newPluginScript(` joplin.plugins.register({ @@ -68,7 +68,7 @@ describe('JoplinSettings', () => { }); test('should allow registering multiple settings', async () => { - const service = new newPluginService() as PluginService; + const service = newPluginService(); const pluginScript = newPluginScript(` joplin.plugins.register({ diff --git a/packages/app-cli/tests/services/plugins/api/JoplinViewMenuItem.ts b/packages/app-cli/tests/services/plugins/api/JoplinViewMenuItem.ts index ba6ef85c7..8a6def746 100644 --- a/packages/app-cli/tests/services/plugins/api/JoplinViewMenuItem.ts +++ b/packages/app-cli/tests/services/plugins/api/JoplinViewMenuItem.ts @@ -1,6 +1,6 @@ import KeymapService from '@joplin/lib/services/KeymapService'; -import PluginService from '@joplin/lib/services/plugins/PluginService'; -const { newPluginService, newPluginScript, setupDatabaseAndSynchronizer, switchClient, afterEachCleanUp } = require('@joplin/lib/testing/test-utils'); +import { setupDatabaseAndSynchronizer, switchClient, afterEachCleanUp } from '@joplin/lib/testing/test-utils'; +import { newPluginScript, newPluginService } from '../../../testUtils'; describe('JoplinViewMenuItem', () => { @@ -15,7 +15,7 @@ describe('JoplinViewMenuItem', () => { }); test('should register commands with the keymap service', async () => { - const service = new newPluginService() as PluginService; + const service = newPluginService(); KeymapService.instance().initialize(); diff --git a/packages/app-cli/tests/services/plugins/api/JoplinWorkspace.ts b/packages/app-cli/tests/services/plugins/api/JoplinWorkspace.ts index c9f2944e0..4bcf53083 100644 --- a/packages/app-cli/tests/services/plugins/api/JoplinWorkspace.ts +++ b/packages/app-cli/tests/services/plugins/api/JoplinWorkspace.ts @@ -1,8 +1,9 @@ import Setting from '@joplin/lib/models/Setting'; -import { newPluginService, newPluginScript, setupDatabaseAndSynchronizer, switchClient, afterEachCleanUp } from '@joplin/lib/testing/test-utils'; +import { setupDatabaseAndSynchronizer, switchClient, afterEachCleanUp } from '@joplin/lib/testing/test-utils'; import Note from '@joplin/lib/models/Note'; import Folder from '@joplin/lib/models/Folder'; import ItemChange from '@joplin/lib/models/ItemChange'; +import { newPluginScript, newPluginService } from '../../../testUtils'; describe('JoplinWorkspace', () => { diff --git a/packages/app-cli/tests/services/plugins/sandboxProxy.ts b/packages/app-cli/tests/services/plugins/sandboxProxy.ts index eddba67b5..98d952ace 100644 --- a/packages/app-cli/tests/services/plugins/sandboxProxy.ts +++ b/packages/app-cli/tests/services/plugins/sandboxProxy.ts @@ -1,6 +1,5 @@ import sandboxProxy, { Target } from '@joplin/lib/services/plugins/sandboxProxy'; - -const { setupDatabaseAndSynchronizer, switchClient } = require('@joplin/lib/testing/test-utils.js'); +import { setupDatabaseAndSynchronizer, switchClient } from '@joplin/lib/testing/test-utils'; describe('services_plugins_sandboxProxy', function() { diff --git a/packages/app-cli/tests/testUtils.ts b/packages/app-cli/tests/testUtils.ts new file mode 100644 index 000000000..f7ffab05e --- /dev/null +++ b/packages/app-cli/tests/testUtils.ts @@ -0,0 +1,41 @@ +import PluginService from '@joplin/lib/services/plugins/PluginService'; +import PluginRunner from '../app/services/plugins/PluginRunner'; + +export interface PluginServiceOptions { + getState?(): Record; +} + +export function newPluginService(appVersion = '1.4', options: PluginServiceOptions = null): PluginService { + options = options || {}; + + const runner = new PluginRunner(); + const service = new PluginService(); + service.initialize( + appVersion, + { + joplin: {}, + }, + runner, + { + dispatch: () => {}, + getState: options.getState ? options.getState : () => {}, + } + ); + return service; +} + +export function newPluginScript(script: string) { + return ` + /* joplin-manifest: + { + "id": "org.joplinapp.plugins.PluginTest", + "manifest_version": 1, + "app_min_version": "1.4", + "name": "JS Bundle test", + "version": "1.0.0" + } + */ + + ${script} + `; +} diff --git a/packages/lib/testing/test-utils.ts b/packages/lib/testing/test-utils.ts index f99f9e83f..e8a82a8ea 100644 --- a/packages/lib/testing/test-utils.ts +++ b/packages/lib/testing/test-utils.ts @@ -13,8 +13,6 @@ import KeymapService from '../services/KeymapService'; import KvStore from '../services/KvStore'; import KeychainServiceDriver from '../services/keychain/KeychainServiceDriver.node'; import KeychainServiceDriverDummy from '../services/keychain/KeychainServiceDriver.dummy'; -import PluginRunner from '../../app-cli/app/services/plugins/PluginRunner'; -import PluginService from '../services/plugins/PluginService'; import FileApiDriverJoplinServer from '../file-api-driver-joplinServer'; import OneDriveApi from '../onedrive-api'; import SyncTargetOneDrive from '../SyncTargetOneDrive'; @@ -766,45 +764,6 @@ async function createTempDir() { return tempDirPath; } -interface PluginServiceOptions { - getState?(): Record; -} - -function newPluginService(appVersion = '1.4', options: PluginServiceOptions = null): PluginService { - options = options || {}; - - const runner = new PluginRunner(); - const service = new PluginService(); - service.initialize( - appVersion, - { - joplin: {}, - }, - runner, - { - dispatch: () => {}, - getState: options.getState ? options.getState : () => {}, - } - ); - return service; -} - -function newPluginScript(script: string) { - return ` - /* joplin-manifest: - { - "id": "org.joplinapp.plugins.PluginTest", - "manifest_version": 1, - "app_min_version": "1.4", - "name": "JS Bundle test", - "version": "1.0.0" - } - */ - - ${script} - `; -} - async function waitForFolderCount(count: number) { const timeout = 2000; const startTime = Date.now(); @@ -901,4 +860,4 @@ class TestApp extends BaseApplication { } } -export { supportDir, waitForFolderCount, afterAllCleanUp, exportDir, newPluginService, newPluginScript, synchronizerStart, afterEachCleanUp, syncTargetName, setSyncTargetName, syncDir, createTempDir, isNetworkSyncTarget, kvStore, expectThrow, logger, expectNotThrow, resourceService, resourceFetcher, tempFilePath, allSyncTargetItemsEncrypted, msleep, setupDatabase, revisionService, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync, checkThrow, encryptionService, loadEncryptionMasterKey, fileContentEqual, decryptionWorker, currentClientId, id, ids, sortedIds, at, createNTestNotes, createNTestFolders, createNTestTags, TestApp }; +export { supportDir, waitForFolderCount, afterAllCleanUp, exportDir, synchronizerStart, afterEachCleanUp, syncTargetName, setSyncTargetName, syncDir, createTempDir, isNetworkSyncTarget, kvStore, expectThrow, logger, expectNotThrow, resourceService, resourceFetcher, tempFilePath, allSyncTargetItemsEncrypted, msleep, setupDatabase, revisionService, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync, checkThrow, encryptionService, loadEncryptionMasterKey, fileContentEqual, decryptionWorker, currentClientId, id, ids, sortedIds, at, createNTestNotes, createNTestFolders, createNTestTags, TestApp }; diff --git a/packages/server/src/utils/routeUtils.ts b/packages/server/src/utils/routeUtils.ts index d15378f76..5a856f5e2 100644 --- a/packages/server/src/utils/routeUtils.ts +++ b/packages/server/src/utils/routeUtils.ts @@ -168,7 +168,7 @@ export async function execRequest(routes: Routers, ctx: AppContext) { if (!match) throw new ErrorNotFound(); const endPoint = match.route.findEndPoint(ctx.request.method as HttpMethod, match.subPath.schema); - if (ctx.URL.origin !== baseUrl(endPoint.type)) throw new ErrorNotFound('Invalid origin', 'invalidOrigin'); + if (ctx.URL && ctx.URL.origin !== baseUrl(endPoint.type)) throw new ErrorNotFound('Invalid origin', 'invalidOrigin'); // This is a generic catch-all for all private end points - if we // couldn't get a valid session, we exit now. Individual end points