ProjectOptionsHandlerTests.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. describe('ProjectOptionsHandler', function () {
  21. const projectId = '4eecaffcbffa66588e000008'
  22. beforeEach(function () {
  23. let Project
  24. this.projectModel = Project = class Project {
  25. constructor(options) {}
  26. }
  27. this.projectModel.updateOne = sinon.stub().resolves()
  28. this.handler = SandboxedModule.require(modulePath, {
  29. requires: {
  30. '../../models/Project': { Project: this.projectModel },
  31. '@overleaf/settings': {
  32. languages: [
  33. { name: 'English', code: 'en' },
  34. { name: 'French', code: 'fr' },
  35. ],
  36. imageRoot: 'docker-repo/subdir',
  37. allowedImageNames: [
  38. { imageName: 'texlive-0000.0', imageDesc: 'test image 0' },
  39. { imageName: 'texlive-1234.5', imageDesc: 'test image 1' },
  40. ],
  41. },
  42. },
  43. })
  44. })
  45. describe('Setting the compiler', function () {
  46. it('should perform and update on mongo', async function () {
  47. await this.handler.promises.setCompiler(projectId, 'xeLaTeX')
  48. const args = this.projectModel.updateOne.args[0]
  49. args[0]._id.should.equal(projectId)
  50. args[1].compiler.should.equal('xelatex')
  51. })
  52. it('should not perform and update on mongo if it is not a recognised compiler', async function () {
  53. const fakeComplier = 'something'
  54. expect(
  55. this.handler.promises.setCompiler(projectId, 'something')
  56. ).to.be.rejectedWith(`invalid compiler: ${fakeComplier}`)
  57. this.projectModel.updateOne.called.should.equal(false)
  58. })
  59. describe('when called without arg', function () {
  60. it('should callback with null', async function () {
  61. await this.handler.promises.setCompiler(projectId, null)
  62. this.projectModel.updateOne.callCount.should.equal(0)
  63. })
  64. })
  65. describe('when mongo update error occurs', function () {
  66. beforeEach(function () {
  67. this.projectModel.updateOne = sinon.stub().yields('error')
  68. })
  69. it('should be rejected', async function () {
  70. expect(this.handler.promises.setCompiler(projectId, 'xeLaTeX')).to.be
  71. .rejected
  72. })
  73. })
  74. })
  75. describe('Setting the imageName', function () {
  76. it('should perform and update on mongo', async function () {
  77. await this.handler.promises.setImageName(projectId, 'texlive-1234.5')
  78. const args = this.projectModel.updateOne.args[0]
  79. args[0]._id.should.equal(projectId)
  80. args[1].imageName.should.equal('docker-repo/subdir/texlive-1234.5')
  81. })
  82. it('should not perform and update on mongo if it is not a reconised image name', async function () {
  83. const fakeImageName = 'something'
  84. expect(
  85. this.handler.promises.setImageName(projectId, fakeImageName)
  86. ).to.be.rejectedWith(`invalid imageName: ${fakeImageName}`)
  87. this.projectModel.updateOne.called.should.equal(false)
  88. })
  89. describe('when called without arg', function () {
  90. it('should callback with null', async function () {
  91. await this.handler.promises.setImageName(projectId, null)
  92. this.projectModel.updateOne.callCount.should.equal(0)
  93. })
  94. })
  95. describe('when mongo update error occurs', function () {
  96. beforeEach(function () {
  97. this.projectModel.updateOne = sinon.stub().yields('error')
  98. })
  99. it('should be rejected', async function () {
  100. expect(this.handler.promises.setImageName(projectId, 'texlive-1234.5'))
  101. .to.be.rejected
  102. })
  103. })
  104. })
  105. describe('setting the spellCheckLanguage', function () {
  106. it('should perform and update on mongo', async function () {
  107. await this.handler.promises.setSpellCheckLanguage(projectId, 'fr')
  108. const args = this.projectModel.updateOne.args[0]
  109. args[0]._id.should.equal(projectId)
  110. args[1].spellCheckLanguage.should.equal('fr')
  111. })
  112. it('should not perform and update on mongo if it is not a reconised langauge', async function () {
  113. const fakeLanguageCode = 'not a lang'
  114. expect(
  115. this.handler.promises.setSpellCheckLanguage(projectId, fakeLanguageCode)
  116. ).to.be.rejectedWith(`invalid languageCode: ${fakeLanguageCode}`)
  117. this.projectModel.updateOne.called.should.equal(false)
  118. })
  119. it('should perform and update on mongo if the language is blank (means turn it off)', async function () {
  120. await this.handler.promises.setSpellCheckLanguage(projectId, '')
  121. this.projectModel.updateOne.called.should.equal(true)
  122. })
  123. describe('when mongo update error occurs', function () {
  124. beforeEach(function () {
  125. this.projectModel.updateOne = sinon.stub().yields('error')
  126. })
  127. it('should be rejected', async function () {
  128. expect(this.handler.promises.setSpellCheckLanguage(projectId)).to.be
  129. .rejected
  130. })
  131. })
  132. })
  133. describe('setting the brandVariationId', function () {
  134. it('should perform and update on mongo', async function () {
  135. await this.handler.promises.setBrandVariationId(projectId, '123')
  136. const args = this.projectModel.updateOne.args[0]
  137. args[0]._id.should.equal(projectId)
  138. args[1].brandVariationId.should.equal('123')
  139. })
  140. it('should not perform and update on mongo if there is no brand variation', async function () {
  141. await this.handler.promises.setBrandVariationId(projectId, null)
  142. this.projectModel.updateOne.called.should.equal(false)
  143. })
  144. it('should not perform and update on mongo if brand variation is an empty string', async function () {
  145. await this.handler.promises.setBrandVariationId(projectId, '')
  146. this.projectModel.updateOne.called.should.equal(false)
  147. })
  148. describe('when mongo update error occurs', function () {
  149. beforeEach(function () {
  150. this.projectModel.updateOne = sinon.stub().yields('error')
  151. })
  152. it('should be rejected', async function () {
  153. expect(this.handler.promises.setBrandVariationId(projectId, '123')).to
  154. .be.rejected
  155. })
  156. })
  157. })
  158. describe('unsetting the brandVariationId', function () {
  159. it('should perform and update on mongo', async function () {
  160. await this.handler.promises.unsetBrandVariationId(projectId)
  161. const args = this.projectModel.updateOne.args[0]
  162. args[0]._id.should.equal(projectId)
  163. expect(args[1]).to.deep.equal({ $unset: { brandVariationId: 1 } })
  164. })
  165. describe('when mongo update error occurs', function () {
  166. beforeEach(function () {
  167. this.projectModel.updateOne = sinon.stub().yields('error')
  168. })
  169. it('should be rejected', async function () {
  170. expect(this.handler.promises.unsetBrandVariationId(projectId)).to.be
  171. .rejected
  172. })
  173. })
  174. })
  175. })