ClsiFormatCheckerTests.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* eslint-disable
  2. node/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const { expect } = require('chai')
  16. const modulePath = '../../../../app/src/Features/Compile/ClsiFormatChecker.js'
  17. const SandboxedModule = require('sandboxed-module')
  18. describe('ClsiFormatChecker', function () {
  19. beforeEach(function () {
  20. this.ClsiFormatChecker = SandboxedModule.require(modulePath, {
  21. requires: {
  22. 'settings-sharelatex': (this.settings = { compileBodySizeLimitMb: 5 }),
  23. },
  24. })
  25. return (this.project_id = 'project-id')
  26. })
  27. describe('checkRecoursesForProblems', function () {
  28. beforeEach(function () {
  29. return (this.resources = [
  30. {
  31. path: 'main.tex',
  32. content: 'stuff',
  33. },
  34. {
  35. path: 'chapters/chapter1',
  36. content: 'other stuff',
  37. },
  38. {
  39. path: 'stuff/image/image.png',
  40. url: `http:somewhere.com/project/${this.project_id}/file/1234124321312`,
  41. modified: 'more stuff',
  42. },
  43. ])
  44. })
  45. it('should call _checkDocsAreUnderSizeLimit and _checkForConflictingPaths', function (done) {
  46. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  47. .stub()
  48. .callsArgWith(1, null)
  49. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  50. .stub()
  51. .callsArgWith(1)
  52. return this.ClsiFormatChecker.checkRecoursesForProblems(
  53. this.resources,
  54. (err, problems) => {
  55. this.ClsiFormatChecker._checkForConflictingPaths.called.should.equal(
  56. true
  57. )
  58. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit.called.should.equal(
  59. true
  60. )
  61. return done()
  62. }
  63. )
  64. })
  65. it('should remove undefined errors', function (done) {
  66. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  67. .stub()
  68. .callsArgWith(1, null, [])
  69. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  70. .stub()
  71. .callsArgWith(1, null, {})
  72. return this.ClsiFormatChecker.checkRecoursesForProblems(
  73. this.resources,
  74. (err, problems) => {
  75. expect(problems).to.not.exist
  76. expect(problems).to.not.exist
  77. return done()
  78. }
  79. )
  80. })
  81. it('should keep populated arrays', function (done) {
  82. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  83. .stub()
  84. .callsArgWith(1, null, [{ path: 'somewhere/main.tex' }])
  85. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  86. .stub()
  87. .callsArgWith(1, null, {})
  88. return this.ClsiFormatChecker.checkRecoursesForProblems(
  89. this.resources,
  90. (err, problems) => {
  91. problems.conflictedPaths[0].path.should.equal('somewhere/main.tex')
  92. expect(problems.sizeCheck).to.not.exist
  93. return done()
  94. }
  95. )
  96. })
  97. it('should keep populated object', function (done) {
  98. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  99. .stub()
  100. .callsArgWith(1, null, [])
  101. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  102. .stub()
  103. .callsArgWith(1, null, {
  104. resources: [{ 'a.tex': 'a.tex' }, { 'b.tex': 'b.tex' }],
  105. totalSize: 1000000,
  106. })
  107. return this.ClsiFormatChecker.checkRecoursesForProblems(
  108. this.resources,
  109. (err, problems) => {
  110. problems.sizeCheck.resources.length.should.equal(2)
  111. problems.sizeCheck.totalSize.should.equal(1000000)
  112. expect(problems.conflictedPaths).to.not.exist
  113. return done()
  114. }
  115. )
  116. })
  117. describe('_checkForConflictingPaths', function () {
  118. beforeEach(function () {
  119. this.resources.push({
  120. path: 'chapters/chapter1.tex',
  121. content: 'other stuff',
  122. })
  123. return this.resources.push({
  124. path: 'chapters.tex',
  125. content: 'other stuff',
  126. })
  127. })
  128. it('should flag up when a nested file has folder with same subpath as file elsewhere', function (done) {
  129. this.resources.push({
  130. path: 'stuff/image',
  131. url: 'http://somwhere.com',
  132. })
  133. return this.ClsiFormatChecker._checkForConflictingPaths(
  134. this.resources,
  135. (err, conflictPathErrors) => {
  136. conflictPathErrors.length.should.equal(1)
  137. conflictPathErrors[0].path.should.equal('stuff/image')
  138. return done()
  139. }
  140. )
  141. })
  142. it('should flag up when a root level file has folder with same subpath as file elsewhere', function (done) {
  143. this.resources.push({
  144. path: 'stuff',
  145. content: 'other stuff',
  146. })
  147. return this.ClsiFormatChecker._checkForConflictingPaths(
  148. this.resources,
  149. (err, conflictPathErrors) => {
  150. conflictPathErrors.length.should.equal(1)
  151. conflictPathErrors[0].path.should.equal('stuff')
  152. return done()
  153. }
  154. )
  155. })
  156. it('should not flag up when the file is a substring of a path', function (done) {
  157. this.resources.push({
  158. path: 'stuf',
  159. content: 'other stuff',
  160. })
  161. return this.ClsiFormatChecker._checkForConflictingPaths(
  162. this.resources,
  163. (err, conflictPathErrors) => {
  164. conflictPathErrors.length.should.equal(0)
  165. return done()
  166. }
  167. )
  168. })
  169. })
  170. describe('_checkDocsAreUnderSizeLimit', function () {
  171. it('should error when there is more than 5mb of data', function (done) {
  172. this.resources.push({
  173. path: 'massive.tex',
  174. content: 'hello world'.repeat(833333), // over 5mb limit
  175. })
  176. while (this.resources.length < 20) {
  177. this.resources.push({
  178. path: 'chapters/chapter1.tex',
  179. url: 'http://somwhere.com',
  180. })
  181. }
  182. return this.ClsiFormatChecker._checkDocsAreUnderSizeLimit(
  183. this.resources,
  184. (err, sizeError) => {
  185. sizeError.totalSize.should.equal(16 + 833333 * 11) // 16 is for earlier resources
  186. sizeError.resources.length.should.equal(10)
  187. sizeError.resources[0].path.should.equal('massive.tex')
  188. sizeError.resources[0].size.should.equal(833333 * 11)
  189. return done()
  190. }
  191. )
  192. })
  193. it('should return nothing when project is correct size', function (done) {
  194. this.resources.push({
  195. path: 'massive.tex',
  196. content: 'x'.repeat(2 * 1000 * 1000),
  197. })
  198. while (this.resources.length < 20) {
  199. this.resources.push({
  200. path: 'chapters/chapter1.tex',
  201. url: 'http://somwhere.com',
  202. })
  203. }
  204. return this.ClsiFormatChecker._checkDocsAreUnderSizeLimit(
  205. this.resources,
  206. (err, sizeError) => {
  207. expect(sizeError).to.not.exist
  208. return done()
  209. }
  210. )
  211. })
  212. })
  213. })
  214. })