UpdateMergerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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-legacy')
  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(new ObjectId()),
  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. this.mergeUpdateResult = 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. it('returns the entity id and rev', function () {
  125. expect(this.mergeUpdateResult.entityId).to.be.instanceOf(ObjectId)
  126. expect(this.mergeUpdateResult.rev).to.equal(2)
  127. })
  128. })
  129. describe('file updates for a new file ', function () {
  130. beforeEach(async function () {
  131. this.FileTypeManager.promises.getType.resolves({ binary: true })
  132. this.mergeUpdateResult = await this.UpdateMerger.promises.mergeUpdate(
  133. this.userId,
  134. this.projectId,
  135. this.filePath,
  136. this.updateRequest,
  137. this.source
  138. )
  139. })
  140. it('should look at the file contents', function () {
  141. expect(this.FileTypeManager.promises.getType).to.have.been.called
  142. })
  143. it('should process update as file', function () {
  144. expect(
  145. this.EditorController.promises.upsertFileWithPath
  146. ).to.have.been.calledWith(
  147. this.projectId,
  148. this.filePath,
  149. this.fsPath,
  150. null,
  151. this.source,
  152. this.userId
  153. )
  154. })
  155. it('removes the temp file from disk', function () {
  156. expect(this.fsPromises.unlink).to.have.been.calledWith(this.fsPath)
  157. })
  158. it('returns the entity id and rev', function () {
  159. expect(this.mergeUpdateResult.entityId).to.be.instanceOf(ObjectId)
  160. expect(this.mergeUpdateResult.rev).to.equal(6)
  161. })
  162. })
  163. describe('doc updates for an existing doc', function () {
  164. beforeEach(async function () {
  165. this.FileTypeManager.promises.getType.resolves({
  166. binary: false,
  167. encoding: 'utf-8',
  168. })
  169. this.mergeUpdateResult = await this.UpdateMerger.promises.mergeUpdate(
  170. this.userId,
  171. this.projectId,
  172. this.existingDocPath,
  173. this.updateRequest,
  174. this.source
  175. )
  176. })
  177. it('should look at the file contents', function () {
  178. expect(this.FileTypeManager.promises.getType).to.have.been.called
  179. })
  180. it('should process update as doc', function () {
  181. expect(
  182. this.EditorController.promises.upsertDocWithPath
  183. ).to.have.been.calledWith(
  184. this.projectId,
  185. this.existingDocPath,
  186. this.docLines,
  187. this.source,
  188. this.userId
  189. )
  190. })
  191. it('removes the temp file from disk', function () {
  192. expect(this.fsPromises.unlink).to.have.been.calledWith(this.fsPath)
  193. })
  194. it('returns the entity id and rev', function () {
  195. expect(this.mergeUpdateResult.entityId).to.be.instanceOf(ObjectId)
  196. expect(this.mergeUpdateResult.rev).to.equal(2)
  197. })
  198. })
  199. describe('file updates for an existing file', function () {
  200. beforeEach(async function () {
  201. this.FileTypeManager.promises.getType.resolves({ binary: true })
  202. this.mergeUpdateResult = await this.UpdateMerger.promises.mergeUpdate(
  203. this.userId,
  204. this.projectId,
  205. this.existingFilePath,
  206. this.updateRequest,
  207. this.source
  208. )
  209. })
  210. it('should look at the file contents', function () {
  211. expect(this.FileTypeManager.promises.getType).to.have.been.called
  212. })
  213. it('should process update as file', function () {
  214. expect(
  215. this.EditorController.promises.upsertFileWithPath
  216. ).to.have.been.calledWith(
  217. this.projectId,
  218. this.existingFilePath,
  219. this.fsPath,
  220. null,
  221. this.source,
  222. this.userId
  223. )
  224. })
  225. it('removes the temp file from disk', function () {
  226. expect(this.fsPromises.unlink).to.have.been.calledWith(this.fsPath)
  227. })
  228. it('returns the entity id and rev', function () {
  229. expect(this.mergeUpdateResult.entityId).to.be.instanceOf(ObjectId)
  230. expect(this.mergeUpdateResult.rev).to.equal(6)
  231. })
  232. })
  233. })
  234. describe('file updates for an existing doc', function () {
  235. beforeEach(async function () {
  236. this.FileTypeManager.promises.getType.resolves({ binary: true })
  237. this.mergeUpdateResult = await this.UpdateMerger.promises.mergeUpdate(
  238. this.userId,
  239. this.projectId,
  240. this.existingDocPath,
  241. this.updateRequest,
  242. this.source
  243. )
  244. })
  245. it('should look at the file contents', function () {
  246. expect(this.FileTypeManager.promises.getType).to.have.been.called
  247. })
  248. it('should process update as file', function () {
  249. expect(
  250. this.EditorController.promises.upsertFileWithPath
  251. ).to.have.been.calledWith(
  252. this.projectId,
  253. this.existingDocPath,
  254. this.fsPath,
  255. null,
  256. this.source,
  257. this.userId
  258. )
  259. })
  260. it('removes the temp file from disk', function () {
  261. expect(this.fsPromises.unlink).to.have.been.calledWith(this.fsPath)
  262. })
  263. it('returns the entity id and rev', function () {
  264. expect(this.mergeUpdateResult.entityId).to.be.instanceOf(ObjectId)
  265. expect(this.mergeUpdateResult.rev).to.equal(6)
  266. })
  267. })
  268. describe('doc updates for an existing file', function () {
  269. beforeEach(async function () {
  270. this.FileTypeManager.promises.getType.resolves({ binary: true })
  271. this.mergeUpdateResult = await this.UpdateMerger.promises.mergeUpdate(
  272. this.userId,
  273. this.projectId,
  274. this.existingFilePath,
  275. this.updateRequest,
  276. this.source
  277. )
  278. })
  279. it('should look at the file contents', function () {
  280. expect(this.FileTypeManager.promises.getType).to.have.been.called
  281. })
  282. it('should not delete the existing file', function () {
  283. expect(this.EditorController.promises.deleteEntityWithPath).to.not.have
  284. .been.called
  285. })
  286. it('should process update as file', function () {
  287. expect(
  288. this.EditorController.promises.upsertFileWithPath
  289. ).to.have.been.calledWith(
  290. this.projectId,
  291. this.existingFilePath,
  292. this.fsPath,
  293. null,
  294. this.source,
  295. this.userId
  296. )
  297. })
  298. it('removes the temp file from disk', function () {
  299. expect(this.fsPromises.unlink).to.have.been.calledWith(this.fsPath)
  300. })
  301. it('returns the entity id and rev', function () {
  302. expect(this.mergeUpdateResult.entityId).to.be.instanceOf(ObjectId)
  303. expect(this.mergeUpdateResult.rev).to.equal(6)
  304. })
  305. })
  306. describe('deleteUpdate', function () {
  307. beforeEach(async function () {
  308. this.deleteUpdateResult = await this.UpdateMerger.promises.deleteUpdate(
  309. this.userId,
  310. this.projectId,
  311. this.docPath,
  312. this.source
  313. )
  314. })
  315. afterEach(function () {
  316. delete this.deleteUpdateResult
  317. })
  318. it('should delete the entity in the editor controller', function () {
  319. expect(
  320. this.EditorController.promises.deleteEntityWithPath
  321. ).to.have.been.calledWith(
  322. this.projectId,
  323. this.docPath,
  324. this.source,
  325. this.userId
  326. )
  327. })
  328. it('returns the entity id', function () {
  329. expect(this.deleteUpdateResult).to.be.instanceOf(ObjectId)
  330. })
  331. })
  332. })