DocumentUpdaterManagerTests.js 5.9 KB

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