SettingADocumentTests.js 12 KB

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