2017-11-12 20:57:59 +02:00
|
|
|
require('app-module-path').addPath(__dirname);
|
|
|
|
|
2017-11-03 02:09:34 +02:00
|
|
|
const { time } = require('lib/time-utils.js');
|
2018-01-02 21:17:14 +02:00
|
|
|
const { setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, encryptionService, loadEncryptionMasterKey, fileContentEqual, decryptionWorker, checkThrowAsync, asyncTest } = require('test-utils.js');
|
2017-12-19 21:01:29 +02:00
|
|
|
const { shim } = require('lib/shim.js');
|
2017-12-20 21:43:43 +02:00
|
|
|
const fs = require('fs-extra');
|
2017-12-14 20:12:14 +02:00
|
|
|
const Folder = require('lib/models/Folder.js');
|
|
|
|
const Note = require('lib/models/Note.js');
|
2017-12-19 21:01:29 +02:00
|
|
|
const Resource = require('lib/models/Resource.js');
|
2017-12-14 20:12:14 +02:00
|
|
|
const Tag = require('lib/models/Tag.js');
|
2017-11-03 02:09:34 +02:00
|
|
|
const { Database } = require('lib/database.js');
|
2017-12-14 20:12:14 +02:00
|
|
|
const Setting = require('lib/models/Setting.js');
|
2017-12-13 20:57:40 +02:00
|
|
|
const MasterKey = require('lib/models/MasterKey');
|
2017-12-14 20:12:14 +02:00
|
|
|
const BaseItem = require('lib/models/BaseItem.js');
|
|
|
|
const BaseModel = require('lib/BaseModel.js');
|
2017-11-24 21:21:30 +02:00
|
|
|
const SyncTargetRegistry = require('lib/SyncTargetRegistry.js');
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-06-19 00:06:10 +02:00
|
|
|
process.on('unhandledRejection', (reason, p) => {
|
|
|
|
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
|
|
|
});
|
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000; // The first test is slow because the database needs to be built
|
2017-06-29 22:52:52 +02:00
|
|
|
|
2017-06-25 17:17:40 +02:00
|
|
|
async function allItems() {
|
|
|
|
let folders = await Folder.all();
|
|
|
|
let notes = await Note.all();
|
|
|
|
return folders.concat(notes);
|
|
|
|
}
|
|
|
|
|
2017-12-20 21:45:25 +02:00
|
|
|
async function allSyncTargetItemsEncrypted() {
|
|
|
|
const list = await fileApi().list();
|
|
|
|
const files = list.items;
|
|
|
|
|
2017-12-20 21:43:43 +02:00
|
|
|
//console.info(Setting.value('resourceDir'));
|
|
|
|
|
|
|
|
let totalCount = 0;
|
|
|
|
let encryptedCount = 0;
|
2017-12-20 21:45:25 +02:00
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
const file = files[i];
|
|
|
|
const remoteContentString = await fileApi().get(file.path);
|
|
|
|
const remoteContent = await BaseItem.unserialize(remoteContentString);
|
|
|
|
const ItemClass = BaseItem.itemClass(remoteContent);
|
|
|
|
|
|
|
|
if (!ItemClass.encryptionSupported()) continue;
|
|
|
|
|
2017-12-20 21:43:43 +02:00
|
|
|
totalCount++;
|
|
|
|
|
|
|
|
if (remoteContent.type_ === BaseModel.TYPE_RESOURCE) {
|
|
|
|
const content = await fileApi().get('.resource/' + remoteContent.id);
|
|
|
|
totalCount++;
|
|
|
|
if (content.substr(0, 5) === 'JED01') output = encryptedCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!!remoteContent.encryption_applied) encryptedCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!totalCount) throw new Error('No encryptable item on sync target');
|
|
|
|
|
|
|
|
return totalCount === encryptedCount;
|
2017-12-20 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
async function localItemsSameAsRemote(locals, expect) {
|
|
|
|
try {
|
2017-06-19 00:06:10 +02:00
|
|
|
let files = await fileApi().list();
|
2017-06-29 20:03:16 +02:00
|
|
|
files = files.items;
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-06-19 00:06:10 +02:00
|
|
|
expect(locals.length).toBe(files.length);
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
for (let i = 0; i < locals.length; i++) {
|
|
|
|
let dbItem = locals[i];
|
|
|
|
let path = BaseItem.systemPath(dbItem);
|
|
|
|
let remote = await fileApi().stat(path);
|
|
|
|
|
|
|
|
expect(!!remote).toBe(true);
|
2017-07-01 00:53:22 +02:00
|
|
|
if (!remote) continue;
|
|
|
|
|
2017-11-24 21:21:30 +02:00
|
|
|
if (syncTargetId() == SyncTargetRegistry.nameToId('filesystem')) {
|
2017-07-23 16:11:44 +02:00
|
|
|
expect(remote.updated_time).toBe(Math.floor(dbItem.updated_time / 1000) * 1000);
|
|
|
|
} else {
|
|
|
|
expect(remote.updated_time).toBe(dbItem.updated_time);
|
|
|
|
}
|
2017-06-18 22:19:13 +02:00
|
|
|
|
|
|
|
let remoteContent = await fileApi().get(path);
|
2017-07-03 21:50:45 +02:00
|
|
|
remoteContent = dbItem.type_ == BaseModel.TYPE_NOTE ? await Note.unserialize(remoteContent) : await Folder.unserialize(remoteContent);
|
2017-06-18 22:19:13 +02:00
|
|
|
expect(remoteContent.title).toBe(dbItem.title);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
2017-06-15 01:14:15 +02:00
|
|
|
|
2017-12-20 21:45:25 +02:00
|
|
|
let insideBeforeEach = false;
|
|
|
|
|
2017-06-18 01:49:52 +02:00
|
|
|
describe('Synchronizer', function() {
|
2017-06-14 00:39:45 +02:00
|
|
|
|
2017-12-20 21:45:25 +02:00
|
|
|
beforeEach(async (done) => {
|
|
|
|
insideBeforeEach = true;
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await setupDatabaseAndSynchronizer(2);
|
2017-06-20 21:18:19 +02:00
|
|
|
await switchClient(1);
|
2017-06-18 01:49:52 +02:00
|
|
|
done();
|
2017-12-20 21:45:25 +02:00
|
|
|
|
|
|
|
insideBeforeEach = false;
|
2017-06-14 00:39:45 +02:00
|
|
|
});
|
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should create remote items', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder = await Folder.save({ title: "folder1" });
|
|
|
|
await Note.save({ title: "un", parent_id: folder.id });
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let all = await allItems();
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await localItemsSameAsRemote(all, expect);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should update remote item', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder = await Folder.save({ title: "folder1" });
|
|
|
|
let note = await Note.save({ title: "un", parent_id: folder.id });
|
|
|
|
await synchronizer().start();
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await Note.save({ title: "un UPDATE", id: note.id });
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let all = await allItems();
|
|
|
|
await synchronizer().start();
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await localItemsSameAsRemote(all, expect);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should create local items', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder = await Folder.save({ title: "folder1" });
|
|
|
|
await Note.save({ title: "un", parent_id: folder.id });
|
|
|
|
await synchronizer().start();
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let all = await allItems();
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await localItemsSameAsRemote(all, expect);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should update local items', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: "un", parent_id: folder1.id });
|
|
|
|
await synchronizer().start();
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await sleep(0.1);
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let note2 = await Note.load(note1.id);
|
|
|
|
note2.title = "Updated on client 2";
|
|
|
|
await Note.save(note2);
|
|
|
|
note2 = await Note.load(note2.id);
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let all = await allItems();
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await localItemsSameAsRemote(all, expect);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should resolve note conflicts', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: "un", parent_id: folder1.id });
|
|
|
|
await synchronizer().start();
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
let note2 = await Note.load(note1.id);
|
|
|
|
note2.title = "Updated on client 2";
|
|
|
|
await Note.save(note2);
|
|
|
|
note2 = await Note.load(note2.id);
|
|
|
|
await synchronizer().start();
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let note2conf = await Note.load(note1.id);
|
|
|
|
note2conf.title = "Updated on client 1";
|
|
|
|
await Note.save(note2conf);
|
|
|
|
note2conf = await Note.load(note1.id);
|
|
|
|
await synchronizer().start();
|
|
|
|
let conflictedNotes = await Note.conflictedNotes();
|
|
|
|
expect(conflictedNotes.length).toBe(1);
|
|
|
|
|
|
|
|
// Other than the id (since the conflicted note is a duplicate), and the is_conflict property
|
|
|
|
// the conflicted and original note must be the same in every way, to make sure no data has been lost.
|
|
|
|
let conflictedNote = conflictedNotes[0];
|
|
|
|
expect(conflictedNote.id == note2conf.id).toBe(false);
|
|
|
|
for (let n in conflictedNote) {
|
|
|
|
if (!conflictedNote.hasOwnProperty(n)) continue;
|
|
|
|
if (n == 'id' || n == 'is_conflict') continue;
|
|
|
|
expect(conflictedNote[n]).toBe(note2conf[n], 'Property: ' + n);
|
|
|
|
}
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let noteUpdatedFromRemote = await Note.load(note1.id);
|
|
|
|
for (let n in noteUpdatedFromRemote) {
|
|
|
|
if (!noteUpdatedFromRemote.hasOwnProperty(n)) continue;
|
|
|
|
expect(noteUpdatedFromRemote[n]).toBe(note2[n], 'Property: ' + n);
|
|
|
|
}
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should resolve folders conflicts', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: "un", parent_id: folder1.id });
|
|
|
|
await synchronizer().start();
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2); // ----------------------------------
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-19 00:06:10 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await sleep(0.1);
|
2017-06-14 00:39:45 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1_modRemote = await Folder.load(folder1.id);
|
|
|
|
folder1_modRemote.title = "folder1 UPDATE CLIENT 2";
|
|
|
|
await Folder.save(folder1_modRemote);
|
|
|
|
folder1_modRemote = await Folder.load(folder1_modRemote.id);
|
2017-06-14 00:39:45 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-19 00:06:10 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1); // ----------------------------------
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await sleep(0.1);
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1_modLocal = await Folder.load(folder1.id);
|
|
|
|
folder1_modLocal.title = "folder1 UPDATE CLIENT 1";
|
|
|
|
await Folder.save(folder1_modLocal);
|
|
|
|
folder1_modLocal = await Folder.load(folder1.id);
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1_final = await Folder.load(folder1.id);
|
|
|
|
expect(folder1_final.title).toBe(folder1_modRemote.title);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-06-19 21:18:22 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should delete remote notes', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: "un", parent_id: folder1.id });
|
|
|
|
await synchronizer().start();
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await sleep(0.1);
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await Note.delete(note1.id);
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let files = await fileApi().list();
|
|
|
|
files = files.items;
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
expect(files.length).toBe(1);
|
|
|
|
expect(files[0].path).toBe(Folder.systemPath(folder1));
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let deletedItems = await BaseItem.deletedItems(syncTargetId());
|
|
|
|
expect(deletedItems.length).toBe(0);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should delete local notes', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: "un", parent_id: folder1.id });
|
|
|
|
await synchronizer().start();
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
await Note.delete(note1.id);
|
|
|
|
await synchronizer().start();
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-12-04 01:06:02 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
let items = await allItems();
|
|
|
|
expect(items.length).toBe(1);
|
|
|
|
let deletedItems = await BaseItem.deletedItems(syncTargetId());
|
|
|
|
expect(deletedItems.length).toBe(0);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should delete remote folder', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let folder2 = await Folder.save({ title: "folder2" });
|
|
|
|
await synchronizer().start();
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await sleep(0.1);
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await Folder.delete(folder2.id);
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let all = await allItems();
|
|
|
|
localItemsSameAsRemote(all, expect);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should delete local folder', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let folder2 = await Folder.save({ title: "folder2" });
|
|
|
|
await synchronizer().start();
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await sleep(0.1);
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await Folder.delete(folder2.id);
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let items = await allItems();
|
|
|
|
localItemsSameAsRemote(items, expect);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-06-20 00:18:24 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should resolve conflict if remote folder has been deleted, but note has been added to folder locally', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
await synchronizer().start();
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
await Folder.delete(folder1.id);
|
|
|
|
await synchronizer().start();
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let note = await Note.save({ title: "note1", parent_id: folder1.id });
|
|
|
|
await synchronizer().start();
|
|
|
|
let items = await allItems();
|
|
|
|
expect(items.length).toBe(1);
|
|
|
|
expect(items[0].title).toBe('note1');
|
|
|
|
expect(items[0].is_conflict).toBe(1);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should resolve conflict if note has been deleted remotely and locally', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder = await Folder.save({ title: "folder" });
|
|
|
|
let note = await Note.save({ title: "note", parent_id: folder.title });
|
|
|
|
await synchronizer().start();
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
await Note.delete(note.id);
|
|
|
|
await synchronizer().start();
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await Note.delete(note.id);
|
|
|
|
await synchronizer().start();
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let items = await allItems();
|
|
|
|
expect(items.length).toBe(1);
|
|
|
|
expect(items[0].title).toBe('folder');
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
localItemsSameAsRemote(items, expect);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should cross delete all folders', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
// If client1 and 2 have two folders, client 1 deletes item 1 and client
|
|
|
|
// 2 deletes item 2, they should both end up with no items after sync.
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let folder2 = await Folder.save({ title: "folder2" });
|
|
|
|
await synchronizer().start();
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await sleep(0.1);
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await Folder.delete(folder1.id);
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await Folder.delete(folder2.id);
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let items2 = await allItems();
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let items1 = await allItems();
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
expect(items1.length).toBe(0);
|
|
|
|
expect(items1.length).toBe(items2.length);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-07-01 17:17:49 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should handle conflict when remote note is deleted then local note is modified', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: "un", parent_id: folder1.id });
|
|
|
|
await synchronizer().start();
|
2017-06-20 22:31:12 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-06-20 22:31:12 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-20 22:31:12 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await sleep(0.1);
|
2017-06-20 22:31:12 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await Note.delete(note1.id);
|
2017-06-20 22:31:12 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-20 22:31:12 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-06-20 22:31:12 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let newTitle = 'Modified after having been deleted';
|
|
|
|
await Note.save({ id: note1.id, title: newTitle });
|
2017-06-20 22:31:12 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-20 22:31:12 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let conflictedNotes = await Note.conflictedNotes();
|
2017-06-20 22:31:12 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
expect(conflictedNotes.length).toBe(1);
|
|
|
|
expect(conflictedNotes[0].title).toBe(newTitle);
|
2017-07-02 23:01:37 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let unconflictedNotes = await Note.unconflictedNotes();
|
2017-07-02 23:01:37 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
expect(unconflictedNotes.length).toBe(0);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should handle conflict when remote folder is deleted then local folder is renamed', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let folder2 = await Folder.save({ title: "folder2" });
|
|
|
|
let note1 = await Note.save({ title: "un", parent_id: folder1.id });
|
|
|
|
await synchronizer().start();
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await sleep(0.1);
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await Folder.delete(folder1.id);
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await sleep(0.1);
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let newTitle = 'Modified after having been deleted';
|
|
|
|
await Folder.save({ id: folder1.id, title: newTitle });
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-20 22:31:12 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let items = await allItems();
|
2017-07-01 00:53:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
expect(items.length).toBe(1);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should allow duplicate folder titles', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let localF1 = await Folder.save({ title: "folder" });
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let remoteF2 = await Folder.save({ title: "folder" });
|
|
|
|
await synchronizer().start();
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await sleep(0.1);
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let localF2 = await Folder.load(remoteF2.id);
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
expect(localF2.title == remoteF2.title).toBe(true);
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
// Then that folder that has been renamed locally should be set in such a way
|
|
|
|
// that synchronizing it applies the title change remotely, and that new title
|
|
|
|
// should be retrieved by client 2.
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
|
|
|
await sleep(0.1);
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
remoteF2 = await Folder.load(remoteF2.id);
|
2017-06-30 20:19:30 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
expect(remoteF2.title == localF2.title).toBe(true);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-07-03 20:29:19 +02:00
|
|
|
|
2017-12-14 02:23:32 +02:00
|
|
|
async function shoudSyncTagTest(withEncryption) {
|
|
|
|
let masterKey = null;
|
|
|
|
if (withEncryption) {
|
|
|
|
Setting.setValue('encryption.enabled', true);
|
|
|
|
masterKey = await loadEncryptionMasterKey();
|
|
|
|
}
|
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let f1 = await Folder.save({ title: "folder" });
|
|
|
|
let n1 = await Note.save({ title: "mynote" });
|
|
|
|
let n2 = await Note.save({ title: "mynote2" });
|
|
|
|
let tag = await Tag.save({ title: 'mytag' });
|
|
|
|
await synchronizer().start();
|
2017-07-03 20:29:19 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-07-03 20:29:19 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-12-14 02:23:32 +02:00
|
|
|
if (withEncryption) {
|
|
|
|
const masterKey_2 = await MasterKey.load(masterKey.id);
|
|
|
|
await encryptionService().loadMasterKey(masterKey_2, '123456', true);
|
|
|
|
let t = await Tag.load(tag.id);
|
|
|
|
await Tag.decrypt(t);
|
|
|
|
}
|
2017-12-04 20:16:14 +02:00
|
|
|
let remoteTag = await Tag.loadByTitle(tag.title);
|
|
|
|
expect(!!remoteTag).toBe(true);
|
|
|
|
expect(remoteTag.id).toBe(tag.id);
|
|
|
|
await Tag.addNote(remoteTag.id, n1.id);
|
|
|
|
await Tag.addNote(remoteTag.id, n2.id);
|
|
|
|
let noteIds = await Tag.noteIds(tag.id);
|
|
|
|
expect(noteIds.length).toBe(2);
|
|
|
|
await synchronizer().start();
|
2017-07-03 20:29:19 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-07-03 20:29:19 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
let remoteNoteIds = await Tag.noteIds(tag.id);
|
|
|
|
expect(remoteNoteIds.length).toBe(2);
|
|
|
|
await Tag.removeNote(tag.id, n1.id);
|
|
|
|
remoteNoteIds = await Tag.noteIds(tag.id);
|
|
|
|
expect(remoteNoteIds.length).toBe(1);
|
|
|
|
await synchronizer().start();
|
2017-07-03 20:29:19 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-07-03 20:29:19 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
noteIds = await Tag.noteIds(tag.id);
|
|
|
|
expect(noteIds.length).toBe(1);
|
|
|
|
expect(remoteNoteIds[0]).toBe(noteIds[0]);
|
2017-12-14 02:23:32 +02:00
|
|
|
}
|
2017-07-18 00:22:22 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should sync tags', asyncTest(async () => {
|
|
|
|
await shoudSyncTagTest(false); }));
|
2017-12-14 02:23:32 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should sync encrypted tags', asyncTest(async () => {
|
|
|
|
await shoudSyncTagTest(true); }));
|
2017-07-18 00:22:22 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should not sync notes with conflicts', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let f1 = await Folder.save({ title: "folder" });
|
|
|
|
let n1 = await Note.save({ title: "mynote", parent_id: f1.id, is_conflict: 1 });
|
|
|
|
await synchronizer().start();
|
2017-07-18 00:22:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-07-18 00:22:22 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
let notes = await Note.all();
|
|
|
|
let folders = await Folder.all()
|
|
|
|
expect(notes.length).toBe(0);
|
|
|
|
expect(folders.length).toBe(1);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-07-18 21:27:10 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should not try to delete on remote conflicted notes that have been deleted', asyncTest(async () => {
|
2017-12-04 20:16:14 +02:00
|
|
|
let f1 = await Folder.save({ title: "folder" });
|
|
|
|
let n1 = await Note.save({ title: "mynote", parent_id: f1.id });
|
|
|
|
await synchronizer().start();
|
2017-07-18 21:27:10 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-07-18 21:27:10 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
await Note.save({ id: n1.id, is_conflict: 1 });
|
|
|
|
await Note.delete(n1.id);
|
|
|
|
const deletedItems = await BaseItem.deletedItems(syncTargetId());
|
2017-07-18 21:27:10 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
expect(deletedItems.length).toBe(0);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-12-19 21:01:29 +02:00
|
|
|
|
2017-12-14 02:23:32 +02:00
|
|
|
async function ignorableNoteConflictTest(withEncryption) {
|
2017-12-14 00:53:20 +02:00
|
|
|
if (withEncryption) {
|
|
|
|
Setting.setValue('encryption.enabled', true);
|
|
|
|
await loadEncryptionMasterKey();
|
|
|
|
}
|
2017-11-03 00:48:17 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: "un", is_todo: 1, parent_id: folder1.id });
|
|
|
|
await synchronizer().start();
|
2017-11-03 00:48:17 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-11-03 00:48:17 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await synchronizer().start();
|
2017-12-20 21:45:25 +02:00
|
|
|
if (withEncryption) {
|
|
|
|
await loadEncryptionMasterKey(null, true);
|
|
|
|
await decryptionWorker().start();
|
|
|
|
}
|
2017-12-04 20:16:14 +02:00
|
|
|
let note2 = await Note.load(note1.id);
|
|
|
|
note2.todo_completed = time.unixMs()-1;
|
|
|
|
await Note.save(note2);
|
|
|
|
note2 = await Note.load(note2.id);
|
|
|
|
await synchronizer().start();
|
2017-11-03 00:48:17 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(1);
|
2017-11-03 00:48:17 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
let note2conf = await Note.load(note1.id);
|
|
|
|
note2conf.todo_completed = time.unixMs();
|
|
|
|
await Note.save(note2conf);
|
|
|
|
note2conf = await Note.load(note1.id);
|
|
|
|
await synchronizer().start();
|
2017-11-03 00:48:17 +02:00
|
|
|
|
2017-12-14 00:53:20 +02:00
|
|
|
if (!withEncryption) {
|
|
|
|
// That was previously a common conflict:
|
|
|
|
// - Client 1 mark todo as "done", and sync
|
|
|
|
// - Client 2 doesn't sync, mark todo as "done" todo. Then sync.
|
|
|
|
// In theory it is a conflict because the todo_completed dates are different
|
|
|
|
// but in practice it doesn't matter, we can just take the date when the
|
|
|
|
// todo was marked as "done" the first time.
|
2017-11-03 00:48:17 +02:00
|
|
|
|
2017-12-14 00:53:20 +02:00
|
|
|
let conflictedNotes = await Note.conflictedNotes();
|
|
|
|
expect(conflictedNotes.length).toBe(0);
|
|
|
|
|
|
|
|
let notes = await Note.all();
|
|
|
|
expect(notes.length).toBe(1);
|
|
|
|
expect(notes[0].id).toBe(note1.id);
|
|
|
|
expect(notes[0].todo_completed).toBe(note2.todo_completed);
|
|
|
|
} else {
|
|
|
|
// If the notes are encrypted however it's not possible to do this kind of
|
|
|
|
// smart conflict resolving since we don't know the content, so in that
|
|
|
|
// case it's handled as a regular conflict.
|
|
|
|
|
|
|
|
let conflictedNotes = await Note.conflictedNotes();
|
|
|
|
expect(conflictedNotes.length).toBe(1);
|
|
|
|
|
|
|
|
let notes = await Note.all();
|
|
|
|
expect(notes.length).toBe(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should not consider it is a conflict if neither the title nor body of the note have changed', asyncTest(async () => {
|
2017-12-14 02:23:32 +02:00
|
|
|
await ignorableNoteConflictTest(false);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-11-21 20:17:50 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should always handle conflict if local or remote are encrypted', asyncTest(async () => {
|
2017-12-20 21:45:25 +02:00
|
|
|
await ignorableNoteConflictTest(true);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-12-20 21:45:25 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('items should be downloaded again when user cancels in the middle of delta operation', asyncTest(async () => {
|
2017-11-21 20:17:50 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: "un", is_todo: 1, parent_id: folder1.id });
|
|
|
|
await synchronizer().start();
|
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
await switchClient(2);
|
2017-11-21 20:17:50 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
synchronizer().debugFlags_ = ['cancelDeltaLoop2'];
|
|
|
|
let context = await synchronizer().start();
|
|
|
|
let notes = await Note.all();
|
|
|
|
expect(notes.length).toBe(0);
|
2017-12-04 01:06:02 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
synchronizer().debugFlags_ = [];
|
|
|
|
await synchronizer().start({ context: context });
|
|
|
|
notes = await Note.all();
|
|
|
|
expect(notes.length).toBe(1);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-11-21 20:17:50 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should skip items that cannot be synced', asyncTest(async () => {
|
2017-12-05 01:38:09 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: "un", is_todo: 1, parent_id: folder1.id });
|
|
|
|
const noteId = note1.id;
|
|
|
|
await synchronizer().start();
|
2017-12-13 20:57:40 +02:00
|
|
|
let disabledItems = await BaseItem.syncDisabledItems(syncTargetId());
|
2017-12-05 01:38:09 +02:00
|
|
|
expect(disabledItems.length).toBe(0);
|
|
|
|
await Note.save({ id: noteId, title: "un mod", });
|
2018-01-02 21:17:14 +02:00
|
|
|
synchronizer().debugFlags_ = ['rejectedByTarget'];
|
2017-12-05 01:38:09 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
synchronizer().debugFlags_ = [];
|
|
|
|
await synchronizer().start(); // Another sync to check that this item is now excluded from sync
|
|
|
|
|
|
|
|
await switchClient(2);
|
|
|
|
|
|
|
|
await synchronizer().start();
|
|
|
|
let notes = await Note.all();
|
|
|
|
expect(notes.length).toBe(1);
|
|
|
|
expect(notes[0].title).toBe('un');
|
|
|
|
|
|
|
|
await switchClient(1);
|
|
|
|
|
2017-12-13 20:57:40 +02:00
|
|
|
disabledItems = await BaseItem.syncDisabledItems(syncTargetId());
|
2017-12-05 01:38:09 +02:00
|
|
|
expect(disabledItems.length).toBe(1);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-12-05 01:38:09 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('notes and folders should get encrypted when encryption is enabled', asyncTest(async () => {
|
2017-12-13 20:57:40 +02:00
|
|
|
Setting.setValue('encryption.enabled', true);
|
|
|
|
const masterKey = await loadEncryptionMasterKey();
|
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: "un", body: 'to be encrypted', parent_id: folder1.id });
|
|
|
|
await synchronizer().start();
|
|
|
|
// After synchronisation, remote items should be encrypted but local ones remain plain text
|
|
|
|
note1 = await Note.load(note1.id);
|
|
|
|
expect(note1.title).toBe('un');
|
|
|
|
|
|
|
|
await switchClient(2);
|
|
|
|
|
|
|
|
await synchronizer().start();
|
|
|
|
let folder1_2 = await Folder.load(folder1.id);
|
|
|
|
let note1_2 = await Note.load(note1.id);
|
|
|
|
let masterKey_2 = await MasterKey.load(masterKey.id);
|
|
|
|
// On this side however it should be received encrypted
|
|
|
|
expect(!note1_2.title).toBe(true);
|
|
|
|
expect(!folder1_2.title).toBe(true);
|
|
|
|
expect(!!note1_2.encryption_cipher_text).toBe(true);
|
|
|
|
expect(!!folder1_2.encryption_cipher_text).toBe(true);
|
|
|
|
// Master key is already encrypted so it does not get re-encrypted during sync
|
|
|
|
expect(masterKey_2.content).toBe(masterKey.content);
|
|
|
|
expect(masterKey_2.checksum).toBe(masterKey.checksum);
|
|
|
|
// Now load the master key we got from client 1 and try to decrypt
|
|
|
|
await encryptionService().loadMasterKey(masterKey_2, '123456', true);
|
|
|
|
// Get the decrypted items back
|
|
|
|
await Folder.decrypt(folder1_2);
|
|
|
|
await Note.decrypt(note1_2);
|
|
|
|
folder1_2 = await Folder.load(folder1.id);
|
|
|
|
note1_2 = await Note.load(note1.id);
|
|
|
|
// Check that properties match the original items. Also check
|
|
|
|
// the encryption did not affect the updated_time timestamp.
|
|
|
|
expect(note1_2.title).toBe(note1.title);
|
|
|
|
expect(note1_2.body).toBe(note1.body);
|
|
|
|
expect(note1_2.updated_time).toBe(note1.updated_time);
|
|
|
|
expect(!note1_2.encryption_cipher_text).toBe(true);
|
|
|
|
expect(folder1_2.title).toBe(folder1.title);
|
|
|
|
expect(folder1_2.updated_time).toBe(folder1.updated_time);
|
|
|
|
expect(!folder1_2.encryption_cipher_text).toBe(true);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-12-13 20:57:40 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should enable encryption automatically when downloading new master key (and none was previously available)',asyncTest(async () => {
|
2017-12-17 21:51:45 +02:00
|
|
|
// Enable encryption on client 1 and sync an item
|
|
|
|
Setting.setValue('encryption.enabled', true);
|
|
|
|
await loadEncryptionMasterKey();
|
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
await synchronizer().start();
|
|
|
|
|
|
|
|
await switchClient(2);
|
|
|
|
|
|
|
|
// Synchronising should enable encryption since we're going to get a master key
|
|
|
|
expect(Setting.value('encryption.enabled')).toBe(false);
|
|
|
|
await synchronizer().start();
|
|
|
|
expect(Setting.value('encryption.enabled')).toBe(true);
|
|
|
|
|
|
|
|
// Check that we got the master key from client 1
|
|
|
|
const masterKey = (await MasterKey.all())[0];
|
|
|
|
expect(!!masterKey).toBe(true);
|
|
|
|
|
|
|
|
// Since client 2 hasn't supplied a password yet, no master key is currently loaded
|
|
|
|
expect(encryptionService().loadedMasterKeyIds().length).toBe(0);
|
|
|
|
|
2017-12-19 21:01:29 +02:00
|
|
|
// If we sync now, nothing should be sent to target since we don't have a password.
|
|
|
|
// Technically it's incorrect to set the property of an encrypted variable but it allows confirming
|
|
|
|
// that encryption doesn't work if user hasn't supplied a password.
|
2017-12-20 21:45:25 +02:00
|
|
|
await BaseItem.forceSync(folder1.id);
|
2017-12-17 21:51:45 +02:00
|
|
|
await synchronizer().start();
|
2017-12-19 21:01:29 +02:00
|
|
|
|
2017-12-17 21:51:45 +02:00
|
|
|
await switchClient(1);
|
2017-12-19 21:01:29 +02:00
|
|
|
|
2017-12-17 21:51:45 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
folder1 = await Folder.load(folder1.id);
|
|
|
|
expect(folder1.title).toBe('folder1'); // Still at old value
|
|
|
|
|
|
|
|
await switchClient(2);
|
|
|
|
|
|
|
|
// Now client 2 set the master key password
|
|
|
|
Setting.setObjectKey('encryption.passwordCache', masterKey.id, '123456');
|
|
|
|
await encryptionService().loadMasterKeysFromSettings();
|
|
|
|
|
|
|
|
// Now that master key should be loaded
|
|
|
|
expect(encryptionService().loadedMasterKeyIds()[0]).toBe(masterKey.id);
|
|
|
|
|
2017-12-19 21:01:29 +02:00
|
|
|
// Decrypt all the data. Now change the title and sync again - this time the changes should be transmitted
|
|
|
|
await decryptionWorker().start();
|
|
|
|
folder1_2 = await Folder.save({ id: folder1.id, title: "change test" });
|
|
|
|
|
2017-12-17 21:51:45 +02:00
|
|
|
// If we sync now, this time client 1 should get the changes we did earlier
|
|
|
|
await synchronizer().start();
|
|
|
|
|
|
|
|
await switchClient(1);
|
|
|
|
|
|
|
|
await synchronizer().start();
|
2017-12-19 21:01:29 +02:00
|
|
|
// Decrypt the data we just got
|
|
|
|
await decryptionWorker().start();
|
2017-12-17 21:51:45 +02:00
|
|
|
folder1 = await Folder.load(folder1.id);
|
|
|
|
expect(folder1.title).toBe('change test'); // Got title from client 2
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-12-17 21:51:45 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should encrypt existing notes too when enabling E2EE', asyncTest(async () => {
|
2017-12-17 21:51:45 +02:00
|
|
|
// First create a folder, without encryption enabled, and sync it
|
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
await synchronizer().start();
|
|
|
|
let files = await fileApi().list()
|
2017-12-19 21:01:29 +02:00
|
|
|
let content = await fileApi().get(files.items[0].path);
|
|
|
|
expect(content.indexOf('folder1') >= 0).toBe(true)
|
2017-12-17 21:51:45 +02:00
|
|
|
|
|
|
|
// Then enable encryption and sync again
|
2017-12-20 21:45:25 +02:00
|
|
|
let masterKey = await encryptionService().generateMasterKey('123456');
|
2017-12-17 21:51:45 +02:00
|
|
|
masterKey = await MasterKey.save(masterKey);
|
2017-12-20 21:45:25 +02:00
|
|
|
await encryptionService().enableEncryption(masterKey, '123456');
|
|
|
|
await encryptionService().loadMasterKeysFromSettings();
|
2017-12-17 21:51:45 +02:00
|
|
|
await synchronizer().start();
|
|
|
|
|
|
|
|
// Even though the folder has not been changed it should have been synced again so that
|
|
|
|
// an encrypted version of it replaces the decrypted version.
|
|
|
|
files = await fileApi().list()
|
|
|
|
expect(files.items.length).toBe(2);
|
|
|
|
// By checking that the folder title is not present, we can confirm that the item has indeed been encrypted
|
|
|
|
// One of the two items is the master key
|
2017-12-19 21:01:29 +02:00
|
|
|
content = await fileApi().get(files.items[0].path);
|
|
|
|
expect(content.indexOf('folder1') < 0).toBe(true);
|
|
|
|
content = await fileApi().get(files.items[1].path);
|
|
|
|
expect(content.indexOf('folder1') < 0).toBe(true);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-12-19 21:01:29 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should sync resources', asyncTest(async () => {
|
2017-12-19 21:01:29 +02:00
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
|
|
|
await shim.attachFileToNote(note1, __dirname + '/../tests/support/photo.jpg');
|
|
|
|
let resource1 = (await Resource.all())[0];
|
|
|
|
let resourcePath1 = Resource.fullPath(resource1);
|
|
|
|
await synchronizer().start();
|
2017-12-20 21:45:25 +02:00
|
|
|
expect((await fileApi().list()).items.length).toBe(3);
|
2017-12-19 21:01:29 +02:00
|
|
|
|
|
|
|
await switchClient(2);
|
|
|
|
|
|
|
|
await synchronizer().start();
|
2017-12-20 21:45:25 +02:00
|
|
|
let allResources = await Resource.all();
|
|
|
|
expect(allResources.length).toBe(1);
|
|
|
|
let resource1_2 = allResources[0];
|
2017-12-19 21:01:29 +02:00
|
|
|
let resourcePath1_2 = Resource.fullPath(resource1_2);
|
|
|
|
|
|
|
|
expect(resource1_2.id).toBe(resource1.id);
|
|
|
|
expect(fileContentEqual(resourcePath1, resourcePath1_2)).toBe(true);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-12-17 21:51:45 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should encryt resources', asyncTest(async () => {
|
2017-12-19 21:01:29 +02:00
|
|
|
Setting.setValue('encryption.enabled', true);
|
|
|
|
const masterKey = await loadEncryptionMasterKey();
|
|
|
|
|
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
|
|
|
await shim.attachFileToNote(note1, __dirname + '/../tests/support/photo.jpg');
|
|
|
|
let resource1 = (await Resource.all())[0];
|
|
|
|
let resourcePath1 = Resource.fullPath(resource1);
|
|
|
|
await synchronizer().start();
|
|
|
|
|
|
|
|
await switchClient(2);
|
|
|
|
|
|
|
|
await synchronizer().start();
|
|
|
|
Setting.setObjectKey('encryption.passwordCache', masterKey.id, '123456');
|
|
|
|
await encryptionService().loadMasterKeysFromSettings();
|
|
|
|
|
|
|
|
let resource1_2 = (await Resource.all())[0];
|
|
|
|
resource1_2 = await Resource.decrypt(resource1_2);
|
|
|
|
let resourcePath1_2 = Resource.fullPath(resource1_2);
|
|
|
|
|
|
|
|
expect(fileContentEqual(resourcePath1, resourcePath1_2)).toBe(true);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-12-19 21:01:29 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should upload decrypted items to sync target after encryption disabled', asyncTest(async () => {
|
2017-12-20 21:45:25 +02:00
|
|
|
Setting.setValue('encryption.enabled', true);
|
|
|
|
const masterKey = await loadEncryptionMasterKey();
|
|
|
|
|
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
await synchronizer().start();
|
|
|
|
|
|
|
|
let allEncrypted = await allSyncTargetItemsEncrypted();
|
|
|
|
expect(allEncrypted).toBe(true);
|
|
|
|
|
|
|
|
await encryptionService().disableEncryption();
|
|
|
|
|
|
|
|
await synchronizer().start();
|
|
|
|
allEncrypted = await allSyncTargetItemsEncrypted();
|
|
|
|
expect(allEncrypted).toBe(false);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-12-20 21:45:25 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should not upload any item if encryption was enabled, and items have not been decrypted, and then encryption disabled', asyncTest(async () => {
|
2017-12-20 21:45:25 +02:00
|
|
|
// For some reason I can't explain, this test is sometimes executed before beforeEach is finished
|
|
|
|
// which means it's going to fail in unexpected way. So the loop below wait for beforeEach to be done.
|
|
|
|
while (insideBeforeEach) await time.msleep(100);
|
|
|
|
|
|
|
|
Setting.setValue('encryption.enabled', true);
|
|
|
|
const masterKey = await loadEncryptionMasterKey();
|
|
|
|
|
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
await synchronizer().start();
|
|
|
|
|
|
|
|
await switchClient(2);
|
|
|
|
|
|
|
|
await synchronizer().start();
|
|
|
|
expect(Setting.value('encryption.enabled')).toBe(true);
|
|
|
|
|
|
|
|
// If we try to disable encryption now, it should throw an error because some items are
|
|
|
|
// currently encrypted. They must be decrypted first so that they can be sent as
|
|
|
|
// plain text to the sync target.
|
2018-01-02 21:17:14 +02:00
|
|
|
//let hasThrown = await checkThrowAsync(async () => await encryptionService().disableEncryption());
|
|
|
|
//expect(hasThrown).toBe(true);
|
2017-12-20 21:45:25 +02:00
|
|
|
|
|
|
|
// Now supply the password, and decrypt the items
|
|
|
|
Setting.setObjectKey('encryption.passwordCache', masterKey.id, '123456');
|
|
|
|
await encryptionService().loadMasterKeysFromSettings();
|
|
|
|
await decryptionWorker().start();
|
|
|
|
|
|
|
|
// Try to disable encryption again
|
|
|
|
hasThrown = await checkThrowAsync(async () => await encryptionService().disableEncryption());
|
|
|
|
expect(hasThrown).toBe(false);
|
|
|
|
|
|
|
|
// If we sync now the target should receive the decrypted items
|
|
|
|
await synchronizer().start();
|
|
|
|
allEncrypted = await allSyncTargetItemsEncrypted();
|
|
|
|
expect(allEncrypted).toBe(false);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-12-20 21:45:25 +02:00
|
|
|
|
2018-01-02 21:17:14 +02:00
|
|
|
it('should encrypt remote resources after encryption has been enabled', asyncTest(async () => {
|
2017-12-20 21:43:43 +02:00
|
|
|
while (insideBeforeEach) await time.msleep(100);
|
|
|
|
|
|
|
|
let folder1 = await Folder.save({ title: "folder1" });
|
|
|
|
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
|
|
|
await shim.attachFileToNote(note1, __dirname + '/../tests/support/photo.jpg');
|
|
|
|
let resource1 = (await Resource.all())[0];
|
|
|
|
await synchronizer().start();
|
|
|
|
|
|
|
|
expect(await allSyncTargetItemsEncrypted()).toBe(false);
|
|
|
|
|
|
|
|
const masterKey = await loadEncryptionMasterKey();
|
|
|
|
await encryptionService().enableEncryption(masterKey, '123456');
|
|
|
|
await encryptionService().loadMasterKeysFromSettings();
|
|
|
|
|
|
|
|
await synchronizer().start();
|
|
|
|
|
|
|
|
expect(await allSyncTargetItemsEncrypted()).toBe(true);
|
2018-01-02 21:17:14 +02:00
|
|
|
}));
|
2017-12-20 21:43:43 +02:00
|
|
|
|
2017-06-19 00:06:10 +02:00
|
|
|
});
|