Fix error reporting from admin-user-helper.js

In some cases errors were not being bubbled out correctly.
This commit is contained in:
Thomas Goyne 2017-08-31 10:17:39 -07:00
parent 3e87ef33ce
commit 9e4313c416
2 changed files with 65 additions and 64 deletions

View File

@ -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");
@ -24,15 +23,16 @@ function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min; return Math.floor(Math.random() * (max - min + 1)) + min;
} }
exports.createAdminUser = function () {
return new Promise((resolve, reject) => {
let isAdminRetryCounter = 0;
let newAdminName = 'admin' + random(1, 100000); let newAdminName = 'admin' + random(1, 100000);
let password = '123'; let password = '123';
exports.createAdminUser = function () {
return new Promise((resolve, reject) => {
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; let userIdentity = user.identity;
user.logout(); user.logout();
@ -43,14 +43,14 @@ exports.createAdminUser = function () {
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));
});
}).then(realm => {
let pendingAdminUser = realm.objectForPrimaryKey('User', userIdentity); let pendingAdminUser = realm.objectForPrimaryKey('User', userIdentity);
realm.write(() => { realm.write(() => {
pendingAdminUser.isAdmin = true; pendingAdminUser.isAdmin = true;
@ -58,6 +58,8 @@ exports.createAdminUser = function () {
admin_token_user.logout(); admin_token_user.logout();
}).then(() => { }).then(() => {
return new Promise((resolve, reject) => {
let isAdminRetryCounter = 0;
let waitForServerToUpdateAdminUser = function () { let waitForServerToUpdateAdminUser = function () {
isAdminRetryCounter++; isAdminRetryCounter++;
if (isAdminRetryCounter > 10) { if (isAdminRetryCounter > 10) {
@ -68,7 +70,9 @@ exports.createAdminUser = function () {
Realm.Sync.User.login('http://localhost:9080', newAdminName, password, (error, newAdminUser) => { Realm.Sync.User.login('http://localhost:9080', newAdminName, password, (error, newAdminUser) => {
if (error) { if (error) {
reject(error); reject(error);
} else { return;
}
let isAdmin = newAdminUser.isAdmin; let isAdmin = newAdminUser.isAdmin;
user.logout(); user.logout();
if (!isAdmin) { if (!isAdmin) {
@ -80,13 +84,9 @@ exports.createAdminUser = function () {
username: newAdminName, username: newAdminName,
password password
}); });
}
}); });
} }
waitForServerToUpdateAdminUser(); waitForServerToUpdateAdminUser();
}); });
}
});
}); });
} }

View File

@ -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()
.then(userInfo => {
global.testAdminUserInfo = userInfo; global.testAdminUserInfo = userInfo;
done(); done();
}) })
.catch(error => { .catch(error => {
console.error("Error running admin-user-helper: " + error); console.error("Error running admin-user-helper", error);
done(); done.fail(error);
}); });
}; };