SettingADocumentTests.js 14 KB

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