2017-07-05 13:01:31 +00:00
|
|
|
'use strict';
|
2017-08-11 08:30:57 +00:00
|
|
|
function node_require(module) {
|
|
|
|
return require(module);
|
2017-07-05 13:01:31 +00:00
|
|
|
|
2017-07-07 10:54:58 +00:00
|
|
|
}
|
|
|
|
let fs = node_require("fs");
|
|
|
|
let path = node_require("path");
|
|
|
|
var Realm = node_require('realm');
|
2017-07-05 13:01:31 +00:00
|
|
|
|
2017-08-11 08:30:57 +00:00
|
|
|
const DEFAULT_ADMIN_TOKEN_PATH = path.join(__dirname, "..", "..", "object-server-for-testing", "admin_token.base64");
|
|
|
|
const ADMIN_TOKEN_PATH = process.env.ADMIN_TOKEN_PATH || DEFAULT_ADMIN_TOKEN_PATH;
|
|
|
|
|
|
|
|
function getAdminToken() {
|
|
|
|
if(fs.existsSync(ADMIN_TOKEN_PATH)) {
|
|
|
|
return fs.readFileSync(ADMIN_TOKEN_PATH, 'utf-8');
|
|
|
|
} else {
|
|
|
|
throw new Error("Missing the file with an admin token: " + ADMIN_TOKEN_PATH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:01:31 +00:00
|
|
|
function random(min, max) {
|
|
|
|
min = Math.ceil(min);
|
|
|
|
max = Math.floor(max);
|
|
|
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.createAdminUser = function () {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-07-07 07:30:24 +00:00
|
|
|
let isAdminRetryCounter = 0;
|
2017-07-05 13:01:31 +00:00
|
|
|
let newAdminName = 'admin' + random(1, 100000);
|
|
|
|
let password = '123';
|
|
|
|
Realm.Sync.User.register('http://localhost:9080', newAdminName, password, (error, user) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
2017-08-11 08:30:57 +00:00
|
|
|
} else {
|
2017-07-07 09:16:12 +00:00
|
|
|
let userIdentity = user.identity;
|
|
|
|
user.logout();
|
|
|
|
|
2017-08-11 08:30:57 +00:00
|
|
|
let admin_token_user = Realm.Sync.User.adminUser(getAdminToken());
|
2017-07-07 09:16:12 +00:00
|
|
|
|
2017-07-05 13:01:31 +00:00
|
|
|
const config = {
|
|
|
|
sync: {
|
|
|
|
user: admin_token_user,
|
|
|
|
url: `realm://localhost:9080/__admin`,
|
2017-08-17 11:49:47 +00:00
|
|
|
error: err => {
|
|
|
|
const error = new Error('Error opening __admin realm error:' + err.user + ' url:' + err.url + ' state:' + err.state)
|
|
|
|
console.log(error);
|
|
|
|
reject(error);
|
|
|
|
}
|
2017-07-05 13:01:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.open(config).then(realm => {
|
2017-07-07 13:44:54 +00:00
|
|
|
let pendingAdminUser = realm.objectForPrimaryKey('User', userIdentity);
|
2017-07-05 13:01:31 +00:00
|
|
|
realm.write(() => {
|
|
|
|
pendingAdminUser.isAdmin = true;
|
|
|
|
});
|
|
|
|
|
2017-07-07 09:16:12 +00:00
|
|
|
admin_token_user.logout();
|
2017-07-07 13:38:13 +00:00
|
|
|
}).then(() => {
|
2017-07-05 13:01:31 +00:00
|
|
|
let waitForServerToUpdateAdminUser = function () {
|
2017-07-07 07:30:24 +00:00
|
|
|
isAdminRetryCounter++;
|
|
|
|
if (isAdminRetryCounter > 10) {
|
|
|
|
reject("admin-user-helper: Create admin user timeout");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-07 09:16:12 +00:00
|
|
|
Realm.Sync.User.login('http://localhost:9080', newAdminName, password, (error, newAdminUser) => {
|
2017-07-05 13:01:31 +00:00
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
2017-07-07 09:16:12 +00:00
|
|
|
let isAdmin = newAdminUser.isAdmin;
|
2017-07-07 07:30:24 +00:00
|
|
|
user.logout();
|
|
|
|
if (!isAdmin) {
|
2017-07-08 17:30:25 +00:00
|
|
|
setTimeout(waitForServerToUpdateAdminUser, 500);
|
2017-07-06 09:27:01 +00:00
|
|
|
return;
|
2017-07-05 13:01:31 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 08:30:57 +00:00
|
|
|
resolve({
|
|
|
|
username: newAdminName,
|
2017-07-07 13:38:13 +00:00
|
|
|
password
|
2017-07-05 13:01:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
waitForServerToUpdateAdminUser();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|