GettingDocsTests.js 2.9 KB

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