TemplatesManagerTests.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const assert = require('assert')
  15. const chai = require('chai')
  16. const sinon = require('sinon')
  17. const should = require('chai').should()
  18. const modulePath = '../../../../app/src/Features/Templates/TemplatesManager'
  19. describe('TemplatesManager', function() {
  20. beforeEach(function() {
  21. this.project_id = 'project-id'
  22. this.brandVariationId = 'brand-variation-id'
  23. this.compiler = 'pdflatex'
  24. this.imageName = 'TL2017'
  25. this.mainFile = 'main.tex'
  26. this.templateId = 'template-id'
  27. this.templateName = 'template name'
  28. this.templateVersionId = 'template-version-id'
  29. this.user_id = 'user-id'
  30. this.dumpPath = `${this.dumpFolder}/${this.uuid}`
  31. this.callback = sinon.stub()
  32. this.request = sinon.stub().returns({
  33. pipe() {},
  34. on() {},
  35. response: {
  36. statusCode: 200
  37. }
  38. })
  39. this.fs = {
  40. unlink: sinon.stub(),
  41. createWriteStream: sinon.stub().returns({ on: sinon.stub().yields() })
  42. }
  43. this.ProjectUploadManager = {
  44. createProjectFromZipArchiveWithName: sinon
  45. .stub()
  46. .callsArgWith(4, null, { _id: this.project_id })
  47. }
  48. this.dumpFolder = 'dump/path'
  49. this.ProjectOptionsHandler = {
  50. setCompiler: sinon.stub().callsArgWith(2),
  51. setImageName: sinon.stub().callsArgWith(2),
  52. setBrandVariationId: sinon.stub().callsArgWith(2)
  53. }
  54. this.uuid = '1234'
  55. this.ProjectRootDocManager = {
  56. setRootDocFromName: sinon.stub().callsArgWith(2)
  57. }
  58. this.ProjectDetailsHandler = {
  59. getProjectDescription: sinon.stub(),
  60. fixProjectName: sinon.stub().returns(this.templateName)
  61. }
  62. this.Project = { update: sinon.stub().callsArgWith(3, null) }
  63. this.FileWriter = { ensureDumpFolderExists: sinon.stub().callsArg(0) }
  64. this.TemplatesManager = SandboxedModule.require(modulePath, {
  65. globals: {
  66. console: console
  67. },
  68. requires: {
  69. '../Uploads/ProjectUploadManager': this.ProjectUploadManager,
  70. '../Project/ProjectOptionsHandler': this.ProjectOptionsHandler,
  71. '../Project/ProjectRootDocManager': this.ProjectRootDocManager,
  72. '../Project/ProjectDetailsHandler': this.ProjectDetailsHandler,
  73. '../Authentication/AuthenticationController': (this.AuthenticationController = {
  74. getLoggedInUserId: sinon.stub()
  75. }),
  76. '../../infrastructure/FileWriter': this.FileWriter,
  77. './TemplatesPublisher': this.TemplatesPublisher,
  78. 'logger-sharelatex': {
  79. log() {},
  80. err() {}
  81. },
  82. 'settings-sharelatex': {
  83. path: {
  84. dumpFolder: this.dumpFolder
  85. },
  86. siteUrl: (this.siteUrl = 'http://localhost:3000'),
  87. apis: {
  88. v1: {
  89. url: (this.v1Url = 'http://overleaf.com'),
  90. user: 'sharelatex',
  91. pass: 'password'
  92. }
  93. },
  94. overleaf: {
  95. host: this.v1Url
  96. }
  97. },
  98. uuid: {
  99. v4: () => this.uuid
  100. },
  101. request: this.request,
  102. fs: this.fs,
  103. '../../models/Project': { Project: this.Project }
  104. }
  105. })
  106. return (this.zipUrl =
  107. '%2Ftemplates%2F52fb86a81ae1e566597a25f6%2Fv%2F4%2Fzip&templateName=Moderncv%20Banking&compiler=pdflatex')
  108. })
  109. describe('createProjectFromV1Template', function() {
  110. describe('when all options passed', function() {
  111. beforeEach(function() {
  112. return this.TemplatesManager.createProjectFromV1Template(
  113. this.brandVariationId,
  114. this.compiler,
  115. this.mainFile,
  116. this.templateId,
  117. this.templateName,
  118. this.templateVersionId,
  119. this.user_id,
  120. this.imageName,
  121. this.callback
  122. )
  123. })
  124. it('should fetch zip from v1 based on template id', function() {
  125. return this.request.should.have.been.calledWith(
  126. `${this.v1Url}/api/v1/sharelatex/templates/${this.templateVersionId}`
  127. )
  128. })
  129. it('should save temporary file', function() {
  130. return this.fs.createWriteStream.should.have.been.calledWith(
  131. this.dumpPath
  132. )
  133. })
  134. it('should create project', function() {
  135. return this.ProjectUploadManager.createProjectFromZipArchiveWithName.should.have.been.calledWithMatch(
  136. this.user_id,
  137. this.templateName,
  138. this.dumpPath,
  139. {
  140. fromV1TemplateId: this.templateId,
  141. fromV1TemplateVersionId: this.templateVersionId
  142. }
  143. )
  144. })
  145. it('should unlink file', function() {
  146. return this.fs.unlink.should.have.been.calledWith(this.dumpPath)
  147. })
  148. it('should set project options when passed', function() {
  149. this.ProjectOptionsHandler.setCompiler.should.have.been.calledWithMatch(
  150. this.project_id,
  151. this.compiler
  152. )
  153. this.ProjectOptionsHandler.setImageName.should.have.been.calledWithMatch(
  154. this.project_id,
  155. this.imageName
  156. )
  157. this.ProjectRootDocManager.setRootDocFromName.should.have.been.calledWithMatch(
  158. this.project_id,
  159. this.mainFile
  160. )
  161. return this.ProjectOptionsHandler.setBrandVariationId.should.have.been.calledWithMatch(
  162. this.project_id,
  163. this.brandVariationId
  164. )
  165. })
  166. it('should update project', function() {
  167. return this.Project.update.should.have.been.calledWithMatch(
  168. { _id: this.project_id },
  169. {
  170. fromV1TemplateId: this.templateId,
  171. fromV1TemplateVersionId: this.templateVersionId
  172. }
  173. )
  174. })
  175. it('should ensure that the dump folder exists', function() {
  176. return sinon.assert.called(this.FileWriter.ensureDumpFolderExists)
  177. })
  178. })
  179. describe('when some options not set', function() {
  180. beforeEach(function() {
  181. return this.TemplatesManager.createProjectFromV1Template(
  182. null,
  183. null,
  184. null,
  185. this.templateId,
  186. this.templateName,
  187. this.templateVersionId,
  188. this.user_id,
  189. null,
  190. this.callback
  191. )
  192. })
  193. it('should not set missing project options', function() {
  194. this.ProjectOptionsHandler.setCompiler.called.should.equal(false)
  195. this.ProjectRootDocManager.setRootDocFromName.called.should.equal(false)
  196. this.ProjectOptionsHandler.setBrandVariationId.called.should.equal(
  197. false
  198. )
  199. return this.ProjectOptionsHandler.setImageName.should.have.been.calledWithMatch(
  200. this.project_id,
  201. 'wl_texlive:2018.1'
  202. )
  203. })
  204. })
  205. })
  206. })