FileSystemImportManagerTests.coffee 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.addDoc = 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.addDoc.called.should.equal false
  38. describe "with replace set to false", ->
  39. beforeEach ->
  40. @EditorController.addDoc = sinon.stub().callsArg(6)
  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.addDoc.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
  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.addDoc = sinon.stub().callsArg(6)
  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.addDoc.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
  56. .should.equal true
  57. describe "with replace set to true", ->
  58. beforeEach ->
  59. @EditorController.upsertDoc = sinon.stub().yields()
  60. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
  61. it "should upsert the doc", ->
  62. @EditorController.upsertDoc
  63. .calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
  64. .should.equal true
  65. describe "addFile with replace set to false", ->
  66. beforeEach ->
  67. @EditorController.addFile = sinon.stub().yields()
  68. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  69. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  70. it "should add the file", ->
  71. @EditorController.addFile.calledWith(@project_id, @folder_id, @name, @path_on_disk, null, "upload", @user_id)
  72. .should.equal true
  73. describe "addFile with symlink", ->
  74. beforeEach ->
  75. @EditorController.addFile = sinon.stub()
  76. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
  77. @EditorController.replaceFile = sinon.stub()
  78. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  79. it "should node add the file", ->
  80. @EditorController.addFile.called.should.equal false
  81. @EditorController.replaceFile.called.should.equal false
  82. describe "addFile with replace set to true", ->
  83. beforeEach ->
  84. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  85. @EditorController.upsertFile = sinon.stub().yields()
  86. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
  87. it "should add the file", ->
  88. @EditorController.upsertFile
  89. .calledWith(@project_id, @folder_id, @name, @path_on_disk, null, "upload", @user_id)
  90. .should.equal true
  91. describe "addFolder", ->
  92. beforeEach ->
  93. @new_folder_id = "new-folder-id"
  94. @EditorController.addFolder = sinon.stub().callsArgWith(4, null, _id: @new_folder_id)
  95. @FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5)
  96. describe "successfully", ->
  97. beforeEach ->
  98. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  99. @FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  100. it "should add a folder to the project", ->
  101. @EditorController.addFolder.calledWith(@project_id, @folder_id, @name, "upload")
  102. .should.equal true
  103. it "should add the folders contents", ->
  104. @FileSystemImportManager.addFolderContents.calledWith(@user_id, @project_id, @new_folder_id, @path_on_disk, @replace)
  105. .should.equal true
  106. describe "with symlink", ->
  107. beforeEach ->
  108. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
  109. @FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  110. it "should not add a folder to the project", ->
  111. @EditorController.addFolder.called.should.equal false
  112. @FileSystemImportManager.addFolderContents.called.should.equal false
  113. describe "addFolderContents", ->
  114. beforeEach ->
  115. @folderEntries = ["path1", "path2", "path3"]
  116. @ignoredEntries = [".DS_Store"]
  117. @fs.readdir = sinon.stub().callsArgWith(1, null, @folderEntries.concat @ignoredEntries)
  118. @FileSystemImportManager.addEntity = sinon.stub().callsArg(6)
  119. @FileTypeManager.shouldIgnore = (path, callback) =>
  120. callback null, @ignoredEntries.indexOf(require("path").basename(path)) != -1
  121. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  122. @FileSystemImportManager.addFolderContents @user_id, @project_id, @folder_id, @path_on_disk, @replace, @callback
  123. it "should call addEntity for each file in the folder which is not ignored", ->
  124. for name in @folderEntries
  125. @FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
  126. .should.equal true
  127. it "should not call addEntity for the ignored files", ->
  128. for name in @ignoredEntries
  129. @FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
  130. .should.equal false
  131. it "should look in the correct directory", ->
  132. @fs.readdir.calledWith(@path_on_disk).should.equal true
  133. describe "addEntity", ->
  134. describe "with directory", ->
  135. beforeEach ->
  136. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, true)
  137. @FileSystemImportManager.addFolder = sinon.stub().callsArg(6)
  138. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  139. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  140. it "should call addFolder", ->
  141. @FileSystemImportManager.addFolder.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace)
  142. .should.equal true
  143. describe "with binary file", ->
  144. beforeEach ->
  145. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
  146. @FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, true)
  147. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  148. @FileSystemImportManager.addFile = sinon.stub().callsArg(6)
  149. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  150. it "should call addFile", ->
  151. @FileSystemImportManager.addFile.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace)
  152. .should.equal true
  153. describe "with text file", ->
  154. beforeEach ->
  155. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
  156. @FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, false)
  157. @FileSystemImportManager.addDoc = sinon.stub().callsArg(6)
  158. @FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
  159. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  160. it "should call addFile", ->
  161. @FileSystemImportManager.addDoc.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace)
  162. .should.equal true