2017-02-01 13:18:59 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/* eslint-env es6, node */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-07-10 14:53:00 +00:00
|
|
|
/* global REALM_MODULE_PATH */
|
|
|
|
|
2017-02-01 13:18:59 +00:00
|
|
|
const Realm = require('realm');
|
|
|
|
const TestCase = require('./asserts');
|
2017-05-11 20:22:28 +00:00
|
|
|
|
2017-05-12 12:38:44 +00:00
|
|
|
const isNodeProccess = (typeof process === 'object' && process + '' === '[object process]');
|
2017-09-01 15:12:06 +00:00
|
|
|
|
2017-05-12 12:38:44 +00:00
|
|
|
function node_require(module) {
|
|
|
|
return require(module);
|
2017-09-27 13:59:21 +00:00
|
|
|
}
|
2017-05-12 12:38:44 +00:00
|
|
|
|
|
|
|
let tmp;
|
|
|
|
let fs;
|
|
|
|
let execFile;
|
2017-09-29 18:43:04 +00:00
|
|
|
let path;
|
2017-05-12 12:38:44 +00:00
|
|
|
|
|
|
|
if (isNodeProccess) {
|
|
|
|
tmp = node_require('tmp');
|
|
|
|
fs = node_require('fs');
|
|
|
|
execFile = node_require('child_process').execFile;
|
|
|
|
tmp.setGracefulCleanup();
|
2017-09-29 18:43:04 +00:00
|
|
|
path = node_require("path");
|
2017-05-12 12:38:44 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 13:18:59 +00:00
|
|
|
|
|
|
|
function uuid() {
|
2017-05-11 20:22:28 +00:00
|
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
|
|
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
|
|
return v.toString(16);
|
|
|
|
});
|
2017-02-01 13:18:59 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 20:08:54 +00:00
|
|
|
function promisifiedRegister(server, username, password) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.Sync.User.register(server, username, password, (error, user) => {
|
|
|
|
if (error) {
|
|
|
|
console.log(`promisifiedRegister ${error}`);
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function promisifiedLogin(server, username, password) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.Sync.User.login(server, username, password, (error, user) => {
|
|
|
|
if (error) {
|
|
|
|
console.log(`promisifiedLogin ${error}`);
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-29 18:43:04 +00:00
|
|
|
function copyFileToTempDir(filename) {
|
|
|
|
let tmpDir = tmp.dirSync();
|
|
|
|
let content = fs.readFileSync(filename);
|
|
|
|
let tmpFile = tmp.fileSync({ dir: tmpDir.name });
|
|
|
|
fs.appendFileSync(tmpFile.fd, content);
|
|
|
|
return tmpFile.name;
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:39:31 +00:00
|
|
|
function runOutOfProcess(nodeJsFilePath) {
|
|
|
|
var nodeArgs = Array.prototype.slice.call(arguments);
|
|
|
|
let tmpDir = tmp.dirSync();
|
|
|
|
let content = fs.readFileSync(nodeJsFilePath, 'utf8');
|
|
|
|
let tmpFile = tmp.fileSync({ dir: tmpDir.name });
|
|
|
|
fs.appendFileSync(tmpFile.fd, content, { encoding: 'utf8' });
|
|
|
|
nodeArgs[0] = tmpFile.name;
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-09-01 15:12:06 +00:00
|
|
|
try {
|
|
|
|
console.log('runOutOfProcess command\n node ' + nodeArgs.join(" "));
|
|
|
|
const child = execFile('node', nodeArgs, { cwd: tmpDir.name }, (error, stdout, stderr) => {
|
|
|
|
if (error) {
|
|
|
|
console.error("runOutOfProcess failed\n" + error);
|
|
|
|
reject(new Error(`Running ${nodeJsFilePath} failed. error: ${error}`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('runOutOfProcess success\n' + stdout);
|
|
|
|
resolve();
|
|
|
|
});
|
2017-06-28 10:39:31 +00:00
|
|
|
}
|
2017-09-01 15:12:06 +00:00
|
|
|
catch (e) {
|
|
|
|
reject(e);
|
2017-09-14 08:35:02 +00:00
|
|
|
}
|
2017-09-01 15:12:06 +00:00
|
|
|
});
|
2017-06-28 10:39:31 +00:00
|
|
|
}
|
|
|
|
|
2017-05-11 20:22:28 +00:00
|
|
|
module.exports = {
|
2017-05-06 23:26:14 +00:00
|
|
|
|
2017-05-11 20:54:29 +00:00
|
|
|
testLocalRealmHasNoSession() {
|
|
|
|
let realm = new Realm();
|
|
|
|
TestCase.assertNull(realm.syncSession);
|
|
|
|
},
|
|
|
|
|
|
|
|
testProperties() {
|
2017-09-12 20:08:54 +00:00
|
|
|
return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => {
|
2017-05-11 20:54:29 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
function checkSuccess() {
|
|
|
|
successCounter++;
|
|
|
|
if (successCounter == 2) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function postTokenRefreshChecks(sender, error) {
|
|
|
|
try {
|
|
|
|
TestCase.assertEqual(error, accessTokenRefreshed);
|
|
|
|
TestCase.assertEqual(sender.url, `realm://localhost:9080/${user.identity}/myrealm`);
|
|
|
|
checkSuccess();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(e)
|
|
|
|
}
|
2017-07-10 14:53:00 +00:00
|
|
|
}
|
2017-05-11 20:54:29 +00:00
|
|
|
|
|
|
|
// Let the error handler trigger our checks when the access token was refreshed.
|
|
|
|
postTokenRefreshChecks._notifyOnAccessTokenRefreshed = accessTokenRefreshed;
|
|
|
|
|
|
|
|
const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm', error: postTokenRefreshChecks } };
|
|
|
|
const realm = new Realm(config);
|
|
|
|
const session = realm.syncSession;
|
|
|
|
TestCase.assertInstanceOf(session, Realm.Sync.Session);
|
|
|
|
TestCase.assertEqual(session.user.identity, user.identity);
|
|
|
|
TestCase.assertEqual(session.config.url, config.sync.url);
|
|
|
|
TestCase.assertEqual(session.config.user.identity, config.sync.user.identity);
|
|
|
|
TestCase.assertUndefined(session.url);
|
|
|
|
TestCase.assertEqual(session.state, 'active');
|
|
|
|
checkSuccess();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-05-11 20:22:28 +00:00
|
|
|
|
|
|
|
testRealmOpen() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2017-05-11 20:54:29 +00:00
|
|
|
|
2017-05-11 20:22:28 +00:00
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2017-06-28 10:39:31 +00:00
|
|
|
.then(() => {
|
2017-09-12 20:08:54 +00:00
|
|
|
return promisifiedLogin('http://localhost:9080', username, 'password').then(user => {
|
2017-06-28 10:39:31 +00:00
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
|
|
|
|
let config = {
|
|
|
|
sync: { user, url: `realm://localhost:9080/~/${realmName}` },
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
|
|
|
|
return Realm.open(config)
|
|
|
|
.then(realm => {
|
|
|
|
let actualObjectsCount = realm.objects('Dog').length;
|
|
|
|
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count");
|
|
|
|
return realm.syncSession;
|
|
|
|
}).then(session => {
|
|
|
|
TestCase.assertInstanceOf(session, Realm.Sync.Session);
|
|
|
|
TestCase.assertEqual(session.user.identity, user.identity);
|
|
|
|
TestCase.assertEqual(session.config.url, config.sync.url);
|
|
|
|
TestCase.assertEqual(session.config.user.identity, config.sync.user.identity);
|
|
|
|
TestCase.assertEqual(session.state, 'active');
|
|
|
|
});
|
|
|
|
});
|
2017-02-01 13:18:59 +00:00
|
|
|
});
|
2017-05-11 20:54:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testRealmOpenAsync() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
2017-05-11 20:22:28 +00:00
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2017-05-11 20:54:29 +00:00
|
|
|
.then(() => {
|
2017-08-24 18:01:12 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user => {
|
2017-05-11 20:54:29 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
|
|
|
|
let config = {
|
|
|
|
sync: { user, url: `realm://localhost:9080/~/${realmName}` },
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.openAsync(config, (error, realm) => {
|
|
|
|
try {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
let actualObjectsCount = realm.objects('Dog').length;
|
|
|
|
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count");
|
|
|
|
|
2017-06-19 11:58:19 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
try {
|
|
|
|
const session = realm.syncSession;
|
|
|
|
TestCase.assertInstanceOf(session, Realm.Sync.Session);
|
|
|
|
TestCase.assertEqual(session.user.identity, user.identity);
|
|
|
|
TestCase.assertEqual(session.config.url, config.sync.url);
|
|
|
|
TestCase.assertEqual(session.config.user.identity, config.sync.user.identity);
|
|
|
|
TestCase.assertEqual(session.state, 'active');
|
|
|
|
resolve();
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
}, 50);
|
2017-05-19 05:59:03 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-06-27 20:45:46 +00:00
|
|
|
testProgressNotificationsForRealmOpen() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2017-06-28 10:39:31 +00:00
|
|
|
.then(() => {
|
2017-08-24 18:01:12 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user => {
|
2017-06-28 10:39:31 +00:00
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
let progressNotificationCalled = false;
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
url: `realm://localhost:9080/~/${realmName}`,
|
2017-06-29 11:28:08 +00:00
|
|
|
_onDownloadProgress: (transferred, total) => {
|
2017-06-28 10:39:31 +00:00
|
|
|
progressNotificationCalled = true
|
|
|
|
},
|
2017-06-27 20:45:46 +00:00
|
|
|
},
|
2017-06-28 10:39:31 +00:00
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
2017-06-27 20:45:46 +00:00
|
|
|
|
2017-06-28 10:39:31 +00:00
|
|
|
return Realm.open(config)
|
|
|
|
.then(realm => {
|
2017-08-07 03:28:01 +00:00
|
|
|
return realm.syncSession;
|
2017-06-28 10:39:31 +00:00
|
|
|
}).then(session => {
|
|
|
|
TestCase.assertTrue(progressNotificationCalled, "Progress notification not called for Realm.open");
|
|
|
|
});
|
|
|
|
});
|
2017-06-27 20:45:46 +00:00
|
|
|
});
|
|
|
|
},
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2017-06-27 20:45:46 +00:00
|
|
|
testProgressNotificationsForRealmOpenAsync() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2017-06-27 20:45:46 +00:00
|
|
|
.then(() => {
|
2017-08-24 18:01:12 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user => {
|
2017-06-27 20:45:46 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let progressNotificationCalled = false;
|
|
|
|
let config = {
|
|
|
|
sync: { user, url: `realm://localhost:9080/~/${realmName}`,
|
2017-09-27 13:59:21 +00:00
|
|
|
_onDownloadProgress: (transferred, total) => {
|
2017-06-27 20:45:46 +00:00
|
|
|
progressNotificationCalled = true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.openAsync(config, (error, realm) => {
|
|
|
|
try {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
try {
|
|
|
|
TestCase.assertTrue(progressNotificationCalled, "Progress notification not called for Realm.openAsync");
|
|
|
|
resolve();
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
}, 50);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-05-19 05:59:03 +00:00
|
|
|
testRealmOpenAsyncNoSchema() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2017-05-19 05:59:03 +00:00
|
|
|
.then(() => {
|
2017-08-24 18:01:12 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user => {
|
2017-05-19 05:59:03 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
|
|
|
|
let config = {
|
|
|
|
sync: { user, url: `realm://localhost:9080/~/${realmName}` }
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.openAsync(config, (error, realm) => {
|
|
|
|
try {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
let actualObjectsCount = realm.objects('Dog').length;
|
|
|
|
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count");
|
|
|
|
|
|
|
|
let firstDog = realm.objects('Dog')[0];
|
|
|
|
TestCase.assertTrue(({}).hasOwnProperty.call(firstDog, 'name'), "Synced realm does not have an inffered schema");
|
|
|
|
TestCase.assertTrue(firstDog.name, "Synced realm object's property should have a value");
|
|
|
|
TestCase.assertTrue(firstDog.name.indexOf('Lassy') !== -1, "Synced realm object's property should contain the actual written value");
|
|
|
|
|
|
|
|
|
2017-05-11 20:54:29 +00:00
|
|
|
const session = realm.syncSession;
|
|
|
|
TestCase.assertInstanceOf(session, Realm.Sync.Session);
|
|
|
|
TestCase.assertEqual(session.user.identity, user.identity);
|
|
|
|
TestCase.assertEqual(session.config.url, config.sync.url);
|
|
|
|
TestCase.assertEqual(session.config.user.identity, config.sync.user.identity);
|
|
|
|
TestCase.assertEqual(session.state, 'active');
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-05-19 06:18:48 +00:00
|
|
|
testRealmOpenLocalRealm() {
|
|
|
|
const username = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
|
|
|
|
let config = {
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.open(config).then(realm => {
|
|
|
|
realm.write(() => {
|
|
|
|
for (let i = 1; i <= 3; i++) {
|
|
|
|
realm.create('Dog', { name: `Lassy ${i}` });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let actualObjectsCount = realm.objects('Dog').length;
|
|
|
|
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Local realm does not contain the expected objects count");
|
|
|
|
resolve();
|
|
|
|
}).catch(error => reject(error));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testRealmOpenAsyncLocalRealm() {
|
|
|
|
const username = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
|
|
|
|
let config = {
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.openAsync(config, (error, realm) => {
|
|
|
|
try {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
realm.write(() => {
|
|
|
|
for (let i = 1; i <= 3; i++) {
|
|
|
|
realm.create('Dog', { name: `Lassy ${i}` });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let actualObjectsCount = realm.objects('Dog').length;
|
|
|
|
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Local realm does not contain the expected objects count");
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-05-11 20:54:29 +00:00
|
|
|
testErrorHandling() {
|
2017-08-24 18:01:12 +00:00
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
2017-05-11 20:54:29 +00:00
|
|
|
return new Promise((resolve, _reject) => {
|
|
|
|
const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm' } };
|
|
|
|
config.sync.error = (sender, error) => {
|
|
|
|
try {
|
|
|
|
TestCase.assertEqual(error.message, 'simulated error');
|
|
|
|
TestCase.assertEqual(error.code, 123);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
_reject(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const realm = new Realm(config);
|
|
|
|
const session = realm.syncSession;
|
|
|
|
|
|
|
|
TestCase.assertEqual(session.config.error, config.sync.error);
|
|
|
|
session._simulateError(123, 'simulated error');
|
|
|
|
});
|
|
|
|
});
|
2017-09-01 15:12:06 +00:00
|
|
|
},
|
|
|
|
|
2017-09-29 18:43:04 +00:00
|
|
|
testIncompatibleSyncedRealmOpen() {
|
|
|
|
let realm = "sync-v1.realm";
|
|
|
|
if (isNodeProccess) {
|
|
|
|
realm = copyFileToTempDir(path.join(process.cwd(), "data", realm));
|
|
|
|
}
|
2017-09-30 13:43:40 +00:00
|
|
|
else {
|
|
|
|
//copy the bundled RN realm files for the test
|
|
|
|
Realm.copyBundledRealmFiles();
|
|
|
|
}
|
2017-09-29 18:43:04 +00:00
|
|
|
|
2017-09-19 09:31:58 +00:00
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
|
|
|
return new Promise((resolve, _reject) => {
|
2017-09-29 18:43:04 +00:00
|
|
|
const config = {
|
|
|
|
path: realm,
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
error : err => cosole.log(err),
|
|
|
|
url: 'realm://localhost:9080/~/sync-v1'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.open(config)
|
|
|
|
.then(realm =>
|
|
|
|
_reject("Should fail with IncompatibleSyncedRealmError"))
|
|
|
|
.catch(e => {
|
|
|
|
if (e.name == "IncompatibleSyncedRealmError") {
|
|
|
|
const backupRealm = new Realm(e.configuration);
|
|
|
|
TestCase.assertEqual(backupRealm.objects('Dog').length, 3);
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
2017-10-02 08:33:56 +00:00
|
|
|
|
|
|
|
function printObject(o) {
|
|
|
|
var out = '';
|
|
|
|
for (var p in o) {
|
|
|
|
out += p + ': ' + o[p] + '\n';
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2017-09-29 18:43:04 +00:00
|
|
|
|
2017-10-02 08:33:56 +00:00
|
|
|
_reject("Failed with unexpected error " + printObject(e));
|
2017-09-29 18:43:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testIncompatibleSyncedRealmOpenAsync() {
|
|
|
|
let realm = "sync-v1.realm";
|
|
|
|
if (isNodeProccess) {
|
|
|
|
realm = copyFileToTempDir(path.join(process.cwd(), "data", realm));
|
|
|
|
}
|
2017-09-30 13:43:40 +00:00
|
|
|
else {
|
|
|
|
//copy the bundled RN realm files for the test
|
|
|
|
Realm.copyBundledRealmFiles();
|
|
|
|
}
|
2017-09-29 18:43:04 +00:00
|
|
|
|
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
|
|
|
return new Promise((resolve, _reject) => {
|
|
|
|
const config = {
|
|
|
|
path: realm,
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
error : err => cosole.log(err),
|
|
|
|
url: 'realm://localhost:9080/~/sync-v1'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.openAsync(config, (error, realm) => {
|
|
|
|
if (!error) {
|
|
|
|
_reject("Should fail with IncompatibleSyncedRealmError");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.name == "IncompatibleSyncedRealmError") {
|
|
|
|
const backupRealm = new Realm(error.configuration);
|
|
|
|
TestCase.assertEqual(backupRealm.objects('Dog').length, 3);
|
2017-09-19 09:31:58 +00:00
|
|
|
resolve();
|
2017-09-29 18:43:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_reject("Failed with unexpected error" + JSON.stringify(error));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testIncompatibleSyncedRealmConsructor() {
|
|
|
|
let realm = "sync-v1.realm";
|
|
|
|
if (isNodeProccess) {
|
|
|
|
realm = copyFileToTempDir(path.join(process.cwd(), "data", realm));
|
|
|
|
}
|
2017-09-30 13:43:40 +00:00
|
|
|
else {
|
|
|
|
//copy the bundled RN realm files for the test
|
|
|
|
Realm.copyBundledRealmFiles();
|
|
|
|
}
|
2017-09-29 18:43:04 +00:00
|
|
|
|
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
|
|
|
return new Promise((resolve, _reject) => {
|
|
|
|
const config = {
|
|
|
|
path: realm,
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
error : err => cosole.log(err),
|
|
|
|
url: 'realm://localhost:9080/~/sync-v1'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const realm = new Realm(config);
|
|
|
|
_reject("Should fail with IncompatibleSyncedRealmError");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (e.name == "IncompatibleSyncedRealmError") {
|
|
|
|
const backupRealm = new Realm(e.configuration);
|
|
|
|
TestCase.assertEqual(backupRealm.objects('Dog').length, 3);
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_reject("Failed with unexpected error" + JSON.stringify(e));
|
2017-09-19 09:31:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-09-01 15:12:06 +00:00
|
|
|
|
|
|
|
testProgressNotificationsForRealmConstructor() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
|
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
|
|
|
.then(() => {
|
2017-09-12 20:08:54 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user => {
|
2017-09-01 15:12:06 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
url: `realm://localhost:9080/~/${realmName}`
|
|
|
|
},
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
|
|
|
|
let realm = new Realm(config);
|
|
|
|
const progressCallback = (transferred, total) => {
|
|
|
|
resolve();
|
|
|
|
};
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
realm.syncSession.addProgressNotification('download', 'reportIndefinitely', progressCallback);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
reject("Progress Notifications API failed to call progress callback for Realm constructor");
|
|
|
|
}, 5000);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-09-27 13:59:21 +00:00
|
|
|
},
|
2017-09-01 15:12:06 +00:00
|
|
|
|
|
|
|
testProgressNotificationsUnregisterForRealmConstructor() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
|
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
|
|
|
.then(() => {
|
2017-09-12 20:08:54 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user => {
|
2017-09-01 15:12:06 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
url: `realm://localhost:9080/~/${realmName}`
|
|
|
|
},
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
|
|
|
|
let realm = new Realm(config);
|
|
|
|
let unregisterFunc;
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
let writeDataFunc = () => {
|
|
|
|
realm.write(() => {
|
|
|
|
for (let i = 1; i <= 3; i++) {
|
|
|
|
realm.create('Dog', { name: `Lassy ${i}` });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let syncFinished = false;
|
|
|
|
let failOnCall = false;
|
|
|
|
const progressCallback = (transferred, total) => {
|
|
|
|
if (failOnCall) {
|
|
|
|
reject(new Error("Progress callback should not be called after removeProgressNotification"));
|
|
|
|
}
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
syncFinished = transferred === total;
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
//unregister and write some new data.
|
2017-09-27 13:59:21 +00:00
|
|
|
if (syncFinished) {
|
2017-09-01 15:12:06 +00:00
|
|
|
failOnCall = true;
|
|
|
|
unregisterFunc();
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
//use second callback to wait for sync finished
|
|
|
|
realm.syncSession.addProgressNotification('upload', 'reportIndefinitely', (x, y) => {
|
|
|
|
if (x === y) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
writeDataFunc();
|
|
|
|
}
|
|
|
|
};
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
realm.syncSession.addProgressNotification('upload', 'reportIndefinitely', progressCallback);
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
unregisterFunc = () => {
|
|
|
|
realm.syncSession.removeProgressNotification(progressCallback);
|
|
|
|
};
|
|
|
|
|
|
|
|
writeDataFunc();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-09-14 08:35:02 +00:00
|
|
|
testProgressNotificationsForRealmOpen2() {
|
2017-09-01 15:12:06 +00:00
|
|
|
if (!isNodeProccess) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
|
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
|
|
|
.then(() => {
|
2017-09-12 20:08:54 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user => {
|
2017-09-01 15:12:06 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
url: `realm://localhost:9080/~/${realmName}`
|
|
|
|
},
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
|
|
|
|
let progressCalled = false;
|
|
|
|
Realm.open(config)
|
|
|
|
.progress((transferred, total) => {
|
|
|
|
progressCalled = true;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
TestCase.assertTrue(progressCalled);
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch((e) => reject(e));
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
reject("Progress Notifications API failed to call progress callback for Realm constructor");
|
|
|
|
}, 5000);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-09-27 13:59:21 +00:00
|
|
|
},
|
2017-09-01 15:12:06 +00:00
|
|
|
|
2017-09-14 08:35:02 +00:00
|
|
|
testProgressNotificationsForRealmOpenAsync2() {
|
2017-09-01 15:12:06 +00:00
|
|
|
if (!isNodeProccess) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
|
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
|
|
|
.then(() => {
|
2017-09-12 20:08:54 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user => {
|
2017-09-01 15:12:06 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
url: `realm://localhost:9080/~/${realmName}`
|
|
|
|
},
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
|
|
|
|
let progressCalled = false;
|
2017-09-27 13:59:21 +00:00
|
|
|
|
|
|
|
Realm.openAsync(config,
|
2017-09-06 07:47:42 +00:00
|
|
|
(error, realm) => {
|
2017-09-01 15:12:06 +00:00
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
TestCase.assertTrue(progressCalled);
|
|
|
|
resolve();
|
2017-09-13 09:37:44 +00:00
|
|
|
},
|
|
|
|
(transferred, total) => {
|
|
|
|
progressCalled = true;
|
2017-09-01 15:12:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
reject("Progress Notifications API failed to call progress callback for Realm constructor");
|
|
|
|
}, 5000);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-09-27 13:59:21 +00:00
|
|
|
},
|
2017-10-02 17:44:24 +00:00
|
|
|
|
|
|
|
testClientReset() {
|
|
|
|
// FIXME: try to enable for React Native
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
|
|
|
return new Promise((resolve, _reject) => {
|
|
|
|
var realm;
|
|
|
|
const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm' } };
|
|
|
|
config.sync.error = (sender, error) => {
|
|
|
|
try {
|
|
|
|
TestCase.assertEqual(error.code, 7); // 7 -> client reset
|
|
|
|
TestCase.assertDefined(error.config);
|
|
|
|
TestCase.assertNotEqual(error.config.path, '');
|
|
|
|
const original_path = realm.path;
|
|
|
|
realm.close();
|
|
|
|
Realm.Sync.initiateClientReset(original_path);
|
|
|
|
// copy required objects from Realm at error.config.path
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
_reject(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
realm = new Realm(config);
|
|
|
|
const session = realm.syncSession;
|
|
|
|
|
|
|
|
TestCase.assertEqual(session.config.error, config.sync.error);
|
|
|
|
session._simulateError(211, 'ClientReset'); // 211 -> divering histories
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-08-07 03:28:01 +00:00
|
|
|
}
|