TemplatesManager.test.mjs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { beforeEach, describe, it, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import { RequestFailedError } from '@overleaf/fetch-utils'
  4. import { ReadableString } from '@overleaf/stream-utils'
  5. const modulePath = '../../../../app/src/Features/Templates/TemplatesManager'
  6. describe('TemplatesManager', function () {
  7. beforeEach(async function (ctx) {
  8. ctx.project_id = 'project-id'
  9. ctx.brandVariationId = 'brand-variation-id'
  10. ctx.compiler = 'pdflatex'
  11. ctx.imageName = 'TL2017'
  12. ctx.mainFile = 'main.tex'
  13. ctx.templateId = 'template-id'
  14. ctx.templateName = 'template name'
  15. ctx.templateVersionId = 'template-version-id'
  16. ctx.user_id = 'user-id'
  17. ctx.dumpFolder = 'dump/path'
  18. ctx.uuid = '1234'
  19. ctx.dumpPath = `${ctx.dumpFolder}/${ctx.uuid}_templates-manager`
  20. ctx.callback = sinon.stub()
  21. ctx.pipeline = sinon.stub().callsFake(async (stream, res) => {
  22. if (res.callback) res.callback()
  23. })
  24. ctx.request = sinon.stub().returns({
  25. pipe() {},
  26. on() {},
  27. response: {
  28. statusCode: 200,
  29. },
  30. })
  31. ctx.fs = {
  32. promises: { unlink: sinon.stub() },
  33. unlink: sinon.stub(),
  34. createWriteStream: sinon.stub().returns({ on: sinon.stub().yields() }),
  35. }
  36. ctx.ProjectUploadManager = {
  37. promises: {
  38. createProjectFromZipArchiveWithName: sinon.stub().resolves({
  39. project: { _id: ctx.project_id },
  40. fileEntries: [],
  41. docEntries: [],
  42. }),
  43. },
  44. }
  45. ctx.ProjectOptionsHandler = {
  46. promises: {
  47. setCompiler: sinon.stub().resolves(),
  48. setImageName: sinon.stub().resolves(),
  49. setBrandVariationId: sinon.stub().resolves(),
  50. normalizeCompiler: sinon.stub().returnsArg(0),
  51. normalizeImageName: sinon.stub().returnsArg(0),
  52. },
  53. }
  54. ctx.ProjectRootDocManager = {
  55. promises: {
  56. setRootDocFromName: sinon.stub().resolves(),
  57. },
  58. }
  59. ctx.ProjectDetailsHandler = {
  60. getProjectDescription: sinon.stub(),
  61. fixProjectName: sinon.stub().returns(ctx.templateName),
  62. }
  63. ctx.Project = { updateOne: sinon.stub().resolves() }
  64. ctx.mockStream = new ReadableString('{}')
  65. ctx.mockResponse = {
  66. status: 200,
  67. headers: new Headers({
  68. 'Content-Length': '2',
  69. 'Content-Type': 'application/json',
  70. }),
  71. }
  72. ctx.FetchUtils = {
  73. fetchJson: sinon.stub(),
  74. fetchStreamWithResponse: sinon.stub().resolves({
  75. stream: ctx.mockStream,
  76. response: ctx.mockResponse,
  77. }),
  78. RequestFailedError,
  79. }
  80. vi.doMock('@overleaf/fetch-utils', () => ctx.FetchUtils)
  81. vi.doMock(
  82. '../../../../app/src/Features/Uploads/ProjectUploadManager',
  83. () => ({ default: ctx.ProjectUploadManager })
  84. )
  85. vi.doMock(
  86. '../../../../app/src/Features/Project/ProjectOptionsHandler',
  87. () => ({ default: ctx.ProjectOptionsHandler })
  88. )
  89. vi.doMock(
  90. '../../../../app/src/Features/Project/ProjectRootDocManager',
  91. () => ({ default: ctx.ProjectRootDocManager })
  92. )
  93. vi.doMock(
  94. '../../../../app/src/Features/Project/ProjectDetailsHandler',
  95. () => ({ default: ctx.ProjectDetailsHandler })
  96. )
  97. ctx.SessionManager = {
  98. getLoggedInUserId: sinon.stub(),
  99. }
  100. vi.doMock(
  101. '../../../../app/src/Features/Authentication/SessionManager',
  102. () => ({ default: ctx.SessionManager })
  103. )
  104. vi.doMock('@overleaf/settings', () => ({
  105. default: {
  106. path: {
  107. dumpFolder: ctx.dumpFolder,
  108. },
  109. siteUrl: (ctx.siteUrl = 'http://127.0.0.1:3000'),
  110. apis: {
  111. v1: {
  112. url: (ctx.v1Url = 'http://overleaf.com'),
  113. user: 'overleaf',
  114. pass: 'password',
  115. timeout: 10,
  116. },
  117. },
  118. overleaf: {
  119. host: ctx.v1Url,
  120. },
  121. },
  122. }))
  123. vi.doMock('node:crypto', () => ({
  124. default: {
  125. randomUUID: () => ctx.uuid,
  126. },
  127. }))
  128. vi.doMock('node:fs', () => ({ default: ctx.fs }))
  129. vi.doMock('request', () => ({ default: ctx.request }))
  130. vi.doMock('../../../../app/src/models/Project', () => ({
  131. Project: ctx.Project,
  132. }))
  133. vi.doMock('node:stream/promises', () => ({ pipeline: ctx.pipeline }))
  134. vi.doMock('../../../../app/src/Features/Compile/ClsiCacheManager', () => ({
  135. default: {
  136. prepareClsiCache: sinon.stub().rejects(new Error('ignore this')),
  137. },
  138. }))
  139. ctx.TemplatesManager = (await import(modulePath)).default.promises
  140. ctx.zipUrl =
  141. '%2Ftemplates%2F52fb86a81ae1e566597a25f6%2Fv%2F4%2Fzip&templateName=Moderncv%20Banking&compiler=pdflatex'
  142. })
  143. describe('createProjectFromV1Template', function () {
  144. describe('when all options passed', function () {
  145. beforeEach(async function (ctx) {
  146. await ctx.TemplatesManager.createProjectFromV1Template(
  147. ctx.brandVariationId,
  148. ctx.compiler,
  149. ctx.mainFile,
  150. ctx.templateId,
  151. ctx.templateName,
  152. ctx.templateVersionId,
  153. ctx.user_id,
  154. ctx.imageName
  155. )
  156. })
  157. it('should fetch zip from v1 based on template id', function (ctx) {
  158. ctx.FetchUtils.fetchStreamWithResponse.should.have.been.calledWith(
  159. `${ctx.v1Url}/api/v1/overleaf/templates/${ctx.templateVersionId}`
  160. )
  161. })
  162. it('should save temporary file', function (ctx) {
  163. ctx.fs.createWriteStream.should.have.been.calledWith(ctx.dumpPath)
  164. })
  165. it('should create project', function (ctx) {
  166. ctx.ProjectUploadManager.promises.createProjectFromZipArchiveWithName.should.have.been.calledWithMatch(
  167. ctx.user_id,
  168. ctx.templateName,
  169. ctx.dumpPath,
  170. {
  171. fromV1TemplateId: ctx.templateId,
  172. fromV1TemplateVersionId: ctx.templateVersionId,
  173. compiler: ctx.compiler,
  174. imageName: ctx.imageName,
  175. brandVariationId: ctx.brandVariationId,
  176. }
  177. )
  178. })
  179. it('should unlink file', function (ctx) {
  180. ctx.fs.promises.unlink.should.have.been.calledWith(ctx.dumpPath)
  181. })
  182. it('should set project rootDoc when passed', function (ctx) {
  183. ctx.ProjectRootDocManager.promises.setRootDocFromName.should.have.been.calledWithMatch(
  184. ctx.project_id,
  185. ctx.mainFile
  186. )
  187. })
  188. })
  189. describe('when some options not set', function () {
  190. beforeEach(async function (ctx) {
  191. await ctx.TemplatesManager.createProjectFromV1Template(
  192. null,
  193. null,
  194. null,
  195. ctx.templateId,
  196. ctx.templateName,
  197. ctx.templateVersionId,
  198. ctx.user_id,
  199. null
  200. )
  201. })
  202. it('should create project', function (ctx) {
  203. ctx.ProjectUploadManager.promises.createProjectFromZipArchiveWithName.should.have.been.calledWithMatch(
  204. ctx.user_id,
  205. ctx.templateName,
  206. ctx.dumpPath,
  207. {
  208. fromV1TemplateId: ctx.templateId,
  209. fromV1TemplateVersionId: ctx.templateVersionId,
  210. compiler: 'pdflatex',
  211. imageName: 'wl_texlive:2018.1',
  212. }
  213. )
  214. })
  215. it('should not set missing project options', function (ctx) {
  216. ctx.ProjectRootDocManager.promises.setRootDocFromName.called.should.equal(
  217. false
  218. )
  219. })
  220. })
  221. })
  222. })