WebApiManagerTests.js 6.4 KB

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