ServerCEScriptsTests.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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', 'pipe'],
  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. describe('change-compile-timeout', function () {
  142. let userA, userB
  143. beforeEach('login', async function () {
  144. userA = new User()
  145. await userA.login()
  146. userB = new User()
  147. await userB.login()
  148. })
  149. async function getCompileTimeout(user) {
  150. const { compileTimeout } = await user.getFeatures()
  151. return compileTimeout
  152. }
  153. let userATimeout, userBTimeout
  154. beforeEach('fetch current state', async function () {
  155. userATimeout = await getCompileTimeout(userA)
  156. userBTimeout = await getCompileTimeout(userB)
  157. })
  158. describe('happy path', function () {
  159. let newUserATimeout
  160. beforeEach('run script on user a', function () {
  161. newUserATimeout = userATimeout - 1
  162. run(
  163. `node modules/server-ce-scripts/scripts/change-compile-timeout --user-id=${userA.id} --compile-timeout=${newUserATimeout}`
  164. )
  165. })
  166. it('should change the timeout for user a', async function () {
  167. const actual = await getCompileTimeout(userA)
  168. expect(actual).to.not.equal(userATimeout)
  169. expect(actual).to.equal(newUserATimeout)
  170. })
  171. it('should leave the timeout for user b as is', async function () {
  172. expect(await getCompileTimeout(userB)).to.equal(userBTimeout)
  173. })
  174. })
  175. describe('bad options', function () {
  176. it('should reject zero timeout', async function () {
  177. try {
  178. run(
  179. `node modules/server-ce-scripts/scripts/change-compile-timeout --user-id=${userA.id} --compile-timeout=0`
  180. )
  181. expect.fail('should error out')
  182. } catch (err) {
  183. expect(err.stderr.toString()).to.include('positive number of seconds')
  184. }
  185. expect(await getCompileTimeout(userA)).to.equal(userATimeout)
  186. expect(await getCompileTimeout(userB)).to.equal(userBTimeout)
  187. })
  188. it('should reject a 20min timeout', async function () {
  189. try {
  190. run(
  191. `node modules/server-ce-scripts/scripts/change-compile-timeout --user-id=${userA.id} --compile-timeout=1200`
  192. )
  193. expect.fail('should error out')
  194. } catch (err) {
  195. expect(err.stderr.toString()).to.include('below 10 minutes')
  196. }
  197. expect(await getCompileTimeout(userA)).to.equal(userATimeout)
  198. expect(await getCompileTimeout(userB)).to.equal(userBTimeout)
  199. })
  200. })
  201. })
  202. })