ServerCEScriptsTests.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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('REDIS_PORT=42 node modules/server-ce-scripts/scripts/check-redis')
  48. } catch (e) {
  49. expect(e.status).to.equal(1)
  50. return
  51. }
  52. expect.fail('command should have failed')
  53. })
  54. })
  55. describe('create-user', function () {
  56. it('should exit with code 0 on success', function () {
  57. const out = run(
  58. 'node modules/server-ce-scripts/scripts/create-user --email=foo@bar.com'
  59. )
  60. expect(out).to.include('/user/activate?token=')
  61. })
  62. it('should create a regular user by default', async function () {
  63. run(
  64. 'node modules/server-ce-scripts/scripts/create-user --email=foo@bar.com'
  65. )
  66. expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: false })
  67. })
  68. it('should create an admin user with --admin flag', async function () {
  69. run(
  70. 'node modules/server-ce-scripts/scripts/create-user --admin --email=foo@bar.com'
  71. )
  72. expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: true })
  73. })
  74. it('should exit with code 1 on missing email', function () {
  75. try {
  76. run('node modules/server-ce-scripts/scripts/create-user')
  77. } catch (e) {
  78. expect(e.status).to.equal(1)
  79. return
  80. }
  81. expect.fail('command should have failed')
  82. })
  83. })
  84. describe('delete-user', function () {
  85. let user
  86. beforeEach(async function () {
  87. user = new User()
  88. await user.login()
  89. })
  90. it('should log missing user', function () {
  91. const email = 'does-not-exist@example.com'
  92. const out = run(
  93. 'node modules/server-ce-scripts/scripts/delete-user --email=' + email
  94. )
  95. expect(out).to.include('not in database, potentially already deleted')
  96. })
  97. it('should exit with code 0 on success', function () {
  98. const email = user.email
  99. run('node modules/server-ce-scripts/scripts/delete-user --email=' + email)
  100. })
  101. it('should have deleted the user on success', async function () {
  102. const email = user.email
  103. run('node modules/server-ce-scripts/scripts/delete-user --email=' + email)
  104. const dbEntry = await user.get()
  105. expect(dbEntry).to.not.exist
  106. })
  107. it('should exit with code 1 on missing email', function () {
  108. try {
  109. run('node modules/server-ce-scripts/scripts/delete-user')
  110. } catch (e) {
  111. expect(e.status).to.equal(1)
  112. return
  113. }
  114. expect.fail('command should have failed')
  115. })
  116. })
  117. describe('rename-tag', function () {
  118. let user
  119. beforeEach(async function () {
  120. user = new User()
  121. await user.login()
  122. })
  123. async function createTag(name) {
  124. await user.doRequest('POST', { url: '/tag', json: { name } })
  125. }
  126. async function getTagNames() {
  127. const { body } = await user.doRequest('GET', { url: '/tag', json: true })
  128. return body.map(tag => tag.name)
  129. }
  130. it('should rename a tag', async function () {
  131. const oldName = 'before'
  132. const newName = 'after'
  133. await createTag(oldName)
  134. expect(await getTagNames()).to.deep.equal([oldName])
  135. run(
  136. `node modules/server-ce-scripts/scripts/rename-tag --user-id=${user.id} --old-name=${oldName} --new-name=${newName}`
  137. )
  138. expect(await getTagNames()).to.deep.equal([newName])
  139. })
  140. })
  141. })