FileSystemImportManagerTests.js 9.9 KB

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