FileSystemImportManagerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/Features/Uploads/FileSystemImportManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "FileSystemImportManager", ->
  7. beforeEach ->
  8. @project_id = "project-id-123"
  9. @folder_id = "folder-id-123"
  10. @name = "test-file.tex"
  11. @path_on_disk = "/path/to/file/#{@name}"
  12. @replace = "replace-boolean-flag-mock"
  13. @user_id = "mock-user-123"
  14. @callback = sinon.stub()
  15. @FileSystemImportManager = SandboxedModule.require modulePath, requires:
  16. "fs" : @fs = {}
  17. "../Editor/EditorController": @EditorController = {}
  18. "./FileTypeManager": @FileTypeManager = {}
  19. "../Project/ProjectLocator": @ProjectLocator = {}
  20. "logger-sharelatex":
  21. log:->
  22. err:->
  23. describe "addDoc", ->
  24. beforeEach ->
  25. @docContent = "one\ntwo\nthree"
  26. @docLines = @docContent.split("\n")
  27. @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
  28. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  29. describe "when path is symlink", ->
  30. beforeEach ->
  31. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
  32. @EditorController.addDocWithoutLock = sinon.stub()
  33. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  34. it "should not read the file from disk", ->
  35. @fs.readFile.called.should.equal false
  36. it "should not insert the doc", ->
  37. @EditorController.addDocWithoutLock.called.should.equal false
  38. describe "with replace set to false", ->
  39. beforeEach ->
  40. @EditorController.addDocWithoutLock = sinon.stub().callsArg(5)
  41. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  42. it "should read the file from disk", ->
  43. @fs.readFile.calledWith(@path_on_disk, "utf8").should.equal true
  44. it "should insert the doc", ->
  45. @EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload")
  46. .should.equal true
  47. describe "with windows line ending", ->
  48. beforeEach ->
  49. @docContent = "one\r\ntwo\r\nthree"
  50. @docLines = ["one", "two", "three"]
  51. @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
  52. @EditorController.addDocWithoutLock = sinon.stub().callsArg(5)
  53. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  54. it "should strip the \\r characters before adding", ->
  55. @EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload")
  56. .should.equal true
  57. describe "with replace set to true", ->
  58. describe "when the doc doesn't exist", ->
  59. beforeEach ->
  60. @folder = {
  61. docs: [{
  62. _id: "doc-id-2"
  63. name: "not-the-right-file.tex"
  64. }]
  65. }
  66. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
  67. @EditorController.addDocWithoutLock = sinon.stub().callsArg(5)
  68. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
  69. it "should look up the folder", ->
  70. @ProjectLocator.findElement
  71. .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
  72. .should.equal true
  73. it "should insert the doc", ->
  74. @EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload")
  75. .should.equal true
  76. describe "when the doc does exist", ->
  77. beforeEach ->
  78. @folder = {
  79. docs: [{
  80. _id: @doc_id = "doc-id-1"
  81. name: @name
  82. }, {
  83. _id: "doc-id-2"
  84. name: "not-the-right-file.tex"
  85. }]
  86. }
  87. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
  88. @EditorController.setDoc = sinon.stub().callsArg(5)
  89. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
  90. it "should look up the folder", ->
  91. @ProjectLocator.findElement
  92. .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
  93. .should.equal true
  94. it "should set the doc with the new doc lines", ->
  95. @EditorController.setDoc.calledWith(@project_id, @doc_id, @user_id, @docLines, "upload")
  96. .should.equal true
  97. describe "addFile with replace set to false", ->
  98. beforeEach ->
  99. @EditorController.addFileWithoutLock = sinon.stub().callsArg(5)
  100. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  101. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  102. it "should add the file", ->
  103. @EditorController.addFileWithoutLock.calledWith(@project_id, @folder_id, @name, @path_on_disk, "upload")
  104. .should.equal true
  105. describe "addFile with symlink", ->
  106. beforeEach ->
  107. @EditorController.addFileWithoutLock = sinon.stub()
  108. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
  109. @EditorController.replaceFile = sinon.stub()
  110. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  111. it "should node add the file", ->
  112. @EditorController.addFileWithoutLock.called.should.equal false
  113. @EditorController.replaceFile.called.should.equal false
  114. describe "addFile with replace set to true", ->
  115. describe "when the file doesn't exist", ->
  116. beforeEach ->
  117. @folder = {
  118. fileRefs: [{
  119. _id: "file-id-2"
  120. name: "not-the-right-file.tex"
  121. }]
  122. }
  123. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  124. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
  125. @EditorController.addFileWithoutLock = sinon.stub().callsArg(5)
  126. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
  127. it "should look up the folder", ->
  128. @ProjectLocator.findElement
  129. .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
  130. .should.equal true
  131. it "should add the file", ->
  132. @EditorController.addFileWithoutLock.calledWith(@project_id, @folder_id, @name, @path_on_disk, "upload")
  133. .should.equal true
  134. describe "when the file does exist", ->
  135. beforeEach ->
  136. @folder = {
  137. fileRefs: [{
  138. _id: @file_id = "file-id-1"
  139. name: @name
  140. }, {
  141. _id: "file-id-2"
  142. name: "not-the-right-file.tex"
  143. }]
  144. }
  145. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  146. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
  147. @EditorController.replaceFile = sinon.stub().callsArg(4)
  148. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
  149. it "should look up the folder", ->
  150. @ProjectLocator.findElement
  151. .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
  152. .should.equal true
  153. it "should replace the file", ->
  154. @EditorController.replaceFile.calledWith(@project_id, @file_id, @path_on_disk, "upload")
  155. .should.equal true
  156. describe "addFolder", ->
  157. beforeEach ->
  158. @new_folder_id = "new-folder-id"
  159. @EditorController.addFolderWithoutLock = sinon.stub().callsArgWith(4, null, _id: @new_folder_id)
  160. @FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5)
  161. describe "successfully", ->
  162. beforeEach ->
  163. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  164. @FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  165. it "should add a folder to the project", ->
  166. @EditorController.addFolderWithoutLock.calledWith(@project_id, @folder_id, @name, "upload")
  167. .should.equal true
  168. it "should add the folders contents", ->
  169. @FileSystemImportManager.addFolderContents.calledWith(@user_id, @project_id, @new_folder_id, @path_on_disk, @replace)
  170. .should.equal true
  171. describe "with symlink", ->
  172. beforeEach ->
  173. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
  174. @FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  175. it "should not add a folder to the project", ->
  176. @EditorController.addFolderWithoutLock.called.should.equal false
  177. @FileSystemImportManager.addFolderContents.called.should.equal false
  178. describe "addFolderContents", ->
  179. beforeEach ->
  180. @folderEntries = ["path1", "path2", "path3"]
  181. @ignoredEntries = [".DS_Store"]
  182. @fs.readdir = sinon.stub().callsArgWith(1, null, @folderEntries.concat @ignoredEntries)
  183. @FileSystemImportManager.addEntity = sinon.stub().callsArg(6)
  184. @FileTypeManager.shouldIgnore = (path, callback) =>
  185. callback null, @ignoredEntries.indexOf(require("path").basename(path)) != -1
  186. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  187. @FileSystemImportManager.addFolderContents @user_id, @project_id, @folder_id, @path_on_disk, @replace, @callback
  188. it "should call addEntity for each file in the folder which is not ignored", ->
  189. for name in @folderEntries
  190. @FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
  191. .should.equal true
  192. it "should not call addEntity for the ignored files", ->
  193. for name in @ignoredEntries
  194. @FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
  195. .should.equal false
  196. it "should look in the correct directory", ->
  197. @fs.readdir.calledWith(@path_on_disk).should.equal true
  198. describe "addEntity", ->
  199. describe "with directory", ->
  200. beforeEach ->
  201. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, true)
  202. @FileSystemImportManager.addFolder = sinon.stub().callsArg(6)
  203. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  204. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  205. it "should call addFolder", ->
  206. @FileSystemImportManager.addFolder.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace)
  207. .should.equal true
  208. describe "with binary file", ->
  209. beforeEach ->
  210. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
  211. @FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, true)
  212. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  213. @FileSystemImportManager.addFile = sinon.stub().callsArg(6)
  214. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  215. it "should call addFile", ->
  216. @FileSystemImportManager.addFile.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback)
  217. .should.equal true
  218. describe "with text file", ->
  219. beforeEach ->
  220. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
  221. @FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, false)
  222. @FileSystemImportManager.addDoc = sinon.stub().callsArg(6)
  223. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  224. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  225. it "should call addFile", ->
  226. @FileSystemImportManager.addDoc.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback)
  227. .should.equal true