| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- import { beforeEach, describe, it, vi, expect } from 'vitest'
- import sinon from 'sinon'
- const MODULE_PATH =
- '../../../../app/src/Features/FileStore/FileStoreHandler.mjs'
- describe('FileStoreHandler', function () {
- beforeEach(async function (ctx) {
- ctx.fileSize = 999
- ctx.fs = {
- createReadStream: sinon.stub(),
- lstat: sinon.stub().callsArgWith(1, null, {
- isFile() {
- return true
- },
- isDirectory() {
- return false
- },
- size: ctx.fileSize,
- }),
- }
- ctx.writeStream = {
- my: 'writeStream',
- on(type, fn) {
- if (type === 'response') {
- fn({ statusCode: 200 })
- }
- },
- }
- ctx.readStream = { my: 'readStream', on: sinon.stub() }
- ctx.request = sinon.stub()
- ctx.request.head = sinon.stub()
- ctx.filestoreUrl = 'http://filestore.overleaf.test'
- ctx.settings = {
- apis: { filestore: { url: ctx.filestoreUrl } },
- }
- ctx.hashValue = '0123456789'
- ctx.fileArgs = { name: 'upload-filename' }
- ctx.fileId = 'file_id_here'
- ctx.projectId = '1312312312'
- ctx.historyId = 123
- ctx.hashValue = '2aae6c35c94fcfb415dbe95f408b9ce91ee846ed'
- ctx.fsPath = 'uploads/myfile.eps'
- ctx.getFileUrl = (projectId, fileId) =>
- `${ctx.filestoreUrl}/project/${projectId}/file/${fileId}`
- ctx.getProjectUrl = projectId => `${ctx.filestoreUrl}/project/${projectId}`
- ctx.FileModel = class File {
- constructor(options) {
- ;({ name: this.name, hash: this.hash } = options)
- this._id = 'file_id_here'
- this.rev = 0
- if (options.linkedFileData != null) {
- this.linkedFileData = options.linkedFileData
- }
- }
- }
- ctx.FileHashManager = {
- computeHash: sinon.stub().callsArgWith(1, null, ctx.hashValue),
- }
- ctx.HistoryManager = {
- uploadBlobFromDisk: sinon.stub().callsArg(4),
- }
- ctx.ProjectDetailsHandler = {
- getDetails: sinon.stub().callsArgWith(1, null, {
- overleaf: { history: { id: ctx.historyId } },
- }),
- }
- ctx.Features = {
- hasFeature: sinon.stub(),
- }
- ctx.Modules = {
- hooks: {
- fire: sinon.stub().callsArgWith(2, null),
- },
- }
- vi.doMock('@overleaf/settings', () => ({
- default: ctx.settings,
- }))
- vi.doMock('request', () => ({
- default: ctx.request,
- }))
- vi.doMock('../../../../app/src/Features/History/HistoryManager', () => ({
- default: ctx.HistoryManager,
- }))
- vi.doMock(
- '../../../../app/src/Features/Project/ProjectDetailsHandler',
- () => ({
- default: ctx.ProjectDetailsHandler,
- })
- )
- vi.doMock('../../../../app/src/Features/FileStore/FileHashManager', () => ({
- default: ctx.FileHashManager,
- }))
- vi.doMock('../../../../app/src/infrastructure/Features', () => ({
- default: ctx.Features,
- }))
- vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
- default: ctx.Modules,
- }))
- vi.doMock('../../../../app/src/models/File', () => ({
- File: ctx.FileModel,
- }))
- vi.doMock('node:fs', () => ({ default: ctx.fs }))
- ctx.handler = (await import(MODULE_PATH)).default
- })
- describe('uploadFileFromDisk', function () {
- beforeEach(function (ctx) {
- ctx.request.returns(ctx.writeStream)
- })
- it('should get the project details', async function (ctx) {
- ctx.fs.createReadStream.returns({
- pipe() {},
- on(type, cb) {
- if (type === 'open') {
- cb()
- }
- },
- })
- await ctx.handler.promises.uploadFileFromDisk(
- ctx.projectId,
- ctx.fileArgs,
- ctx.fsPath
- )
- ctx.ProjectDetailsHandler.getDetails
- .calledWith(ctx.projectId)
- .should.equal(true)
- })
- it('should compute the file hash', async function (ctx) {
- ctx.fs.createReadStream.returns({
- pipe() {},
- on(type, cb) {
- if (type === 'open') {
- cb()
- }
- },
- })
- await ctx.handler.promises.uploadFileFromDisk(
- ctx.projectId,
- ctx.fileArgs,
- ctx.fsPath
- )
- ctx.FileHashManager.computeHash.calledWith(ctx.fsPath).should.equal(true)
- })
- it('should call the preUploadFile hook', async function (ctx) {
- ctx.fs.createReadStream.returns({
- pipe() {},
- on(type, cb) {
- if (type === 'open') {
- cb()
- }
- },
- })
- await ctx.handler.promises.uploadFileFromDisk(
- ctx.projectId,
- ctx.fileArgs,
- ctx.fsPath
- )
- ctx.Modules.hooks.fire
- .calledWith('preUploadFile', {
- projectId: ctx.projectId,
- historyId: ctx.historyId,
- fileArgs: ctx.fileArgs,
- fsPath: ctx.fsPath,
- size: ctx.fileSize,
- })
- .should.equal(true)
- })
- it('should upload the file to the history store as a blob', async function (ctx) {
- ctx.fs.createReadStream.returns({
- pipe() {},
- on(type, cb) {
- if (type === 'open') {
- cb()
- }
- },
- })
- await ctx.handler.promises.uploadFileFromDisk(
- ctx.projectId,
- ctx.fileArgs,
- ctx.fsPath
- )
- ctx.HistoryManager.uploadBlobFromDisk
- .calledWith(ctx.historyId, ctx.hashValue, ctx.fileSize, ctx.fsPath)
- .should.equal(true)
- })
- it('should not open file handle', async function (ctx) {
- await ctx.handler.promises.uploadFileFromDisk(
- ctx.projectId,
- ctx.fileArgs,
- ctx.fsPath
- )
- expect(ctx.fs.createReadStream).to.not.have.been.called
- })
- it('should not talk to filestore', async function (ctx) {
- await ctx.handler.promises.uploadFileFromDisk(
- ctx.projectId,
- ctx.fileArgs,
- ctx.fsPath
- )
- expect(ctx.request).to.not.have.been.called
- })
- it('should call the postUploadFile hook', async function (ctx) {
- ctx.fs.createReadStream.returns({
- pipe() {},
- on(type, cb) {
- if (type === 'open') {
- cb()
- }
- },
- })
- await ctx.handler.promises.uploadFileFromDisk(
- ctx.projectId,
- ctx.fileArgs,
- ctx.fsPath
- )
- ctx.Modules.hooks.fire
- .calledWith('postUploadFile', {
- projectId: ctx.projectId,
- fileRef: sinon.match.instanceOf(ctx.FileModel),
- size: ctx.fileSize,
- })
- .should.equal(true)
- })
- it('should resolve with the url and fileRef', async function (ctx) {
- const { fileRef } = await ctx.handler.promises.uploadFileFromDisk(
- ctx.projectId,
- ctx.fileArgs,
- ctx.fsPath
- )
- expect(fileRef._id).to.equal(ctx.fileId)
- expect(fileRef.hash).to.equal(ctx.hashValue)
- })
- describe('symlink', function () {
- it('should not read file if it is symlink', async function (ctx) {
- ctx.fs.lstat = sinon.stub().callsArgWith(1, null, {
- isFile() {
- return false
- },
- isDirectory() {
- return false
- },
- })
- let error
- try {
- await ctx.handler.promises.uploadFileFromDisk(
- ctx.projectId,
- ctx.fileArgs,
- ctx.fsPath
- )
- } catch (err) {
- error = err
- }
- expect(error).to.exist
- ctx.fs.createReadStream.called.should.equal(false)
- })
- it('should not read file stat returns nothing', async function (ctx) {
- ctx.fs.lstat = sinon.stub().callsArgWith(1, null, null)
- let error
- try {
- await ctx.handler.promises.uploadFileFromDisk(
- ctx.projectId,
- ctx.fileArgs,
- ctx.fsPath
- )
- } catch (err) {
- error = err
- }
- expect(error).to.exist
- ctx.fs.createReadStream.called.should.equal(false)
- })
- })
- })
- })
|