DocManagerTests.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-dupe-keys,
  5. no-return-assign,
  6. no-unused-vars,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const SandboxedModule = require('sandboxed-module')
  16. const sinon = require('sinon')
  17. const { assert, expect } = require('chai')
  18. const modulePath = require('path').join(__dirname, '../../../app/js/DocManager')
  19. const { ObjectId } = require('mongodb')
  20. const Errors = require('../../../app/js/Errors')
  21. describe('DocManager', function () {
  22. beforeEach(function () {
  23. this.DocManager = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './MongoManager': (this.MongoManager = {}),
  26. './DocArchiveManager': (this.DocArchiveManager = {}),
  27. './RangeManager': (this.RangeManager = {
  28. jsonRangesToMongo(r) {
  29. return r
  30. },
  31. shouldUpdateRanges: sinon.stub().returns(false)
  32. }),
  33. '@overleaf/settings': (this.settings = { docstore: {} }),
  34. './Errors': Errors
  35. }
  36. })
  37. this.doc_id = ObjectId().toString()
  38. this.project_id = ObjectId().toString()
  39. this.another_project_id = ObjectId().toString()
  40. this.callback = sinon.stub()
  41. return (this.stubbedError = new Error('blew up'))
  42. })
  43. describe('getFullDoc', function () {
  44. beforeEach(function () {
  45. this.DocManager._getDoc = sinon.stub()
  46. return (this.doc = {
  47. _id: this.doc_id,
  48. lines: ['2134']
  49. })
  50. })
  51. it('should call get doc with a quick filter', function (done) {
  52. this.DocManager._getDoc.callsArgWith(3, null, this.doc)
  53. return this.DocManager.getFullDoc(
  54. this.project_id,
  55. this.doc_id,
  56. (err, doc) => {
  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. 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 = (
  236. project_id,
  237. doc_id,
  238. callback
  239. ) => {
  240. this.doc.inS3 = false
  241. return callback()
  242. }
  243. sinon.spy(this.DocArchiveManager, 'unarchiveDoc')
  244. return this.DocManager._getDoc(
  245. this.project_id,
  246. this.doc_id,
  247. { version: true, inS3: true },
  248. this.callback
  249. )
  250. })
  251. it('should call the DocArchive to unarchive the doc', function () {
  252. return this.DocArchiveManager.unarchiveDoc
  253. .calledWith(this.project_id, this.doc_id)
  254. .should.equal(true)
  255. })
  256. it('should look up the doc twice', function () {
  257. return this.MongoManager.findDoc.calledTwice.should.equal(true)
  258. })
  259. return it('should return the doc', function () {
  260. return this.callback.calledWith(null, this.doc).should.equal(true)
  261. })
  262. })
  263. return describe('when the doc does not exist in the docs collection', function () {
  264. beforeEach(function () {
  265. this.MongoManager.findDoc = sinon.stub().yields(null, null)
  266. return this.DocManager._getDoc(
  267. this.project_id,
  268. this.doc_id,
  269. { version: true, inS3: true },
  270. this.callback
  271. )
  272. })
  273. return it('should return a NotFoundError', function () {
  274. return this.callback
  275. .calledWith(
  276. sinon.match.has(
  277. 'message',
  278. `No such doc: ${this.doc_id} in project ${this.project_id}`
  279. )
  280. )
  281. .should.equal(true)
  282. })
  283. })
  284. })
  285. describe('getAllNonDeletedDocs', function () {
  286. describe('when the project exists', function () {
  287. beforeEach(function () {
  288. this.docs = [
  289. {
  290. _id: this.doc_id,
  291. project_id: this.project_id,
  292. lines: ['mock-lines']
  293. }
  294. ]
  295. this.MongoManager.getProjectsDocs = sinon
  296. .stub()
  297. .callsArgWith(3, null, this.docs)
  298. this.DocArchiveManager.unArchiveAllDocs = sinon
  299. .stub()
  300. .callsArgWith(1, null, this.docs)
  301. this.filter = { lines: true }
  302. return this.DocManager.getAllNonDeletedDocs(
  303. this.project_id,
  304. this.filter,
  305. this.callback
  306. )
  307. })
  308. it('should get the project from the database', function () {
  309. return this.MongoManager.getProjectsDocs
  310. .calledWith(this.project_id, { include_deleted: false }, this.filter)
  311. .should.equal(true)
  312. })
  313. return it('should return the docs', function () {
  314. return this.callback.calledWith(null, this.docs).should.equal(true)
  315. })
  316. })
  317. return describe('when there are no docs for the project', function () {
  318. beforeEach(function () {
  319. this.MongoManager.getProjectsDocs = sinon
  320. .stub()
  321. .callsArgWith(3, null, null)
  322. this.DocArchiveManager.unArchiveAllDocs = sinon
  323. .stub()
  324. .callsArgWith(1, null)
  325. return this.DocManager.getAllNonDeletedDocs(
  326. this.project_id,
  327. this.filter,
  328. this.callback
  329. )
  330. })
  331. return it('should return a NotFoundError', function () {
  332. return this.callback
  333. .calledWith(
  334. sinon.match.has('message', `No docs for project ${this.project_id}`)
  335. )
  336. .should.equal(true)
  337. })
  338. })
  339. })
  340. describe('patchDoc', function () {
  341. describe('when the doc exists', function () {
  342. beforeEach(function () {
  343. this.lines = ['mock', 'doc', 'lines']
  344. this.rev = 77
  345. this.MongoManager.findDoc = sinon
  346. .stub()
  347. .yields(null, { _id: ObjectId(this.doc_id) })
  348. this.MongoManager.patchDoc = sinon.stub().yields(null)
  349. this.DocArchiveManager.archiveDocById = sinon.stub().yields(null)
  350. this.meta = {}
  351. })
  352. describe('standard path', function () {
  353. beforeEach(function (done) {
  354. this.callback = sinon.stub().callsFake(done)
  355. this.DocManager.patchDoc(
  356. this.project_id,
  357. this.doc_id,
  358. this.meta,
  359. this.callback
  360. )
  361. })
  362. it('should get the doc', function () {
  363. expect(this.MongoManager.findDoc).to.have.been.calledWith(
  364. this.project_id,
  365. this.doc_id
  366. )
  367. })
  368. it('should persist the meta', function () {
  369. expect(this.MongoManager.patchDoc).to.have.been.calledWith(
  370. this.project_id,
  371. this.doc_id,
  372. this.meta
  373. )
  374. })
  375. it('should return the callback', function () {
  376. expect(this.callback).to.have.been.calledWith(null)
  377. })
  378. })
  379. describe('background flush disabled and deleting a doc', function () {
  380. beforeEach(function (done) {
  381. this.settings.docstore.archiveOnSoftDelete = false
  382. this.meta.deleted = true
  383. this.callback = sinon.stub().callsFake(done)
  384. this.DocManager.patchDoc(
  385. this.project_id,
  386. this.doc_id,
  387. this.meta,
  388. this.callback
  389. )
  390. })
  391. it('should not flush the doc out of mongo', function () {
  392. expect(this.DocArchiveManager.archiveDocById).to.not.have.been.called
  393. })
  394. })
  395. describe('background flush enabled and not deleting a doc', function () {
  396. beforeEach(function (done) {
  397. this.settings.docstore.archiveOnSoftDelete = false
  398. this.meta.deleted = false
  399. this.callback = sinon.stub().callsFake(done)
  400. this.DocManager.patchDoc(
  401. this.project_id,
  402. this.doc_id,
  403. this.meta,
  404. this.callback
  405. )
  406. })
  407. it('should not flush the doc out of mongo', function () {
  408. expect(this.DocArchiveManager.archiveDocById).to.not.have.been.called
  409. })
  410. })
  411. describe('background flush enabled and deleting a doc', function () {
  412. beforeEach(function () {
  413. this.settings.docstore.archiveOnSoftDelete = true
  414. this.meta.deleted = true
  415. })
  416. describe('when the background flush succeeds', function () {
  417. beforeEach(function (done) {
  418. this.DocArchiveManager.archiveDocById = sinon.stub().yields(null)
  419. this.callback = sinon.stub().callsFake(done)
  420. this.DocManager.patchDoc(
  421. this.project_id,
  422. this.doc_id,
  423. this.meta,
  424. this.callback
  425. )
  426. })
  427. it('should not log a warning', function () {
  428. expect(this.logger.warn).to.not.have.been.called
  429. })
  430. it('should flush the doc out of mongo', function () {
  431. expect(
  432. this.DocArchiveManager.archiveDocById
  433. ).to.have.been.calledWith(this.project_id, this.doc_id)
  434. })
  435. })
  436. describe('when the background flush fails', function () {
  437. beforeEach(function (done) {
  438. this.err = new Error('foo')
  439. this.DocArchiveManager.archiveDocById = sinon
  440. .stub()
  441. .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. })