ClsiFormatCheckerTests.coffee 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Compile/ClsiFormatChecker.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "ClsiFormatChecker", ->
  8. beforeEach ->
  9. @ClsiFormatChecker = SandboxedModule.require modulePath, requires:
  10. "settings-sharelatex": @settings ={}
  11. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
  12. @project_id = "project-id"
  13. describe "checkRecoursesForProblems", ->
  14. beforeEach ->
  15. @resources = [{
  16. path: "main.tex"
  17. content: "stuff"
  18. }, {
  19. path: "chapters/chapter1"
  20. content: "other stuff"
  21. }, {
  22. path: "stuff/image/image.png"
  23. url: "http:somewhere.com/project/#{@project_id}/file/1234124321312"
  24. modified: "more stuff"
  25. }]
  26. it "should call _checkForDuplicatePaths and _checkForConflictingPaths", (done)->
  27. @ClsiFormatChecker._checkForConflictingPaths = sinon.stub().callsArgWith(1, null)
  28. @ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon.stub().callsArgWith(1)
  29. @ClsiFormatChecker.checkRecoursesForProblems @resources, (err, problems)=>
  30. @ClsiFormatChecker._checkForConflictingPaths.called.should.equal true
  31. @ClsiFormatChecker._checkDocsAreUnderSizeLimit.called.should.equal true
  32. done()
  33. it "should remove undefined errors", (done)->
  34. @ClsiFormatChecker._checkForConflictingPaths = sinon.stub().callsArgWith(1, null, [])
  35. @ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon.stub().callsArgWith(1, null, {})
  36. @ClsiFormatChecker.checkRecoursesForProblems @resources, (err, problems)=>
  37. expect(problems).to.not.exist
  38. expect(problems).to.not.exist
  39. done()
  40. it "should keep populated arrays", (done)->
  41. @ClsiFormatChecker._checkForConflictingPaths = sinon.stub().callsArgWith(1, null, [{path:"somewhere/main.tex"}])
  42. @ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon.stub().callsArgWith(1, null, {})
  43. @ClsiFormatChecker.checkRecoursesForProblems @resources, (err, problems)=>
  44. problems.conflictedPaths[0].path.should.equal "somewhere/main.tex"
  45. expect(problems.sizeCheck).to.not.exist
  46. done()
  47. it "should keep populated object", (done)->
  48. @ClsiFormatChecker._checkForConflictingPaths = sinon.stub().callsArgWith(1, null, [])
  49. @ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon.stub().callsArgWith(1, null, {resources:[{"a.tex"},{"b.tex"}], totalSize:1000000})
  50. @ClsiFormatChecker.checkRecoursesForProblems @resources, (err, problems)=>
  51. problems.sizeCheck.resources.length.should.equal 2
  52. problems.sizeCheck.totalSize.should.equal 1000000
  53. expect(problems.conflictedPaths).to.not.exist
  54. done()
  55. describe "_checkForConflictingPaths", ->
  56. beforeEach ->
  57. @resources.push({
  58. path: "chapters/chapter1.tex"
  59. content: "other stuff"
  60. })
  61. @resources.push({
  62. path: "chapters.tex"
  63. content: "other stuff"
  64. })
  65. it "should flag up when a nested file has folder with same subpath as file elsewhere", (done)->
  66. @resources.push({
  67. path: "stuff/image"
  68. url: "http://somwhere.com"
  69. })
  70. @ClsiFormatChecker._checkForConflictingPaths @resources, (err, conflictPathErrors)->
  71. conflictPathErrors.length.should.equal 1
  72. conflictPathErrors[0].path.should.equal "stuff/image"
  73. done()
  74. it "should flag up when a root level file has folder with same subpath as file elsewhere", (done)->
  75. @resources.push({
  76. path: "stuff"
  77. content: "other stuff"
  78. })
  79. @ClsiFormatChecker._checkForConflictingPaths @resources, (err, conflictPathErrors)->
  80. conflictPathErrors.length.should.equal 1
  81. conflictPathErrors[0].path.should.equal "stuff"
  82. done()
  83. it "should not flag up when the file is a substring of a path", (done)->
  84. @resources.push({
  85. path: "stuf"
  86. content: "other stuff"
  87. })
  88. @ClsiFormatChecker._checkForConflictingPaths @resources, (err, conflictPathErrors)->
  89. conflictPathErrors.length.should.equal 0
  90. done()
  91. describe "_checkDocsAreUnderSizeLimit", ->
  92. it "should error when there is more than 5mb of data", (done)->
  93. @resources.push({
  94. path: "massive.tex"
  95. content: require("crypto").randomBytes(1000 * 1000 * 5).toString("hex")
  96. })
  97. while @resources.length < 20
  98. @resources.push({path:"chapters/chapter1.tex",url: "http://somwhere.com"})
  99. @ClsiFormatChecker._checkDocsAreUnderSizeLimit @resources, (err, sizeError)->
  100. sizeError.totalSize.should.equal 10000016
  101. sizeError.resources.length.should.equal 10
  102. sizeError.resources[0].path.should.equal "massive.tex"
  103. sizeError.resources[0].size.should.equal 1000 * 1000 * 10
  104. done()
  105. it "should return nothing when project is correct size", (done)->
  106. @resources.push({
  107. path: "massive.tex"
  108. content: require("crypto").randomBytes(1000 * 1000 * 1).toString("hex")
  109. })
  110. while @resources.length < 20
  111. @resources.push({path:"chapters/chapter1.tex",url: "http://somwhere.com"})
  112. @ClsiFormatChecker._checkDocsAreUnderSizeLimit @resources, (err, sizeError)->
  113. expect(sizeError).to.not.exist
  114. done()