UpdateMergerTests.js 11 KB

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