UpdateMergerTests.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. const { Writable } = require('stream')
  5. const { ObjectId } = require('mongodb')
  6. const MODULE_PATH =
  7. '../../../../app/src/Features/ThirdPartyDataStore/UpdateMerger.js'
  8. describe('UpdateMerger :', function () {
  9. beforeEach(function () {
  10. this.projectId = 'project_id_here'
  11. this.userId = 'mock-user-id'
  12. this.randomUUID = 'random-uuid'
  13. this.dumpPath = '/dump'
  14. this.docPath = this.newDocPath = '/folder/doc.tex'
  15. this.filePath = this.newFilePath = '/folder/file.png'
  16. this.existingDocPath = '/folder/other.tex'
  17. this.existingFilePath = '/folder/fig1.pdf'
  18. this.linkedFileData = { provider: 'url' }
  19. this.existingDocs = [{ path: '/main.tex' }, { path: '/folder/other.tex' }]
  20. this.existingFiles = [{ path: '/figure.pdf' }, { path: '/folder/fig1.pdf' }]
  21. this.fsPath = `${this.dumpPath}/${this.projectId}_${this.randomUUID}`
  22. this.fileContents = `\\documentclass{article}
  23. \\usepackage[utf8]{inputenc}
  24. \\title{42}
  25. \\author{Jane Doe}
  26. \\date{June 2011}`
  27. this.docLines = this.fileContents.split('\n')
  28. this.source = 'dropbox'
  29. this.updateRequest = new Writable()
  30. this.writeStream = new Writable()
  31. this.fsPromises = {
  32. unlink: sinon.stub().resolves(),
  33. readFile: sinon.stub().withArgs(this.fsPath).resolves(this.fileContents),
  34. mkdir: sinon.stub().resolves(),
  35. }
  36. this.fs = {
  37. createWriteStream: sinon.stub().returns(this.writeStream),
  38. }
  39. this.doc = {
  40. _id: new ObjectId(),
  41. rev: 2,
  42. }
  43. this.file = {
  44. _id: new ObjectId(),
  45. rev: 6,
  46. }
  47. this.folder = {
  48. _id: new ObjectId(),
  49. }
  50. this.EditorController = {
  51. promises: {
  52. deleteEntityWithPath: sinon.stub().resolves(),
  53. upsertDocWithPath: sinon
  54. .stub()
  55. .resolves({ doc: this.doc, folder: this.folder }),
  56. upsertFileWithPath: sinon
  57. .stub()
  58. .resolves({ file: this.file, folder: this.folder }),
  59. },
  60. }
  61. this.FileTypeManager = {
  62. promises: {
  63. getType: sinon.stub(),
  64. },
  65. }
  66. this.crypto = {
  67. randomUUID: sinon.stub().returns(this.randomUUID),
  68. }
  69. this.ProjectEntityHandler = {
  70. promises: {
  71. getAllEntities: sinon.stub().resolves({
  72. docs: this.existingDocs,
  73. files: this.existingFiles,
  74. }),
  75. },
  76. }
  77. this.Settings = { path: { dumpFolder: this.dumpPath } }
  78. this.stream = { pipeline: sinon.stub().resolves() }
  79. this.UpdateMerger = SandboxedModule.require(MODULE_PATH, {
  80. requires: {
  81. 'fs/promises': this.fsPromises,
  82. fs: this.fs,
  83. '../Editor/EditorController': this.EditorController,
  84. '../Uploads/FileTypeManager': this.FileTypeManager,
  85. '../Project/ProjectEntityHandler': this.ProjectEntityHandler,
  86. '@overleaf/settings': this.Settings,
  87. 'stream/promises': this.stream,
  88. crypto: this.crypto,
  89. },
  90. })
  91. })
  92. describe('mergeUpdate', function () {
  93. describe('doc updates for a new doc', function () {
  94. beforeEach(async function () {
  95. this.FileTypeManager.promises.getType.resolves({
  96. binary: false,
  97. encoding: 'utf-8',
  98. })
  99. await this.UpdateMerger.promises.mergeUpdate(
  100. this.userId,
  101. this.projectId,
  102. this.docPath,
  103. this.updateRequest,
  104. this.source
  105. )
  106. })
  107. it('should look at the file contents', function () {
  108. expect(this.FileTypeManager.promises.getType).to.have.been.called
  109. })
  110. it('should process update as doc', function () {
  111. expect(
  112. this.EditorController.promises.upsertDocWithPath
  113. ).to.have.been.calledWith(
  114. this.projectId,
  115. this.docPath,
  116. this.docLines,
  117. this.source,
  118. this.userId
  119. )
  120. })
  121. it('removes the temp file from disk', function () {
  122. expect(this.fsPromises.unlink).to.have.been.calledWith(this.fsPath)
  123. })
  124. })
  125. describe('file updates for a new file ', function () {
  126. beforeEach(async function () {
  127. this.FileTypeManager.promises.getType.resolves({ binary: true })
  128. await this.UpdateMerger.promises.mergeUpdate(
  129. this.userId,
  130. this.projectId,
  131. this.filePath,
  132. this.updateRequest,
  133. this.source
  134. )
  135. })
  136. it('should look at the file contents', function () {
  137. expect(this.FileTypeManager.promises.getType).to.have.been.called
  138. })
  139. it('should process update as file', function () {
  140. expect(
  141. this.EditorController.promises.upsertFileWithPath
  142. ).to.have.been.calledWith(
  143. this.projectId,
  144. this.filePath,
  145. this.fsPath,
  146. null,
  147. this.source,
  148. this.userId
  149. )
  150. })
  151. it('removes the temp file from disk', function () {
  152. expect(this.fsPromises.unlink).to.have.been.calledWith(this.fsPath)
  153. })
  154. })
  155. describe('doc updates for an existing doc', function () {
  156. beforeEach(async function () {
  157. this.FileTypeManager.promises.getType.resolves({
  158. binary: false,
  159. encoding: 'utf-8',
  160. })
  161. await this.UpdateMerger.promises.mergeUpdate(
  162. this.userId,
  163. this.projectId,
  164. this.existingDocPath,
  165. this.updateRequest,
  166. this.source
  167. )
  168. })
  169. it('should look at the file contents', function () {
  170. expect(this.FileTypeManager.promises.getType).to.have.been.called
  171. })
  172. it('should process update as doc', function () {
  173. expect(
  174. this.EditorController.promises.upsertDocWithPath
  175. ).to.have.been.calledWith(
  176. this.projectId,
  177. this.existingDocPath,
  178. this.docLines,
  179. this.source,
  180. this.userId
  181. )
  182. })
  183. it('removes the temp file from disk', function () {
  184. expect(this.fsPromises.unlink).to.have.been.calledWith(this.fsPath)
  185. })
  186. })
  187. describe('file updates for an existing file', function () {
  188. beforeEach(async function () {
  189. this.FileTypeManager.promises.getType.resolves({ binary: true })
  190. await this.UpdateMerger.promises.mergeUpdate(
  191. this.userId,
  192. this.projectId,
  193. this.existingFilePath,
  194. this.updateRequest,
  195. this.source
  196. )
  197. })
  198. it('should look at the file contents', function () {
  199. expect(this.FileTypeManager.promises.getType).to.have.been.called
  200. })
  201. it('should process update as file', function () {
  202. expect(
  203. this.EditorController.promises.upsertFileWithPath
  204. ).to.have.been.calledWith(
  205. this.projectId,
  206. this.existingFilePath,
  207. this.fsPath,
  208. null,
  209. this.source,
  210. this.userId
  211. )
  212. })
  213. it('removes the temp file from disk', function () {
  214. expect(this.fsPromises.unlink).to.have.been.calledWith(this.fsPath)
  215. })
  216. })
  217. })
  218. describe('file updates for an existing doc', function () {
  219. beforeEach(async function () {
  220. this.FileTypeManager.promises.getType.resolves({ binary: true })
  221. await this.UpdateMerger.promises.mergeUpdate(
  222. this.userId,
  223. this.projectId,
  224. this.existingDocPath,
  225. this.updateRequest,
  226. this.source
  227. )
  228. })
  229. it('should look at the file contents', function () {
  230. expect(this.FileTypeManager.promises.getType).to.have.been.called
  231. })
  232. it('should process update as file', function () {
  233. expect(
  234. this.EditorController.promises.upsertFileWithPath
  235. ).to.have.been.calledWith(
  236. this.projectId,
  237. this.existingDocPath,
  238. this.fsPath,
  239. null,
  240. this.source,
  241. this.userId
  242. )
  243. })
  244. it('removes the temp file from disk', function () {
  245. expect(this.fsPromises.unlink).to.have.been.calledWith(this.fsPath)
  246. })
  247. })
  248. describe('doc updates for an existing file', function () {
  249. beforeEach(async function () {
  250. this.FileTypeManager.promises.getType.resolves({ binary: true })
  251. await this.UpdateMerger.promises.mergeUpdate(
  252. this.userId,
  253. this.projectId,
  254. this.existingFilePath,
  255. this.updateRequest,
  256. this.source
  257. )
  258. })
  259. it('should look at the file contents', function () {
  260. expect(this.FileTypeManager.promises.getType).to.have.been.called
  261. })
  262. it('should not delete the existing file', function () {
  263. expect(this.EditorController.promises.deleteEntityWithPath).to.not.have
  264. .been.called
  265. })
  266. it('should process update as file', function () {
  267. expect(
  268. this.EditorController.promises.upsertFileWithPath
  269. ).to.have.been.calledWith(
  270. this.projectId,
  271. this.existingFilePath,
  272. this.fsPath,
  273. null,
  274. this.source,
  275. this.userId
  276. )
  277. })
  278. it('removes the temp file from disk', function () {
  279. expect(this.fsPromises.unlink).to.have.been.calledWith(this.fsPath)
  280. })
  281. })
  282. describe('deleteUpdate', function () {
  283. beforeEach(async function () {
  284. await this.UpdateMerger.promises.deleteUpdate(
  285. this.userId,
  286. this.projectId,
  287. this.docPath,
  288. this.source
  289. )
  290. })
  291. it('should delete the entity in the editor controller', function () {
  292. expect(
  293. this.EditorController.promises.deleteEntityWithPath
  294. ).to.have.been.calledWith(
  295. this.projectId,
  296. this.docPath,
  297. this.source,
  298. this.userId
  299. )
  300. })
  301. })
  302. })