ProjectOptionsHandlerTests.js 7.3 KB

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