ServerCEScriptsTests.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. cwd: 'modules/server-ce-scripts/scripts',
  18. }).toString()
  19. }
  20. async function getUser(email) {
  21. return db.users.findOne({ email }, { projection: { _id: 0, isAdmin: 1 } })
  22. }
  23. describe('ServerCEScripts', function () {
  24. describe('check-mongodb', function () {
  25. it('should exit with code 0 on success', function () {
  26. run('node check-mongodb')
  27. })
  28. it('should exit with code 1 on error', function () {
  29. try {
  30. run(
  31. 'MONGO_SERVER_SELECTION_TIMEOUT=1' +
  32. 'MONGO_CONNECTION_STRING=mongodb://localhost:4242 ' +
  33. 'node check-mongodb'
  34. )
  35. } catch (e) {
  36. expect(e.status).to.equal(1)
  37. return
  38. }
  39. expect.fail('command should have failed')
  40. })
  41. })
  42. describe('check-redis', function () {
  43. it('should exit with code 0 on success', function () {
  44. run('node check-redis')
  45. })
  46. it('should exit with code 1 on error', function () {
  47. try {
  48. run('REDIS_HOST=localhost node check-redis')
  49. } catch (e) {
  50. expect(e.status).to.equal(1)
  51. return
  52. }
  53. expect.fail('command should have failed')
  54. })
  55. })
  56. describe('create-user', function () {
  57. it('should exit with code 0 on success', function () {
  58. const out = run('node create-user --email=foo@bar.com')
  59. expect(out).to.include('/user/activate?token=')
  60. })
  61. it('should create a regular user by default', async function () {
  62. run('node create-user --email=foo@bar.com')
  63. expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: false })
  64. })
  65. it('should create an admin user with --admin flag', async function () {
  66. run('node create-user --admin --email=foo@bar.com')
  67. expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: true })
  68. })
  69. it('should exit with code 1 on missing email', function () {
  70. try {
  71. run('node create-user')
  72. } catch (e) {
  73. expect(e.status).to.equal(1)
  74. return
  75. }
  76. expect.fail('command should have failed')
  77. })
  78. })
  79. describe('delete-user', function () {
  80. let user
  81. beforeEach(async function () {
  82. user = new User()
  83. await user.login()
  84. })
  85. it('should log missing user', function () {
  86. const email = 'does-not-exist@example.com'
  87. const out = run('node delete-user --email=' + email)
  88. expect(out).to.include('not in database, potentially already deleted')
  89. })
  90. it('should exit with code 0 on success', function () {
  91. const email = user.email
  92. run('node delete-user --email=' + email)
  93. })
  94. it('should have deleted the user on success', async function () {
  95. const email = user.email
  96. run('node delete-user --email=' + email)
  97. const dbEntry = await user.get()
  98. expect(dbEntry).to.not.exist
  99. })
  100. it('should exit with code 1 on missing email', function () {
  101. try {
  102. run('node delete-user')
  103. } catch (e) {
  104. expect(e.status).to.equal(1)
  105. return
  106. }
  107. expect.fail('command should have failed')
  108. })
  109. })
  110. })