ProjectOptionsHandlerTests.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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().yields()
  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', function (done) {
  47. this.handler.setCompiler(projectId, 'xeLaTeX', err => {
  48. const args = this.projectModel.updateOne.args[0]
  49. args[0]._id.should.equal(projectId)
  50. args[1].compiler.should.equal('xelatex')
  51. done()
  52. })
  53. })
  54. it('should not perform and update on mongo if it is not a recognised compiler', function (done) {
  55. this.handler.setCompiler(projectId, 'something', err => {
  56. this.projectModel.updateOne.called.should.equal(false)
  57. done()
  58. })
  59. })
  60. describe('when called without arg', function () {
  61. it('should callback with null', function (done) {
  62. this.handler.setCompiler(projectId, null, err => {
  63. expect(err).to.be.undefined
  64. this.projectModel.updateOne.callCount.should.equal(0)
  65. done()
  66. })
  67. })
  68. })
  69. describe('when mongo update error occurs', function () {
  70. beforeEach(function () {
  71. this.projectModel.updateOne = sinon.stub().yields('error')
  72. })
  73. it('should callback with error', function (done) {
  74. this.handler.setCompiler(projectId, 'xeLaTeX', err => {
  75. err.should.equal('error')
  76. done()
  77. })
  78. })
  79. })
  80. })
  81. describe('Setting the imageName', function () {
  82. it('should perform and update on mongo', function (done) {
  83. this.handler.setImageName(projectId, 'texlive-1234.5', err => {
  84. const args = this.projectModel.updateOne.args[0]
  85. args[0]._id.should.equal(projectId)
  86. args[1].imageName.should.equal('docker-repo/subdir/texlive-1234.5')
  87. done()
  88. })
  89. })
  90. it('should not perform and update on mongo if it is not a reconised compiler', function (done) {
  91. this.handler.setImageName(projectId, 'something', err => {
  92. this.projectModel.updateOne.called.should.equal(false)
  93. done()
  94. })
  95. })
  96. describe('when called without arg', function () {
  97. it('should callback with null', function (done) {
  98. this.handler.setImageName(projectId, null, err => {
  99. expect(err).to.be.undefined
  100. this.projectModel.updateOne.callCount.should.equal(0)
  101. done()
  102. })
  103. })
  104. })
  105. describe('when mongo update error occurs', function () {
  106. beforeEach(function () {
  107. this.projectModel.updateOne = sinon.stub().yields('error')
  108. })
  109. it('should callback with error', function (done) {
  110. this.handler.setImageName(projectId, 'texlive-1234.5', err => {
  111. err.should.equal('error')
  112. done()
  113. })
  114. })
  115. })
  116. })
  117. describe('setting the spellCheckLanguage', function () {
  118. it('should perform and update on mongo', function (done) {
  119. this.handler.setSpellCheckLanguage(projectId, 'fr', err => {
  120. const args = this.projectModel.updateOne.args[0]
  121. args[0]._id.should.equal(projectId)
  122. args[1].spellCheckLanguage.should.equal('fr')
  123. done()
  124. })
  125. })
  126. it('should not perform and update on mongo if it is not a reconised compiler', function (done) {
  127. this.handler.setSpellCheckLanguage(projectId, 'no a lang', err => {
  128. this.projectModel.updateOne.called.should.equal(false)
  129. done()
  130. })
  131. })
  132. it('should perform and update on mongo if the language is blank (means turn it off)', function (done) {
  133. this.handler.setSpellCheckLanguage(projectId, '', err => {
  134. this.projectModel.updateOne.called.should.equal(true)
  135. done()
  136. })
  137. })
  138. describe('when mongo update error occurs', function () {
  139. beforeEach(function () {
  140. this.projectModel.updateOne = sinon.stub().yields('error')
  141. })
  142. it('should callback with error', function (done) {
  143. this.handler.setSpellCheckLanguage(projectId, 'fr', err => {
  144. err.should.equal('error')
  145. done()
  146. })
  147. })
  148. })
  149. })
  150. describe('setting the brandVariationId', function () {
  151. it('should perform and update on mongo', function (done) {
  152. this.handler.setBrandVariationId(projectId, '123', err => {
  153. const args = this.projectModel.updateOne.args[0]
  154. args[0]._id.should.equal(projectId)
  155. args[1].brandVariationId.should.equal('123')
  156. done()
  157. })
  158. })
  159. it('should not perform and update on mongo if there is no brand variation', function (done) {
  160. this.handler.setBrandVariationId(projectId, null, err => {
  161. this.projectModel.updateOne.called.should.equal(false)
  162. done()
  163. })
  164. })
  165. it('should not perform and update on mongo if brand variation is an empty string', function (done) {
  166. this.handler.setBrandVariationId(projectId, '', err => {
  167. this.projectModel.updateOne.called.should.equal(false)
  168. done()
  169. })
  170. })
  171. describe('when mongo update error occurs', function () {
  172. beforeEach(function () {
  173. this.projectModel.updateOne = sinon.stub().yields('error')
  174. })
  175. it('should callback with error', function (done) {
  176. this.handler.setBrandVariationId(projectId, '123', err => {
  177. err.should.equal('error')
  178. done()
  179. })
  180. })
  181. })
  182. })
  183. describe('unsetting the brandVariationId', function () {
  184. it('should perform and update on mongo', function (done) {
  185. this.handler.unsetBrandVariationId(projectId, err => {
  186. const args = this.projectModel.updateOne.args[0]
  187. args[0]._id.should.equal(projectId)
  188. expect(args[1]).to.deep.equal({ $unset: { brandVariationId: 1 } })
  189. done()
  190. })
  191. })
  192. describe('when mongo update error occurs', function () {
  193. beforeEach(function () {
  194. this.projectModel.updateOne = sinon.stub().yields('error')
  195. })
  196. it('should callback with error', function (done) {
  197. this.handler.unsetBrandVariationId(projectId, err => {
  198. err.should.equal('error')
  199. done()
  200. })
  201. })
  202. })
  203. })
  204. })