ProjectOptionsHandlerTests.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. no-useless-constructor,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const sinon = require('sinon')
  16. const { expect } = require('chai')
  17. const modulePath =
  18. '../../../../app/src/Features/Project/ProjectOptionsHandler.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. const { ObjectId } = require('mongodb')
  21. describe('ProjectOptionsHandler', function () {
  22. const projectId = '4eecaffcbffa66588e000008'
  23. beforeEach(function () {
  24. let Project
  25. this.projectModel = Project = class Project {
  26. constructor(options) {}
  27. }
  28. this.projectModel.updateOne = sinon.stub().resolves()
  29. this.db = {
  30. projects: {
  31. updateOne: sinon.stub().resolves(),
  32. },
  33. }
  34. this.handler = SandboxedModule.require(modulePath, {
  35. requires: {
  36. '../../models/Project': { Project: this.projectModel },
  37. '@overleaf/settings': {
  38. languages: [
  39. { name: 'English', code: 'en' },
  40. { name: 'French', code: 'fr' },
  41. ],
  42. imageRoot: 'docker-repo/subdir',
  43. allowedImageNames: [
  44. { imageName: 'texlive-0000.0', imageDesc: 'test image 0' },
  45. { imageName: 'texlive-1234.5', imageDesc: 'test image 1' },
  46. ],
  47. },
  48. '../../infrastructure/mongodb': { db: this.db, ObjectId },
  49. },
  50. })
  51. })
  52. describe('Setting the compiler', function () {
  53. it('should perform and update on mongo', async function () {
  54. await this.handler.promises.setCompiler(projectId, 'xeLaTeX')
  55. const args = this.projectModel.updateOne.args[0]
  56. args[0]._id.should.equal(projectId)
  57. args[1].compiler.should.equal('xelatex')
  58. })
  59. it('should not perform and update on mongo if it is not a recognised compiler', async function () {
  60. const fakeComplier = 'something'
  61. expect(
  62. this.handler.promises.setCompiler(projectId, 'something')
  63. ).to.be.rejectedWith(`invalid compiler: ${fakeComplier}`)
  64. this.projectModel.updateOne.called.should.equal(false)
  65. })
  66. describe('when called without arg', function () {
  67. it('should callback with null', async function () {
  68. await this.handler.promises.setCompiler(projectId, null)
  69. this.projectModel.updateOne.callCount.should.equal(0)
  70. })
  71. })
  72. describe('when mongo update error occurs', function () {
  73. beforeEach(function () {
  74. this.projectModel.updateOne = sinon.stub().yields('error')
  75. })
  76. it('should be rejected', async function () {
  77. expect(this.handler.promises.setCompiler(projectId, 'xeLaTeX')).to.be
  78. .rejected
  79. })
  80. })
  81. })
  82. describe('Setting the imageName', function () {
  83. it('should perform and update on mongo', async function () {
  84. await this.handler.promises.setImageName(projectId, 'texlive-1234.5')
  85. const args = this.projectModel.updateOne.args[0]
  86. args[0]._id.should.equal(projectId)
  87. args[1].imageName.should.equal('docker-repo/subdir/texlive-1234.5')
  88. })
  89. it('should not perform and update on mongo if it is not a reconised image name', async function () {
  90. const fakeImageName = 'something'
  91. expect(
  92. this.handler.promises.setImageName(projectId, fakeImageName)
  93. ).to.be.rejectedWith(`invalid imageName: ${fakeImageName}`)
  94. this.projectModel.updateOne.called.should.equal(false)
  95. })
  96. describe('when called without arg', function () {
  97. it('should callback with null', async function () {
  98. await this.handler.promises.setImageName(projectId, null)
  99. this.projectModel.updateOne.callCount.should.equal(0)
  100. })
  101. })
  102. describe('when mongo update error occurs', function () {
  103. beforeEach(function () {
  104. this.projectModel.updateOne = sinon.stub().yields('error')
  105. })
  106. it('should be rejected', async function () {
  107. expect(this.handler.promises.setImageName(projectId, 'texlive-1234.5'))
  108. .to.be.rejected
  109. })
  110. })
  111. })
  112. describe('setting the spellCheckLanguage', function () {
  113. it('should perform and update on mongo', async function () {
  114. await this.handler.promises.setSpellCheckLanguage(projectId, 'fr')
  115. const args = this.projectModel.updateOne.args[0]
  116. args[0]._id.should.equal(projectId)
  117. args[1].spellCheckLanguage.should.equal('fr')
  118. })
  119. it('should not perform and update on mongo if it is not a reconised langauge', async function () {
  120. const fakeLanguageCode = 'not a lang'
  121. expect(
  122. this.handler.promises.setSpellCheckLanguage(projectId, fakeLanguageCode)
  123. ).to.be.rejectedWith(`invalid languageCode: ${fakeLanguageCode}`)
  124. this.projectModel.updateOne.called.should.equal(false)
  125. })
  126. it('should perform and update on mongo if the language is blank (means turn it off)', async function () {
  127. await this.handler.promises.setSpellCheckLanguage(projectId, '')
  128. this.projectModel.updateOne.called.should.equal(true)
  129. })
  130. describe('when mongo update error occurs', function () {
  131. beforeEach(function () {
  132. this.projectModel.updateOne = sinon.stub().yields('error')
  133. })
  134. it('should be rejected', async function () {
  135. expect(this.handler.promises.setSpellCheckLanguage(projectId)).to.be
  136. .rejected
  137. })
  138. })
  139. })
  140. describe('setting the brandVariationId', function () {
  141. it('should perform and update on mongo', async function () {
  142. await this.handler.promises.setBrandVariationId(projectId, '123')
  143. const args = this.projectModel.updateOne.args[0]
  144. args[0]._id.should.equal(projectId)
  145. args[1].brandVariationId.should.equal('123')
  146. })
  147. it('should not perform and update on mongo if there is no brand variation', async function () {
  148. await this.handler.promises.setBrandVariationId(projectId, null)
  149. this.projectModel.updateOne.called.should.equal(false)
  150. })
  151. it('should not perform and update on mongo if brand variation is an empty string', async function () {
  152. await this.handler.promises.setBrandVariationId(projectId, '')
  153. this.projectModel.updateOne.called.should.equal(false)
  154. })
  155. describe('when mongo update error occurs', function () {
  156. beforeEach(function () {
  157. this.projectModel.updateOne = sinon.stub().yields('error')
  158. })
  159. it('should be rejected', async function () {
  160. expect(this.handler.promises.setBrandVariationId(projectId, '123')).to
  161. .be.rejected
  162. })
  163. })
  164. })
  165. describe('setting the rangesSupportEnabled', function () {
  166. it('should perform and update on mongo', async function () {
  167. await this.handler.promises.setHistoryRangesSupport(projectId, true)
  168. sinon.assert.calledWith(
  169. this.db.projects.updateOne,
  170. { _id: new ObjectId(projectId) },
  171. { $set: { 'overleaf.history.rangesSupportEnabled': true } }
  172. )
  173. })
  174. describe('when mongo update error occurs', function () {
  175. beforeEach(function () {
  176. this.db.projects.updateOne = sinon.stub().yields('error')
  177. })
  178. it('should be rejected', async function () {
  179. expect(this.handler.promises.setHistoryRangesSupport(projectId, true))
  180. .to.be.rejected
  181. })
  182. })
  183. })
  184. describe('unsetting the brandVariationId', function () {
  185. it('should perform and update on mongo', async function () {
  186. await this.handler.promises.unsetBrandVariationId(projectId)
  187. const args = this.projectModel.updateOne.args[0]
  188. args[0]._id.should.equal(projectId)
  189. expect(args[1]).to.deep.equal({ $unset: { brandVariationId: 1 } })
  190. })
  191. describe('when mongo update error occurs', function () {
  192. beforeEach(function () {
  193. this.projectModel.updateOne = sinon.stub().yields('error')
  194. })
  195. it('should be rejected', async function () {
  196. expect(this.handler.promises.unsetBrandVariationId(projectId)).to.be
  197. .rejected
  198. })
  199. })
  200. })
  201. })