Fix error reporting from admin-user-helper.js
In some cases errors were not being bubbled out correctly.
This commit is contained in:
parent
3e87ef33ce
commit
9e4313c416
|
@ -1,7 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
function node_require(module) {
|
function node_require(module) {
|
||||||
return require(module);
|
return require(module);
|
||||||
|
|
||||||
}
|
}
|
||||||
let fs = node_require("fs");
|
let fs = node_require("fs");
|
||||||
let path = node_require("path");
|
let path = node_require("path");
|
||||||
|
@ -11,11 +10,11 @@ const DEFAULT_ADMIN_TOKEN_PATH = path.join(__dirname, "..", "..", "object-server
|
||||||
const ADMIN_TOKEN_PATH = process.env.ADMIN_TOKEN_PATH || DEFAULT_ADMIN_TOKEN_PATH;
|
const ADMIN_TOKEN_PATH = process.env.ADMIN_TOKEN_PATH || DEFAULT_ADMIN_TOKEN_PATH;
|
||||||
|
|
||||||
function getAdminToken() {
|
function getAdminToken() {
|
||||||
if(fs.existsSync(ADMIN_TOKEN_PATH)) {
|
if (fs.existsSync(ADMIN_TOKEN_PATH)) {
|
||||||
return fs.readFileSync(ADMIN_TOKEN_PATH, 'utf-8');
|
return fs.readFileSync(ADMIN_TOKEN_PATH, 'utf-8');
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Missing the file with an admin token: " + ADMIN_TOKEN_PATH);
|
throw new Error("Missing the file with an admin token: " + ADMIN_TOKEN_PATH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function random(min, max) {
|
function random(min, max) {
|
||||||
|
@ -24,69 +23,70 @@ function random(min, max) {
|
||||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let newAdminName = 'admin' + random(1, 100000);
|
||||||
|
let password = '123';
|
||||||
|
|
||||||
exports.createAdminUser = function () {
|
exports.createAdminUser = function () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let isAdminRetryCounter = 0;
|
|
||||||
let newAdminName = 'admin' + random(1, 100000);
|
|
||||||
let password = '123';
|
|
||||||
Realm.Sync.User.register('http://localhost:9080', newAdminName, password, (error, user) => {
|
Realm.Sync.User.register('http://localhost:9080', newAdminName, password, (error, user) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
} else {
|
return;
|
||||||
let userIdentity = user.identity;
|
}
|
||||||
user.logout();
|
let userIdentity = user.identity;
|
||||||
|
user.logout();
|
||||||
|
|
||||||
let admin_token_user = Realm.Sync.User.adminUser(getAdminToken());
|
let admin_token_user = Realm.Sync.User.adminUser(getAdminToken());
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
sync: {
|
sync: {
|
||||||
user: admin_token_user,
|
user: admin_token_user,
|
||||||
url: `realm://localhost:9080/__admin`,
|
url: `realm://localhost:9080/__admin`,
|
||||||
error: err => {
|
error: err => {
|
||||||
const error = new Error('Error opening __admin realm error:' + err.user + ' url:' + err.url + ' state:' + err.state)
|
reject(new Error('Error opening __admin realm error:' + err.user + ' url:' + err.url + ' state:' + err.state));
|
||||||
console.log(error);
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Realm.open(config).then(realm => {
|
resolve(Realm.open(config));
|
||||||
let pendingAdminUser = realm.objectForPrimaryKey('User', userIdentity);
|
});
|
||||||
realm.write(() => {
|
}).then(realm => {
|
||||||
pendingAdminUser.isAdmin = true;
|
let pendingAdminUser = realm.objectForPrimaryKey('User', userIdentity);
|
||||||
|
realm.write(() => {
|
||||||
|
pendingAdminUser.isAdmin = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
admin_token_user.logout();
|
||||||
|
}).then(() => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let isAdminRetryCounter = 0;
|
||||||
|
let waitForServerToUpdateAdminUser = function () {
|
||||||
|
isAdminRetryCounter++;
|
||||||
|
if (isAdminRetryCounter > 10) {
|
||||||
|
reject("admin-user-helper: Create admin user timeout");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Realm.Sync.User.login('http://localhost:9080', newAdminName, password, (error, newAdminUser) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let isAdmin = newAdminUser.isAdmin;
|
||||||
|
user.logout();
|
||||||
|
if (!isAdmin) {
|
||||||
|
setTimeout(waitForServerToUpdateAdminUser, 500);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve({
|
||||||
|
username: newAdminName,
|
||||||
|
password
|
||||||
});
|
});
|
||||||
|
|
||||||
admin_token_user.logout();
|
|
||||||
}).then(() => {
|
|
||||||
let waitForServerToUpdateAdminUser = function () {
|
|
||||||
isAdminRetryCounter++;
|
|
||||||
if (isAdminRetryCounter > 10) {
|
|
||||||
reject("admin-user-helper: Create admin user timeout");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Realm.Sync.User.login('http://localhost:9080', newAdminName, password, (error, newAdminUser) => {
|
|
||||||
if (error) {
|
|
||||||
reject(error);
|
|
||||||
} else {
|
|
||||||
let isAdmin = newAdminUser.isAdmin;
|
|
||||||
user.logout();
|
|
||||||
if (!isAdmin) {
|
|
||||||
setTimeout(waitForServerToUpdateAdminUser, 500);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve({
|
|
||||||
username: newAdminName,
|
|
||||||
password
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
waitForServerToUpdateAdminUser();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
waitForServerToUpdateAdminUser();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,14 +87,15 @@ exports.prepare = function(done) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let helper = require('./admin-user-helper');
|
require('./admin-user-helper')
|
||||||
helper.createAdminUser().then(userInfo => {
|
.createAdminUser()
|
||||||
global.testAdminUserInfo = userInfo;
|
.then(userInfo => {
|
||||||
done();
|
global.testAdminUserInfo = userInfo;
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
console.error("Error running admin-user-helper: " + error);
|
|
||||||
done();
|
done();
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error("Error running admin-user-helper", error);
|
||||||
|
done.fail(error);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue