ResourceWriterTests.coffee 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. should = require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/ResourceWriter'
  5. path = require "path"
  6. describe "ResourceWriter", ->
  7. beforeEach ->
  8. @ResourceWriter = SandboxedModule.require modulePath, requires:
  9. "fs": @fs =
  10. mkdir: sinon.stub().callsArg(1)
  11. unlink: sinon.stub().callsArg(1)
  12. "./ResourceStateManager": @ResourceStateManager = {}
  13. "wrench": @wrench = {}
  14. "./UrlCache" : @UrlCache = {}
  15. "mkdirp" : @mkdirp = sinon.stub().callsArg(1)
  16. "./OutputFileFinder": @OutputFileFinder = {}
  17. "logger-sharelatex": {log: sinon.stub(), err: sinon.stub()}
  18. "./Metrics": @Metrics =
  19. Timer: class Timer
  20. done: sinon.stub()
  21. @project_id = "project-id-123"
  22. @basePath = "/path/to/write/files/to"
  23. @callback = sinon.stub()
  24. describe "syncResourcesToDisk on a full request", ->
  25. beforeEach ->
  26. @resources = [
  27. "resource-1-mock"
  28. "resource-2-mock"
  29. "resource-3-mock"
  30. ]
  31. @ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
  32. @ResourceWriter._removeExtraneousFiles = sinon.stub().callsArg(2)
  33. @ResourceStateManager.saveProjectState = sinon.stub().callsArg(3)
  34. @ResourceWriter.syncResourcesToDisk({
  35. project_id: @project_id
  36. syncState: @syncState = "0123456789abcdef"
  37. resources: @resources
  38. }, @basePath, @callback)
  39. it "should remove old files", ->
  40. @ResourceWriter._removeExtraneousFiles
  41. .calledWith(@resources, @basePath)
  42. .should.equal true
  43. it "should write each resource to disk", ->
  44. for resource in @resources
  45. @ResourceWriter._writeResourceToDisk
  46. .calledWith(@project_id, resource, @basePath)
  47. .should.equal true
  48. it "should store the sync state and resource list", ->
  49. @ResourceStateManager.saveProjectState
  50. .calledWith(@syncState, @resources, @basePath)
  51. .should.equal true
  52. it "should call the callback", ->
  53. @callback.called.should.equal true
  54. describe "syncResourcesToDisk on an incremental update", ->
  55. beforeEach ->
  56. @resources = [
  57. "resource-1-mock"
  58. ]
  59. @ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
  60. @ResourceWriter._removeExtraneousFiles = sinon.stub().callsArgWith(2, null, @outputFiles = [], @allFiles = [])
  61. @ResourceStateManager.checkProjectStateMatches = sinon.stub().callsArgWith(2, null, @resources)
  62. @ResourceStateManager.saveProjectState = sinon.stub().callsArg(3)
  63. @ResourceStateManager.checkResourceFiles = sinon.stub().callsArg(3)
  64. @ResourceWriter.syncResourcesToDisk({
  65. project_id: @project_id,
  66. syncType: "incremental",
  67. syncState: @syncState = "1234567890abcdef",
  68. resources: @resources
  69. }, @basePath, @callback)
  70. it "should check the sync state matches", ->
  71. @ResourceStateManager.checkProjectStateMatches
  72. .calledWith(@syncState, @basePath)
  73. .should.equal true
  74. it "should remove old files", ->
  75. @ResourceWriter._removeExtraneousFiles
  76. .calledWith(@resources, @basePath)
  77. .should.equal true
  78. it "should check each resource exists", ->
  79. @ResourceStateManager.checkResourceFiles
  80. .calledWith(@resources, @allFiles, @basePath)
  81. .should.equal true
  82. it "should write each resource to disk", ->
  83. for resource in @resources
  84. @ResourceWriter._writeResourceToDisk
  85. .calledWith(@project_id, resource, @basePath)
  86. .should.equal true
  87. it "should call the callback", ->
  88. @callback.called.should.equal true
  89. describe "syncResourcesToDisk on an incremental update when the state does not match", ->
  90. beforeEach ->
  91. @resources = [
  92. "resource-1-mock"
  93. ]
  94. @ResourceStateManager.checkProjectStateMatches = sinon.stub().callsArgWith(2, @error = new Error())
  95. @ResourceWriter.syncResourcesToDisk({
  96. project_id: @project_id,
  97. syncType: "incremental",
  98. syncState: @syncState = "1234567890abcdef",
  99. resources: @resources
  100. }, @basePath, @callback)
  101. it "should check whether the sync state matches", ->
  102. @ResourceStateManager.checkProjectStateMatches
  103. .calledWith(@syncState, @basePath)
  104. .should.equal true
  105. it "should call the callback with an error", ->
  106. @callback.calledWith(@error).should.equal true
  107. describe "_removeExtraneousFiles", ->
  108. beforeEach ->
  109. @output_files = [{
  110. path: "output.pdf"
  111. type: "pdf"
  112. }, {
  113. path: "extra/file.tex"
  114. type: "tex"
  115. }, {
  116. path: "extra.aux"
  117. type: "aux"
  118. }, {
  119. path: "cache/_chunk1"
  120. }]
  121. @resources = "mock-resources"
  122. @OutputFileFinder.findOutputFiles = sinon.stub().callsArgWith(2, null, @output_files)
  123. @ResourceWriter._deleteFileIfNotDirectory = sinon.stub().callsArg(1)
  124. @ResourceWriter._removeExtraneousFiles(@resources, @basePath, @callback)
  125. it "should find the existing output files", ->
  126. @OutputFileFinder.findOutputFiles
  127. .calledWith(@resources, @basePath)
  128. .should.equal true
  129. it "should delete the output files", ->
  130. @ResourceWriter._deleteFileIfNotDirectory
  131. .calledWith(path.join(@basePath, "output.pdf"))
  132. .should.equal true
  133. it "should delete the extra files", ->
  134. @ResourceWriter._deleteFileIfNotDirectory
  135. .calledWith(path.join(@basePath, "extra/file.tex"))
  136. .should.equal true
  137. it "should not delete the extra aux files", ->
  138. @ResourceWriter._deleteFileIfNotDirectory
  139. .calledWith(path.join(@basePath, "extra.aux"))
  140. .should.equal false
  141. it "should not delete the knitr cache file", ->
  142. @ResourceWriter._deleteFileIfNotDirectory
  143. .calledWith(path.join(@basePath, "cache/_chunk1"))
  144. .should.equal false
  145. it "should call the callback", ->
  146. @callback.called.should.equal true
  147. it "should time the request", ->
  148. @Metrics.Timer::done.called.should.equal true
  149. describe "_writeResourceToDisk", ->
  150. describe "with a url based resource", ->
  151. beforeEach ->
  152. @resource =
  153. path: "main.tex"
  154. url: "http://www.example.com/main.tex"
  155. modified: Date.now()
  156. @UrlCache.downloadUrlToFile = sinon.stub().callsArgWith(4, "fake error downloading file")
  157. @ResourceWriter._writeResourceToDisk(@project_id, @resource, @basePath, @callback)
  158. it "should ensure the directory exists", ->
  159. @mkdirp
  160. .calledWith(path.dirname(path.join(@basePath, @resource.path)))
  161. .should.equal true
  162. it "should write the URL from the cache", ->
  163. @UrlCache.downloadUrlToFile
  164. .calledWith(@project_id, @resource.url, path.join(@basePath, @resource.path), @resource.modified)
  165. .should.equal true
  166. it "should call the callback", ->
  167. @callback.called.should.equal true
  168. it "should not return an error if the resource writer errored", ->
  169. should.not.exist @callback.args[0][0]
  170. describe "with a content based resource", ->
  171. beforeEach ->
  172. @resource =
  173. path: "main.tex"
  174. content: "Hello world"
  175. @fs.writeFile = sinon.stub().callsArg(2)
  176. @ResourceWriter._writeResourceToDisk(@project_id, @resource, @basePath, @callback)
  177. it "should ensure the directory exists", ->
  178. @mkdirp
  179. .calledWith(path.dirname(path.join(@basePath, @resource.path)))
  180. .should.equal true
  181. it "should write the contents to disk", ->
  182. @fs.writeFile
  183. .calledWith(path.join(@basePath, @resource.path), @resource.content)
  184. .should.equal true
  185. it "should call the callback", ->
  186. @callback.called.should.equal true
  187. describe "with a file path that breaks out of the root folder", ->
  188. beforeEach ->
  189. @resource =
  190. path: "../../main.tex"
  191. content: "Hello world"
  192. @fs.writeFile = sinon.stub().callsArg(2)
  193. @ResourceWriter._writeResourceToDisk(@project_id, @resource, @basePath, @callback)
  194. it "should not write to disk", ->
  195. @fs.writeFile.called.should.equal false
  196. it "should return an error", ->
  197. @callback
  198. .calledWith(new Error("resource path is outside root directory"))
  199. .should.equal true
  200. describe "checkPath", ->
  201. describe "with a valid path", ->
  202. beforeEach ->
  203. @ResourceWriter.checkPath("foo", "bar", @callback)
  204. it "should return the joined path", ->
  205. @callback.calledWith(null, "foo/bar")
  206. .should.equal true
  207. describe "with an invalid path", ->
  208. beforeEach ->
  209. @ResourceWriter.checkPath("foo", "baz/../../bar", @callback)
  210. it "should return an error", ->
  211. @callback.calledWith(new Error("resource path is outside root directory"))
  212. .should.equal true
  213. describe "with another invalid path matching on a prefix", ->
  214. beforeEach ->
  215. @ResourceWriter.checkPath("foo", "../foobar/baz", @callback)
  216. it "should return an error", ->
  217. @callback.calledWith(new Error("resource path is outside root directory"))
  218. .should.equal true