SettingADocumentTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const chai = require('chai')
  16. chai.should()
  17. const { expect } = require('chai')
  18. const Settings = require('settings-sharelatex')
  19. const rclient_du = require('redis-sharelatex').createClient(
  20. Settings.redis.documentupdater
  21. )
  22. const Keys = Settings.redis.documentupdater.key_schema
  23. const MockTrackChangesApi = require('./helpers/MockTrackChangesApi')
  24. const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
  25. const MockWebApi = require('./helpers/MockWebApi')
  26. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  27. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  28. describe('Setting a document', function () {
  29. before(function (done) {
  30. this.lines = ['one', 'two', 'three']
  31. this.version = 42
  32. this.update = {
  33. doc: this.doc_id,
  34. op: [
  35. {
  36. i: 'one and a half\n',
  37. p: 4
  38. }
  39. ],
  40. v: this.version
  41. }
  42. this.result = ['one', 'one and a half', 'two', 'three']
  43. this.newLines = ['these', 'are', 'the', 'new', 'lines']
  44. this.source = 'dropbox'
  45. this.user_id = 'user-id-123'
  46. sinon.spy(MockTrackChangesApi, 'flushDoc')
  47. sinon.spy(MockProjectHistoryApi, 'flushProject')
  48. sinon.spy(MockWebApi, 'setDocument')
  49. return DocUpdaterApp.ensureRunning(done)
  50. })
  51. after(function () {
  52. MockTrackChangesApi.flushDoc.restore()
  53. MockProjectHistoryApi.flushProject.restore()
  54. return MockWebApi.setDocument.restore()
  55. })
  56. describe('when the updated doc exists in the doc updater', function () {
  57. before(function (done) {
  58. this.project_id = DocUpdaterClient.randomId()
  59. this.doc_id = DocUpdaterClient.randomId()
  60. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  61. lines: this.lines,
  62. version: this.version
  63. })
  64. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, (error) => {
  65. if (error != null) {
  66. throw error
  67. }
  68. return DocUpdaterClient.sendUpdate(
  69. this.project_id,
  70. this.doc_id,
  71. this.update,
  72. (error) => {
  73. if (error != null) {
  74. throw error
  75. }
  76. return setTimeout(() => {
  77. return DocUpdaterClient.setDocLines(
  78. this.project_id,
  79. this.doc_id,
  80. this.newLines,
  81. this.source,
  82. this.user_id,
  83. false,
  84. (error, res, body) => {
  85. this.statusCode = res.statusCode
  86. return done()
  87. }
  88. )
  89. }, 200)
  90. }
  91. )
  92. })
  93. return null
  94. })
  95. after(function () {
  96. MockTrackChangesApi.flushDoc.reset()
  97. MockProjectHistoryApi.flushProject.reset()
  98. return MockWebApi.setDocument.reset()
  99. })
  100. it('should return a 204 status code', function () {
  101. return this.statusCode.should.equal(204)
  102. })
  103. it('should send the updated doc lines and version to the web api', function () {
  104. return MockWebApi.setDocument
  105. .calledWith(this.project_id, this.doc_id, this.newLines)
  106. .should.equal(true)
  107. })
  108. it('should update the lines in the doc updater', function (done) {
  109. DocUpdaterClient.getDoc(
  110. this.project_id,
  111. this.doc_id,
  112. (error, res, doc) => {
  113. doc.lines.should.deep.equal(this.newLines)
  114. return done()
  115. }
  116. )
  117. return null
  118. })
  119. it('should bump the version in the doc updater', function (done) {
  120. DocUpdaterClient.getDoc(
  121. this.project_id,
  122. this.doc_id,
  123. (error, res, doc) => {
  124. doc.version.should.equal(this.version + 2)
  125. return done()
  126. }
  127. )
  128. return null
  129. })
  130. return it('should leave the document in redis', function (done) {
  131. rclient_du.get(Keys.docLines({ doc_id: this.doc_id }), (error, lines) => {
  132. if (error != null) {
  133. throw error
  134. }
  135. expect(JSON.parse(lines)).to.deep.equal(this.newLines)
  136. return done()
  137. })
  138. return null
  139. })
  140. })
  141. describe('when the updated doc does not exist in the doc updater', function () {
  142. before(function (done) {
  143. this.project_id = DocUpdaterClient.randomId()
  144. this.doc_id = DocUpdaterClient.randomId()
  145. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  146. lines: this.lines,
  147. version: this.version
  148. })
  149. DocUpdaterClient.setDocLines(
  150. this.project_id,
  151. this.doc_id,
  152. this.newLines,
  153. this.source,
  154. this.user_id,
  155. false,
  156. (error, res, body) => {
  157. this.statusCode = res.statusCode
  158. return setTimeout(done, 200)
  159. }
  160. )
  161. return null
  162. })
  163. after(function () {
  164. MockTrackChangesApi.flushDoc.reset()
  165. MockProjectHistoryApi.flushProject.reset()
  166. return MockWebApi.setDocument.reset()
  167. })
  168. it('should return a 204 status code', function () {
  169. return this.statusCode.should.equal(204)
  170. })
  171. it('should send the updated doc lines to the web api', function () {
  172. return MockWebApi.setDocument
  173. .calledWith(this.project_id, this.doc_id, this.newLines)
  174. .should.equal(true)
  175. })
  176. it('should flush track changes', function () {
  177. return MockTrackChangesApi.flushDoc
  178. .calledWith(this.doc_id)
  179. .should.equal(true)
  180. })
  181. it('should flush project history', function () {
  182. return MockProjectHistoryApi.flushProject
  183. .calledWith(this.project_id)
  184. .should.equal(true)
  185. })
  186. return it('should remove the document from redis', function (done) {
  187. rclient_du.get(Keys.docLines({ doc_id: this.doc_id }), (error, lines) => {
  188. if (error != null) {
  189. throw error
  190. }
  191. expect(lines).to.not.exist
  192. return done()
  193. })
  194. return null
  195. })
  196. })
  197. describe('when the updated doc is too large for the body parser', function () {
  198. before(function (done) {
  199. this.project_id = DocUpdaterClient.randomId()
  200. this.doc_id = DocUpdaterClient.randomId()
  201. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  202. lines: this.lines,
  203. version: this.version
  204. })
  205. this.newLines = []
  206. while (
  207. JSON.stringify(this.newLines).length <= Settings.maxJsonRequestSize
  208. ) {
  209. this.newLines.push('(a long line of text)'.repeat(10000))
  210. }
  211. DocUpdaterClient.setDocLines(
  212. this.project_id,
  213. this.doc_id,
  214. this.newLines,
  215. this.source,
  216. this.user_id,
  217. false,
  218. (error, res, body) => {
  219. this.statusCode = res.statusCode
  220. return setTimeout(done, 200)
  221. }
  222. )
  223. return null
  224. })
  225. after(function () {
  226. MockTrackChangesApi.flushDoc.reset()
  227. MockProjectHistoryApi.flushProject.reset()
  228. return MockWebApi.setDocument.reset()
  229. })
  230. it('should return a 413 status code', function () {
  231. return this.statusCode.should.equal(413)
  232. })
  233. it('should not send the updated doc lines to the web api', function () {
  234. return MockWebApi.setDocument.called.should.equal(false)
  235. })
  236. it('should not flush track changes', function () {
  237. return MockTrackChangesApi.flushDoc.called.should.equal(false)
  238. })
  239. return it('should not flush project history', function () {
  240. return MockProjectHistoryApi.flushProject.called.should.equal(false)
  241. })
  242. })
  243. describe('when the updated doc is large but under the bodyParser and HTTPController size limit', function () {
  244. before(function (done) {
  245. this.project_id = DocUpdaterClient.randomId()
  246. this.doc_id = DocUpdaterClient.randomId()
  247. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  248. lines: this.lines,
  249. version: this.version
  250. })
  251. this.newLines = []
  252. while (JSON.stringify(this.newLines).length < 2 * 1024 * 1024) {
  253. // limit in HTTPController
  254. this.newLines.push('(a long line of text)'.repeat(10000))
  255. }
  256. this.newLines.pop() // remove the line which took it over the limit
  257. DocUpdaterClient.setDocLines(
  258. this.project_id,
  259. this.doc_id,
  260. this.newLines,
  261. this.source,
  262. this.user_id,
  263. false,
  264. (error, res, body) => {
  265. this.statusCode = res.statusCode
  266. return setTimeout(done, 200)
  267. }
  268. )
  269. return null
  270. })
  271. after(function () {
  272. MockTrackChangesApi.flushDoc.reset()
  273. MockProjectHistoryApi.flushProject.reset()
  274. return MockWebApi.setDocument.reset()
  275. })
  276. it('should return a 204 status code', function () {
  277. return this.statusCode.should.equal(204)
  278. })
  279. return it('should send the updated doc lines to the web api', function () {
  280. return MockWebApi.setDocument
  281. .calledWith(this.project_id, this.doc_id, this.newLines)
  282. .should.equal(true)
  283. })
  284. })
  285. return describe('with track changes', function () {
  286. before(function () {
  287. this.lines = ['one', 'one and a half', 'two', 'three']
  288. this.id_seed = '587357bd35e64f6157'
  289. return (this.update = {
  290. doc: this.doc_id,
  291. op: [
  292. {
  293. d: 'one and a half\n',
  294. p: 4
  295. }
  296. ],
  297. meta: {
  298. tc: this.id_seed,
  299. user_id: this.user_id
  300. },
  301. v: this.version
  302. })
  303. })
  304. describe('with the undo flag', function () {
  305. before(function (done) {
  306. this.project_id = DocUpdaterClient.randomId()
  307. this.doc_id = DocUpdaterClient.randomId()
  308. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  309. lines: this.lines,
  310. version: this.version
  311. })
  312. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, (error) => {
  313. if (error != null) {
  314. throw error
  315. }
  316. return DocUpdaterClient.sendUpdate(
  317. this.project_id,
  318. this.doc_id,
  319. this.update,
  320. (error) => {
  321. if (error != null) {
  322. throw error
  323. }
  324. // Go back to old lines, with undo flag
  325. return DocUpdaterClient.setDocLines(
  326. this.project_id,
  327. this.doc_id,
  328. this.lines,
  329. this.source,
  330. this.user_id,
  331. true,
  332. (error, res, body) => {
  333. this.statusCode = res.statusCode
  334. return setTimeout(done, 200)
  335. }
  336. )
  337. }
  338. )
  339. })
  340. return null
  341. })
  342. after(function () {
  343. MockTrackChangesApi.flushDoc.reset()
  344. MockProjectHistoryApi.flushProject.reset()
  345. return MockWebApi.setDocument.reset()
  346. })
  347. return it('should undo the tracked changes', function (done) {
  348. DocUpdaterClient.getDoc(
  349. this.project_id,
  350. this.doc_id,
  351. (error, res, data) => {
  352. if (error != null) {
  353. throw error
  354. }
  355. const { ranges } = data
  356. expect(ranges.changes).to.be.undefined
  357. return done()
  358. }
  359. )
  360. return null
  361. })
  362. })
  363. return describe('without the undo flag', function () {
  364. before(function (done) {
  365. this.project_id = DocUpdaterClient.randomId()
  366. this.doc_id = DocUpdaterClient.randomId()
  367. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  368. lines: this.lines,
  369. version: this.version
  370. })
  371. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, (error) => {
  372. if (error != null) {
  373. throw error
  374. }
  375. return DocUpdaterClient.sendUpdate(
  376. this.project_id,
  377. this.doc_id,
  378. this.update,
  379. (error) => {
  380. if (error != null) {
  381. throw error
  382. }
  383. // Go back to old lines, without undo flag
  384. return DocUpdaterClient.setDocLines(
  385. this.project_id,
  386. this.doc_id,
  387. this.lines,
  388. this.source,
  389. this.user_id,
  390. false,
  391. (error, res, body) => {
  392. this.statusCode = res.statusCode
  393. return setTimeout(done, 200)
  394. }
  395. )
  396. }
  397. )
  398. })
  399. return null
  400. })
  401. after(function () {
  402. MockTrackChangesApi.flushDoc.reset()
  403. MockProjectHistoryApi.flushProject.reset()
  404. return MockWebApi.setDocument.reset()
  405. })
  406. return it('should not undo the tracked changes', function (done) {
  407. DocUpdaterClient.getDoc(
  408. this.project_id,
  409. this.doc_id,
  410. (error, res, data) => {
  411. if (error != null) {
  412. throw error
  413. }
  414. const { ranges } = data
  415. expect(ranges.changes.length).to.equal(1)
  416. return done()
  417. }
  418. )
  419. return null
  420. })
  421. })
  422. })
  423. })