ProjectOptionsHandlerTests.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* eslint-disable
  2. camelcase,
  3. 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 chai = require('chai')
  18. const { expect } = chai
  19. const should = chai.should()
  20. const modulePath =
  21. '../../../../app/src/Features/Project/ProjectOptionsHandler.js'
  22. const SandboxedModule = require('sandboxed-module')
  23. describe('ProjectOptionsHandler', function() {
  24. const project_id = '4eecaffcbffa66588e000008'
  25. beforeEach(function() {
  26. let Project
  27. this.projectModel = Project = class Project {
  28. constructor(options) {}
  29. }
  30. this.projectModel.update = sinon.stub().yields()
  31. this.handler = SandboxedModule.require(modulePath, {
  32. globals: {
  33. console: console
  34. },
  35. requires: {
  36. '../../models/Project': { Project: this.projectModel },
  37. 'settings-sharelatex': {
  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. 'logger-sharelatex': {
  49. log() {},
  50. err() {}
  51. }
  52. }
  53. })
  54. })
  55. describe('Setting the compiler', function() {
  56. it('should perform and update on mongo', function(done) {
  57. this.handler.setCompiler(project_id, 'xeLaTeX', err => {
  58. const args = this.projectModel.update.args[0]
  59. args[0]._id.should.equal(project_id)
  60. args[1].compiler.should.equal('xelatex')
  61. done()
  62. })
  63. })
  64. it('should not perform and update on mongo if it is not a recognised compiler', function(done) {
  65. this.handler.setCompiler(project_id, 'something', err => {
  66. this.projectModel.update.called.should.equal(false)
  67. done()
  68. })
  69. })
  70. describe('when called without arg', function() {
  71. it('should callback with null', function(done) {
  72. this.handler.setCompiler(project_id, null, err => {
  73. expect(err).to.be.undefined
  74. this.projectModel.update.callCount.should.equal(0)
  75. done()
  76. })
  77. })
  78. })
  79. describe('when mongo update error occurs', function() {
  80. beforeEach(function() {
  81. this.projectModel.update = sinon.stub().yields('error')
  82. })
  83. it('should callback with error', function(done) {
  84. this.handler.setCompiler(project_id, 'xeLaTeX', err => {
  85. err.should.equal('error')
  86. done()
  87. })
  88. })
  89. })
  90. })
  91. describe('Setting the imageName', function() {
  92. it('should perform and update on mongo', function(done) {
  93. this.handler.setImageName(project_id, 'texlive-1234.5', err => {
  94. const args = this.projectModel.update.args[0]
  95. args[0]._id.should.equal(project_id)
  96. args[1].imageName.should.equal('docker-repo/subdir/texlive-1234.5')
  97. done()
  98. })
  99. })
  100. it('should not perform and update on mongo if it is not a reconised compiler', function(done) {
  101. this.handler.setImageName(project_id, 'something', err => {
  102. this.projectModel.update.called.should.equal(false)
  103. done()
  104. })
  105. })
  106. describe('when called without arg', function() {
  107. it('should callback with null', function(done) {
  108. this.handler.setImageName(project_id, null, err => {
  109. expect(err).to.be.undefined
  110. this.projectModel.update.callCount.should.equal(0)
  111. done()
  112. })
  113. })
  114. })
  115. describe('when mongo update error occurs', function() {
  116. beforeEach(function() {
  117. this.projectModel.update = sinon.stub().yields('error')
  118. })
  119. it('should callback with error', function(done) {
  120. this.handler.setImageName(project_id, 'texlive-1234.5', err => {
  121. err.should.equal('error')
  122. done()
  123. })
  124. })
  125. })
  126. })
  127. describe('setting the spellCheckLanguage', function() {
  128. it('should perform and update on mongo', function(done) {
  129. this.handler.setSpellCheckLanguage(project_id, 'fr', err => {
  130. const args = this.projectModel.update.args[0]
  131. args[0]._id.should.equal(project_id)
  132. args[1].spellCheckLanguage.should.equal('fr')
  133. done()
  134. })
  135. })
  136. it('should not perform and update on mongo if it is not a reconised compiler', function(done) {
  137. this.handler.setSpellCheckLanguage(project_id, 'no a lang', err => {
  138. this.projectModel.update.called.should.equal(false)
  139. done()
  140. })
  141. })
  142. it('should perform and update on mongo if the language is blank (means turn it off)', function(done) {
  143. this.handler.setSpellCheckLanguage(project_id, '', err => {
  144. this.projectModel.update.called.should.equal(true)
  145. done()
  146. })
  147. })
  148. describe('when mongo update error occurs', function() {
  149. beforeEach(function() {
  150. this.projectModel.update = sinon.stub().yields('error')
  151. })
  152. it('should callback with error', function(done) {
  153. this.handler.setSpellCheckLanguage(project_id, 'fr', err => {
  154. err.should.equal('error')
  155. done()
  156. })
  157. })
  158. })
  159. })
  160. describe('setting the brandVariationId', function() {
  161. it('should perform and update on mongo', function(done) {
  162. this.handler.setBrandVariationId(project_id, '123', err => {
  163. const args = this.projectModel.update.args[0]
  164. args[0]._id.should.equal(project_id)
  165. args[1].brandVariationId.should.equal('123')
  166. done()
  167. })
  168. })
  169. it('should not perform and update on mongo if there is no brand variation', function(done) {
  170. this.handler.setBrandVariationId(project_id, null, err => {
  171. this.projectModel.update.called.should.equal(false)
  172. done()
  173. })
  174. })
  175. it('should not perform and update on mongo if brand variation is an empty string', function(done) {
  176. this.handler.setBrandVariationId(project_id, '', err => {
  177. this.projectModel.update.called.should.equal(false)
  178. done()
  179. })
  180. })
  181. describe('when mongo update error occurs', function() {
  182. beforeEach(function() {
  183. this.projectModel.update = sinon.stub().yields('error')
  184. })
  185. it('should callback with error', function(done) {
  186. this.handler.setBrandVariationId(project_id, '123', err => {
  187. err.should.equal('error')
  188. done()
  189. })
  190. })
  191. })
  192. })
  193. describe('unsetting the brandVariationId', function() {
  194. it('should perform and update on mongo', function(done) {
  195. this.handler.unsetBrandVariationId(project_id, err => {
  196. const args = this.projectModel.update.args[0]
  197. args[0]._id.should.equal(project_id)
  198. expect(args[1]).to.deep.equal({ $unset: { brandVariationId: 1 } })
  199. done()
  200. })
  201. })
  202. describe('when mongo update error occurs', function() {
  203. beforeEach(function() {
  204. this.projectModel.update = sinon.stub().yields('error')
  205. })
  206. it('should callback with error', function(done) {
  207. this.handler.unsetBrandVariationId(project_id, err => {
  208. err.should.equal('error')
  209. done()
  210. })
  211. })
  212. })
  213. })
  214. })