realm-js/tests/js/admin-user-helper.js
Kræn Hansen e4e1431c55 Running js tests in Electron renderer process (#1196)
* First take on running the tests inside an Electron render process

* Making progress

* Making it possible to override the location of the admin token

* Ignoreing the realm-object-server files

* Fixing a console.log to return a string instead of a boolean

But it should probably be removed entirely

* Making the downloaded ROS log less

* Adjusting comments in the test.sh

* Checking number of windows to determine an Electron crash

* Added a test that triggers garbage collection of ArrayBuffers

* Enabled the ability to run the tests in Electrons main process

* Run tests first in main process and the render

* Added a README.md that documents how Electron tests can be run

* Added a comment on why the garbage-collection test was added.

* Waiting with reading the admin token, until it is actually used

* Disabling the GarbageCollectionTests for now
2017-08-11 10:30:57 +02:00

90 lines
3.2 KiB
JavaScript

'use strict';
function node_require(module) {
return require(module);
}
let fs = node_require("fs");
let path = node_require("path");
var Realm = node_require('realm');
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);
}
}
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) => {
let isAdminRetryCounter = 0;
let newAdminName = 'admin' + random(1, 100000);
let password = '123';
Realm.Sync.User.register('http://localhost:9080', newAdminName, password, (error, user) => {
if (error) {
reject(error);
} else {
let userIdentity = user.identity;
user.logout();
let admin_token_user = Realm.Sync.User.adminUser(getAdminToken());
const config = {
sync: {
user: admin_token_user,
url: `realm://localhost:9080/__admin`,
error: err =>
console.log('Error opening __admin realm ' + err.user + ' ' + err.url + ' ' + err.state),
}
};
Realm.open(config).then(realm => {
let pendingAdminUser = realm.objectForPrimaryKey('User', userIdentity);
realm.write(() => {
pendingAdminUser.isAdmin = true;
});
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();
});
}
});
});
}