GettingProjectDocsTests.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const { expect } = require('chai')
  15. const MockWebApi = require('./helpers/MockWebApi')
  16. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  17. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  18. describe('Getting documents for project', function () {
  19. before(function (done) {
  20. this.lines = ['one', 'two', 'three']
  21. this.version = 42
  22. return DocUpdaterApp.ensureRunning(done)
  23. })
  24. describe('when project state hash does not match', function () {
  25. before(function (done) {
  26. this.projectStateHash = DocUpdaterClient.randomId()
  27. ;[this.project_id, this.doc_id] = Array.from([
  28. DocUpdaterClient.randomId(),
  29. DocUpdaterClient.randomId(),
  30. ])
  31. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  32. lines: this.lines,
  33. version: this.version,
  34. })
  35. return DocUpdaterClient.preloadDoc(
  36. this.project_id,
  37. this.doc_id,
  38. error => {
  39. if (error != null) {
  40. throw error
  41. }
  42. return DocUpdaterClient.getProjectDocs(
  43. this.project_id,
  44. this.projectStateHash,
  45. (error, res, returnedDocs) => {
  46. if (error) return done(error)
  47. this.res = res
  48. this.returnedDocs = returnedDocs
  49. return done()
  50. }
  51. )
  52. }
  53. )
  54. })
  55. return it('should return a 409 Conflict response', function () {
  56. return this.res.statusCode.should.equal(409)
  57. })
  58. })
  59. describe('when project state hash matches', function () {
  60. before(function (done) {
  61. this.projectStateHash = DocUpdaterClient.randomId()
  62. ;[this.project_id, this.doc_id] = Array.from([
  63. DocUpdaterClient.randomId(),
  64. DocUpdaterClient.randomId(),
  65. ])
  66. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  67. lines: this.lines,
  68. version: this.version,
  69. })
  70. return DocUpdaterClient.preloadDoc(
  71. this.project_id,
  72. this.doc_id,
  73. error => {
  74. if (error != null) {
  75. throw error
  76. }
  77. return DocUpdaterClient.getProjectDocs(
  78. this.project_id,
  79. this.projectStateHash,
  80. (error, res0, returnedDocs0) => {
  81. if (error) return done(error)
  82. // set the hash
  83. this.res0 = res0
  84. this.returnedDocs0 = returnedDocs0
  85. return DocUpdaterClient.getProjectDocs(
  86. this.project_id,
  87. this.projectStateHash,
  88. (error, res, returnedDocs) => {
  89. if (error) return done(error)
  90. // the hash should now match
  91. this.res = res
  92. this.returnedDocs = returnedDocs
  93. return done()
  94. }
  95. )
  96. }
  97. )
  98. }
  99. )
  100. })
  101. it('should return a 200 response', function () {
  102. return this.res.statusCode.should.equal(200)
  103. })
  104. return it('should return the documents', function () {
  105. return this.returnedDocs.should.deep.equal([
  106. { _id: this.doc_id, lines: this.lines, v: this.version },
  107. ])
  108. })
  109. })
  110. return describe('when the doc has been removed', function () {
  111. before(function (done) {
  112. this.projectStateHash = DocUpdaterClient.randomId()
  113. ;[this.project_id, this.doc_id] = Array.from([
  114. DocUpdaterClient.randomId(),
  115. DocUpdaterClient.randomId(),
  116. ])
  117. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  118. lines: this.lines,
  119. version: this.version,
  120. })
  121. return DocUpdaterClient.preloadDoc(
  122. this.project_id,
  123. this.doc_id,
  124. error => {
  125. if (error != null) {
  126. throw error
  127. }
  128. return DocUpdaterClient.getProjectDocs(
  129. this.project_id,
  130. this.projectStateHash,
  131. (error, res0, returnedDocs0) => {
  132. if (error) return done(error)
  133. // set the hash
  134. this.res0 = res0
  135. this.returnedDocs0 = returnedDocs0
  136. return DocUpdaterClient.deleteDoc(
  137. this.project_id,
  138. this.doc_id,
  139. (error, res, body) => {
  140. if (error) return done(error)
  141. // delete the doc
  142. return DocUpdaterClient.getProjectDocs(
  143. this.project_id,
  144. this.projectStateHash,
  145. (error, res1, returnedDocs) => {
  146. if (error) return done(error)
  147. // the hash would match, but the doc has been deleted
  148. this.res = res1
  149. this.returnedDocs = returnedDocs
  150. return done()
  151. }
  152. )
  153. }
  154. )
  155. }
  156. )
  157. }
  158. )
  159. })
  160. return it('should return a 409 Conflict response', function () {
  161. return this.res.statusCode.should.equal(409)
  162. })
  163. })
  164. })