GettingDocsTests.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import mongodb from 'mongodb-legacy'
  2. import { expect } from 'chai'
  3. import DocstoreApp from './helpers/DocstoreApp.js'
  4. import DocstoreClient from './helpers/DocstoreClient.js'
  5. const { ObjectId } = mongodb
  6. describe('Getting a doc', function () {
  7. beforeEach(async function () {
  8. this.project_id = new ObjectId()
  9. this.doc_id = new ObjectId()
  10. this.lines = ['original', 'lines']
  11. this.version = 42
  12. this.ranges = {
  13. changes: [
  14. {
  15. id: new ObjectId().toString(),
  16. op: { i: 'foo', p: 3 },
  17. meta: {
  18. user_id: new ObjectId().toString(),
  19. ts: new Date().toJSON(),
  20. },
  21. },
  22. ],
  23. comments: [
  24. {
  25. id: new ObjectId().toString(),
  26. op: { c: 'comment', p: 1, t: new ObjectId().toString() },
  27. metadata: {
  28. user_id: new ObjectId().toString(),
  29. ts: new Date().toJSON(),
  30. },
  31. },
  32. ],
  33. }
  34. this.fixedRanges = {
  35. ...this.ranges,
  36. comments: [
  37. { ...this.ranges.comments[0], id: this.ranges.comments[0].op.t },
  38. ],
  39. }
  40. await DocstoreApp.ensureRunning()
  41. await DocstoreClient.createDoc(
  42. this.project_id,
  43. this.doc_id,
  44. this.lines,
  45. this.version,
  46. this.ranges
  47. )
  48. })
  49. describe('when the doc exists', function () {
  50. it('should get the doc lines and version', async function () {
  51. const doc = await DocstoreClient.getDoc(this.project_id, this.doc_id)
  52. doc.lines.should.deep.equal(this.lines)
  53. doc.version.should.equal(this.version)
  54. doc.ranges.should.deep.equal(this.fixedRanges)
  55. })
  56. })
  57. describe('when the doc does not exist', function () {
  58. it('should return a 404', async function () {
  59. const missingDocId = new ObjectId()
  60. await expect(DocstoreClient.getDoc(this.project_id, missingDocId))
  61. .to.eventually.be.rejected.and.have.property('info')
  62. .to.contain({ status: 404 })
  63. })
  64. })
  65. describe('when the doc is a deleted doc', function () {
  66. beforeEach(async function () {
  67. this.deleted_doc_id = new ObjectId()
  68. await DocstoreClient.createDoc(
  69. this.project_id,
  70. this.deleted_doc_id,
  71. this.lines,
  72. this.version,
  73. this.ranges
  74. )
  75. await DocstoreClient.deleteDoc(this.project_id, this.deleted_doc_id)
  76. })
  77. it('should return the doc', async function () {
  78. const doc = await DocstoreClient.getDoc(
  79. this.project_id,
  80. this.deleted_doc_id,
  81. { include_deleted: true }
  82. )
  83. doc.lines.should.deep.equal(this.lines)
  84. doc.version.should.equal(this.version)
  85. doc.ranges.should.deep.equal(this.fixedRanges)
  86. doc.deleted.should.equal(true)
  87. })
  88. it('should return a 404 when the query string is not set', async function () {
  89. await expect(DocstoreClient.getDoc(this.project_id, this.deleted_doc_id))
  90. .to.eventually.be.rejected.and.have.property('info')
  91. .to.contain({ status: 404 })
  92. })
  93. })
  94. })