DocManagerTests.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /* eslint-disable
  2. no-dupe-keys,
  3. no-return-assign,
  4. no-unused-vars,
  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. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const sinon = require('sinon')
  15. const { assert, expect } = require('chai')
  16. const modulePath = require('path').join(__dirname, '../../../app/js/DocManager')
  17. const { ObjectId } = require('mongodb')
  18. const Errors = require('../../../app/js/Errors')
  19. describe('DocManager', function () {
  20. beforeEach(function () {
  21. this.DocManager = SandboxedModule.require(modulePath, {
  22. requires: {
  23. './MongoManager': (this.MongoManager = {}),
  24. './DocArchiveManager': (this.DocArchiveManager = {}),
  25. './RangeManager': (this.RangeManager = {
  26. jsonRangesToMongo(r) {
  27. return r
  28. },
  29. shouldUpdateRanges: sinon.stub().returns(false),
  30. }),
  31. '@overleaf/settings': (this.settings = { docstore: {} }),
  32. './Errors': Errors,
  33. },
  34. })
  35. this.doc_id = ObjectId().toString()
  36. this.project_id = ObjectId().toString()
  37. this.another_project_id = ObjectId().toString()
  38. this.callback = sinon.stub()
  39. return (this.stubbedError = new Error('blew up'))
  40. })
  41. describe('getFullDoc', function () {
  42. beforeEach(function () {
  43. this.DocManager._getDoc = sinon.stub()
  44. return (this.doc = {
  45. _id: this.doc_id,
  46. lines: ['2134'],
  47. })
  48. })
  49. it('should call get doc with a quick filter', function (done) {
  50. this.DocManager._getDoc.callsArgWith(3, null, this.doc)
  51. return this.DocManager.getFullDoc(
  52. this.project_id,
  53. this.doc_id,
  54. (err, doc) => {
  55. if (err) return done(err)
  56. doc.should.equal(this.doc)
  57. this.DocManager._getDoc
  58. .calledWith(this.project_id, this.doc_id, {
  59. lines: true,
  60. rev: true,
  61. deleted: true,
  62. version: true,
  63. ranges: true,
  64. inS3: true,
  65. })
  66. .should.equal(true)
  67. return done()
  68. }
  69. )
  70. })
  71. return it('should return error when get doc errors', function (done) {
  72. this.DocManager._getDoc.callsArgWith(3, 'error')
  73. return this.DocManager.getFullDoc(
  74. this.project_id,
  75. this.doc_id,
  76. (err, exist) => {
  77. err.should.equal('error')
  78. return done()
  79. }
  80. )
  81. })
  82. })
  83. describe('getRawDoc', function () {
  84. beforeEach(function () {
  85. this.DocManager._getDoc = sinon.stub()
  86. return (this.doc = { lines: ['2134'] })
  87. })
  88. it('should call get doc with a quick filter', function (done) {
  89. this.DocManager._getDoc.callsArgWith(3, null, this.doc)
  90. return this.DocManager.getDocLines(
  91. this.project_id,
  92. this.doc_id,
  93. (err, doc) => {
  94. if (err) return done(err)
  95. doc.should.equal(this.doc)
  96. this.DocManager._getDoc
  97. .calledWith(this.project_id, this.doc_id, {
  98. lines: true,
  99. inS3: true,
  100. })
  101. .should.equal(true)
  102. return done()
  103. }
  104. )
  105. })
  106. return it('should return error when get doc errors', function (done) {
  107. this.DocManager._getDoc.callsArgWith(3, 'error')
  108. return this.DocManager.getDocLines(
  109. this.project_id,
  110. this.doc_id,
  111. (err, exist) => {
  112. err.should.equal('error')
  113. return done()
  114. }
  115. )
  116. })
  117. })
  118. describe('getDoc', function () {
  119. beforeEach(function () {
  120. this.project = { name: 'mock-project' }
  121. this.doc = {
  122. _id: this.doc_id,
  123. project_id: this.project_id,
  124. lines: ['mock-lines'],
  125. }
  126. this.version = 42
  127. this.MongoManager.findDoc = sinon.stub()
  128. return (this.MongoManager.getDocVersion = sinon
  129. .stub()
  130. .yields(null, this.version))
  131. })
  132. describe('when using a filter', function () {
  133. beforeEach(function () {
  134. return this.MongoManager.findDoc.yields(null, this.doc)
  135. })
  136. it('should error if inS3 is not set to true', function (done) {
  137. return this.DocManager._getDoc(
  138. this.project_id,
  139. this.doc_id,
  140. { inS3: false },
  141. err => {
  142. expect(err).to.exist
  143. return done()
  144. }
  145. )
  146. })
  147. it('should always get inS3 even when no filter is passed', function (done) {
  148. return this.DocManager._getDoc(
  149. this.project_id,
  150. this.doc_id,
  151. undefined,
  152. err => {
  153. this.MongoManager.findDoc.called.should.equal(false)
  154. expect(err).to.exist
  155. return done()
  156. }
  157. )
  158. })
  159. return it('should not error if inS3 is set to true', function (done) {
  160. return this.DocManager._getDoc(
  161. this.project_id,
  162. this.doc_id,
  163. { inS3: true },
  164. err => {
  165. expect(err).to.not.exist
  166. return done()
  167. }
  168. )
  169. })
  170. })
  171. describe('when the doc is in the doc collection', function () {
  172. beforeEach(function () {
  173. this.MongoManager.findDoc.yields(null, this.doc)
  174. return this.DocManager._getDoc(
  175. this.project_id,
  176. this.doc_id,
  177. { version: true, inS3: true },
  178. this.callback
  179. )
  180. })
  181. it('should get the doc from the doc collection', function () {
  182. return this.MongoManager.findDoc
  183. .calledWith(this.project_id, this.doc_id)
  184. .should.equal(true)
  185. })
  186. it('should get the doc version from the docOps collection', function () {
  187. return this.MongoManager.getDocVersion
  188. .calledWith(this.doc_id)
  189. .should.equal(true)
  190. })
  191. return it('should return the callback with the doc with the version', function () {
  192. this.callback.called.should.equal(true)
  193. const doc = this.callback.args[0][1]
  194. doc.lines.should.equal(this.doc.lines)
  195. return doc.version.should.equal(this.version)
  196. })
  197. })
  198. describe('without the version filter', function () {
  199. beforeEach(function () {
  200. this.MongoManager.findDoc.yields(null, this.doc)
  201. return this.DocManager._getDoc(
  202. this.project_id,
  203. this.doc_id,
  204. { version: false, inS3: true },
  205. this.callback
  206. )
  207. })
  208. return it('should not get the doc version from the docOps collection', function () {
  209. return this.MongoManager.getDocVersion.called.should.equal(false)
  210. })
  211. })
  212. describe('when MongoManager.findDoc errors', function () {
  213. beforeEach(function () {
  214. this.MongoManager.findDoc.yields(this.stubbedError)
  215. return this.DocManager._getDoc(
  216. this.project_id,
  217. this.doc_id,
  218. { version: true, inS3: true },
  219. this.callback
  220. )
  221. })
  222. return it('should return the error', function () {
  223. return this.callback.calledWith(this.stubbedError).should.equal(true)
  224. })
  225. })
  226. describe('when the doc is archived', function () {
  227. beforeEach(function () {
  228. this.doc = {
  229. _id: this.doc_id,
  230. project_id: this.project_id,
  231. lines: ['mock-lines'],
  232. inS3: true,
  233. }
  234. this.MongoManager.findDoc.yields(null, this.doc)
  235. this.DocArchiveManager.unarchiveDoc = (projectId, docId, callback) => {
  236. this.doc.inS3 = false
  237. return callback()
  238. }
  239. sinon.spy(this.DocArchiveManager, 'unarchiveDoc')
  240. return this.DocManager._getDoc(
  241. this.project_id,
  242. this.doc_id,
  243. { version: true, inS3: true },
  244. this.callback
  245. )
  246. })
  247. it('should call the DocArchive to unarchive the doc', function () {
  248. return this.DocArchiveManager.unarchiveDoc
  249. .calledWith(this.project_id, this.doc_id)
  250. .should.equal(true)
  251. })
  252. it('should look up the doc twice', function () {
  253. return this.MongoManager.findDoc.calledTwice.should.equal(true)
  254. })
  255. return it('should return the doc', function () {
  256. return this.callback.calledWith(null, this.doc).should.equal(true)
  257. })
  258. })
  259. return describe('when the doc does not exist in the docs collection', function () {
  260. beforeEach(function () {
  261. this.MongoManager.findDoc = sinon.stub().yields(null, null)
  262. return this.DocManager._getDoc(
  263. this.project_id,
  264. this.doc_id,
  265. { version: true, inS3: true },
  266. this.callback
  267. )
  268. })
  269. return it('should return a NotFoundError', function () {
  270. return this.callback
  271. .calledWith(
  272. sinon.match.has(
  273. 'message',
  274. `No such doc: ${this.doc_id} in project ${this.project_id}`
  275. )
  276. )
  277. .should.equal(true)
  278. })
  279. })
  280. })
  281. describe('getAllNonDeletedDocs', function () {
  282. describe('when the project exists', function () {
  283. beforeEach(function () {
  284. this.docs = [
  285. {
  286. _id: this.doc_id,
  287. project_id: this.project_id,
  288. lines: ['mock-lines'],
  289. },
  290. ]
  291. this.MongoManager.getProjectsDocs = sinon
  292. .stub()
  293. .callsArgWith(3, null, this.docs)
  294. this.DocArchiveManager.unArchiveAllDocs = sinon
  295. .stub()
  296. .callsArgWith(1, null, this.docs)
  297. this.filter = { lines: true }
  298. return this.DocManager.getAllNonDeletedDocs(
  299. this.project_id,
  300. this.filter,
  301. this.callback
  302. )
  303. })
  304. it('should get the project from the database', function () {
  305. return this.MongoManager.getProjectsDocs
  306. .calledWith(this.project_id, { include_deleted: false }, this.filter)
  307. .should.equal(true)
  308. })
  309. return it('should return the docs', function () {
  310. return this.callback.calledWith(null, this.docs).should.equal(true)
  311. })
  312. })
  313. return describe('when there are no docs for the project', function () {
  314. beforeEach(function () {
  315. this.MongoManager.getProjectsDocs = sinon
  316. .stub()
  317. .callsArgWith(3, null, null)
  318. this.DocArchiveManager.unArchiveAllDocs = sinon
  319. .stub()
  320. .callsArgWith(1, null)
  321. return this.DocManager.getAllNonDeletedDocs(
  322. this.project_id,
  323. this.filter,
  324. this.callback
  325. )
  326. })
  327. return it('should return a NotFoundError', function () {
  328. return this.callback
  329. .calledWith(
  330. sinon.match.has('message', `No docs for project ${this.project_id}`)
  331. )
  332. .should.equal(true)
  333. })
  334. })
  335. })
  336. describe('patchDoc', function () {
  337. describe('when the doc exists', function () {
  338. beforeEach(function () {
  339. this.lines = ['mock', 'doc', 'lines']
  340. this.rev = 77
  341. this.MongoManager.findDoc = sinon
  342. .stub()
  343. .yields(null, { _id: ObjectId(this.doc_id) })
  344. this.MongoManager.patchDoc = sinon.stub().yields(null)
  345. this.DocArchiveManager.archiveDoc = sinon.stub().yields(null)
  346. this.meta = {}
  347. })
  348. describe('standard path', function () {
  349. beforeEach(function (done) {
  350. this.callback = sinon.stub().callsFake(done)
  351. this.DocManager.patchDoc(
  352. this.project_id,
  353. this.doc_id,
  354. this.meta,
  355. this.callback
  356. )
  357. })
  358. it('should get the doc', function () {
  359. expect(this.MongoManager.findDoc).to.have.been.calledWith(
  360. this.project_id,
  361. this.doc_id
  362. )
  363. })
  364. it('should persist the meta', function () {
  365. expect(this.MongoManager.patchDoc).to.have.been.calledWith(
  366. this.project_id,
  367. this.doc_id,
  368. this.meta
  369. )
  370. })
  371. it('should return the callback', function () {
  372. expect(this.callback).to.have.been.calledWith(null)
  373. })
  374. })
  375. describe('background flush disabled and deleting a doc', function () {
  376. beforeEach(function (done) {
  377. this.settings.docstore.archiveOnSoftDelete = false
  378. this.meta.deleted = true
  379. this.callback = sinon.stub().callsFake(done)
  380. this.DocManager.patchDoc(
  381. this.project_id,
  382. this.doc_id,
  383. this.meta,
  384. this.callback
  385. )
  386. })
  387. it('should not flush the doc out of mongo', function () {
  388. expect(this.DocArchiveManager.archiveDoc).to.not.have.been.called
  389. })
  390. })
  391. describe('background flush enabled and not deleting a doc', function () {
  392. beforeEach(function (done) {
  393. this.settings.docstore.archiveOnSoftDelete = false
  394. this.meta.deleted = false
  395. this.callback = sinon.stub().callsFake(done)
  396. this.DocManager.patchDoc(
  397. this.project_id,
  398. this.doc_id,
  399. this.meta,
  400. this.callback
  401. )
  402. })
  403. it('should not flush the doc out of mongo', function () {
  404. expect(this.DocArchiveManager.archiveDoc).to.not.have.been.called
  405. })
  406. })
  407. describe('background flush enabled and deleting a doc', function () {
  408. beforeEach(function () {
  409. this.settings.docstore.archiveOnSoftDelete = true
  410. this.meta.deleted = true
  411. })
  412. describe('when the background flush succeeds', function () {
  413. beforeEach(function (done) {
  414. this.DocArchiveManager.archiveDoc = sinon.stub().yields(null)
  415. this.callback = sinon.stub().callsFake(done)
  416. this.DocManager.patchDoc(
  417. this.project_id,
  418. this.doc_id,
  419. this.meta,
  420. this.callback
  421. )
  422. })
  423. it('should not log a warning', function () {
  424. expect(this.logger.warn).to.not.have.been.called
  425. })
  426. it('should flush the doc out of mongo', function () {
  427. expect(this.DocArchiveManager.archiveDoc).to.have.been.calledWith(
  428. this.project_id,
  429. this.doc_id
  430. )
  431. })
  432. })
  433. describe('when the background flush fails', function () {
  434. beforeEach(function (done) {
  435. this.err = new Error('foo')
  436. this.DocArchiveManager.archiveDoc = sinon.stub().yields(this.err)
  437. this.callback = sinon.stub().callsFake(done)
  438. this.DocManager.patchDoc(
  439. this.project_id,
  440. this.doc_id,
  441. this.meta,
  442. this.callback
  443. )
  444. })
  445. it('should log a warning', function () {
  446. expect(this.logger.warn).to.have.been.calledWith(
  447. sinon.match({
  448. projectId: this.project_id,
  449. docId: this.doc_id,
  450. err: this.err,
  451. }),
  452. 'archiving a single doc in the background failed'
  453. )
  454. })
  455. it('should not fail the delete process', function () {
  456. expect(this.callback).to.have.been.calledWith(null)
  457. })
  458. })
  459. })
  460. })
  461. describe('when the doc does not exist', function () {
  462. beforeEach(function () {
  463. this.MongoManager.findDoc = sinon.stub().yields(null)
  464. this.DocManager.patchDoc(
  465. this.project_id,
  466. this.doc_id,
  467. {},
  468. this.callback
  469. )
  470. })
  471. it('should return a NotFoundError', function () {
  472. expect(this.callback).to.have.been.calledWith(
  473. sinon.match.has(
  474. 'message',
  475. `No such project/doc to delete: ${this.project_id}/${this.doc_id}`
  476. )
  477. )
  478. })
  479. })
  480. })
  481. return describe('updateDoc', function () {
  482. beforeEach(function () {
  483. this.oldDocLines = ['old', 'doc', 'lines']
  484. this.newDocLines = ['new', 'doc', 'lines']
  485. this.originalRanges = {
  486. changes: [
  487. {
  488. id: ObjectId().toString(),
  489. op: { i: 'foo', p: 3 },
  490. meta: {
  491. user_id: ObjectId().toString(),
  492. ts: new Date().toString(),
  493. },
  494. },
  495. ],
  496. }
  497. this.newRanges = {
  498. changes: [
  499. {
  500. id: ObjectId().toString(),
  501. op: { i: 'bar', p: 6 },
  502. meta: {
  503. user_id: ObjectId().toString(),
  504. ts: new Date().toString(),
  505. },
  506. },
  507. ],
  508. }
  509. this.version = 42
  510. this.doc = {
  511. _id: this.doc_id,
  512. project_id: this.project_id,
  513. lines: this.oldDocLines,
  514. rev: (this.rev = 5),
  515. version: this.version,
  516. ranges: this.originalRanges,
  517. }
  518. this.MongoManager.upsertIntoDocCollection = sinon.stub().callsArg(3)
  519. this.MongoManager.setDocVersion = sinon.stub().yields()
  520. return (this.DocManager._getDoc = sinon.stub())
  521. })
  522. describe('when only the doc lines have changed', function () {
  523. beforeEach(function () {
  524. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, this.doc)
  525. return this.DocManager.updateDoc(
  526. this.project_id,
  527. this.doc_id,
  528. this.newDocLines,
  529. this.version,
  530. this.originalRanges,
  531. this.callback
  532. )
  533. })
  534. it('should get the existing doc', function () {
  535. return this.DocManager._getDoc
  536. .calledWith(this.project_id, this.doc_id, {
  537. version: true,
  538. rev: true,
  539. lines: true,
  540. version: true,
  541. ranges: true,
  542. inS3: true,
  543. })
  544. .should.equal(true)
  545. })
  546. it('should upsert the document to the doc collection', function () {
  547. return this.MongoManager.upsertIntoDocCollection
  548. .calledWith(this.project_id, this.doc_id, { lines: this.newDocLines })
  549. .should.equal(true)
  550. })
  551. it('should not update the version', function () {
  552. return this.MongoManager.setDocVersion.called.should.equal(false)
  553. })
  554. return it('should return the callback with the new rev', function () {
  555. return this.callback
  556. .calledWith(null, true, this.rev + 1)
  557. .should.equal(true)
  558. })
  559. })
  560. describe('when the doc ranges have changed', function () {
  561. beforeEach(function () {
  562. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, this.doc)
  563. this.RangeManager.shouldUpdateRanges.returns(true)
  564. return this.DocManager.updateDoc(
  565. this.project_id,
  566. this.doc_id,
  567. this.oldDocLines,
  568. this.version,
  569. this.newRanges,
  570. this.callback
  571. )
  572. })
  573. it('should upsert the ranges', function () {
  574. return this.MongoManager.upsertIntoDocCollection
  575. .calledWith(this.project_id, this.doc_id, { ranges: this.newRanges })
  576. .should.equal(true)
  577. })
  578. it('should not update the version', function () {
  579. return this.MongoManager.setDocVersion.called.should.equal(false)
  580. })
  581. return it('should return the callback with the new rev', function () {
  582. return this.callback
  583. .calledWith(null, true, this.rev + 1)
  584. .should.equal(true)
  585. })
  586. })
  587. describe('when only the version has changed', function () {
  588. beforeEach(function () {
  589. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, this.doc)
  590. return this.DocManager.updateDoc(
  591. this.project_id,
  592. this.doc_id,
  593. this.oldDocLines,
  594. this.version + 1,
  595. this.originalRanges,
  596. this.callback
  597. )
  598. })
  599. it('should not change the lines or ranges', function () {
  600. return this.MongoManager.upsertIntoDocCollection.called.should.equal(
  601. false
  602. )
  603. })
  604. it('should update the version', function () {
  605. return this.MongoManager.setDocVersion
  606. .calledWith(this.doc_id, this.version + 1)
  607. .should.equal(true)
  608. })
  609. return it('should return the callback with the old rev', function () {
  610. return this.callback.calledWith(null, true, this.rev).should.equal(true)
  611. })
  612. })
  613. describe('when the doc has not changed at all', function () {
  614. beforeEach(function () {
  615. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, this.doc)
  616. return this.DocManager.updateDoc(
  617. this.project_id,
  618. this.doc_id,
  619. this.oldDocLines,
  620. this.version,
  621. this.originalRanges,
  622. this.callback
  623. )
  624. })
  625. it('should not update the ranges or lines', function () {
  626. return this.MongoManager.upsertIntoDocCollection.called.should.equal(
  627. false
  628. )
  629. })
  630. it('should not update the version', function () {
  631. return this.MongoManager.setDocVersion.called.should.equal(false)
  632. })
  633. return it('should return the callback with the old rev and modified == false', function () {
  634. return this.callback
  635. .calledWith(null, false, this.rev)
  636. .should.equal(true)
  637. })
  638. })
  639. describe('when the version is null', function () {
  640. beforeEach(function () {
  641. return this.DocManager.updateDoc(
  642. this.project_id,
  643. this.doc_id,
  644. this.newDocLines,
  645. null,
  646. this.originalRanges,
  647. this.callback
  648. )
  649. })
  650. return it('should return an error', function () {
  651. return this.callback
  652. .calledWith(
  653. sinon.match.has('message', 'no lines, version or ranges provided')
  654. )
  655. .should.equal(true)
  656. })
  657. })
  658. describe('when the lines are null', function () {
  659. beforeEach(function () {
  660. return this.DocManager.updateDoc(
  661. this.project_id,
  662. this.doc_id,
  663. null,
  664. this.version,
  665. this.originalRanges,
  666. this.callback
  667. )
  668. })
  669. return it('should return an error', function () {
  670. return this.callback
  671. .calledWith(
  672. sinon.match.has('message', 'no lines, version or ranges provided')
  673. )
  674. .should.equal(true)
  675. })
  676. })
  677. describe('when the ranges are null', function () {
  678. beforeEach(function () {
  679. return this.DocManager.updateDoc(
  680. this.project_id,
  681. this.doc_id,
  682. this.newDocLines,
  683. this.version,
  684. null,
  685. this.callback
  686. )
  687. })
  688. return it('should return an error', function () {
  689. return this.callback
  690. .calledWith(
  691. sinon.match.has('message', 'no lines, version or ranges provided')
  692. )
  693. .should.equal(true)
  694. })
  695. })
  696. describe('when there is a generic error getting the doc', function () {
  697. beforeEach(function () {
  698. this.error = new Error('doc could not be found')
  699. this.DocManager._getDoc = sinon
  700. .stub()
  701. .callsArgWith(3, this.error, null, null)
  702. return this.DocManager.updateDoc(
  703. this.project_id,
  704. this.doc_id,
  705. this.newDocLines,
  706. this.version,
  707. this.originalRanges,
  708. this.callback
  709. )
  710. })
  711. it('should not upsert the document to the doc collection', function () {
  712. return this.MongoManager.upsertIntoDocCollection.called.should.equal(
  713. false
  714. )
  715. })
  716. return it('should return the callback with the error', function () {
  717. return this.callback.calledWith(this.error).should.equal(true)
  718. })
  719. })
  720. describe('when the version was decremented', function () {
  721. beforeEach(function () {
  722. this.DocManager._getDoc = sinon.stub().yields(null, this.doc)
  723. this.DocManager.updateDoc(
  724. this.project_id,
  725. this.doc_id,
  726. this.newDocLines,
  727. this.version - 1,
  728. this.originalRanges,
  729. this.callback
  730. )
  731. })
  732. it('should return an error', function () {
  733. this.callback.should.have.been.calledWith(
  734. sinon.match.instanceOf(Errors.DocVersionDecrementedError)
  735. )
  736. })
  737. })
  738. describe('when the doc lines have not changed', function () {
  739. beforeEach(function () {
  740. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, this.doc)
  741. return this.DocManager.updateDoc(
  742. this.project_id,
  743. this.doc_id,
  744. this.oldDocLines.slice(),
  745. this.version,
  746. this.originalRanges,
  747. this.callback
  748. )
  749. })
  750. it('should not update the doc', function () {
  751. return this.MongoManager.upsertIntoDocCollection.called.should.equal(
  752. false
  753. )
  754. })
  755. return it('should return the callback with the existing rev', function () {
  756. return this.callback
  757. .calledWith(null, false, this.rev)
  758. .should.equal(true)
  759. })
  760. })
  761. return describe('when the doc does not exist', function () {
  762. beforeEach(function () {
  763. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, null, null)
  764. return this.DocManager.updateDoc(
  765. this.project_id,
  766. this.doc_id,
  767. this.newDocLines,
  768. this.version,
  769. this.originalRanges,
  770. this.callback
  771. )
  772. })
  773. it('should upsert the document to the doc collection', function () {
  774. return this.MongoManager.upsertIntoDocCollection
  775. .calledWith(this.project_id, this.doc_id, {
  776. lines: this.newDocLines,
  777. ranges: this.originalRanges,
  778. })
  779. .should.equal(true)
  780. })
  781. it('should set the version', function () {
  782. return this.MongoManager.setDocVersion
  783. .calledWith(this.doc_id, this.version)
  784. .should.equal(true)
  785. })
  786. return it('should return the callback with the new rev', function () {
  787. return this.callback.calledWith(null, true, 1).should.equal(true)
  788. })
  789. })
  790. })
  791. })