Update tests to not use deprecated APIs
Switch to the newer promise-based APIs rather than the callback versions.
This commit is contained in:
parent
c532c139d9
commit
8972a20459
|
@ -3,27 +3,20 @@ function node_require(module) {
|
|||
return require(module);
|
||||
}
|
||||
|
||||
var Realm = node_require('realm');
|
||||
const Realm = node_require('realm');
|
||||
|
||||
const adminName = "realm-admin"
|
||||
const password = '';
|
||||
|
||||
exports.createAdminUser = function () {
|
||||
return new Promise((resolve, reject) => {
|
||||
Realm.Sync.User.login('http://localhost:9080', adminName, password, (error, user) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
return Realm.Sync.User.login('http://localhost:9080', adminName, password).then((user) => {
|
||||
if (!user.isAdmin) {
|
||||
throw new Error(`${adminName} user is not an admin user on this server`);
|
||||
}
|
||||
|
||||
if (!user.isAdmin) {
|
||||
reject(adminName + " user is not an admin user on this server");
|
||||
}
|
||||
|
||||
resolve({
|
||||
username: adminName,
|
||||
password
|
||||
});
|
||||
});
|
||||
return {
|
||||
username: adminName,
|
||||
password
|
||||
};
|
||||
});
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ const username = process.argv[2];
|
|||
const realmName = process.argv[3];
|
||||
const realmModule = process.argv[4];
|
||||
|
||||
var Realm = require(realmModule);
|
||||
const Realm = require(realmModule);
|
||||
|
||||
function createObjects(user) {
|
||||
const config = {
|
||||
|
@ -18,7 +18,7 @@ function createObjects(user) {
|
|||
schema: [{ name: 'Dog', properties: { name: 'string' } }]
|
||||
};
|
||||
|
||||
var realm = new Realm(config);
|
||||
const realm = new Realm(config);
|
||||
|
||||
realm.write(() => {
|
||||
for (let i = 1; i <= 3; i++) {
|
||||
|
@ -27,24 +27,29 @@ function createObjects(user) {
|
|||
});
|
||||
|
||||
console.log("Dogs count " + realm.objects('Dog').length);
|
||||
setTimeout(() => process.exit(0), 3000);
|
||||
|
||||
let session = realm.syncSession;
|
||||
return new Promise((resolve, reject) => {
|
||||
let callback = (transferred, total) => {
|
||||
if (transferred === total) {
|
||||
session.removeProgressNotification(callback);
|
||||
resolve(realm);
|
||||
}
|
||||
}
|
||||
session.addProgressNotification('upload', 'forCurrentlyOutstandingWork', callback);
|
||||
});
|
||||
}
|
||||
|
||||
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, registeredUser) => {
|
||||
if (error) {
|
||||
const registrationError = JSON.stringify(error);
|
||||
Realm.Sync.User.login('http://localhost:9080', username, 'password', (err, loggedUser) => {
|
||||
if (err) {
|
||||
const loginError = JSON.stringify(err);
|
||||
console.error("download-api-helper failed:\n User.register() error:\n" + err + "\n" + registrationError + "\n User.login() error:\n" + loginError);
|
||||
process.exit(-2);
|
||||
}
|
||||
else {
|
||||
createObjects(loggedUser);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
createObjects(registeredUser);
|
||||
}
|
||||
});
|
||||
let registrationError;
|
||||
Realm.Sync.User.register('http://localhost:9080', username, 'password')
|
||||
.catch((error) => {
|
||||
registrationError = JSON.stringify(error);
|
||||
return Realm.Sync.User.login('http://localhost:9080', username, 'password')
|
||||
})
|
||||
.catch((error) => {
|
||||
const loginError = JSON.stringify(err);
|
||||
console.error(`download-api-helper failed:\n User.register() error:\n${registrationError}\n User.login() error:\n${registrationError}`);
|
||||
process.exit(-2);
|
||||
})
|
||||
.then((user) => createObjects(user))
|
||||
.then(() => process.exit(0));
|
||||
|
|
|
@ -30,24 +30,15 @@ function uuid() {
|
|||
}
|
||||
|
||||
function createUsersWithTestRealms(count) {
|
||||
const createUserWithTestRealm = username => new Promise((resolve, reject) => {
|
||||
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
}
|
||||
else {
|
||||
new Realm({ sync: { user, url: 'realm://localhost:9080/~/test'}}).close();
|
||||
resolve(user);
|
||||
}
|
||||
})
|
||||
});
|
||||
const createUserWithTestRealm = () => {
|
||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password')
|
||||
.then(user => {
|
||||
new Realm({sync: {user, url: 'realm://localhost:9080/~/test'}}).close();
|
||||
return user;
|
||||
});
|
||||
};
|
||||
|
||||
// Generate some usernames
|
||||
const usernames = new Array(count).fill(undefined).map(uuid);
|
||||
|
||||
// And turn them into users and realms
|
||||
const userPromises = usernames.map(createUserWithTestRealm);
|
||||
return Promise.all(userPromises);
|
||||
return Promise.all(Array.from({length: count}, createUserWithTestRealm));
|
||||
}
|
||||
|
||||
function wait(t) {
|
||||
|
@ -103,28 +94,29 @@ module.exports = {
|
|||
},
|
||||
|
||||
testInvalidatePermissionOffer() {
|
||||
let user1, user2, token;
|
||||
return createUsersWithTestRealms(2)
|
||||
.then(([user1, user2]) => {
|
||||
user1.offerPermissions(`/${user1.identity}/test`, 'read')
|
||||
.then((token) => {
|
||||
return user1.invalidatePermissionOffer(token)
|
||||
// Since we don't yet support notification when the invalidation has gone through,
|
||||
// wait for a bit and hope the server is done processing.
|
||||
.then(wait(100))
|
||||
.then(user2.acceptPermissionOffer(token))
|
||||
// We want the call to fail, i.e. the catch() below should be called.
|
||||
.then(() => { throw new Error("User was able to accept an invalid permission offer token"); })
|
||||
.catch(error => {
|
||||
try {
|
||||
TestCase.assertEqual(error.message, 'The permission offer is expired.');
|
||||
TestCase.assertEqual(error.statusCode, 701);
|
||||
}
|
||||
catch (e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
.then(users => {
|
||||
user1 = users[0];
|
||||
user2 = users[1];
|
||||
return user1.offerPermissions(`/${user1.identity}/test`, 'read');
|
||||
})
|
||||
.then(t => { token = t; return user1.invalidatePermissionOffer(token); })
|
||||
// Since we don't yet support notification when the invalidation has gone through,
|
||||
// wait for a bit and hope the server is done processing.
|
||||
.then(wait(100))
|
||||
.then(() => user2.acceptPermissionOffer(token))
|
||||
// We want the call to fail, i.e. the catch() below should be called.
|
||||
.then(() => { throw new Error("User was able to accept an invalid permission offer token"); })
|
||||
.catch(error => {
|
||||
try {
|
||||
TestCase.assertEqual(error.message, 'The permission offer is expired.');
|
||||
TestCase.assertEqual(error.statusCode, 701);
|
||||
}
|
||||
catch (e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,6 @@ if (isNodeProccess) {
|
|||
path = node_require("path");
|
||||
}
|
||||
|
||||
|
||||
function uuid() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
||||
|
@ -52,32 +51,6 @@ function uuid() {
|
|||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function copyFileToTempDir(filename) {
|
||||
let tmpDir = tmp.dirSync();
|
||||
let content = fs.readFileSync(filename);
|
||||
|
@ -120,9 +93,8 @@ module.exports = {
|
|||
},
|
||||
|
||||
testProperties() {
|
||||
return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => {
|
||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
const accessTokenRefreshed = this;
|
||||
let successCounter = 0;
|
||||
function checkSuccess() {
|
||||
|
@ -162,43 +134,43 @@ module.exports = {
|
|||
|
||||
testRealmOpen() {
|
||||
if (!isNodeProccess) {
|
||||
return Promise.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const username = uuid();
|
||||
const realmName = uuid();
|
||||
const expectedObjectsCount = 3;
|
||||
|
||||
let user, config;
|
||||
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
||||
.then(() => {
|
||||
return promisifiedLogin('http://localhost:9080', username, 'password').then(user => {
|
||||
const accessTokenRefreshed = this;
|
||||
let successCounter = 0;
|
||||
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
||||
.then(u => {
|
||||
user = u;
|
||||
const accessTokenRefreshed = this;
|
||||
let successCounter = 0;
|
||||
|
||||
let config = {
|
||||
sync: { user, url: `realm://localhost:9080/~/${realmName}` },
|
||||
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
||||
};
|
||||
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');
|
||||
});
|
||||
});
|
||||
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");
|
||||
|
||||
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');
|
||||
});
|
||||
},
|
||||
|
||||
testRealmOpenAsync() {
|
||||
if (!isNodeProccess) {
|
||||
return Promise.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const username = uuid();
|
||||
|
@ -206,44 +178,42 @@ module.exports = {
|
|||
const expectedObjectsCount = 3;
|
||||
|
||||
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
||||
.then(() => {
|
||||
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const accessTokenRefreshed = this;
|
||||
let successCounter = 0;
|
||||
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
||||
.then(user => {
|
||||
const accessTokenRefreshed = this;
|
||||
let successCounter = 0;
|
||||
|
||||
let config = {
|
||||
sync: { user, url: `realm://localhost:9080/~/${realmName}` },
|
||||
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
||||
};
|
||||
let config = {
|
||||
sync: { user, url: `realm://localhost:9080/~/${realmName}` },
|
||||
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
||||
};
|
||||
return new Promise((resolve, reject) => {
|
||||
Realm.openAsync(config, (error, realm) => {
|
||||
try {
|
||||
if (error) {
|
||||
reject(error);
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
let actualObjectsCount = realm.objects('Dog').length;
|
||||
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count");
|
||||
|
||||
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);
|
||||
}
|
||||
catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}, 50);
|
||||
}
|
||||
catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -251,7 +221,7 @@ module.exports = {
|
|||
|
||||
testRealmOpenAsyncNoSchema() {
|
||||
if (!isNodeProccess) {
|
||||
return Promise.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const username = uuid();
|
||||
|
@ -397,8 +367,8 @@ module.exports = {
|
|||
Realm.copyBundledRealmFiles();
|
||||
}
|
||||
|
||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
||||
return new Promise((resolve, _reject) => {
|
||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password')
|
||||
.then(user => {
|
||||
const config = {
|
||||
path: realm,
|
||||
sync: {
|
||||
|
@ -407,30 +377,26 @@ module.exports = {
|
|||
url: 'realm://localhost:9080/~/sync-v1'
|
||||
}
|
||||
};
|
||||
return Realm.open(config)
|
||||
})
|
||||
.then(realm => { throw new Error("Should fail with IncompatibleSyncedRealmError") })
|
||||
.catch(e => {
|
||||
if (e.name == "IncompatibleSyncedRealmError") {
|
||||
const backupRealm = new Realm(e.configuration);
|
||||
TestCase.assertEqual(backupRealm.objects('Dog').length, 3);
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
function printObject(o) {
|
||||
var out = '';
|
||||
for (var p in o) {
|
||||
out += p + ': ' + o[p] + '\n';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function printObject(o) {
|
||||
var out = '';
|
||||
for (var p in o) {
|
||||
out += p + ': ' + o[p] + '\n';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
_reject("Failed with unexpected error " + printObject(e));
|
||||
});
|
||||
throw new Error("Failed with unexpected error " + printObject(e));
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
testIncompatibleSyncedRealmOpenAsync() {
|
||||
|
@ -514,42 +480,36 @@ module.exports = {
|
|||
|
||||
testProgressNotificationsForRealmConstructor() {
|
||||
if (!isNodeProccess) {
|
||||
return Promise.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const username = uuid();
|
||||
const realmName = uuid();
|
||||
|
||||
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
||||
.then(() => {
|
||||
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let config = {
|
||||
sync: {
|
||||
user,
|
||||
url: `realm://localhost:9080/~/${realmName}`
|
||||
},
|
||||
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
||||
};
|
||||
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
||||
.then(user => {
|
||||
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();
|
||||
};
|
||||
|
||||
realm.syncSession.addProgressNotification('download', 'reportIndefinitely', progressCallback);
|
||||
|
||||
setTimeout(function() {
|
||||
reject("Progress Notifications API failed to call progress callback for Realm constructor");
|
||||
}, 5000);
|
||||
});
|
||||
const realm = new Realm(config);
|
||||
return new Promise((resolve, reject) => {
|
||||
realm.syncSession.addProgressNotification('download', 'reportIndefinitely', resolve);
|
||||
setTimeout(function() {
|
||||
reject("Progress Notifications API failed to call progress callback for Realm constructor");
|
||||
}, 5000);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
testProgressNotificationsUnregisterForRealmConstructor() {
|
||||
if (!isNodeProccess) {
|
||||
return Promise.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const username = uuid();
|
||||
|
@ -616,7 +576,7 @@ module.exports = {
|
|||
|
||||
testProgressNotificationsForRealmOpen() {
|
||||
if (!isNodeProccess) {
|
||||
return Promise.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const username = uuid();
|
||||
|
@ -655,7 +615,7 @@ module.exports = {
|
|||
|
||||
testProgressNotificationsForRealmOpenAsync() {
|
||||
if (!isNodeProccess) {
|
||||
return Promise.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const username = uuid();
|
||||
|
@ -700,7 +660,7 @@ module.exports = {
|
|||
testPartialSync() {
|
||||
// FIXME: try to enable for React Native
|
||||
if (!isNodeProccess) {
|
||||
return Promise.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const username = uuid();
|
||||
|
@ -733,7 +693,7 @@ module.exports = {
|
|||
testClientReset() {
|
||||
// FIXME: try to enable for React Native
|
||||
if (!isNodeProccess) {
|
||||
return Promise.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
||||
|
|
|
@ -26,7 +26,7 @@ const isNodeProcess = typeof process === 'object' && process + '' === '[object p
|
|||
|
||||
function uuid() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
||||
const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
@ -65,35 +65,11 @@ function assertIsAuthError(error, code, title) {
|
|||
}
|
||||
}
|
||||
|
||||
function failOnError(error) {
|
||||
if (error) {
|
||||
throw new Error(`Unexpected error in test: ${error}\n${error.stack}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Test the given requestFunc, passing it the given callback after it's been wrapped appropriately.
|
||||
// This function makes sure that errors thrown in the async callback rejects the promise (making tests actually run).
|
||||
function callbackTest(requestFunc, callback) {
|
||||
return new Promise((resolve, reject) => {
|
||||
function callbackWrapper() {
|
||||
try {
|
||||
callback.apply(this, Array.from(arguments));
|
||||
resolve();
|
||||
}
|
||||
catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
}
|
||||
requestFunc(callbackWrapper);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
testLogout() {
|
||||
var username = uuid();
|
||||
return callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', username, 'password', callback), (error, user) => {
|
||||
failOnError(error);
|
||||
const username = uuid();
|
||||
return Realm.Sync.User.register('http://localhost:9080', username, 'password').then((user) => {
|
||||
assertIsUser(user);
|
||||
|
||||
assertIsSameUser(user, Realm.Sync.User.current);
|
||||
|
@ -103,272 +79,188 @@ module.exports = {
|
|||
TestCase.assertUndefined(Realm.Sync.User.current);
|
||||
|
||||
// Can we open a realm with the registered user?
|
||||
TestCase.assertThrows(function() {
|
||||
var _realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}});
|
||||
});
|
||||
})
|
||||
TestCase.assertThrows(() => new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}}));
|
||||
});
|
||||
},
|
||||
|
||||
testRegisterUser() {
|
||||
var username = uuid();
|
||||
return callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', username, 'password', callback), (error, user) => {
|
||||
failOnError(error);
|
||||
assertIsUser(user);
|
||||
|
||||
const username = uuid();
|
||||
return Realm.Sync.User.register('http://localhost:9080', username, 'password').then((user) => {
|
||||
// Can we open a realm with the registered user?
|
||||
var realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}});
|
||||
const realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}});
|
||||
TestCase.assertInstanceOf(realm, Realm);
|
||||
});
|
||||
},
|
||||
|
||||
testRegisterExistingUser() {
|
||||
var username = uuid();
|
||||
return new Promise((resolve, reject) => {
|
||||
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
||||
failOnError(error);
|
||||
assertIsUser(user);
|
||||
|
||||
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
||||
try {
|
||||
assertIsAuthError(error, 611, "The provided credentials are invalid or the user does not exist.");
|
||||
TestCase.assertUndefined(user);
|
||||
resolve();
|
||||
} catch(e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
const username = uuid();
|
||||
return Realm.Sync.User.register('http://localhost:9080', username, 'password').then((user) => {
|
||||
assertIsUser(user);
|
||||
return Realm.Sync.User.register('http://localhost:9080', username, 'password')
|
||||
.then((user) => { throw new Error(user); })
|
||||
.catch((e) => {
|
||||
assertIsAuthError(e, 611, "The provided credentials are invalid or the user does not exist.");
|
||||
})
|
||||
});
|
||||
},
|
||||
|
||||
testRegisterMissingUsername() {
|
||||
return new Promise((resolve, _reject) => {
|
||||
TestCase.assertThrows(() => {
|
||||
Realm.Sync.User.register('http://localhost:9080', undefined, 'password', () => {});
|
||||
});
|
||||
resolve();
|
||||
});
|
||||
TestCase.assertThrows(() => Realm.Sync.User.register('http://localhost:9080', undefined, 'password'));
|
||||
},
|
||||
|
||||
testRegisterMissingPassword() {
|
||||
var username = uuid();
|
||||
return new Promise((resolve, _reject) => {
|
||||
TestCase.assertThrows(() => {
|
||||
Realm.Sync.User.register('http://localhost:9080', username, undefined, () => {});
|
||||
});
|
||||
resolve();
|
||||
});
|
||||
const username = uuid();
|
||||
TestCase.assertThrows(() => Realm.Sync.User.register('http://localhost:9080', username, undefined));
|
||||
},
|
||||
|
||||
testRegisterServerOffline() {
|
||||
var username = uuid();
|
||||
const username = uuid();
|
||||
// Because it waits for answer this takes some time..
|
||||
return callbackTest((callback) => Realm.Sync.User.register('http://fake_host.local', username, 'password', callback), (error, user) => {
|
||||
assertIsError(error);
|
||||
TestCase.assertUndefined(user);
|
||||
});
|
||||
return Realm.Sync.User.register('http://fake_host.local', username, 'password')
|
||||
.catch((e) => {})
|
||||
.then((user) => { if (user) { throw new Error('should not have been able to register'); }})
|
||||
},
|
||||
|
||||
testLogin() {
|
||||
var username = uuid();
|
||||
const username = uuid();
|
||||
// Create user, logout the new user, then login
|
||||
return callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', username, 'password', callback), (error, user) => {
|
||||
failOnError(error);
|
||||
return Realm.Sync.User.register('http://localhost:9080', username, 'password').then((user) => {
|
||||
user.logout();
|
||||
|
||||
Realm.Sync.User.login('http://localhost:9080', username, 'password', (error, user) => {
|
||||
failOnError(error);
|
||||
return Realm.Sync.User.login('http://localhost:9080', username, 'password');
|
||||
}).then((user => {
|
||||
assertIsUser(user);
|
||||
|
||||
// Can we open a realm with the logged-in user?
|
||||
var realm = new Realm({ sync: { user: user, url: 'realm://localhost:9080/~/test' } });
|
||||
const realm = new Realm({ sync: { user: user, url: 'realm://localhost:9080/~/test' } });
|
||||
TestCase.assertInstanceOf(realm, Realm);
|
||||
realm.close();
|
||||
});
|
||||
});
|
||||
}))
|
||||
},
|
||||
|
||||
testLoginMissingUsername() {
|
||||
return new Promise((resolve, _reject) => {
|
||||
TestCase.assertThrows(() => {
|
||||
Realm.Sync.User.login('http://localhost:9080', undefined, 'password', () => {});
|
||||
});
|
||||
resolve();
|
||||
});
|
||||
TestCase.assertThrows(() => Realm.Sync.User.login('http://localhost:9080', undefined, 'password'));
|
||||
},
|
||||
|
||||
testLoginMissingPassword() {
|
||||
var username = uuid();
|
||||
return new Promise((resolve, _reject) => {
|
||||
TestCase.assertThrows(() => {
|
||||
Realm.Sync.User.login('http://localhost:9080', username, undefined, () => {});
|
||||
});
|
||||
resolve();
|
||||
});
|
||||
const username = uuid();
|
||||
TestCase.assertThrows(() => Realm.Sync.User.login('http://localhost:9080', username, undefined));
|
||||
},
|
||||
|
||||
testLoginNonExistingUser() {
|
||||
return callbackTest((callback) => Realm.Sync.User.login('http://localhost:9080', 'does_not', 'exist', callback), (error, user) => {
|
||||
assertIsAuthError(error, 611, "The provided credentials are invalid or the user does not exist.");
|
||||
TestCase.assertUndefined(user);
|
||||
});
|
||||
return Realm.Sync.User.login('http://localhost:9080', 'does_not', 'exist')
|
||||
.then((user) => { throw new Error(user); })
|
||||
.catch((e) => assertIsAuthError(e, 611, "The provided credentials are invalid or the user does not exist."))
|
||||
},
|
||||
|
||||
testLoginServerOffline() {
|
||||
var username = uuid();
|
||||
const username = uuid();
|
||||
|
||||
// Because it waits for answer this takes some time..
|
||||
return new Promise((resolve, reject) => {
|
||||
Realm.Sync.User.register('http://fake_host.local', username, 'password', (error, user) => {
|
||||
try {
|
||||
assertIsError(error);
|
||||
TestCase.assertUndefined(user);
|
||||
resolve();
|
||||
}
|
||||
catch (e) { reject(e) }
|
||||
});
|
||||
});
|
||||
return Realm.Sync.User.register('http://fake_host.local', username, 'password')
|
||||
.then((user) => { throw new Error(user); })
|
||||
.catch((e) => assertIsError(e));
|
||||
},
|
||||
|
||||
testAll() {
|
||||
return new Promise((resolve, reject) => {
|
||||
let all;
|
||||
all = Realm.Sync.User.all;
|
||||
TestCase.assertArrayLength(Object.keys(all), 0);
|
||||
const all = Realm.Sync.User.all;
|
||||
TestCase.assertArrayLength(Object.keys(all), 0);
|
||||
|
||||
callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', callback), (error, user1) => {
|
||||
failOnError(error);
|
||||
let user1;
|
||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then((user) => {
|
||||
const all = Realm.Sync.User.all;
|
||||
TestCase.assertArrayLength(Object.keys(all), 1);
|
||||
assertIsSameUser(all[user.identity], user);
|
||||
user1 = user;
|
||||
|
||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password');
|
||||
}).then((user2) => {
|
||||
let all = Realm.Sync.User.all;
|
||||
TestCase.assertArrayLength(Object.keys(all), 2);
|
||||
// NOTE: the list of users is in latest-first order.
|
||||
assertIsSameUser(all[user2.identity], user2);
|
||||
assertIsSameUser(all[user1.identity], user1);
|
||||
|
||||
user2.logout();
|
||||
all = Realm.Sync.User.all;
|
||||
TestCase.assertArrayLength(Object.keys(all), 1);
|
||||
assertIsSameUser(all[user1.identity], user1);
|
||||
|
||||
Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', (error, user2) => {
|
||||
failOnError(error);
|
||||
|
||||
all = Realm.Sync.User.all;
|
||||
TestCase.assertArrayLength(Object.keys(all), 2);
|
||||
// NOTE: the list of users is in latest-first order.
|
||||
assertIsSameUser(all[user2.identity], user2);
|
||||
assertIsSameUser(all[user1.identity], user1);
|
||||
|
||||
user2.logout();
|
||||
all = Realm.Sync.User.all;
|
||||
TestCase.assertArrayLength(Object.keys(all), 1);
|
||||
assertIsSameUser(all[user1.identity], user1);
|
||||
|
||||
user1.logout();
|
||||
all = Realm.Sync.User.all;
|
||||
TestCase.assertArrayLength(Object.keys(all), 0);
|
||||
resolve();
|
||||
});
|
||||
}).catch(e => reject(e));
|
||||
});
|
||||
},
|
||||
|
||||
testCurrent() {
|
||||
return new Promise((resolve, reject) => {
|
||||
TestCase.assertUndefined(Realm.Sync.User.current);
|
||||
|
||||
callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', callback), (error, user1) => {
|
||||
failOnError(error);
|
||||
assertIsSameUser(Realm.Sync.User.current, user1);
|
||||
|
||||
Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', (error, user2) => {
|
||||
failOnError(error);
|
||||
TestCase.assertThrows(() => Realm.Sync.User.current, 'We expect Realm.Sync.User.current to throw if > 1 user.');
|
||||
user2.logout();
|
||||
|
||||
assertIsSameUser(Realm.Sync.User.current, user1);
|
||||
|
||||
user1.logout();
|
||||
TestCase.assertUndefined(Realm.Sync.User.current);
|
||||
|
||||
resolve();
|
||||
|
||||
});
|
||||
}).catch(e => reject(e));
|
||||
user1.logout();
|
||||
all = Realm.Sync.User.all;
|
||||
TestCase.assertArrayLength(Object.keys(all), 0);
|
||||
});
|
||||
},
|
||||
|
||||
testManagementRealm() {
|
||||
return callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', callback), (error, user) => {
|
||||
failOnError(error);
|
||||
testCurrent() {
|
||||
TestCase.assertUndefined(Realm.Sync.User.current);
|
||||
|
||||
let user1;
|
||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then((user) => {
|
||||
user1 = user;
|
||||
assertIsSameUser(Realm.Sync.User.current, user1);
|
||||
|
||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password');
|
||||
}).then((user2) => {
|
||||
TestCase.assertThrows(() => Realm.Sync.User.current, 'We expect Realm.Sync.User.current to throw if > 1 user.');
|
||||
user2.logout();
|
||||
|
||||
assertIsSameUser(Realm.Sync.User.current, user1);
|
||||
|
||||
user1.logout();
|
||||
TestCase.assertUndefined(Realm.Sync.User.current);
|
||||
});
|
||||
},
|
||||
|
||||
testManagementRealm() {
|
||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then((user) => {
|
||||
let realm = user.openManagementRealm();
|
||||
TestCase.assertInstanceOf(realm, Realm);
|
||||
|
||||
let objectSchemaNames = realm.schema.map(o => o.name);
|
||||
TestCase.assertArraysEqual(objectSchemaNames, [ 'PermissionChange', 'PermissionOffer', 'PermissionOfferResponse' ]);
|
||||
TestCase.assertArraysEqual(realm.schema.map(o => o.name),
|
||||
['PermissionChange', 'PermissionOffer', 'PermissionOfferResponse']);
|
||||
});
|
||||
},
|
||||
|
||||
testRetrieveAccount() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!isNodeProcess) {
|
||||
resolve();
|
||||
}
|
||||
if (!isNodeProcess) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!global.testAdminUserInfo) {
|
||||
reject("Test requires an admin user");
|
||||
}
|
||||
if (!global.testAdminUserInfo) {
|
||||
throw new Error("Test requires an admin user");
|
||||
}
|
||||
|
||||
Realm.Sync.User.login('http://localhost:9080', global.testAdminUserInfo.username, global.testAdminUserInfo.password, (error, user) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
}
|
||||
return Realm.Sync.User.login('http://localhost:9080', global.testAdminUserInfo.username, global.testAdminUserInfo.password).then((user) => {
|
||||
TestCase.assertTrue(user.isAdmin, "Test requires an admin user");
|
||||
|
||||
TestCase.assertTrue(user.isAdmin, "Test requires an admin user");
|
||||
|
||||
user.retrieveAccount('password', global.testAdminUserInfo.username)
|
||||
.then(account => {
|
||||
TestCase.assertEqual(account.accounts[0].provider_id, global.testAdminUserInfo.username);
|
||||
TestCase.assertEqual(account.accounts[0].provider, 'password');
|
||||
TestCase.assertTrue(account.is_admin);
|
||||
TestCase.assertTrue(account.user_id);
|
||||
resolve();
|
||||
})
|
||||
.catch(e => reject(e));
|
||||
})
|
||||
return user.retrieveAccount('password', global.testAdminUserInfo.username)
|
||||
}).then((account) => {
|
||||
TestCase.assertEqual(account.accounts[0].provider_id, global.testAdminUserInfo.username);
|
||||
TestCase.assertEqual(account.accounts[0].provider, 'password');
|
||||
TestCase.assertTrue(account.is_admin);
|
||||
TestCase.assertTrue(account.user_id);
|
||||
});
|
||||
},
|
||||
|
||||
testRetrieveNotExistingAccount() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!isNodeProcess) {
|
||||
resolve();
|
||||
}
|
||||
if (!isNodeProcess) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!global.testAdminUserInfo) {
|
||||
reject("Test requires an admin user");
|
||||
}
|
||||
if (!global.testAdminUserInfo) {
|
||||
throw new Error("Test requires an admin user");
|
||||
}
|
||||
|
||||
Realm.Sync.User.login('http://localhost:9080', global.testAdminUserInfo.username, global.testAdminUserInfo.password, (error, user) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
}
|
||||
return Realm.Sync.User.login('http://localhost:9080', global.testAdminUserInfo.username, global.testAdminUserInfo.password).then((user) => {
|
||||
TestCase.assertTrue(user.isAdmin, "Test requires an admin user");
|
||||
|
||||
TestCase.assertTrue(user.isAdmin, "Test requires an admin user");
|
||||
|
||||
let notExistingUsername = uuid();
|
||||
user.retrieveAccount('password', notExistingUsername)
|
||||
.then(account => {
|
||||
reject("Retrieving not existing account should fail");
|
||||
})
|
||||
.catch(e => {
|
||||
try {
|
||||
TestCase.assertEqual(e.status, 404);
|
||||
TestCase.assertEqual(e.code, 612);
|
||||
TestCase.assertEqual(e.message, "The account does not exist.");
|
||||
TestCase.assertEqual(e.type, "https://realm.io/docs/object-server/problems/unknown-account");
|
||||
}
|
||||
catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
resolve()
|
||||
});
|
||||
})
|
||||
});
|
||||
let notExistingUsername = uuid();
|
||||
return user.retrieveAccount('password', notExistingUsername)
|
||||
}).catch(e => {
|
||||
TestCase.assertEqual(e.status, 404);
|
||||
TestCase.assertEqual(e.code, 612);
|
||||
TestCase.assertEqual(e.message, "The account does not exist.");
|
||||
TestCase.assertEqual(e.type, "https://realm.io/docs/object-server/problems/unknown-account");
|
||||
}).then(account => { if (account) { throw new Error("Retrieving nonexistent account should fail"); }});
|
||||
},
|
||||
|
||||
/* This test fails because of realm-object-store #243 . We should use 2 users.
|
||||
|
|
Loading…
Reference in New Issue