ServerCEScriptsTests.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const { execSync } = require('child_process')
  2. const { expect } = require('chai')
  3. const { db } = require('../../../../../app/src/infrastructure/mongodb')
  4. const User = require('../../../../../test/acceptance/src/helpers/User').promises
  5. /**
  6. * @param {string} cmd
  7. * @return {string}
  8. */
  9. function run(cmd) {
  10. // https://nodejs.org/docs/latest-v12.x/api/child_process.html#child_process_child_process_execsync_command_options
  11. // > stderr by default will be output to the parent process' stderr
  12. // > unless stdio is specified.
  13. // https://nodejs.org/docs/latest-v12.x/api/child_process.html#child_process_options_stdio
  14. // Pipe stdin from /dev/null, store stdout, pipe stderr to /dev/null.
  15. return execSync(cmd, {
  16. stdio: ['ignore', 'pipe', 'ignore'],
  17. }).toString()
  18. }
  19. async function getUser(email) {
  20. return db.users.findOne({ email }, { projection: { _id: 0, isAdmin: 1 } })
  21. }
  22. describe('ServerCEScripts', function () {
  23. describe('check-mongodb', function () {
  24. it('should exit with code 0 on success', function () {
  25. run('node modules/server-ce-scripts/scripts/check-mongodb')
  26. })
  27. it('should exit with code 1 on error', function () {
  28. try {
  29. run(
  30. 'MONGO_SERVER_SELECTION_TIMEOUT=1' +
  31. 'MONGO_CONNECTION_STRING=mongodb://localhost:4242 ' +
  32. 'node modules/server-ce-scripts/scripts/check-mongodb'
  33. )
  34. } catch (e) {
  35. expect(e.status).to.equal(1)
  36. return
  37. }
  38. expect.fail('command should have failed')
  39. })
  40. })
  41. describe('check-redis', function () {
  42. it('should exit with code 0 on success', function () {
  43. run('node modules/server-ce-scripts/scripts/check-redis')
  44. })
  45. it('should exit with code 1 on error', function () {
  46. try {
  47. run(
  48. 'REDIS_HOST=localhost node modules/server-ce-scripts/scripts/check-redis'
  49. )
  50. } catch (e) {
  51. expect(e.status).to.equal(1)
  52. return
  53. }
  54. expect.fail('command should have failed')
  55. })
  56. })
  57. describe('create-user', function () {
  58. it('should exit with code 0 on success', function () {
  59. const out = run(
  60. 'node modules/server-ce-scripts/scripts/create-user --email=foo@bar.com'
  61. )
  62. expect(out).to.include('/user/activate?token=')
  63. })
  64. it('should create a regular user by default', async function () {
  65. run(
  66. 'node modules/server-ce-scripts/scripts/create-user --email=foo@bar.com'
  67. )
  68. expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: false })
  69. })
  70. it('should create an admin user with --admin flag', async function () {
  71. run(
  72. 'node modules/server-ce-scripts/scripts/create-user --admin --email=foo@bar.com'
  73. )
  74. expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: true })
  75. })
  76. it('should exit with code 1 on missing email', function () {
  77. try {
  78. run('node modules/server-ce-scripts/scripts/create-user')
  79. } catch (e) {
  80. expect(e.status).to.equal(1)
  81. return
  82. }
  83. expect.fail('command should have failed')
  84. })
  85. })
  86. describe('delete-user', function () {
  87. let user
  88. beforeEach(async function () {
  89. user = new User()
  90. await user.login()
  91. })
  92. it('should log missing user', function () {
  93. const email = 'does-not-exist@example.com'
  94. const out = run(
  95. 'node modules/server-ce-scripts/scripts/delete-user --email=' + email
  96. )
  97. expect(out).to.include('not in database, potentially already deleted')
  98. })
  99. it('should exit with code 0 on success', function () {
  100. const email = user.email
  101. run('node modules/server-ce-scripts/scripts/delete-user --email=' + email)
  102. })
  103. it('should have deleted the user on success', async function () {
  104. const email = user.email
  105. run('node modules/server-ce-scripts/scripts/delete-user --email=' + email)
  106. const dbEntry = await user.get()
  107. expect(dbEntry).to.not.exist
  108. })
  109. it('should exit with code 1 on missing email', function () {
  110. try {
  111. run('node modules/server-ce-scripts/scripts/delete-user')
  112. } catch (e) {
  113. expect(e.status).to.equal(1)
  114. return
  115. }
  116. expect.fail('command should have failed')
  117. })
  118. })
  119. })