2019-07-30 09:35:42 +02:00
|
|
|
/* eslint-disable require-atomic-updates */
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
const fs = require('fs-extra');
|
2020-11-07 17:59:37 +02:00
|
|
|
const { JoplinDatabase } = require('@joplin/lib/joplin-database.js');
|
|
|
|
const { DatabaseDriverNode } = require('@joplin/lib/database-driver-node.js');
|
|
|
|
const BaseApplication = require('@joplin/lib/BaseApplication').default;
|
|
|
|
const BaseModel = require('@joplin/lib/BaseModel').default;
|
|
|
|
const Folder = require('@joplin/lib/models/Folder.js');
|
|
|
|
const Note = require('@joplin/lib/models/Note.js');
|
|
|
|
const ItemChange = require('@joplin/lib/models/ItemChange.js');
|
|
|
|
const Resource = require('@joplin/lib/models/Resource.js');
|
|
|
|
const Tag = require('@joplin/lib/models/Tag.js');
|
|
|
|
const NoteTag = require('@joplin/lib/models/NoteTag.js');
|
|
|
|
const Revision = require('@joplin/lib/models/Revision.js');
|
|
|
|
const Logger = require('@joplin/lib/Logger').default;
|
|
|
|
const Setting = require('@joplin/lib/models/Setting').default;
|
|
|
|
const MasterKey = require('@joplin/lib/models/MasterKey');
|
|
|
|
const BaseItem = require('@joplin/lib/models/BaseItem.js');
|
|
|
|
const { FileApi } = require('@joplin/lib/file-api.js');
|
|
|
|
const { FileApiDriverMemory } = require('@joplin/lib/file-api-driver-memory.js');
|
|
|
|
const { FileApiDriverLocal } = require('@joplin/lib/file-api-driver-local.js');
|
|
|
|
const { FileApiDriverWebDav } = require('@joplin/lib/file-api-driver-webdav.js');
|
|
|
|
const { FileApiDriverDropbox } = require('@joplin/lib/file-api-driver-dropbox.js');
|
|
|
|
const { FileApiDriverOneDrive } = require('@joplin/lib/file-api-driver-onedrive.js');
|
|
|
|
const { FileApiDriverAmazonS3 } = require('@joplin/lib/file-api-driver-amazon-s3.js');
|
|
|
|
const BaseService = require('@joplin/lib/services/BaseService').default;
|
|
|
|
const FsDriverNode = require('@joplin/lib/fs-driver-node').default;
|
|
|
|
const time = require('@joplin/lib/time').default;
|
|
|
|
const { shimInit } = require('@joplin/lib/shim-init-node.js');
|
|
|
|
const shim = require('@joplin/lib/shim').default;
|
|
|
|
const uuid = require('@joplin/lib/uuid').default;
|
|
|
|
const SyncTargetRegistry = require('@joplin/lib/SyncTargetRegistry.js');
|
|
|
|
const SyncTargetMemory = require('@joplin/lib/SyncTargetMemory.js');
|
|
|
|
const SyncTargetFilesystem = require('@joplin/lib/SyncTargetFilesystem.js');
|
|
|
|
const SyncTargetOneDrive = require('@joplin/lib/SyncTargetOneDrive.js');
|
|
|
|
const SyncTargetNextcloud = require('@joplin/lib/SyncTargetNextcloud.js');
|
|
|
|
const SyncTargetDropbox = require('@joplin/lib/SyncTargetDropbox.js');
|
|
|
|
const SyncTargetAmazonS3 = require('@joplin/lib/SyncTargetAmazonS3.js');
|
|
|
|
const EncryptionService = require('@joplin/lib/services/EncryptionService.js');
|
|
|
|
const DecryptionWorker = require('@joplin/lib/services/DecryptionWorker.js');
|
2020-11-08 18:46:48 +02:00
|
|
|
const ResourceService = require('@joplin/lib/services/ResourceService').default;
|
2020-11-07 17:59:37 +02:00
|
|
|
const RevisionService = require('@joplin/lib/services/RevisionService.js');
|
|
|
|
const ResourceFetcher = require('@joplin/lib/services/ResourceFetcher.js');
|
2020-11-08 03:08:33 +02:00
|
|
|
const KvStore = require('@joplin/lib/services/KvStore').default;
|
2020-11-07 17:59:37 +02:00
|
|
|
const WebDavApi = require('@joplin/lib/WebDavApi');
|
|
|
|
const DropboxApi = require('@joplin/lib/DropboxApi');
|
|
|
|
const { OneDriveApi } = require('@joplin/lib/onedrive-api');
|
|
|
|
const { loadKeychainServiceAndSettings } = require('@joplin/lib/services/SettingUtils');
|
|
|
|
const KeychainServiceDriver = require('@joplin/lib/services/keychain/KeychainServiceDriver.node').default;
|
|
|
|
const KeychainServiceDriverDummy = require('@joplin/lib/services/keychain/KeychainServiceDriver.dummy').default;
|
2020-05-31 22:07:24 +02:00
|
|
|
const md5 = require('md5');
|
2020-07-15 11:22:55 +02:00
|
|
|
const S3 = require('aws-sdk/clients/s3');
|
2020-11-07 17:59:37 +02:00
|
|
|
const { Dirnames } = require('@joplin/lib/services/synchronizer/utils/types');
|
2020-11-05 18:58:23 +02:00
|
|
|
const sharp = require('sharp');
|
2017-06-14 21:59:02 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const databases_ = [];
|
2020-08-02 13:28:50 +02:00
|
|
|
let synchronizers_ = [];
|
|
|
|
const synchronizerContexts_ = {};
|
|
|
|
const fileApis_ = {};
|
2020-03-14 01:46:14 +02:00
|
|
|
const encryptionServices_ = [];
|
|
|
|
const revisionServices_ = [];
|
|
|
|
const decryptionWorkers_ = [];
|
|
|
|
const resourceServices_ = [];
|
2020-05-31 01:31:29 +02:00
|
|
|
const resourceFetchers_ = [];
|
2020-03-14 01:46:14 +02:00
|
|
|
const kvStores_ = [];
|
2017-06-18 22:19:13 +02:00
|
|
|
let currentClient_ = 1;
|
2017-06-14 21:59:02 +02:00
|
|
|
|
2019-11-02 12:19:24 +02:00
|
|
|
// The line `process.on('unhandledRejection'...` in all the test files is going to
|
|
|
|
// make it throw this error. It's not too big a problem so disable it for now.
|
|
|
|
// https://stackoverflow.com/questions/9768444/possible-eventemitter-memory-leak-detected
|
|
|
|
process.setMaxListeners(0);
|
|
|
|
|
2020-11-05 18:58:23 +02:00
|
|
|
let keytar;
|
|
|
|
try {
|
|
|
|
keytar = shim.platformSupportsKeyChain() ? require('keytar') : null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Cannot load keytar - keychain support will be disabled', error);
|
|
|
|
keytar = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
shimInit(sharp, keytar);
|
2017-12-13 20:57:40 +02:00
|
|
|
|
2019-11-02 12:19:24 +02:00
|
|
|
shim.setIsTestingEnv(true);
|
|
|
|
|
2017-07-06 00:29:03 +02:00
|
|
|
const fsDriver = new FsDriverNode();
|
|
|
|
Logger.fsDriver_ = fsDriver;
|
|
|
|
Resource.fsDriver_ = fsDriver;
|
2017-12-18 21:54:03 +02:00
|
|
|
EncryptionService.fsDriver_ = fsDriver;
|
2018-01-17 20:51:15 +02:00
|
|
|
FileApiDriverLocal.fsDriver_ = fsDriver;
|
2017-07-06 00:29:03 +02:00
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
const logDir = `${__dirname}/../tests/logs`;
|
2020-10-22 16:55:29 +02:00
|
|
|
const baseTempDir = `${__dirname}/../tests/tmp`;
|
2017-07-06 21:48:17 +02:00
|
|
|
fs.mkdirpSync(logDir, 0o755);
|
2020-10-22 16:55:29 +02:00
|
|
|
fs.mkdirpSync(baseTempDir, 0o755);
|
2020-03-10 01:24:57 +02:00
|
|
|
fs.mkdirpSync(`${__dirname}/data`);
|
2017-07-06 21:48:17 +02:00
|
|
|
|
2017-11-24 21:21:30 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetMemory);
|
|
|
|
SyncTargetRegistry.addClass(SyncTargetFilesystem);
|
2017-11-28 00:50:46 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetOneDrive);
|
2018-01-25 23:15:58 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetNextcloud);
|
2018-03-24 21:35:10 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetDropbox);
|
2020-07-15 11:22:55 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetAmazonS3);
|
2017-11-24 21:21:30 +02:00
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
let syncTargetName_ = '';
|
|
|
|
let syncTargetId_ = null;
|
|
|
|
let sleepTime = 0;
|
|
|
|
let isNetworkSyncTarget_ = false;
|
|
|
|
|
|
|
|
function syncTargetName() {
|
|
|
|
return syncTargetName_;
|
|
|
|
}
|
2017-07-23 16:11:44 +02:00
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
function setSyncTargetName(name) {
|
|
|
|
if (name === syncTargetName_) return syncTargetName_;
|
|
|
|
const previousName = syncTargetName_;
|
|
|
|
syncTargetName_ = name;
|
|
|
|
syncTargetId_ = SyncTargetRegistry.nameToId(syncTargetName_);
|
|
|
|
sleepTime = syncTargetId_ == SyncTargetRegistry.nameToId('filesystem') ? 1001 : 100;// 400;
|
|
|
|
isNetworkSyncTarget_ = ['nextcloud', 'dropbox', 'onedrive', 'amazon_s3'].includes(syncTargetName_);
|
|
|
|
synchronizers_ = [];
|
|
|
|
return previousName;
|
|
|
|
}
|
|
|
|
|
|
|
|
setSyncTargetName('memory');
|
|
|
|
// setSyncTargetName('nextcloud');
|
|
|
|
// setSyncTargetName('dropbox');
|
|
|
|
// setSyncTargetName('onedrive');
|
|
|
|
// setSyncTargetName('amazon_s3');
|
2017-07-24 20:58:11 +02:00
|
|
|
|
2020-11-10 17:59:30 +02:00
|
|
|
// console.info(`Testing with sync target: ${syncTargetName_}`);
|
2020-08-02 13:28:50 +02:00
|
|
|
|
|
|
|
const syncDir = `${__dirname}/../tests/sync`;
|
|
|
|
|
2020-11-19 14:44:20 +02:00
|
|
|
// TODO: Should probably update this for Jest?
|
|
|
|
|
|
|
|
// let defaultJasmineTimeout = 90 * 1000;
|
|
|
|
// if (isNetworkSyncTarget_) defaultJasmineTimeout = 60 * 1000 * 10;
|
|
|
|
// if (typeof jasmine !== 'undefined') jasmine.DEFAULT_TIMEOUT_INTERVAL = defaultJasmineTimeout;
|
2018-01-25 23:15:58 +02:00
|
|
|
|
2019-05-14 23:23:34 +02:00
|
|
|
const dbLogger = new Logger();
|
|
|
|
dbLogger.addTarget('console');
|
2019-09-19 23:51:18 +02:00
|
|
|
dbLogger.addTarget('file', { path: `${logDir}/log.txt` });
|
2019-05-14 23:23:34 +02:00
|
|
|
dbLogger.setLevel(Logger.LEVEL_WARN);
|
|
|
|
|
2017-06-25 17:17:40 +02:00
|
|
|
const logger = new Logger();
|
2018-03-09 22:59:12 +02:00
|
|
|
logger.addTarget('console');
|
2019-09-19 23:51:18 +02:00
|
|
|
logger.addTarget('file', { path: `${logDir}/log.txt` });
|
2018-03-15 19:46:54 +02:00
|
|
|
logger.setLevel(Logger.LEVEL_WARN); // Set to DEBUG to display sync process in console
|
2017-06-25 17:17:40 +02:00
|
|
|
|
2020-11-19 17:25:02 +02:00
|
|
|
Logger.initializeGlobalLogger(logger);
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
BaseItem.loadClass('Note', Note);
|
|
|
|
BaseItem.loadClass('Folder', Folder);
|
|
|
|
BaseItem.loadClass('Resource', Resource);
|
|
|
|
BaseItem.loadClass('Tag', Tag);
|
|
|
|
BaseItem.loadClass('NoteTag', NoteTag);
|
|
|
|
BaseItem.loadClass('MasterKey', MasterKey);
|
2019-05-06 22:35:29 +02:00
|
|
|
BaseItem.loadClass('Revision', Revision);
|
2017-07-06 21:48:17 +02:00
|
|
|
|
2020-06-03 18:07:50 +02:00
|
|
|
Setting.setConstant('appId', 'net.cozic.joplintest-cli');
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setConstant('appType', 'cli');
|
2020-10-22 16:55:29 +02:00
|
|
|
Setting.setConstant('tempDir', baseTempDir);
|
2020-10-09 19:35:46 +02:00
|
|
|
Setting.setConstant('env', 'dev');
|
2017-07-07 00:15:31 +02:00
|
|
|
|
2018-03-15 19:46:54 +02:00
|
|
|
BaseService.logger_ = logger;
|
|
|
|
|
2018-02-15 19:12:09 +02:00
|
|
|
Setting.autoSaveEnabled = false;
|
|
|
|
|
2017-07-23 16:11:44 +02:00
|
|
|
function syncTargetId() {
|
2017-07-24 20:58:11 +02:00
|
|
|
return syncTargetId_;
|
2017-07-23 16:11:44 +02:00
|
|
|
}
|
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
function isNetworkSyncTarget() {
|
|
|
|
return isNetworkSyncTarget_;
|
|
|
|
}
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
function sleep(n) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
shim.setTimeout(() => {
|
2017-06-18 22:19:13 +02:00
|
|
|
resolve();
|
2017-06-19 21:18:22 +02:00
|
|
|
}, Math.round(n * 1000));
|
2017-06-18 22:19:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
function msleep(ms) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
shim.setTimeout(() => {
|
2020-08-02 13:28:50 +02:00
|
|
|
resolve();
|
|
|
|
}, ms);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-16 04:30:54 +02:00
|
|
|
function currentClientId() {
|
|
|
|
return currentClient_;
|
|
|
|
}
|
|
|
|
|
2020-11-10 17:59:30 +02:00
|
|
|
async function afterEachCleanUp() {
|
|
|
|
await ItemChange.waitForAllSaved();
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:07:50 +02:00
|
|
|
async function switchClient(id, options = null) {
|
|
|
|
options = Object.assign({}, { keychainEnabled: false }, options);
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
if (!databases_[id]) throw new Error(`Call setupDatabaseAndSynchronizer(${id}) first!!`);
|
2019-04-21 14:49:40 +02:00
|
|
|
|
2017-07-24 20:58:11 +02:00
|
|
|
await time.msleep(sleepTime); // Always leave a little time so that updated_time properties don't overlap
|
2017-07-03 20:29:19 +02:00
|
|
|
await Setting.saveAll();
|
2017-06-20 21:18:19 +02:00
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
currentClient_ = id;
|
2020-03-16 04:30:54 +02:00
|
|
|
BaseModel.setDb(databases_[id]);
|
2017-06-20 21:18:19 +02:00
|
|
|
|
2017-12-13 20:57:40 +02:00
|
|
|
BaseItem.encryptionService_ = encryptionServices_[id];
|
2017-12-18 21:54:03 +02:00
|
|
|
Resource.encryptionService_ = encryptionServices_[id];
|
2019-05-06 22:35:29 +02:00
|
|
|
BaseItem.revisionService_ = revisionServices_[id];
|
2017-12-18 21:54:03 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
await Setting.reset();
|
2020-04-14 00:55:24 +02:00
|
|
|
Setting.setConstant('resourceDirName', resourceDirName(id));
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setConstant('resourceDir', resourceDir(id));
|
2020-11-20 01:46:04 +02:00
|
|
|
Setting.setConstant('pluginDir', pluginDir(id));
|
2017-12-13 20:57:40 +02:00
|
|
|
|
2020-06-03 18:07:50 +02:00
|
|
|
await loadKeychainServiceAndSettings(options.keychainEnabled ? KeychainServiceDriver : KeychainServiceDriverDummy);
|
2019-09-25 20:40:04 +02:00
|
|
|
|
|
|
|
Setting.setValue('sync.wipeOutFailSafe', false); // To keep things simple, always disable fail-safe unless explicitely set in the test itself
|
2017-06-18 22:19:13 +02:00
|
|
|
}
|
|
|
|
|
2017-12-20 21:45:25 +02:00
|
|
|
async function clearDatabase(id = null) {
|
2017-06-18 22:19:13 +02:00
|
|
|
if (id === null) id = currentClient_;
|
2020-02-27 20:25:42 +02:00
|
|
|
if (!databases_[id]) return;
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2018-12-10 20:54:46 +02:00
|
|
|
await ItemChange.waitForAllSaved();
|
|
|
|
|
2019-05-06 22:35:29 +02:00
|
|
|
const tableNames = [
|
|
|
|
'notes',
|
|
|
|
'folders',
|
|
|
|
'resources',
|
|
|
|
'tags',
|
|
|
|
'note_tags',
|
|
|
|
'master_keys',
|
|
|
|
'item_changes',
|
|
|
|
'note_resources',
|
2019-07-30 09:35:42 +02:00
|
|
|
'settings',
|
2019-05-06 22:35:29 +02:00
|
|
|
'deleted_items',
|
|
|
|
'sync_items',
|
|
|
|
'notes_normalized',
|
|
|
|
'revisions',
|
2019-06-07 10:05:15 +02:00
|
|
|
'key_values',
|
2017-06-18 22:19:13 +02:00
|
|
|
];
|
|
|
|
|
2019-05-06 22:35:29 +02:00
|
|
|
const queries = [];
|
|
|
|
for (const n of tableNames) {
|
2019-09-19 23:51:18 +02:00
|
|
|
queries.push(`DELETE FROM ${n}`);
|
|
|
|
queries.push(`DELETE FROM sqlite_sequence WHERE name="${n}"`); // Reset autoincremented IDs
|
2019-05-06 22:35:29 +02:00
|
|
|
}
|
2017-12-20 21:45:25 +02:00
|
|
|
await databases_[id].transactionExecBatch(queries);
|
2017-06-18 22:19:13 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 18:07:50 +02:00
|
|
|
async function setupDatabase(id = null, options = null) {
|
|
|
|
options = Object.assign({}, { keychainEnabled: false }, options);
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
|
2017-12-20 21:45:25 +02:00
|
|
|
Setting.cancelScheduleSave();
|
|
|
|
Setting.cache_ = null;
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
if (databases_[id]) {
|
2020-03-16 04:30:54 +02:00
|
|
|
BaseModel.setDb(databases_[id]);
|
2017-12-20 21:45:25 +02:00
|
|
|
await clearDatabase(id);
|
2020-06-03 18:07:50 +02:00
|
|
|
await loadKeychainServiceAndSettings(options.keychainEnabled ? KeychainServiceDriver : KeychainServiceDriverDummy);
|
2017-12-20 21:45:25 +02:00
|
|
|
return;
|
2017-06-14 21:59:02 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
const filePath = `${__dirname}/data/test-${id}.sqlite`;
|
2017-12-20 21:45:25 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
await fs.unlink(filePath);
|
|
|
|
} catch (error) {
|
2017-06-14 21:59:02 +02:00
|
|
|
// Don't care if the file doesn't exist
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
2017-12-20 21:45:25 +02:00
|
|
|
|
|
|
|
databases_[id] = new JoplinDatabase(new DatabaseDriverNode());
|
2019-05-14 23:23:34 +02:00
|
|
|
databases_[id].setLogger(dbLogger);
|
2017-12-20 21:45:25 +02:00
|
|
|
await databases_[id].open({ name: filePath });
|
|
|
|
|
2020-03-16 04:30:54 +02:00
|
|
|
BaseModel.setDb(databases_[id]);
|
2020-06-03 18:07:50 +02:00
|
|
|
await loadKeychainServiceAndSettings(options.keychainEnabled ? KeychainServiceDriver : KeychainServiceDriverDummy);
|
2017-06-14 21:59:02 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 00:55:24 +02:00
|
|
|
function resourceDirName(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
return `resources-${id}`;
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:54:03 +02:00
|
|
|
function resourceDir(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
2020-04-14 00:55:24 +02:00
|
|
|
return `${__dirname}/data/${resourceDirName(id)}`;
|
2017-12-18 21:54:03 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 01:46:04 +02:00
|
|
|
function pluginDir(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
return `${__dirname}/data/plugins-${id}`;
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:07:50 +02:00
|
|
|
async function setupDatabaseAndSynchronizer(id = null, options = null) {
|
2017-06-18 22:19:13 +02:00
|
|
|
if (id === null) id = currentClient_;
|
2017-06-18 01:49:52 +02:00
|
|
|
|
2020-03-16 04:30:54 +02:00
|
|
|
BaseService.logger_ = logger;
|
|
|
|
|
2020-06-03 18:07:50 +02:00
|
|
|
await setupDatabase(id, options);
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-20 21:45:25 +02:00
|
|
|
EncryptionService.instance_ = null;
|
|
|
|
DecryptionWorker.instance_ = null;
|
|
|
|
|
2017-12-18 21:54:03 +02:00
|
|
|
await fs.remove(resourceDir(id));
|
|
|
|
await fs.mkdirp(resourceDir(id), 0o755);
|
|
|
|
|
2020-11-20 01:46:04 +02:00
|
|
|
await fs.remove(pluginDir(id));
|
|
|
|
await fs.mkdirp(pluginDir(id), 0o755);
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
if (!synchronizers_[id]) {
|
2017-11-24 20:37:40 +02:00
|
|
|
const SyncTargetClass = SyncTargetRegistry.classById(syncTargetId_);
|
|
|
|
const syncTarget = new SyncTargetClass(db(id));
|
2020-08-02 13:28:50 +02:00
|
|
|
await initFileApi();
|
2017-11-24 20:37:40 +02:00
|
|
|
syncTarget.setFileApi(fileApi());
|
|
|
|
syncTarget.setLogger(logger);
|
|
|
|
synchronizers_[id] = await syncTarget.synchronizer();
|
2020-08-02 13:28:50 +02:00
|
|
|
synchronizerContexts_[id] = null;
|
2017-06-18 01:49:52 +02:00
|
|
|
}
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-19 21:01:29 +02:00
|
|
|
encryptionServices_[id] = new EncryptionService();
|
2019-05-06 22:35:29 +02:00
|
|
|
revisionServices_[id] = new RevisionService();
|
2017-12-19 21:01:29 +02:00
|
|
|
decryptionWorkers_[id] = new DecryptionWorker();
|
|
|
|
decryptionWorkers_[id].setEncryptionService(encryptionServices_[id]);
|
2019-04-21 14:49:40 +02:00
|
|
|
resourceServices_[id] = new ResourceService();
|
2020-05-31 01:31:29 +02:00
|
|
|
resourceFetchers_[id] = new ResourceFetcher(() => { return synchronizers_[id].api(); });
|
2019-06-08 00:11:08 +02:00
|
|
|
kvStores_[id] = new KvStore();
|
2017-12-13 20:57:40 +02:00
|
|
|
|
2018-01-26 00:44:09 +02:00
|
|
|
await fileApi().clearRoot();
|
2017-06-14 21:59:02 +02:00
|
|
|
}
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
function db(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
return databases_[id];
|
2017-06-14 21:59:02 +02:00
|
|
|
}
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
function synchronizer(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
return synchronizers_[id];
|
2017-06-14 21:59:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
// This is like calling synchronizer.start() but it handles the
|
|
|
|
// complexity of passing around the sync context depending on
|
|
|
|
// the client.
|
|
|
|
async function synchronizerStart(id = null, extraOptions = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
const context = synchronizerContexts_[id];
|
|
|
|
const options = Object.assign({}, extraOptions);
|
|
|
|
if (context) options.context = context;
|
|
|
|
const newContext = await synchronizer(id).start(options);
|
|
|
|
synchronizerContexts_[id] = newContext;
|
|
|
|
return newContext;
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:57:40 +02:00
|
|
|
function encryptionService(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
return encryptionServices_[id];
|
|
|
|
}
|
|
|
|
|
2019-06-08 00:11:08 +02:00
|
|
|
function kvStore(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
const o = kvStores_[id];
|
|
|
|
o.setDb(db(id));
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2019-05-06 22:35:29 +02:00
|
|
|
function revisionService(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
return revisionServices_[id];
|
|
|
|
}
|
|
|
|
|
2017-12-19 21:01:29 +02:00
|
|
|
function decryptionWorker(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
2019-06-08 00:11:08 +02:00
|
|
|
const o = decryptionWorkers_[id];
|
|
|
|
o.setKvStore(kvStore(id));
|
|
|
|
return o;
|
2017-12-19 21:01:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-21 14:49:40 +02:00
|
|
|
function resourceService(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
return resourceServices_[id];
|
|
|
|
}
|
|
|
|
|
2020-05-31 01:31:29 +02:00
|
|
|
function resourceFetcher(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
return resourceFetchers_[id];
|
|
|
|
}
|
|
|
|
|
2017-12-17 21:51:45 +02:00
|
|
|
async function loadEncryptionMasterKey(id = null, useExisting = false) {
|
2017-12-13 20:57:40 +02:00
|
|
|
const service = encryptionService(id);
|
|
|
|
|
2017-12-17 21:51:45 +02:00
|
|
|
let masterKey = null;
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!useExisting) { // Create it
|
|
|
|
masterKey = await service.generateMasterKey('123456');
|
2017-12-17 21:51:45 +02:00
|
|
|
masterKey = await MasterKey.save(masterKey);
|
2018-03-09 22:59:12 +02:00
|
|
|
} else { // Use the one already available
|
2019-07-30 09:35:42 +02:00
|
|
|
const masterKeys = await MasterKey.all();
|
2019-10-29 11:02:42 +02:00
|
|
|
if (!masterKeys.length) throw new Error('No master key available');
|
2019-07-30 09:35:42 +02:00
|
|
|
masterKey = masterKeys[0];
|
2017-12-17 21:51:45 +02:00
|
|
|
}
|
2017-12-13 20:57:40 +02:00
|
|
|
|
2020-01-23 00:01:58 +02:00
|
|
|
await service.loadMasterKey_(masterKey, '123456', true);
|
2017-12-13 20:57:40 +02:00
|
|
|
|
|
|
|
return masterKey;
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
async function initFileApi() {
|
|
|
|
if (fileApis_[syncTargetId_]) return;
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
let fileApi = null;
|
2018-03-09 22:59:12 +02:00
|
|
|
if (syncTargetId_ == SyncTargetRegistry.nameToId('filesystem')) {
|
2019-07-30 09:35:42 +02:00
|
|
|
fs.removeSync(syncDir);
|
2017-07-23 16:11:44 +02:00
|
|
|
fs.mkdirpSync(syncDir, 0o755);
|
2020-08-02 13:28:50 +02:00
|
|
|
fileApi = new FileApi(syncDir, new FileApiDriverLocal());
|
2018-03-09 22:59:12 +02:00
|
|
|
} else if (syncTargetId_ == SyncTargetRegistry.nameToId('memory')) {
|
2020-08-02 13:28:50 +02:00
|
|
|
fileApi = new FileApi('/root', new FileApiDriverMemory());
|
2018-03-09 22:59:12 +02:00
|
|
|
} else if (syncTargetId_ == SyncTargetRegistry.nameToId('nextcloud')) {
|
2020-08-02 13:28:50 +02:00
|
|
|
const options = require(`${__dirname}/../tests/support/nextcloud-auth.json`);
|
|
|
|
const api = new WebDavApi({
|
|
|
|
baseUrl: () => options.baseUrl,
|
|
|
|
username: () => options.username,
|
|
|
|
password: () => options.password,
|
|
|
|
});
|
|
|
|
fileApi = new FileApi('', new FileApiDriverWebDav(api));
|
2018-03-24 21:35:10 +02:00
|
|
|
} else if (syncTargetId_ == SyncTargetRegistry.nameToId('dropbox')) {
|
2020-08-02 13:28:50 +02:00
|
|
|
// To get a token, go to the App Console:
|
|
|
|
// https://www.dropbox.com/developers/apps/
|
|
|
|
// Then select "JoplinTest" and click "Generated access token"
|
2018-03-24 21:35:10 +02:00
|
|
|
const api = new DropboxApi();
|
2019-09-19 23:51:18 +02:00
|
|
|
const authTokenPath = `${__dirname}/support/dropbox-auth.txt`;
|
2018-03-24 21:35:10 +02:00
|
|
|
const authToken = fs.readFileSync(authTokenPath, 'utf8');
|
2019-09-19 23:51:18 +02:00
|
|
|
if (!authToken) throw new Error(`Dropbox auth token missing in ${authTokenPath}`);
|
2018-03-24 21:35:10 +02:00
|
|
|
api.setAuthToken(authToken);
|
2020-08-02 13:28:50 +02:00
|
|
|
fileApi = new FileApi('', new FileApiDriverDropbox(api));
|
|
|
|
} else if (syncTargetId_ == SyncTargetRegistry.nameToId('onedrive')) {
|
|
|
|
// To get a token, open the URL below, then copy the *complete*
|
|
|
|
// redirection URL in onedrive-auth.txt. Keep in mind that auth data
|
|
|
|
// only lasts 1h for OneDrive.
|
|
|
|
// https://login.live.com/oauth20_authorize.srf?client_id=f1e68e1e-a729-4514-b041-4fdd5c7ac03a&scope=files.readwrite,offline_access&response_type=token&redirect_uri=https://joplinapp.org
|
2020-11-07 17:59:37 +02:00
|
|
|
const { parameters, setEnvOverride } = require('@joplin/lib/parameters.js');
|
2020-08-02 13:28:50 +02:00
|
|
|
Setting.setConstant('env', 'dev');
|
|
|
|
setEnvOverride('test');
|
|
|
|
const config = parameters().oneDriveTest;
|
|
|
|
const api = new OneDriveApi(config.id, config.secret, false);
|
|
|
|
const authData = fs.readFileSync(`${__dirname}/support/onedrive-auth.txt`, 'utf8');
|
|
|
|
const urlInfo = require('url-parse')(authData, true);
|
|
|
|
const auth = require('querystring').parse(urlInfo.hash.substr(1));
|
|
|
|
api.setAuth(auth);
|
|
|
|
const appDir = await api.appDirectory();
|
|
|
|
fileApi = new FileApi(appDir, new FileApiDriverOneDrive(api));
|
2020-07-15 11:22:55 +02:00
|
|
|
} else if (syncTargetId_ == SyncTargetRegistry.nameToId('amazon_s3')) {
|
|
|
|
const amazonS3CredsPath = `${__dirname}/support/amazon-s3-auth.json`;
|
|
|
|
const amazonS3Creds = require(amazonS3CredsPath);
|
|
|
|
if (!amazonS3Creds || !amazonS3Creds.accessKeyId) throw new Error(`AWS auth JSON missing in ${amazonS3CredsPath} format should be: { "accessKeyId": "", "secretAccessKey": "", "bucket": "mybucket"}`);
|
|
|
|
const api = new S3({ accessKeyId: amazonS3Creds.accessKeyId, secretAccessKey: amazonS3Creds.secretAccessKey, s3UseArnRegion: true });
|
2020-08-02 13:28:50 +02:00
|
|
|
fileApi = new FileApi('', new FileApiDriverAmazonS3(api, amazonS3Creds.bucket));
|
2017-07-24 20:58:11 +02:00
|
|
|
}
|
2018-01-25 23:15:58 +02:00
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
fileApi.setLogger(logger);
|
|
|
|
fileApi.setSyncTargetId(syncTargetId_);
|
2020-09-12 00:33:34 +02:00
|
|
|
fileApi.setTempDirName(Dirnames.Temp);
|
2020-08-02 13:28:50 +02:00
|
|
|
fileApi.requestRepeatCount_ = isNetworkSyncTarget_ ? 1 : 0;
|
2020-07-15 11:22:55 +02:00
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
fileApis_[syncTargetId_] = fileApi;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileApi() {
|
|
|
|
return fileApis_[syncTargetId_];
|
2017-06-14 21:59:02 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 20:57:40 +02:00
|
|
|
function objectsEqual(o1, o2) {
|
|
|
|
if (Object.getOwnPropertyNames(o1).length !== Object.getOwnPropertyNames(o2).length) return false;
|
2020-03-14 01:46:14 +02:00
|
|
|
for (const n in o1) {
|
2017-12-13 20:57:40 +02:00
|
|
|
if (!o1.hasOwnProperty(n)) continue;
|
|
|
|
if (o1[n] !== o2[n]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkThrowAsync(asyncFn) {
|
|
|
|
let hasThrown = false;
|
|
|
|
try {
|
|
|
|
await asyncFn();
|
|
|
|
} catch (error) {
|
|
|
|
hasThrown = true;
|
|
|
|
}
|
|
|
|
return hasThrown;
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
async function expectThrow(asyncFn, errorCode = undefined) {
|
|
|
|
let hasThrown = false;
|
|
|
|
let thrownError = null;
|
|
|
|
try {
|
|
|
|
await asyncFn();
|
|
|
|
} catch (error) {
|
|
|
|
hasThrown = true;
|
|
|
|
thrownError = error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasThrown) {
|
|
|
|
expect('not throw').toBe('throw', 'Expected function to throw an error but did not');
|
|
|
|
} else if (thrownError.code !== errorCode) {
|
|
|
|
console.error(thrownError);
|
|
|
|
expect(`error code: ${thrownError.code}`).toBe(`error code: ${errorCode}`);
|
|
|
|
} else {
|
|
|
|
expect(true).toBe(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function expectNotThrow(asyncFn) {
|
|
|
|
let thrownError = null;
|
|
|
|
try {
|
|
|
|
await asyncFn();
|
|
|
|
} catch (error) {
|
|
|
|
thrownError = error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thrownError) {
|
2020-11-09 11:21:05 +02:00
|
|
|
console.error(thrownError);
|
2020-08-02 13:28:50 +02:00
|
|
|
expect(thrownError.message).toBe('', 'Expected function not to throw an error but it did');
|
|
|
|
} else {
|
|
|
|
expect(true).toBe(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 17:20:18 +02:00
|
|
|
function checkThrow(fn) {
|
|
|
|
let hasThrown = false;
|
|
|
|
try {
|
|
|
|
fn();
|
|
|
|
} catch (error) {
|
|
|
|
hasThrown = true;
|
|
|
|
}
|
|
|
|
return hasThrown;
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:54:03 +02:00
|
|
|
function fileContentEqual(path1, path2) {
|
2018-03-09 22:59:12 +02:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const content1 = fs.readFileSync(path1, 'base64');
|
|
|
|
const content2 = fs.readFileSync(path2, 'base64');
|
2017-12-18 21:54:03 +02:00
|
|
|
return content1 === content2;
|
|
|
|
}
|
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
// Wrap an async test in a try/catch block so that done() is always called
|
|
|
|
// and display a proper error message instead of "unhandled promise error"
|
|
|
|
function asyncTest(callback) {
|
|
|
|
return async function(done) {
|
|
|
|
try {
|
|
|
|
await callback();
|
|
|
|
} catch (error) {
|
2020-01-23 00:01:58 +02:00
|
|
|
if (error.constructor && error.constructor.name === 'ExpectationFailed') {
|
2020-11-19 14:44:20 +02:00
|
|
|
// OK - will be reported by Jest
|
2020-01-23 00:01:58 +02:00
|
|
|
} else {
|
2020-11-10 17:59:30 +02:00
|
|
|
// Better to rethrow exception as stack trace is more useful in this case
|
|
|
|
throw error;
|
|
|
|
// console.error(error);
|
|
|
|
// expect(0).toBe(1, 'Test has thrown an exception - see above error');
|
2020-01-23 00:01:58 +02:00
|
|
|
}
|
2018-03-27 18:48:55 +02:00
|
|
|
} finally {
|
|
|
|
done();
|
2018-01-02 21:17:14 +02:00
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2018-01-02 21:17:14 +02:00
|
|
|
}
|
|
|
|
|
2019-04-21 14:49:40 +02:00
|
|
|
async function allSyncTargetItemsEncrypted() {
|
2020-08-02 13:28:50 +02:00
|
|
|
const list = await fileApi().list('', { includeDirs: false });
|
2019-04-21 14:49:40 +02:00
|
|
|
const files = list.items;
|
|
|
|
|
|
|
|
let totalCount = 0;
|
|
|
|
let encryptedCount = 0;
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
const file = files[i];
|
2020-08-02 13:28:50 +02:00
|
|
|
if (!BaseItem.isSystemPath(file.path)) continue;
|
|
|
|
|
2019-04-21 14:49:40 +02:00
|
|
|
const remoteContentString = await fileApi().get(file.path);
|
|
|
|
const remoteContent = await BaseItem.unserialize(remoteContentString);
|
|
|
|
const ItemClass = BaseItem.itemClass(remoteContent);
|
|
|
|
|
|
|
|
if (!ItemClass.encryptionSupported()) continue;
|
|
|
|
|
|
|
|
totalCount++;
|
|
|
|
|
|
|
|
if (remoteContent.type_ === BaseModel.TYPE_RESOURCE) {
|
2019-09-19 23:51:18 +02:00
|
|
|
const content = await fileApi().get(`.resource/${remoteContent.id}`);
|
2019-04-21 14:49:40 +02:00
|
|
|
totalCount++;
|
2019-07-30 09:35:42 +02:00
|
|
|
if (content.substr(0, 5) === 'JED01') encryptedCount++;
|
2019-04-21 14:49:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
if (remoteContent.encryption_applied) encryptedCount++;
|
2019-04-21 14:49:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!totalCount) throw new Error('No encryptable item on sync target');
|
|
|
|
|
|
|
|
return totalCount === encryptedCount;
|
|
|
|
}
|
|
|
|
|
2020-02-27 20:25:42 +02:00
|
|
|
function id(a) {
|
|
|
|
return a.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ids(a) {
|
2020-05-21 10:14:33 +02:00
|
|
|
return a.map(n => n.id);
|
2020-02-27 20:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function sortedIds(a) {
|
|
|
|
return ids(a).sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
function at(a, indexes) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const out = [];
|
2020-02-27 20:25:42 +02:00
|
|
|
for (let i = 0; i < indexes.length; i++) {
|
|
|
|
out.push(a[indexes[i]]);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createNTestFolders(n) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folders = [];
|
2020-02-27 20:25:42 +02:00
|
|
|
for (let i = 0; i < n; i++) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder = await Folder.save({ title: 'folder' });
|
2020-02-27 20:25:42 +02:00
|
|
|
folders.push(folder);
|
2020-03-16 04:30:54 +02:00
|
|
|
await time.msleep(10);
|
2020-02-27 20:25:42 +02:00
|
|
|
}
|
|
|
|
return folders;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createNTestNotes(n, folder, tagIds = null, title = 'note') {
|
2020-03-14 01:46:14 +02:00
|
|
|
const notes = [];
|
2020-02-27 20:25:42 +02:00
|
|
|
for (let i = 0; i < n; i++) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const title_ = n > 1 ? `${title}${i}` : title;
|
|
|
|
const note = await Note.save({ title: title_, parent_id: folder.id, is_conflict: 0 });
|
2020-02-27 20:25:42 +02:00
|
|
|
notes.push(note);
|
2020-03-16 04:30:54 +02:00
|
|
|
await time.msleep(10);
|
2020-02-27 20:25:42 +02:00
|
|
|
}
|
|
|
|
if (tagIds) {
|
|
|
|
for (let i = 0; i < notes.length; i++) {
|
|
|
|
await Tag.setNoteTagsByIds(notes[i].id, tagIds);
|
2020-03-16 04:30:54 +02:00
|
|
|
await time.msleep(10);
|
2020-02-27 20:25:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return notes;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createNTestTags(n) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const tags = [];
|
2020-02-27 20:25:42 +02:00
|
|
|
for (let i = 0; i < n; i++) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const tag = await Tag.save({ title: 'tag' });
|
2020-02-27 20:25:42 +02:00
|
|
|
tags.push(tag);
|
2020-03-16 04:30:54 +02:00
|
|
|
await time.msleep(10);
|
2020-02-27 20:25:42 +02:00
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
2020-05-31 22:07:24 +02:00
|
|
|
function tempFilePath(ext) {
|
|
|
|
return `${Setting.value('tempDir')}/${md5(Date.now() + Math.random())}.${ext}`;
|
|
|
|
}
|
|
|
|
|
2020-10-22 16:55:29 +02:00
|
|
|
async function createTempDir() {
|
|
|
|
const tempDirPath = `${baseTempDir}/${uuid.createNano()}`;
|
|
|
|
await fs.mkdirp(tempDirPath);
|
|
|
|
return tempDirPath;
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:44:20 +02:00
|
|
|
// TODO: Update for Jest
|
2020-10-09 22:51:11 +02:00
|
|
|
|
2020-11-19 14:44:20 +02:00
|
|
|
// function mockDate(year, month, day, tick) {
|
|
|
|
// const fixedDate = new Date(2020, 0, 1);
|
|
|
|
// jasmine.clock().install();
|
|
|
|
// jasmine.clock().mockDate(fixedDate);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// function restoreDate() {
|
|
|
|
// jasmine.clock().uninstall();
|
|
|
|
// }
|
2020-10-09 22:51:11 +02:00
|
|
|
|
2020-03-16 04:30:54 +02:00
|
|
|
// Application for feature integration testing
|
2020-02-22 13:25:16 +02:00
|
|
|
class TestApp extends BaseApplication {
|
2020-03-16 04:30:54 +02:00
|
|
|
constructor(hasGui = true) {
|
2020-02-22 13:25:16 +02:00
|
|
|
super();
|
2020-03-16 04:30:54 +02:00
|
|
|
this.hasGui_ = hasGui;
|
2020-02-22 13:25:16 +02:00
|
|
|
this.middlewareCalls_ = [];
|
2020-03-16 04:30:54 +02:00
|
|
|
this.logger_ = super.logger();
|
|
|
|
}
|
|
|
|
|
|
|
|
hasGui() {
|
|
|
|
return this.hasGui_;
|
2020-02-22 13:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async start(argv) {
|
2020-03-16 04:30:54 +02:00
|
|
|
this.logger_.info('Test app starting...');
|
2020-02-27 20:25:42 +02:00
|
|
|
|
2020-03-16 04:30:54 +02:00
|
|
|
if (!argv.includes('--profile')) {
|
|
|
|
argv = argv.concat(['--profile', `tests-build/profile/${uuid.create()}`]);
|
|
|
|
}
|
2020-02-27 20:25:42 +02:00
|
|
|
argv = await super.start(['',''].concat(argv));
|
2020-03-16 04:30:54 +02:00
|
|
|
|
|
|
|
// For now, disable sync and encryption to avoid spurious intermittent failures
|
|
|
|
// caused by them interupting processing and causing delays.
|
|
|
|
Setting.setValue('sync.interval', 0);
|
|
|
|
Setting.setValue('encryption.enabled', false);
|
|
|
|
|
2020-02-22 13:25:16 +02:00
|
|
|
this.initRedux();
|
|
|
|
Setting.dispatchUpdateAll();
|
2020-03-16 04:30:54 +02:00
|
|
|
await ItemChange.waitForAllSaved();
|
|
|
|
await this.wait();
|
|
|
|
|
|
|
|
this.logger_.info('Test app started...');
|
2020-02-22 13:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async generalMiddleware(store, next, action) {
|
|
|
|
this.middlewareCalls_.push(true);
|
|
|
|
try {
|
|
|
|
await super.generalMiddleware(store, next, action);
|
|
|
|
} finally {
|
|
|
|
this.middlewareCalls_.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 04:30:54 +02:00
|
|
|
async wait() {
|
2020-02-22 13:25:16 +02:00
|
|
|
return new Promise((resolve) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
const iid = shim.setInterval(() => {
|
2020-02-22 13:25:16 +02:00
|
|
|
if (!this.middlewareCalls_.length) {
|
|
|
|
clearInterval(iid);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-16 04:30:54 +02:00
|
|
|
async profileDir() {
|
2020-11-20 01:46:04 +02:00
|
|
|
return Setting.value('profileDir');
|
2020-03-16 04:30:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-22 13:25:16 +02:00
|
|
|
async destroy() {
|
2020-03-16 04:30:54 +02:00
|
|
|
this.logger_.info('Test app stopping...');
|
|
|
|
await this.wait();
|
|
|
|
await ItemChange.waitForAllSaved();
|
2020-02-27 20:25:42 +02:00
|
|
|
this.deinitRedux();
|
2020-02-22 13:25:16 +02:00
|
|
|
await super.destroy();
|
2020-03-16 04:30:54 +02:00
|
|
|
await time.msleep(100);
|
2020-02-22 13:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:44:20 +02:00
|
|
|
module.exports = { 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, asyncTest, currentClientId, id, ids, sortedIds, at, createNTestNotes, createNTestFolders, createNTestTags, TestApp };
|