SettingADocumentTests.js 13 KB

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