FileSystemImportManagerTests.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const mockFs = require('mock-fs')
  4. const SandboxedModule = require('sandboxed-module')
  5. const { ObjectId } = require('mongodb')
  6. const MODULE_PATH =
  7. '../../../../app/src/Features/Uploads/FileSystemImportManager.js'
  8. describe('FileSystemImportManager', function () {
  9. beforeEach(function () {
  10. this.projectId = new ObjectId()
  11. this.folderId = new ObjectId()
  12. this.newFolderId = new ObjectId()
  13. this.userId = new ObjectId()
  14. this.EditorController = {
  15. promises: {
  16. addDoc: sinon.stub().resolves(),
  17. addFile: sinon.stub().resolves(),
  18. upsertDoc: sinon.stub().resolves(),
  19. upsertFile: sinon.stub().resolves(),
  20. addFolder: sinon.stub().resolves({ _id: this.newFolderId }),
  21. },
  22. }
  23. this.FileSystemImportManager = SandboxedModule.require(MODULE_PATH, {
  24. requires: {
  25. '@overleaf/settings': {
  26. textExtensions: ['tex', 'txt'],
  27. },
  28. '../Editor/EditorController': this.EditorController,
  29. },
  30. })
  31. })
  32. describe('importDir', function () {
  33. beforeEach(async function () {
  34. mockFs({
  35. 'import-test': {
  36. 'main.tex': 'My thesis',
  37. 'link-to-main.tex': mockFs.symlink({ path: 'import-test/main.tex' }),
  38. '.DS_Store': 'Should be ignored',
  39. images: {
  40. 'cat.jpg': Buffer.from([1, 2, 3, 4]),
  41. },
  42. 'line-endings': {
  43. 'unix.txt': 'one\ntwo\nthree',
  44. 'mac.txt': 'uno\rdos\rtres',
  45. 'windows.txt': 'ein\r\nzwei\r\ndrei',
  46. 'mixed.txt': 'uno\rdue\r\ntre\nquattro',
  47. },
  48. encodings: {
  49. 'utf16le.txt': Buffer.from('\ufeffétonnant!', 'utf16le'),
  50. 'latin1.txt': Buffer.from('tétanisant!', 'latin1'),
  51. },
  52. },
  53. symlink: mockFs.symlink({ path: 'import-test' }),
  54. })
  55. this.entries = await this.FileSystemImportManager.promises.importDir(
  56. 'import-test'
  57. )
  58. this.projectPaths = this.entries.map(x => x.projectPath)
  59. })
  60. afterEach(function () {
  61. mockFs.restore()
  62. })
  63. it('should import regular docs', function () {
  64. expect(this.entries).to.deep.include({
  65. type: 'doc',
  66. projectPath: '/main.tex',
  67. lines: ['My thesis'],
  68. })
  69. })
  70. it('should skip symlinks inside the import folder', function () {
  71. expect(this.projectPaths).not.to.include('/link-to-main.tex')
  72. })
  73. it('should skip ignored files', function () {
  74. expect(this.projectPaths).not.to.include('/.DS_Store')
  75. })
  76. it('should import binary files', function () {
  77. expect(this.entries).to.deep.include({
  78. type: 'file',
  79. projectPath: '/images/cat.jpg',
  80. fsPath: 'import-test/images/cat.jpg',
  81. })
  82. })
  83. it('should deal with Mac/Windows/Unix line endings', function () {
  84. expect(this.entries).to.deep.include({
  85. type: 'doc',
  86. projectPath: '/line-endings/unix.txt',
  87. lines: ['one', 'two', 'three'],
  88. })
  89. expect(this.entries).to.deep.include({
  90. type: 'doc',
  91. projectPath: '/line-endings/mac.txt',
  92. lines: ['uno', 'dos', 'tres'],
  93. })
  94. expect(this.entries).to.deep.include({
  95. type: 'doc',
  96. projectPath: '/line-endings/windows.txt',
  97. lines: ['ein', 'zwei', 'drei'],
  98. })
  99. expect(this.entries).to.deep.include({
  100. type: 'doc',
  101. projectPath: '/line-endings/mixed.txt',
  102. lines: ['uno', 'due', 'tre', 'quattro'],
  103. })
  104. })
  105. it('should import documents with latin1 encoding', function () {
  106. expect(this.entries).to.deep.include({
  107. type: 'doc',
  108. projectPath: '/encodings/latin1.txt',
  109. lines: ['tétanisant!'],
  110. })
  111. })
  112. it('should import documents with utf16-le encoding', function () {
  113. expect(this.entries).to.deep.include({
  114. type: 'doc',
  115. projectPath: '/encodings/utf16le.txt',
  116. lines: ['\ufeffétonnant!'],
  117. })
  118. })
  119. it('should error when the root folder is a symlink', async function () {
  120. await expect(this.FileSystemImportManager.promises.importDir('symlink'))
  121. .to.be.rejected
  122. })
  123. })
  124. describe('addEntity', function () {
  125. describe('with directory', function () {
  126. beforeEach(async function () {
  127. mockFs({
  128. path: {
  129. to: {
  130. folder: {
  131. 'doc.tex': 'one\ntwo\nthree',
  132. 'image.jpg': Buffer.from([1, 2, 3, 4]),
  133. },
  134. },
  135. },
  136. })
  137. await this.FileSystemImportManager.promises.addEntity(
  138. this.userId,
  139. this.projectId,
  140. this.folderId,
  141. 'folder',
  142. 'path/to/folder',
  143. false
  144. )
  145. })
  146. afterEach(function () {
  147. mockFs.restore()
  148. })
  149. it('should add a folder to the project', function () {
  150. this.EditorController.promises.addFolder.should.have.been.calledWith(
  151. this.projectId,
  152. this.folderId,
  153. 'folder',
  154. 'upload'
  155. )
  156. })
  157. it("should add the folder's contents", function () {
  158. this.EditorController.promises.addDoc.should.have.been.calledWith(
  159. this.projectId,
  160. this.newFolderId,
  161. 'doc.tex',
  162. ['one', 'two', 'three'],
  163. 'upload',
  164. this.userId
  165. )
  166. this.EditorController.promises.addFile.should.have.been.calledWith(
  167. this.projectId,
  168. this.newFolderId,
  169. 'image.jpg',
  170. 'path/to/folder/image.jpg',
  171. null,
  172. 'upload',
  173. this.userId
  174. )
  175. })
  176. })
  177. describe('with binary file', function () {
  178. beforeEach(function () {
  179. mockFs({ 'uploaded-file': Buffer.from([1, 2, 3, 4]) })
  180. })
  181. afterEach(function () {
  182. mockFs.restore()
  183. })
  184. describe('with replace set to false', function () {
  185. beforeEach(async function () {
  186. await this.FileSystemImportManager.promises.addEntity(
  187. this.userId,
  188. this.projectId,
  189. this.folderId,
  190. 'image.jpg',
  191. 'uploaded-file',
  192. false
  193. )
  194. })
  195. it('should add the file', function () {
  196. this.EditorController.promises.addFile.should.have.been.calledWith(
  197. this.projectId,
  198. this.folderId,
  199. 'image.jpg',
  200. 'uploaded-file',
  201. null,
  202. 'upload',
  203. this.userId
  204. )
  205. })
  206. })
  207. describe('with replace set to true', function () {
  208. beforeEach(async function () {
  209. await this.FileSystemImportManager.promises.addEntity(
  210. this.userId,
  211. this.projectId,
  212. this.folderId,
  213. 'image.jpg',
  214. 'uploaded-file',
  215. true
  216. )
  217. })
  218. it('should add the file', function () {
  219. this.EditorController.promises.upsertFile.should.have.been.calledWith(
  220. this.projectId,
  221. this.folderId,
  222. 'image.jpg',
  223. 'uploaded-file',
  224. null,
  225. 'upload',
  226. this.userId
  227. )
  228. })
  229. })
  230. })
  231. for (const [lineEndingDescription, lineEnding] of [
  232. ['Unix', '\n'],
  233. ['Mac', '\r'],
  234. ['Windows', '\r\n'],
  235. ]) {
  236. describe(`with text file (${lineEndingDescription} line endings)`, function () {
  237. beforeEach(function () {
  238. mockFs({
  239. path: {
  240. to: { 'uploaded-file': `one${lineEnding}two${lineEnding}three` },
  241. },
  242. })
  243. })
  244. afterEach(function () {
  245. mockFs.restore()
  246. })
  247. describe('with replace set to false', function () {
  248. beforeEach(async function () {
  249. await this.FileSystemImportManager.promises.addEntity(
  250. this.userId,
  251. this.projectId,
  252. this.folderId,
  253. 'doc.tex',
  254. 'path/to/uploaded-file',
  255. false
  256. )
  257. })
  258. it('should insert the doc', function () {
  259. this.EditorController.promises.addDoc.should.have.been.calledWith(
  260. this.projectId,
  261. this.folderId,
  262. 'doc.tex',
  263. ['one', 'two', 'three'],
  264. 'upload',
  265. this.userId
  266. )
  267. })
  268. })
  269. describe('with replace set to true', function () {
  270. beforeEach(async function () {
  271. await this.FileSystemImportManager.promises.addEntity(
  272. this.userId,
  273. this.projectId,
  274. this.folderId,
  275. 'doc.tex',
  276. 'path/to/uploaded-file',
  277. true
  278. )
  279. })
  280. it('should upsert the doc', function () {
  281. this.EditorController.promises.upsertDoc.should.have.been.calledWith(
  282. this.projectId,
  283. this.folderId,
  284. 'doc.tex',
  285. ['one', 'two', 'three'],
  286. 'upload',
  287. this.userId
  288. )
  289. })
  290. })
  291. })
  292. }
  293. describe('with symlink', function () {
  294. beforeEach(function () {
  295. mockFs({
  296. path: { to: { symlink: mockFs.symlink({ path: '/etc/passwd' }) } },
  297. })
  298. })
  299. afterEach(function () {
  300. mockFs.restore()
  301. })
  302. it('should stop with an error', async function () {
  303. await expect(
  304. this.FileSystemImportManager.promises.addEntity(
  305. this.userId,
  306. this.projectId,
  307. this.folderId,
  308. 'main.tex',
  309. 'path/to/symlink',
  310. false
  311. )
  312. ).to.be.rejectedWith('path is symlink')
  313. this.EditorController.promises.addFolder.should.not.have.been.called
  314. this.EditorController.promises.addDoc.should.not.have.been.called
  315. this.EditorController.promises.addFile.should.not.have.been.called
  316. })
  317. })
  318. })
  319. })