DocumentUpdaterManagerTests.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. describe('when the document updater returns not found', function () {
  78. beforeEach(function () {
  79. this.request.get = sinon
  80. .stub()
  81. .callsArgWith(1, null, { statusCode: 404 }, '')
  82. return this.DocumentUpdaterManager.getDocument(
  83. this.project_id,
  84. this.doc_id,
  85. this.callback
  86. )
  87. })
  88. it('should return the callback with a "not found" error', function () {
  89. return this.callback
  90. .calledWith(sinon.match.has('message', 'doc not found'))
  91. .should.equal(true)
  92. })
  93. })
  94. return describe('when the document updater returns a failure error code', function () {
  95. beforeEach(function () {
  96. this.request.get = sinon
  97. .stub()
  98. .callsArgWith(1, null, { statusCode: 500 }, '')
  99. return this.DocumentUpdaterManager.getDocument(
  100. this.project_id,
  101. this.doc_id,
  102. this.callback
  103. )
  104. })
  105. return it('should return the callback with an error', function () {
  106. return this.callback
  107. .calledWith(
  108. sinon.match.has(
  109. 'message',
  110. 'doc updater returned a non-success status code: 500'
  111. )
  112. )
  113. .should.equal(true)
  114. })
  115. })
  116. })
  117. return describe('setDocument', function () {
  118. beforeEach(function () {
  119. this.content = 'mock content'
  120. return (this.user_id = 'user-id-123')
  121. })
  122. describe('successfully', function () {
  123. beforeEach(function () {
  124. this.request.post = sinon
  125. .stub()
  126. .callsArgWith(1, null, { statusCode: 200 })
  127. return this.DocumentUpdaterManager.setDocument(
  128. this.project_id,
  129. this.doc_id,
  130. this.content,
  131. this.user_id,
  132. this.callback
  133. )
  134. })
  135. it('should set the document in the document updater', function () {
  136. const url = `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}`
  137. return this.request.post
  138. .calledWith({
  139. url,
  140. json: {
  141. lines: this.content.split('\n'),
  142. source: 'restore',
  143. user_id: this.user_id,
  144. undoing: true,
  145. },
  146. })
  147. .should.equal(true)
  148. })
  149. return it('should call the callback', function () {
  150. return this.callback.calledWith(null).should.equal(true)
  151. })
  152. })
  153. describe('when the document updater API returns an error', function () {
  154. beforeEach(function () {
  155. this.request.post = sinon
  156. .stub()
  157. .callsArgWith(
  158. 1,
  159. (this.error = new Error('something went wrong')),
  160. null,
  161. null
  162. )
  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 an error to the callback', function () {
  172. return this.callback.calledWith(this.error).should.equal(true)
  173. })
  174. })
  175. return describe('when the document updater returns a failure error code', function () {
  176. beforeEach(function () {
  177. this.request.post = sinon
  178. .stub()
  179. .callsArgWith(1, null, { statusCode: 500 }, '')
  180. return this.DocumentUpdaterManager.setDocument(
  181. this.project_id,
  182. this.doc_id,
  183. this.content,
  184. this.user_id,
  185. this.callback
  186. )
  187. })
  188. return it('should return the callback with an error', function () {
  189. return this.callback
  190. .calledWith(
  191. sinon.match.has(
  192. 'message',
  193. 'doc updater returned a non-success status code: 500'
  194. )
  195. )
  196. .should.equal(true)
  197. })
  198. })
  199. })
  200. })