WebApiManagerTests.js 5.7 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 modulePath = '../../../app/js/WebApiManager.js'
  14. const SandboxedModule = require('sandboxed-module')
  15. const { CodedError } = require('../../../app/js/Errors')
  16. describe('WebApiManager', function () {
  17. beforeEach(function () {
  18. this.project_id = 'project-id-123'
  19. this.user_id = 'user-id-123'
  20. this.user = { _id: this.user_id }
  21. this.callback = sinon.stub()
  22. return (this.WebApiManager = SandboxedModule.require(modulePath, {
  23. requires: {
  24. request: (this.request = {}),
  25. 'settings-sharelatex': (this.settings = {
  26. apis: {
  27. web: {
  28. url: 'http://web.example.com',
  29. user: 'username',
  30. pass: 'password'
  31. }
  32. }
  33. })
  34. }
  35. }))
  36. })
  37. return describe('joinProject', function () {
  38. describe('successfully', function () {
  39. beforeEach(function () {
  40. this.response = {
  41. project: { name: 'Test project' },
  42. privilegeLevel: 'owner',
  43. isRestrictedUser: true
  44. }
  45. this.request.post = sinon
  46. .stub()
  47. .callsArgWith(1, null, { statusCode: 200 }, this.response)
  48. return this.WebApiManager.joinProject(
  49. this.project_id,
  50. this.user,
  51. this.callback
  52. )
  53. })
  54. it('should send a request to web to join the project', function () {
  55. return this.request.post
  56. .calledWith({
  57. url: `${this.settings.apis.web.url}/project/${this.project_id}/join`,
  58. qs: {
  59. user_id: this.user_id
  60. },
  61. auth: {
  62. user: this.settings.apis.web.user,
  63. pass: this.settings.apis.web.pass,
  64. sendImmediately: true
  65. },
  66. json: true,
  67. jar: false,
  68. headers: {}
  69. })
  70. .should.equal(true)
  71. })
  72. return it('should return the project, privilegeLevel, and restricted flag', function () {
  73. return this.callback
  74. .calledWith(
  75. null,
  76. this.response.project,
  77. this.response.privilegeLevel,
  78. this.response.isRestrictedUser
  79. )
  80. .should.equal(true)
  81. })
  82. })
  83. describe('when web replies with a 403', function () {
  84. beforeEach(function () {
  85. this.request.post = sinon
  86. .stub()
  87. .callsArgWith(1, null, { statusCode: 403 }, null)
  88. this.WebApiManager.joinProject(
  89. this.project_id,
  90. this.user_id,
  91. this.callback
  92. )
  93. })
  94. it('should call the callback with an error', function () {
  95. this.callback
  96. .calledWith(
  97. sinon.match({
  98. message: 'not authorized'
  99. })
  100. )
  101. .should.equal(true)
  102. })
  103. })
  104. describe('when web replies with a 404', function () {
  105. beforeEach(function () {
  106. this.request.post = sinon
  107. .stub()
  108. .callsArgWith(1, null, { statusCode: 404 }, null)
  109. this.WebApiManager.joinProject(
  110. this.project_id,
  111. this.user_id,
  112. this.callback
  113. )
  114. })
  115. it('should call the callback with an error', function () {
  116. this.callback
  117. .calledWith(
  118. sinon.match({
  119. message: 'project not found',
  120. info: { code: 'ProjectNotFound' }
  121. })
  122. )
  123. .should.equal(true)
  124. })
  125. })
  126. describe('with an error from web', function () {
  127. beforeEach(function () {
  128. this.request.post = sinon
  129. .stub()
  130. .callsArgWith(1, null, { statusCode: 500 }, null)
  131. return this.WebApiManager.joinProject(
  132. this.project_id,
  133. this.user_id,
  134. this.callback
  135. )
  136. })
  137. return it('should call the callback with an error', function () {
  138. return this.callback
  139. .calledWith(
  140. sinon.match({
  141. message: 'non-success status code from web',
  142. info: { statusCode: 500 }
  143. })
  144. )
  145. .should.equal(true)
  146. })
  147. })
  148. describe('with no data from web', function () {
  149. beforeEach(function () {
  150. this.request.post = sinon
  151. .stub()
  152. .callsArgWith(1, null, { statusCode: 200 }, null)
  153. return this.WebApiManager.joinProject(
  154. this.project_id,
  155. this.user_id,
  156. this.callback
  157. )
  158. })
  159. return it('should call the callback with an error', function () {
  160. return this.callback
  161. .calledWith(
  162. sinon.match({
  163. message: 'no data returned from joinProject request'
  164. })
  165. )
  166. .should.equal(true)
  167. })
  168. })
  169. return describe('when the project is over its rate limit', function () {
  170. beforeEach(function () {
  171. this.request.post = sinon
  172. .stub()
  173. .callsArgWith(1, null, { statusCode: 429 }, null)
  174. return this.WebApiManager.joinProject(
  175. this.project_id,
  176. this.user_id,
  177. this.callback
  178. )
  179. })
  180. return it('should call the callback with a TooManyRequests error code', function () {
  181. return this.callback
  182. .calledWith(
  183. sinon.match({
  184. message: 'rate-limit hit when joining project',
  185. info: {
  186. code: 'TooManyRequests'
  187. }
  188. })
  189. )
  190. .should.equal(true)
  191. })
  192. })
  193. })
  194. })