FileStoreHandler.test.mjs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import { beforeEach, describe, it, vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. const MODULE_PATH =
  4. '../../../../app/src/Features/FileStore/FileStoreHandler.mjs'
  5. describe('FileStoreHandler', function () {
  6. beforeEach(async function (ctx) {
  7. ctx.fileSize = 999
  8. ctx.fs = {
  9. createReadStream: sinon.stub(),
  10. lstat: sinon.stub().callsArgWith(1, null, {
  11. isFile() {
  12. return true
  13. },
  14. isDirectory() {
  15. return false
  16. },
  17. size: ctx.fileSize,
  18. }),
  19. }
  20. ctx.writeStream = {
  21. my: 'writeStream',
  22. on(type, fn) {
  23. if (type === 'response') {
  24. fn({ statusCode: 200 })
  25. }
  26. },
  27. }
  28. ctx.readStream = { my: 'readStream', on: sinon.stub() }
  29. ctx.request = sinon.stub()
  30. ctx.request.head = sinon.stub()
  31. ctx.filestoreUrl = 'http://filestore.overleaf.test'
  32. ctx.settings = {
  33. apis: { filestore: { url: ctx.filestoreUrl } },
  34. }
  35. ctx.hashValue = '0123456789'
  36. ctx.fileArgs = { name: 'upload-filename' }
  37. ctx.fileId = 'file_id_here'
  38. ctx.projectId = '1312312312'
  39. ctx.historyId = 123
  40. ctx.hashValue = '2aae6c35c94fcfb415dbe95f408b9ce91ee846ed'
  41. ctx.fsPath = 'uploads/myfile.eps'
  42. ctx.getFileUrl = (projectId, fileId) =>
  43. `${ctx.filestoreUrl}/project/${projectId}/file/${fileId}`
  44. ctx.getProjectUrl = projectId => `${ctx.filestoreUrl}/project/${projectId}`
  45. ctx.FileModel = class File {
  46. constructor(options) {
  47. ;({ name: this.name, hash: this.hash } = options)
  48. this._id = 'file_id_here'
  49. this.rev = 0
  50. if (options.linkedFileData != null) {
  51. this.linkedFileData = options.linkedFileData
  52. }
  53. }
  54. }
  55. ctx.FileHashManager = {
  56. computeHash: sinon.stub().callsArgWith(1, null, ctx.hashValue),
  57. }
  58. ctx.HistoryManager = {
  59. uploadBlobFromDisk: sinon.stub().callsArg(4),
  60. }
  61. ctx.ProjectDetailsHandler = {
  62. getDetails: sinon.stub().callsArgWith(1, null, {
  63. overleaf: { history: { id: ctx.historyId } },
  64. }),
  65. }
  66. ctx.Features = {
  67. hasFeature: sinon.stub(),
  68. }
  69. ctx.Modules = {
  70. hooks: {
  71. fire: sinon.stub().callsArgWith(2, null),
  72. },
  73. }
  74. vi.doMock('@overleaf/settings', () => ({
  75. default: ctx.settings,
  76. }))
  77. vi.doMock('request', () => ({
  78. default: ctx.request,
  79. }))
  80. vi.doMock('../../../../app/src/Features/History/HistoryManager', () => ({
  81. default: ctx.HistoryManager,
  82. }))
  83. vi.doMock(
  84. '../../../../app/src/Features/Project/ProjectDetailsHandler',
  85. () => ({
  86. default: ctx.ProjectDetailsHandler,
  87. })
  88. )
  89. vi.doMock('../../../../app/src/Features/FileStore/FileHashManager', () => ({
  90. default: ctx.FileHashManager,
  91. }))
  92. vi.doMock('../../../../app/src/infrastructure/Features', () => ({
  93. default: ctx.Features,
  94. }))
  95. vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
  96. default: ctx.Modules,
  97. }))
  98. vi.doMock('../../../../app/src/models/File', () => ({
  99. File: ctx.FileModel,
  100. }))
  101. vi.doMock('node:fs', () => ({ default: ctx.fs }))
  102. ctx.handler = (await import(MODULE_PATH)).default
  103. })
  104. describe('uploadFileFromDisk', function () {
  105. beforeEach(function (ctx) {
  106. ctx.request.returns(ctx.writeStream)
  107. })
  108. it('should get the project details', async function (ctx) {
  109. ctx.fs.createReadStream.returns({
  110. pipe() {},
  111. on(type, cb) {
  112. if (type === 'open') {
  113. cb()
  114. }
  115. },
  116. })
  117. await ctx.handler.promises.uploadFileFromDisk(
  118. ctx.projectId,
  119. ctx.fileArgs,
  120. ctx.fsPath
  121. )
  122. ctx.ProjectDetailsHandler.getDetails
  123. .calledWith(ctx.projectId)
  124. .should.equal(true)
  125. })
  126. it('should compute the file hash', async function (ctx) {
  127. ctx.fs.createReadStream.returns({
  128. pipe() {},
  129. on(type, cb) {
  130. if (type === 'open') {
  131. cb()
  132. }
  133. },
  134. })
  135. await ctx.handler.promises.uploadFileFromDisk(
  136. ctx.projectId,
  137. ctx.fileArgs,
  138. ctx.fsPath
  139. )
  140. ctx.FileHashManager.computeHash.calledWith(ctx.fsPath).should.equal(true)
  141. })
  142. it('should call the preUploadFile hook', async function (ctx) {
  143. ctx.fs.createReadStream.returns({
  144. pipe() {},
  145. on(type, cb) {
  146. if (type === 'open') {
  147. cb()
  148. }
  149. },
  150. })
  151. await ctx.handler.promises.uploadFileFromDisk(
  152. ctx.projectId,
  153. ctx.fileArgs,
  154. ctx.fsPath
  155. )
  156. ctx.Modules.hooks.fire
  157. .calledWith('preUploadFile', {
  158. projectId: ctx.projectId,
  159. historyId: ctx.historyId,
  160. fileArgs: ctx.fileArgs,
  161. fsPath: ctx.fsPath,
  162. size: ctx.fileSize,
  163. })
  164. .should.equal(true)
  165. })
  166. it('should upload the file to the history store as a blob', async function (ctx) {
  167. ctx.fs.createReadStream.returns({
  168. pipe() {},
  169. on(type, cb) {
  170. if (type === 'open') {
  171. cb()
  172. }
  173. },
  174. })
  175. await ctx.handler.promises.uploadFileFromDisk(
  176. ctx.projectId,
  177. ctx.fileArgs,
  178. ctx.fsPath
  179. )
  180. ctx.HistoryManager.uploadBlobFromDisk
  181. .calledWith(ctx.historyId, ctx.hashValue, ctx.fileSize, ctx.fsPath)
  182. .should.equal(true)
  183. })
  184. it('should not open file handle', async function (ctx) {
  185. await ctx.handler.promises.uploadFileFromDisk(
  186. ctx.projectId,
  187. ctx.fileArgs,
  188. ctx.fsPath
  189. )
  190. expect(ctx.fs.createReadStream).to.not.have.been.called
  191. })
  192. it('should not talk to filestore', async function (ctx) {
  193. await ctx.handler.promises.uploadFileFromDisk(
  194. ctx.projectId,
  195. ctx.fileArgs,
  196. ctx.fsPath
  197. )
  198. expect(ctx.request).to.not.have.been.called
  199. })
  200. it('should call the postUploadFile hook', async function (ctx) {
  201. ctx.fs.createReadStream.returns({
  202. pipe() {},
  203. on(type, cb) {
  204. if (type === 'open') {
  205. cb()
  206. }
  207. },
  208. })
  209. await ctx.handler.promises.uploadFileFromDisk(
  210. ctx.projectId,
  211. ctx.fileArgs,
  212. ctx.fsPath
  213. )
  214. ctx.Modules.hooks.fire
  215. .calledWith('postUploadFile', {
  216. projectId: ctx.projectId,
  217. fileRef: sinon.match.instanceOf(ctx.FileModel),
  218. size: ctx.fileSize,
  219. })
  220. .should.equal(true)
  221. })
  222. it('should resolve with the url and fileRef', async function (ctx) {
  223. const { fileRef } = await ctx.handler.promises.uploadFileFromDisk(
  224. ctx.projectId,
  225. ctx.fileArgs,
  226. ctx.fsPath
  227. )
  228. expect(fileRef._id).to.equal(ctx.fileId)
  229. expect(fileRef.hash).to.equal(ctx.hashValue)
  230. })
  231. describe('symlink', function () {
  232. it('should not read file if it is symlink', async function (ctx) {
  233. ctx.fs.lstat = sinon.stub().callsArgWith(1, null, {
  234. isFile() {
  235. return false
  236. },
  237. isDirectory() {
  238. return false
  239. },
  240. })
  241. let error
  242. try {
  243. await ctx.handler.promises.uploadFileFromDisk(
  244. ctx.projectId,
  245. ctx.fileArgs,
  246. ctx.fsPath
  247. )
  248. } catch (err) {
  249. error = err
  250. }
  251. expect(error).to.exist
  252. ctx.fs.createReadStream.called.should.equal(false)
  253. })
  254. it('should not read file stat returns nothing', async function (ctx) {
  255. ctx.fs.lstat = sinon.stub().callsArgWith(1, null, null)
  256. let error
  257. try {
  258. await ctx.handler.promises.uploadFileFromDisk(
  259. ctx.projectId,
  260. ctx.fileArgs,
  261. ctx.fsPath
  262. )
  263. } catch (err) {
  264. error = err
  265. }
  266. expect(error).to.exist
  267. ctx.fs.createReadStream.called.should.equal(false)
  268. })
  269. })
  270. })
  271. })