DocumentUpdaterManagerTests.js 5.8 KB

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