DocumentUpdaterManagerTests.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* eslint-disable
  2. no-return-assign,
  3. no-undef,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. import sinon from 'sinon'
  14. import { expect } from 'chai'
  15. import { strict as esmock } from 'esmock'
  16. const MODULE_PATH = '../../../../app/js/DocumentUpdaterManager.js'
  17. describe('DocumentUpdaterManager', function () {
  18. beforeEach(async function () {
  19. this.settings = {
  20. apis: { documentupdater: { url: 'http://example.com' } },
  21. }
  22. this.request = {
  23. get: sinon.stub(),
  24. post: sinon.stub(),
  25. }
  26. this.DocumentUpdaterManager = await esmock(MODULE_PATH, {
  27. request: this.request,
  28. '@overleaf/settings': this.settings,
  29. })
  30. this.callback = sinon.stub()
  31. this.lines = ['one', 'two', 'three']
  32. return (this.version = 42)
  33. })
  34. describe('getDocument', function () {
  35. describe('successfully', function () {
  36. beforeEach(function () {
  37. this.body = JSON.stringify({
  38. lines: this.lines,
  39. version: this.version,
  40. ops: [],
  41. })
  42. this.request.get.yields(null, { statusCode: 200 }, this.body)
  43. return this.DocumentUpdaterManager.getDocument(
  44. this.project_id,
  45. this.doc_id,
  46. this.callback
  47. )
  48. })
  49. it('should get the document from the document updater', function () {
  50. const url = `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}`
  51. return this.request.get.calledWith(url).should.equal(true)
  52. })
  53. return it('should call the callback with the content and version', function () {
  54. return this.callback
  55. .calledWith(null, this.lines.join('\n'), this.version)
  56. .should.equal(true)
  57. })
  58. })
  59. describe('when the document updater API returns an error', function () {
  60. beforeEach(function () {
  61. this.error = new Error('something went wrong')
  62. this.request.get.yields(this.error, null, null)
  63. return this.DocumentUpdaterManager.getDocument(
  64. this.project_id,
  65. this.doc_id,
  66. this.callback
  67. )
  68. })
  69. return it('should return an error to the callback', function () {
  70. return this.callback.calledWith(this.error).should.equal(true)
  71. })
  72. })
  73. return describe('when the document updater returns a failure error code', function () {
  74. beforeEach(function () {
  75. this.request.get.yields(null, { statusCode: 500 }, '')
  76. return this.DocumentUpdaterManager.getDocument(
  77. this.project_id,
  78. this.doc_id,
  79. this.callback
  80. )
  81. })
  82. return it('should return the callback with an error', function () {
  83. return this.callback
  84. .calledWith(
  85. sinon.match.has(
  86. 'message',
  87. 'doc updater returned a non-success status code: 500'
  88. )
  89. )
  90. .should.equal(true)
  91. })
  92. })
  93. })
  94. return describe('setDocument', function () {
  95. beforeEach(function () {
  96. this.content = 'mock content'
  97. return (this.user_id = 'user-id-123')
  98. })
  99. describe('successfully', function () {
  100. beforeEach(function () {
  101. this.request.post.yields(null, { statusCode: 200 })
  102. return this.DocumentUpdaterManager.setDocument(
  103. this.project_id,
  104. this.doc_id,
  105. this.content,
  106. this.user_id,
  107. this.callback
  108. )
  109. })
  110. it('should set the document in the document updater', function () {
  111. const url = `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}`
  112. return this.request.post
  113. .calledWith({
  114. url,
  115. json: {
  116. lines: this.content.split('\n'),
  117. source: 'restore',
  118. user_id: this.user_id,
  119. undoing: true,
  120. },
  121. })
  122. .should.equal(true)
  123. })
  124. return it('should call the callback', function () {
  125. return this.callback.calledWith(null).should.equal(true)
  126. })
  127. })
  128. describe('when the document updater API returns an error', function () {
  129. beforeEach(function () {
  130. this.error = new Error('something went wrong')
  131. this.request.post.yields(this.error, null, null)
  132. return this.DocumentUpdaterManager.setDocument(
  133. this.project_id,
  134. this.doc_id,
  135. this.content,
  136. this.user_id,
  137. this.callback
  138. )
  139. })
  140. return it('should return an error to the callback', function () {
  141. return this.callback.calledWith(this.error).should.equal(true)
  142. })
  143. })
  144. return describe('when the document updater returns a failure error code', function () {
  145. beforeEach(function () {
  146. this.request.post.yields(null, { statusCode: 500 }, '')
  147. return this.DocumentUpdaterManager.setDocument(
  148. this.project_id,
  149. this.doc_id,
  150. this.content,
  151. this.user_id,
  152. this.callback
  153. )
  154. })
  155. return it('should return the callback with an error', function () {
  156. return this.callback
  157. .calledWith(
  158. sinon.match.has(
  159. 'message',
  160. 'doc updater returned a non-success status code: 500'
  161. )
  162. )
  163. .should.equal(true)
  164. })
  165. })
  166. })
  167. })