test-ros-server.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const ROS = require('realm-object-server');
  2. const fs = require('fs');
  3. const os = require('os');
  4. const path = require('path');
  5. // Bypass the mandatory email prompt.
  6. process.env.ROS_TOS_EMAIL_ADDRESS = 'ci@realm.io';
  7. process.env.DOCKER_DATA_PATH = '/tmp';
  8. // Don't bother calling fsync() because we're throwing away all the files
  9. // between runs anyway
  10. process.env.REALM_DISABLE_SYNC_TO_DISK = 'true';
  11. // Workaround for <https://github.com/realm/realm-object-server-private/issues/950>.
  12. process.env.ROS_SUPERAGENT_RETRY_DELAY = '0';
  13. // Enable timestamps in the logs
  14. process.env.ROS_LOG_TIMESTAMP = '1';
  15. if (!process.env.SYNC_WORKER_FEATURE_TOKEN) {
  16. try {
  17. require(os.homedir() + '/.ros-feature-token.js');
  18. }
  19. catch (e) {
  20. console.error('ROS feature token not found. Running Object Server tests requires setting the SYNC_WORKER_FEATURE_TOKEN environment variable.');
  21. process.exit(1);
  22. }
  23. }
  24. // A "email handler" which actually just writes the tokens to files that the
  25. // tests can read
  26. class PasswordEmailHandler {
  27. constructor(dataRoot) {
  28. this.dataRoot = dataRoot;
  29. fs.mkdirSync(this.dataRoot);
  30. }
  31. resetPassword(email, token, userAgent, remoteIp) {
  32. fs.writeFileSync(path.join(this.dataRoot, email), token);
  33. return new Promise(r => setTimeout(r, 0));
  34. }
  35. confirmEmail(email, token) {
  36. fs.writeFileSync(path.join(this.dataRoot, email), token);
  37. return new Promise(r => setTimeout(r, 0));
  38. }
  39. }
  40. const server = new ROS.BasicServer();
  41. server.start({
  42. // The desired logging threshold. Can be one of: all, trace, debug, detail, info, warn, error, fatal, off)
  43. logLevel: 'off',
  44. // For all the full list of configuration parameters see:
  45. // https://realm.io/docs/realm-object-server/latest/api/ros/interfaces/serverconfig.html
  46. address: '0.0.0.0',
  47. port: 9080,
  48. httpsPort: 9443,
  49. https: true,
  50. httpsKeyPath: __dirname + '/certificates/localhost-cert-key.pem',
  51. httpsCertChainPath: __dirname + '/certificates/localhost-cert.pem',
  52. httpsForInternalComponents: false,
  53. dataPath: process.argv[2],
  54. authProviders: [
  55. new ROS.auth.DebugAuthProvider(),
  56. new ROS.auth.PasswordAuthProvider({
  57. autoCreateAdminUser: true,
  58. emailHandler: new PasswordEmailHandler(path.join(process.argv[2], 'email')),
  59. }),
  60. ],
  61. autoKeyGen: true,
  62. }).then(() => {
  63. console.log('started');
  64. fs.closeSync(1);
  65. }).catch(err => {
  66. console.error(`Error starting Realm Object Server: ${err.message}`)
  67. });