WebApiManagerTests.js 6.0 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 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. '@overleaf/settings': (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. isTokenMember: true,
  45. isInvitedMember: true,
  46. }
  47. this.request.post = sinon
  48. .stub()
  49. .callsArgWith(1, null, { statusCode: 200 }, this.response)
  50. return this.WebApiManager.joinProject(
  51. this.project_id,
  52. this.user,
  53. this.callback
  54. )
  55. })
  56. it('should send a request to web to join the project', function () {
  57. return this.request.post
  58. .calledWith({
  59. url: `${this.settings.apis.web.url}/project/${this.project_id}/join`,
  60. qs: {
  61. user_id: this.user_id,
  62. },
  63. auth: {
  64. user: this.settings.apis.web.user,
  65. pass: this.settings.apis.web.pass,
  66. sendImmediately: true,
  67. },
  68. json: true,
  69. jar: false,
  70. headers: {},
  71. })
  72. .should.equal(true)
  73. })
  74. return it('should return the project, privilegeLevel, and restricted flag', function () {
  75. return this.callback
  76. .calledWith(
  77. null,
  78. this.response.project,
  79. this.response.privilegeLevel,
  80. {
  81. isRestrictedUser: this.response.isRestrictedUser,
  82. isTokenMember: this.response.isTokenMember,
  83. isInvitedMember: this.response.isInvitedMember,
  84. }
  85. )
  86. .should.equal(true)
  87. })
  88. })
  89. describe('when web replies with a 403', function () {
  90. beforeEach(function () {
  91. this.request.post = sinon
  92. .stub()
  93. .callsArgWith(1, null, { statusCode: 403 }, null)
  94. this.WebApiManager.joinProject(
  95. this.project_id,
  96. this.user_id,
  97. this.callback
  98. )
  99. })
  100. it('should call the callback with an error', function () {
  101. this.callback
  102. .calledWith(
  103. sinon.match({
  104. message: 'not authorized',
  105. })
  106. )
  107. .should.equal(true)
  108. })
  109. })
  110. describe('when web replies with a 404', function () {
  111. beforeEach(function () {
  112. this.request.post = sinon
  113. .stub()
  114. .callsArgWith(1, null, { statusCode: 404 }, null)
  115. this.WebApiManager.joinProject(
  116. this.project_id,
  117. this.user_id,
  118. this.callback
  119. )
  120. })
  121. it('should call the callback with an error', function () {
  122. this.callback
  123. .calledWith(
  124. sinon.match({
  125. message: 'project not found',
  126. info: { code: 'ProjectNotFound' },
  127. })
  128. )
  129. .should.equal(true)
  130. })
  131. })
  132. describe('with an error from web', function () {
  133. beforeEach(function () {
  134. this.request.post = sinon
  135. .stub()
  136. .callsArgWith(1, null, { statusCode: 500 }, null)
  137. return this.WebApiManager.joinProject(
  138. this.project_id,
  139. this.user_id,
  140. this.callback
  141. )
  142. })
  143. return it('should call the callback with an error', function () {
  144. return this.callback
  145. .calledWith(
  146. sinon.match({
  147. message: 'non-success status code from web',
  148. info: { statusCode: 500 },
  149. })
  150. )
  151. .should.equal(true)
  152. })
  153. })
  154. describe('with no data from web', function () {
  155. beforeEach(function () {
  156. this.request.post = sinon
  157. .stub()
  158. .callsArgWith(1, null, { statusCode: 200 }, null)
  159. return this.WebApiManager.joinProject(
  160. this.project_id,
  161. this.user_id,
  162. this.callback
  163. )
  164. })
  165. return it('should call the callback with an error', function () {
  166. return this.callback
  167. .calledWith(
  168. sinon.match({
  169. message: 'no data returned from joinProject request',
  170. })
  171. )
  172. .should.equal(true)
  173. })
  174. })
  175. return describe('when the project is over its rate limit', function () {
  176. beforeEach(function () {
  177. this.request.post = sinon
  178. .stub()
  179. .callsArgWith(1, null, { statusCode: 429 }, null)
  180. return this.WebApiManager.joinProject(
  181. this.project_id,
  182. this.user_id,
  183. this.callback
  184. )
  185. })
  186. return it('should call the callback with a TooManyRequests error code', function () {
  187. return this.callback
  188. .calledWith(
  189. sinon.match({
  190. message: 'rate-limit hit when joining project',
  191. info: {
  192. code: 'TooManyRequests',
  193. },
  194. })
  195. )
  196. .should.equal(true)
  197. })
  198. })
  199. })
  200. })