TemplatesManagerTests.js 6.7 KB

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