FileSystemImportManagerTests.coffee 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. @encoding = "latin1"
  16. @DocumentHelper =
  17. convertTexEncodingsToUtf8: sinon.stub().returnsArg(0)
  18. @FileSystemImportManager = SandboxedModule.require modulePath, requires:
  19. "fs" : @fs = {}
  20. "../Editor/EditorController": @EditorController = {}
  21. "./FileTypeManager": @FileTypeManager = {}
  22. "../Project/ProjectLocator": @ProjectLocator = {}
  23. "../Documents/DocumentHelper": @DocumentHelper
  24. "logger-sharelatex":
  25. log:->
  26. err:->
  27. describe "addDoc", ->
  28. beforeEach ->
  29. @docContent = "one\ntwo\nthree"
  30. @docLines = @docContent.split("\n")
  31. @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
  32. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  33. describe "when path is symlink", ->
  34. beforeEach ->
  35. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
  36. @EditorController.addDoc = sinon.stub()
  37. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, @encoding, false, @callback
  38. it "should not read the file from disk", ->
  39. @fs.readFile.called.should.equal false
  40. it "should not insert the doc", ->
  41. @EditorController.addDoc.called.should.equal false
  42. describe "with replace set to false", ->
  43. beforeEach ->
  44. @EditorController.addDoc = sinon.stub().callsArg(6)
  45. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, @encoding, false, @callback
  46. it "should read the file from disk", ->
  47. @fs.readFile.calledWith(@path_on_disk).should.equal true
  48. it "should insert the doc", ->
  49. @EditorController.addDoc.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
  50. .should.equal true
  51. describe "with windows line ending", ->
  52. beforeEach ->
  53. @docContent = "one\r\ntwo\r\nthree"
  54. @docLines = ["one", "two", "three"]
  55. @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
  56. @EditorController.addDoc = sinon.stub().callsArg(6)
  57. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, @encoding, false, @callback
  58. it "should strip the \\r characters before adding", ->
  59. @EditorController.addDoc.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
  60. .should.equal true
  61. describe "with \r line endings", ->
  62. beforeEach ->
  63. @docContent = "one\rtwo\rthree"
  64. @docLines = ["one", "two", "three"]
  65. @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
  66. @EditorController.addDoc = sinon.stub().callsArg(6)
  67. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, @encoding, false, @callback
  68. it "should treat the \\r characters as newlines", ->
  69. @EditorController.addDoc.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
  70. .should.equal true
  71. describe "with replace set to true", ->
  72. beforeEach ->
  73. @EditorController.upsertDoc = sinon.stub().yields()
  74. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, @encoding, true, @callback
  75. it "should upsert the doc", ->
  76. @EditorController.upsertDoc
  77. .calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
  78. .should.equal true
  79. it "should read the file with the correct encoding", ->
  80. sinon.assert.calledWith(@fs.readFile, @path_on_disk, @encoding)
  81. describe "addFile with replace set to false", ->
  82. beforeEach ->
  83. @EditorController.addFile = sinon.stub().yields()
  84. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  85. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  86. it "should add the file", ->
  87. @EditorController.addFile.calledWith(@project_id, @folder_id, @name, @path_on_disk, null, "upload", @user_id)
  88. .should.equal true
  89. describe "addFile with symlink", ->
  90. beforeEach ->
  91. @EditorController.addFile = sinon.stub()
  92. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
  93. @EditorController.replaceFile = sinon.stub()
  94. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  95. it "should node add the file", ->
  96. @EditorController.addFile.called.should.equal false
  97. @EditorController.replaceFile.called.should.equal false
  98. describe "addFile with replace set to true", ->
  99. beforeEach ->
  100. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  101. @EditorController.upsertFile = sinon.stub().yields()
  102. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
  103. it "should add the file", ->
  104. @EditorController.upsertFile
  105. .calledWith(@project_id, @folder_id, @name, @path_on_disk, null, "upload", @user_id)
  106. .should.equal true
  107. describe "addFolder", ->
  108. beforeEach ->
  109. @new_folder_id = "new-folder-id"
  110. @EditorController.addFolder = sinon.stub().callsArgWith(4, null, _id: @new_folder_id)
  111. @FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5)
  112. describe "successfully", ->
  113. beforeEach ->
  114. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  115. @FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  116. it "should add a folder to the project", ->
  117. @EditorController.addFolder.calledWith(@project_id, @folder_id, @name, "upload")
  118. .should.equal true
  119. it "should add the folders contents", ->
  120. @FileSystemImportManager.addFolderContents.calledWith(@user_id, @project_id, @new_folder_id, @path_on_disk, @replace)
  121. .should.equal true
  122. describe "with symlink", ->
  123. beforeEach ->
  124. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
  125. @FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  126. it "should not add a folder to the project", ->
  127. @EditorController.addFolder.called.should.equal false
  128. @FileSystemImportManager.addFolderContents.called.should.equal false
  129. describe "addFolderContents", ->
  130. beforeEach ->
  131. @folderEntries = ["path1", "path2", "path3"]
  132. @ignoredEntries = [".DS_Store"]
  133. @fs.readdir = sinon.stub().callsArgWith(1, null, @folderEntries.concat @ignoredEntries)
  134. @FileSystemImportManager.addEntity = sinon.stub().callsArg(6)
  135. @FileTypeManager.shouldIgnore = (path, callback) =>
  136. callback null, @ignoredEntries.indexOf(require("path").basename(path)) != -1
  137. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  138. @FileSystemImportManager.addFolderContents @user_id, @project_id, @folder_id, @path_on_disk, @replace, @callback
  139. it "should call addEntity for each file in the folder which is not ignored", ->
  140. for name in @folderEntries
  141. @FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
  142. .should.equal true
  143. it "should not call addEntity for the ignored files", ->
  144. for name in @ignoredEntries
  145. @FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
  146. .should.equal false
  147. it "should look in the correct directory", ->
  148. @fs.readdir.calledWith(@path_on_disk).should.equal true
  149. describe "addEntity", ->
  150. describe "with directory", ->
  151. beforeEach ->
  152. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, true)
  153. @FileSystemImportManager.addFolder = sinon.stub().callsArg(6)
  154. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  155. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  156. it "should call addFolder", ->
  157. @FileSystemImportManager.addFolder.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace)
  158. .should.equal true
  159. describe "with binary file", ->
  160. beforeEach ->
  161. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
  162. @FileTypeManager.getType = sinon.stub().callsArgWith(2, null, true)
  163. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  164. @FileSystemImportManager.addFile = sinon.stub().callsArg(6)
  165. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  166. it "should call addFile", ->
  167. @FileSystemImportManager.addFile.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace)
  168. .should.equal true
  169. describe "with text file", ->
  170. beforeEach ->
  171. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
  172. @FileTypeManager.getType = sinon.stub().callsArgWith(2, null, false, 'latin1')
  173. @FileSystemImportManager.addDoc = sinon.stub().callsArg(7)
  174. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  175. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  176. it "should call addFile", ->
  177. sinon.assert.calledWith(@FileSystemImportManager.addDoc, @user_id, @project_id, @folder_id, @name, @path_on_disk, "latin1", @replace)