WebApiManagerTests.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/WebApiManager.js'
  15. const SandboxedModule = require('sandboxed-module')
  16. describe('WebApiManager', function () {
  17. beforeEach(function () {
  18. this.WebApiManager = SandboxedModule.require(modulePath, {
  19. requires: {
  20. requestretry: (this.request = {}),
  21. '@overleaf/settings': (this.settings = {
  22. apis: {
  23. web: {
  24. url: 'http://example.com',
  25. user: 'sharelatex',
  26. pass: 'password',
  27. },
  28. },
  29. }),
  30. },
  31. })
  32. this.callback = sinon.stub()
  33. this.user_id = 'mock-user-id'
  34. this.project_id = 'mock-project-id'
  35. this.user_info = {
  36. email: 'leo@sharelatex.com',
  37. id: this.user_id,
  38. first_name: 'Leo',
  39. last_nane: 'Lion',
  40. extra_param: 'blah',
  41. }
  42. return (this.project = { features: 'mock-features' })
  43. })
  44. describe('getUserInfo', function () {
  45. describe('successfully', function () {
  46. beforeEach(function () {
  47. this.body = JSON.stringify(this.user_info)
  48. this.request.get = sinon
  49. .stub()
  50. .callsArgWith(1, null, { statusCode: 200 }, this.body)
  51. return this.WebApiManager.getUserInfo(this.user_id, this.callback)
  52. })
  53. it('should get the user from the web api', function () {
  54. return this.request.get
  55. .calledWithMatch({
  56. url: `${this.settings.apis.web.url}/user/${this.user_id}/personal_info`,
  57. auth: {
  58. user: this.settings.apis.web.user,
  59. pass: this.settings.apis.web.pass,
  60. sendImmediately: true,
  61. },
  62. })
  63. .should.equal(true)
  64. })
  65. return it('should call the callback with only the email, id and names', function () {
  66. return this.callback
  67. .calledWith(null, {
  68. id: this.user_id,
  69. email: this.user_info.email,
  70. first_name: this.user_info.first_name,
  71. last_name: this.user_info.last_name,
  72. })
  73. .should.equal(true)
  74. })
  75. })
  76. describe('when the web API returns an error', function () {
  77. beforeEach(function () {
  78. this.request.get = sinon
  79. .stub()
  80. .callsArgWith(
  81. 1,
  82. (this.error = new Error('something went wrong')),
  83. null,
  84. null
  85. )
  86. return this.WebApiManager.getUserInfo(this.user_id, this.callback)
  87. })
  88. return it('should return an error to the callback', function () {
  89. return this.callback.calledWith(this.error).should.equal(true)
  90. })
  91. })
  92. describe('when the web returns a failure error code', function () {
  93. beforeEach(function () {
  94. this.request.get = sinon
  95. .stub()
  96. .callsArgWith(1, null, { statusCode: 500, attempts: 42 }, '')
  97. return this.WebApiManager.getUserInfo(this.user_id, this.callback)
  98. })
  99. return it('should return the callback with an error', function () {
  100. return this.callback
  101. .calledWith(
  102. sinon.match.has(
  103. 'message',
  104. 'web returned a non-success status code: 500 (attempts: 42)'
  105. )
  106. )
  107. .should.equal(true)
  108. })
  109. })
  110. return describe('when the user cannot be found', function () {
  111. beforeEach(function () {
  112. this.request.get = sinon
  113. .stub()
  114. .callsArgWith(1, null, { statusCode: 404 }, 'nothing')
  115. return this.WebApiManager.getUserInfo(this.user_id, this.callback)
  116. })
  117. return it('should return a null value', function () {
  118. return this.callback.calledWith(null, null).should.equal(true)
  119. })
  120. })
  121. })
  122. return describe('getProjectDetails', function () {
  123. describe('successfully', function () {
  124. beforeEach(function () {
  125. this.body = JSON.stringify(this.project)
  126. this.request.get = sinon
  127. .stub()
  128. .callsArgWith(1, null, { statusCode: 200 }, this.body)
  129. return this.WebApiManager.getProjectDetails(
  130. this.project_id,
  131. this.callback
  132. )
  133. })
  134. it('should get the project from the web api', function () {
  135. return this.request.get
  136. .calledWithMatch({
  137. url: `${this.settings.apis.web.url}/project/${this.project_id}/details`,
  138. auth: {
  139. user: this.settings.apis.web.user,
  140. pass: this.settings.apis.web.pass,
  141. sendImmediately: true,
  142. },
  143. })
  144. .should.equal(true)
  145. })
  146. return it('should call the callback with the project', function () {
  147. return this.callback.calledWith(null, this.project).should.equal(true)
  148. })
  149. })
  150. describe('when the web API returns an error', function () {
  151. beforeEach(function () {
  152. this.request.get = sinon
  153. .stub()
  154. .callsArgWith(
  155. 1,
  156. (this.error = new Error('something went wrong')),
  157. null,
  158. null
  159. )
  160. return this.WebApiManager.getProjectDetails(
  161. this.project_id,
  162. this.callback
  163. )
  164. })
  165. return it('should return an error to the callback', function () {
  166. return this.callback.calledWith(this.error).should.equal(true)
  167. })
  168. })
  169. return describe('when the web returns a failure error code', function () {
  170. beforeEach(function () {
  171. this.request.get = sinon
  172. .stub()
  173. .callsArgWith(1, null, { statusCode: 500, attempts: 42 }, '')
  174. return this.WebApiManager.getProjectDetails(
  175. this.project_id,
  176. this.callback
  177. )
  178. })
  179. return it('should return the callback with an error', function () {
  180. return this.callback
  181. .calledWith(
  182. sinon.match.has(
  183. 'message',
  184. 'web returned a non-success status code: 500 (attempts: 42)'
  185. )
  186. )
  187. .should.equal(true)
  188. })
  189. })
  190. })
  191. })