FileSystemImportManagerTests.coffee 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. describe "addDoc", ->
  21. beforeEach ->
  22. @docContent = "one\ntwo\nthree"
  23. @docLines = @docContent.split("\n")
  24. @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
  25. describe "with replace set to false", ->
  26. beforeEach ->
  27. @EditorController.addDocWithoutLock = sinon.stub().callsArg(5)
  28. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  29. it "should read the file from disk", ->
  30. @fs.readFile.calledWith(@path_on_disk, "utf8").should.equal true
  31. it "should insert the doc", ->
  32. @EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload")
  33. .should.equal true
  34. describe "with windows line ending", ->
  35. beforeEach ->
  36. @docContent = "one\r\ntwo\r\nthree"
  37. @docLines = ["one", "two", "three"]
  38. @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
  39. @EditorController.addDocWithoutLock = sinon.stub().callsArg(5)
  40. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  41. it "should strip the \\r characters before adding", ->
  42. @EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload")
  43. .should.equal true
  44. describe "with replace set to true", ->
  45. describe "when the doc doesn't exist", ->
  46. beforeEach ->
  47. @folder = {
  48. docs: [{
  49. _id: "doc-id-2"
  50. name: "not-the-right-file.tex"
  51. }]
  52. }
  53. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
  54. @EditorController.addDocWithoutLock = sinon.stub().callsArg(5)
  55. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
  56. it "should look up the folder", ->
  57. @ProjectLocator.findElement
  58. .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
  59. .should.equal true
  60. it "should insert the doc", ->
  61. @EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload")
  62. .should.equal true
  63. describe "when the doc does exist", ->
  64. beforeEach ->
  65. @folder = {
  66. docs: [{
  67. _id: @doc_id = "doc-id-1"
  68. name: @name
  69. }, {
  70. _id: "doc-id-2"
  71. name: "not-the-right-file.tex"
  72. }]
  73. }
  74. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
  75. @EditorController.setDoc = sinon.stub().callsArg(5)
  76. @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
  77. it "should look up the folder", ->
  78. @ProjectLocator.findElement
  79. .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
  80. .should.equal true
  81. it "should set the doc with the new doc lines", ->
  82. @EditorController.setDoc.calledWith(@project_id, @doc_id, @user_id, @docLines, "upload")
  83. .should.equal true
  84. describe "addFile with replace set to false", ->
  85. beforeEach ->
  86. @EditorController.addFileWithoutLock = sinon.stub().callsArg(5)
  87. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
  88. it "should add the file", ->
  89. @EditorController.addFileWithoutLock.calledWith(@project_id, @folder_id, @name, @path_on_disk, "upload")
  90. .should.equal true
  91. describe "addFile with replace set to true", ->
  92. describe "when the file doesn't exist", ->
  93. beforeEach ->
  94. @folder = {
  95. fileRefs: [{
  96. _id: "file-id-2"
  97. name: "not-the-right-file.tex"
  98. }]
  99. }
  100. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
  101. @EditorController.addFileWithoutLock = sinon.stub().callsArg(5)
  102. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
  103. it "should look up the folder", ->
  104. @ProjectLocator.findElement
  105. .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
  106. .should.equal true
  107. it "should add the file", ->
  108. @EditorController.addFileWithoutLock.calledWith(@project_id, @folder_id, @name, @path_on_disk, "upload")
  109. .should.equal true
  110. describe "when the file does exist", ->
  111. beforeEach ->
  112. @folder = {
  113. fileRefs: [{
  114. _id: @file_id = "file-id-1"
  115. name: @name
  116. }, {
  117. _id: "file-id-2"
  118. name: "not-the-right-file.tex"
  119. }]
  120. }
  121. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
  122. @EditorController.replaceFile = sinon.stub().callsArg(4)
  123. @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
  124. it "should look up the folder", ->
  125. @ProjectLocator.findElement
  126. .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
  127. .should.equal true
  128. it "should replace the file", ->
  129. @EditorController.replaceFile.calledWith(@project_id, @file_id, @path_on_disk, "upload")
  130. .should.equal true
  131. describe "addFolder", ->
  132. beforeEach ->
  133. @new_folder_id = "new-folder-id"
  134. @EditorController.addFolderWithoutLock = sinon.stub().callsArgWith(4, null, _id: @new_folder_id)
  135. @FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5)
  136. @FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  137. it "should add a folder to the project", ->
  138. @EditorController.addFolderWithoutLock.calledWith(@project_id, @folder_id, @name, "upload")
  139. .should.equal true
  140. it "should add the folders contents", ->
  141. @FileSystemImportManager.addFolderContents.calledWith(@user_id, @project_id, @new_folder_id, @path_on_disk, @replace)
  142. .should.equal true
  143. describe "addFolderContents", ->
  144. beforeEach ->
  145. @folderEntries = ["path1", "path2", "path3"]
  146. @ignoredEntries = [".DS_Store"]
  147. @fs.readdir = sinon.stub().callsArgWith(1, null, @folderEntries.concat @ignoredEntries)
  148. @FileSystemImportManager.addEntity = sinon.stub().callsArg(6)
  149. @FileTypeManager.shouldIgnore = (path, callback) =>
  150. callback null, @ignoredEntries.indexOf(require("path").basename(path)) != -1
  151. @FileSystemImportManager.addFolderContents @user_id, @project_id, @folder_id, @path_on_disk, @replace, @callback
  152. it "should call addEntity for each file in the folder which is not ignored", ->
  153. for name in @folderEntries
  154. @FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
  155. .should.equal true
  156. it "should not call addEntity for the ignored files", ->
  157. for name in @ignoredEntries
  158. @FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
  159. .should.equal false
  160. it "should look in the correct directory", ->
  161. @fs.readdir.calledWith(@path_on_disk).should.equal true
  162. describe "addEntity", ->
  163. describe "with directory", ->
  164. beforeEach ->
  165. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, true)
  166. @FileSystemImportManager.addFolder = sinon.stub().callsArg(6)
  167. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  168. it "should call addFolder", ->
  169. @FileSystemImportManager.addFolder.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace)
  170. .should.equal true
  171. describe "with binary file", ->
  172. beforeEach ->
  173. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
  174. @FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, true)
  175. @FileSystemImportManager.addFile = sinon.stub().callsArg(6)
  176. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  177. it "should call addFile", ->
  178. @FileSystemImportManager.addFile.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback)
  179. .should.equal true
  180. describe "with text file", ->
  181. beforeEach ->
  182. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
  183. @FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, false)
  184. @FileSystemImportManager.addDoc = sinon.stub().callsArg(6)
  185. @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  186. it "should call addFile", ->
  187. @FileSystemImportManager.addDoc.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback)
  188. .should.equal true