DocManagerTests.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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.archiveDoc = 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.archiveDoc).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.archiveDoc).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.archiveDoc = 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(this.DocArchiveManager.archiveDoc).to.have.been.calledWith(
  433. this.project_id,
  434. this.doc_id
  435. )
  436. })
  437. })
  438. describe('when the background flush fails', function () {
  439. beforeEach(function (done) {
  440. this.err = new Error('foo')
  441. this.DocArchiveManager.archiveDoc = sinon.stub().yields(this.err)
  442. this.callback = sinon.stub().callsFake(done)
  443. this.DocManager.patchDoc(
  444. this.project_id,
  445. this.doc_id,
  446. this.meta,
  447. this.callback
  448. )
  449. })
  450. it('should log a warning', function () {
  451. expect(this.logger.warn).to.have.been.calledWith(
  452. sinon.match({
  453. project_id: this.project_id,
  454. doc_id: this.doc_id,
  455. err: this.err,
  456. }),
  457. 'archiving a single doc in the background failed'
  458. )
  459. })
  460. it('should not fail the delete process', function () {
  461. expect(this.callback).to.have.been.calledWith(null)
  462. })
  463. })
  464. })
  465. })
  466. describe('when the doc does not exist', function () {
  467. beforeEach(function () {
  468. this.MongoManager.findDoc = sinon.stub().yields(null)
  469. this.DocManager.patchDoc(
  470. this.project_id,
  471. this.doc_id,
  472. {},
  473. this.callback
  474. )
  475. })
  476. it('should return a NotFoundError', function () {
  477. expect(this.callback).to.have.been.calledWith(
  478. sinon.match.has(
  479. 'message',
  480. `No such project/doc to delete: ${this.project_id}/${this.doc_id}`
  481. )
  482. )
  483. })
  484. })
  485. })
  486. return describe('updateDoc', function () {
  487. beforeEach(function () {
  488. this.oldDocLines = ['old', 'doc', 'lines']
  489. this.newDocLines = ['new', 'doc', 'lines']
  490. this.originalRanges = {
  491. changes: [
  492. {
  493. id: ObjectId().toString(),
  494. op: { i: 'foo', p: 3 },
  495. meta: {
  496. user_id: ObjectId().toString(),
  497. ts: new Date().toString(),
  498. },
  499. },
  500. ],
  501. }
  502. this.newRanges = {
  503. changes: [
  504. {
  505. id: ObjectId().toString(),
  506. op: { i: 'bar', p: 6 },
  507. meta: {
  508. user_id: ObjectId().toString(),
  509. ts: new Date().toString(),
  510. },
  511. },
  512. ],
  513. }
  514. this.version = 42
  515. this.doc = {
  516. _id: this.doc_id,
  517. project_id: this.project_id,
  518. lines: this.oldDocLines,
  519. rev: (this.rev = 5),
  520. version: this.version,
  521. ranges: this.originalRanges,
  522. }
  523. this.MongoManager.upsertIntoDocCollection = sinon.stub().callsArg(3)
  524. this.MongoManager.setDocVersion = sinon.stub().yields()
  525. return (this.DocManager._getDoc = sinon.stub())
  526. })
  527. describe('when only the doc lines have changed', function () {
  528. beforeEach(function () {
  529. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, this.doc)
  530. return this.DocManager.updateDoc(
  531. this.project_id,
  532. this.doc_id,
  533. this.newDocLines,
  534. this.version,
  535. this.originalRanges,
  536. this.callback
  537. )
  538. })
  539. it('should get the existing doc', function () {
  540. return this.DocManager._getDoc
  541. .calledWith(this.project_id, this.doc_id, {
  542. version: true,
  543. rev: true,
  544. lines: true,
  545. version: true,
  546. ranges: true,
  547. inS3: true,
  548. })
  549. .should.equal(true)
  550. })
  551. it('should upsert the document to the doc collection', function () {
  552. return this.MongoManager.upsertIntoDocCollection
  553. .calledWith(this.project_id, this.doc_id, { lines: this.newDocLines })
  554. .should.equal(true)
  555. })
  556. it('should not update the version', function () {
  557. return this.MongoManager.setDocVersion.called.should.equal(false)
  558. })
  559. return it('should return the callback with the new rev', function () {
  560. return this.callback
  561. .calledWith(null, true, this.rev + 1)
  562. .should.equal(true)
  563. })
  564. })
  565. describe('when the doc ranges have changed', function () {
  566. beforeEach(function () {
  567. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, this.doc)
  568. this.RangeManager.shouldUpdateRanges.returns(true)
  569. return this.DocManager.updateDoc(
  570. this.project_id,
  571. this.doc_id,
  572. this.oldDocLines,
  573. this.version,
  574. this.newRanges,
  575. this.callback
  576. )
  577. })
  578. it('should upsert the ranges', function () {
  579. return this.MongoManager.upsertIntoDocCollection
  580. .calledWith(this.project_id, this.doc_id, { ranges: this.newRanges })
  581. .should.equal(true)
  582. })
  583. it('should not update the version', function () {
  584. return this.MongoManager.setDocVersion.called.should.equal(false)
  585. })
  586. return it('should return the callback with the new rev', function () {
  587. return this.callback
  588. .calledWith(null, true, this.rev + 1)
  589. .should.equal(true)
  590. })
  591. })
  592. describe('when only the version has changed', function () {
  593. beforeEach(function () {
  594. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, this.doc)
  595. return this.DocManager.updateDoc(
  596. this.project_id,
  597. this.doc_id,
  598. this.oldDocLines,
  599. this.version + 1,
  600. this.originalRanges,
  601. this.callback
  602. )
  603. })
  604. it('should not change the lines or ranges', function () {
  605. return this.MongoManager.upsertIntoDocCollection.called.should.equal(
  606. false
  607. )
  608. })
  609. it('should update the version', function () {
  610. return this.MongoManager.setDocVersion
  611. .calledWith(this.doc_id, this.version + 1)
  612. .should.equal(true)
  613. })
  614. return it('should return the callback with the old rev', function () {
  615. return this.callback.calledWith(null, true, this.rev).should.equal(true)
  616. })
  617. })
  618. describe('when the doc has not changed at all', function () {
  619. beforeEach(function () {
  620. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, this.doc)
  621. return this.DocManager.updateDoc(
  622. this.project_id,
  623. this.doc_id,
  624. this.oldDocLines,
  625. this.version,
  626. this.originalRanges,
  627. this.callback
  628. )
  629. })
  630. it('should not update the ranges or lines', function () {
  631. return this.MongoManager.upsertIntoDocCollection.called.should.equal(
  632. false
  633. )
  634. })
  635. it('should not update the version', function () {
  636. return this.MongoManager.setDocVersion.called.should.equal(false)
  637. })
  638. return it('should return the callback with the old rev and modified == false', function () {
  639. return this.callback
  640. .calledWith(null, false, this.rev)
  641. .should.equal(true)
  642. })
  643. })
  644. describe('when the version is null', function () {
  645. beforeEach(function () {
  646. return this.DocManager.updateDoc(
  647. this.project_id,
  648. this.doc_id,
  649. this.newDocLines,
  650. null,
  651. this.originalRanges,
  652. this.callback
  653. )
  654. })
  655. return it('should return an error', function () {
  656. return this.callback
  657. .calledWith(
  658. sinon.match.has('message', 'no lines, version or ranges provided')
  659. )
  660. .should.equal(true)
  661. })
  662. })
  663. describe('when the lines are null', function () {
  664. beforeEach(function () {
  665. return this.DocManager.updateDoc(
  666. this.project_id,
  667. this.doc_id,
  668. null,
  669. this.version,
  670. this.originalRanges,
  671. this.callback
  672. )
  673. })
  674. return it('should return an error', function () {
  675. return this.callback
  676. .calledWith(
  677. sinon.match.has('message', 'no lines, version or ranges provided')
  678. )
  679. .should.equal(true)
  680. })
  681. })
  682. describe('when the ranges are null', function () {
  683. beforeEach(function () {
  684. return this.DocManager.updateDoc(
  685. this.project_id,
  686. this.doc_id,
  687. this.newDocLines,
  688. this.version,
  689. null,
  690. this.callback
  691. )
  692. })
  693. return it('should return an error', function () {
  694. return this.callback
  695. .calledWith(
  696. sinon.match.has('message', 'no lines, version or ranges provided')
  697. )
  698. .should.equal(true)
  699. })
  700. })
  701. describe('when there is a generic error getting the doc', function () {
  702. beforeEach(function () {
  703. this.error = new Error('doc could not be found')
  704. this.DocManager._getDoc = sinon
  705. .stub()
  706. .callsArgWith(3, this.error, null, null)
  707. return this.DocManager.updateDoc(
  708. this.project_id,
  709. this.doc_id,
  710. this.newDocLines,
  711. this.version,
  712. this.originalRanges,
  713. this.callback
  714. )
  715. })
  716. it('should not upsert the document to the doc collection', function () {
  717. return this.MongoManager.upsertIntoDocCollection.called.should.equal(
  718. false
  719. )
  720. })
  721. return it('should return the callback with the error', function () {
  722. return this.callback.calledWith(this.error).should.equal(true)
  723. })
  724. })
  725. describe('when the doc lines have not changed', function () {
  726. beforeEach(function () {
  727. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, this.doc)
  728. return this.DocManager.updateDoc(
  729. this.project_id,
  730. this.doc_id,
  731. this.oldDocLines.slice(),
  732. this.version,
  733. this.originalRanges,
  734. this.callback
  735. )
  736. })
  737. it('should not update the doc', function () {
  738. return this.MongoManager.upsertIntoDocCollection.called.should.equal(
  739. false
  740. )
  741. })
  742. return it('should return the callback with the existing rev', function () {
  743. return this.callback
  744. .calledWith(null, false, this.rev)
  745. .should.equal(true)
  746. })
  747. })
  748. return describe('when the doc does not exist', function () {
  749. beforeEach(function () {
  750. this.DocManager._getDoc = sinon.stub().callsArgWith(3, null, null, null)
  751. return this.DocManager.updateDoc(
  752. this.project_id,
  753. this.doc_id,
  754. this.newDocLines,
  755. this.version,
  756. this.originalRanges,
  757. this.callback
  758. )
  759. })
  760. it('should upsert the document to the doc collection', function () {
  761. return this.MongoManager.upsertIntoDocCollection
  762. .calledWith(this.project_id, this.doc_id, {
  763. lines: this.newDocLines,
  764. ranges: this.originalRanges,
  765. })
  766. .should.equal(true)
  767. })
  768. it('should set the version', function () {
  769. return this.MongoManager.setDocVersion
  770. .calledWith(this.doc_id, this.version)
  771. .should.equal(true)
  772. })
  773. return it('should return the callback with the new rev', function () {
  774. return this.callback.calledWith(null, true, 1).should.equal(true)
  775. })
  776. })
  777. })
  778. })