DocManagerTests.js 25 KB

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